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 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | |
| 53 | switch (SL->getKind()) { |
| 54 | case StringLiteral::Ascii: |
| 55 | case StringLiteral::UTF8: |
| 56 | // char array can be initialized with a narrow string. |
| 57 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 59 | case StringLiteral::UTF16: |
| 60 | return ElemTy->isChar16Type() ? Init : 0; |
| 61 | case StringLiteral::UTF32: |
| 62 | return ElemTy->isChar32Type() ? Init : 0; |
| 63 | case StringLiteral::Wide: |
| 64 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 65 | // correction from DR343): "An array with element type compatible with a |
| 66 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 67 | // string literal, optionally enclosed in braces." |
| 68 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 69 | ElemTy.getUnqualifiedType())) |
| 70 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 78 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 79 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 80 | if (!arrayType) return 0; |
| 81 | |
| 82 | return IsStringInit(init, arrayType, Context); |
| 83 | } |
| 84 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 85 | /// Update the type of a string literal, including any surrounding parentheses, |
| 86 | /// to match the type of the object which it is initializing. |
| 87 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame^] | 88 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 89 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame^] | 90 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 91 | break; |
| 92 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 93 | E = PE->getSubExpr(); |
| 94 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 95 | E = UO->getSubExpr(); |
| 96 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 97 | E = GSE->getResultExpr(); |
| 98 | else |
| 99 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 100 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 101 | } |
| 102 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 103 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 104 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 105 | // Get the length of the string as parsed. |
| 106 | uint64_t StrLength = |
| 107 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 110 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | // 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] | 112 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 113 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 114 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 115 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 116 | ConstVal, |
| 117 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 118 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 119 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 122 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 124 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 125 | // the size may be smaller or larger than the string we are initializing. |
| 126 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 127 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 128 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 129 | // For Pascal strings it's OK to strip off the terminating null character, |
| 130 | // so the example below is valid: |
| 131 | // |
| 132 | // unsigned char a[2] = "\pa"; |
| 133 | if (SL->isPascal()) |
| 134 | StrLength--; |
| 135 | } |
| 136 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 137 | // [dcl.init.string]p2 |
| 138 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 139 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 140 | diag::err_initializer_string_for_char_array_too_long) |
| 141 | << Str->getSourceRange(); |
| 142 | } else { |
| 143 | // C99 6.7.8p14. |
| 144 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 145 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 146 | diag::warn_initializer_string_for_char_array_too_long) |
| 147 | << Str->getSourceRange(); |
| 148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 150 | // Set the type to the actual size that we are initializing. If we have |
| 151 | // something like: |
| 152 | // char x[1] = "foo"; |
| 153 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 154 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 157 | //===----------------------------------------------------------------------===// |
| 158 | // Semantic checking for initializer lists. |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 161 | /// @brief Semantic checking for initializer lists. |
| 162 | /// |
| 163 | /// The InitListChecker class contains a set of routines that each |
| 164 | /// handle the initialization of a certain kind of entity, e.g., |
| 165 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 166 | /// InitListChecker itself performs a recursive walk of the subobject |
| 167 | /// structure of the type to be initialized, while stepping through |
| 168 | /// the initializer list one element at a time. The IList and Index |
| 169 | /// parameters to each of the Check* routines contain the active |
| 170 | /// (syntactic) initializer list and the index into that initializer |
| 171 | /// list that represents the current initializer. Each routine is |
| 172 | /// responsible for moving that Index forward as it consumes elements. |
| 173 | /// |
| 174 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 175 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 176 | /// initializer list and the index into that initializer list where we |
| 177 | /// are copying initializers as we map them over to the semantic |
| 178 | /// list. Once we have completed our recursive walk of the subobject |
| 179 | /// structure, we will have constructed a full semantic initializer |
| 180 | /// list. |
| 181 | /// |
| 182 | /// C99 designators cause changes in the initializer list traversal, |
| 183 | /// because they make the initialization "jump" into a specific |
| 184 | /// subobject and then continue the initialization from that |
| 185 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 186 | /// designated subobject and manages backing out the recursion to |
| 187 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 188 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 190 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 191 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 192 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 193 | bool AllowBraceElision; |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 194 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 195 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 197 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 198 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 199 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 200 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 201 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 202 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 203 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 204 | unsigned &StructuredIndex, |
| 205 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 206 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 207 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 209 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 210 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 211 | unsigned &StructuredIndex, |
| 212 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 213 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 214 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 215 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 216 | InitListExpr *StructuredList, |
| 217 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 218 | void CheckComplexType(const InitializedEntity &Entity, |
| 219 | InitListExpr *IList, QualType DeclType, |
| 220 | unsigned &Index, |
| 221 | InitListExpr *StructuredList, |
| 222 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 223 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 224 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 225 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 226 | InitListExpr *StructuredList, |
| 227 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 228 | void CheckReferenceType(const InitializedEntity &Entity, |
| 229 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 230 | unsigned &Index, |
| 231 | InitListExpr *StructuredList, |
| 232 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 233 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 234 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 235 | InitListExpr *StructuredList, |
| 236 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 237 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 238 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 241 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 242 | unsigned &StructuredIndex, |
| 243 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 244 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 245 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 247 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
| 249 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 251 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 252 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 254 | RecordDecl::field_iterator *NextField, |
| 255 | llvm::APSInt *NextElementIndex, |
| 256 | unsigned &Index, |
| 257 | InitListExpr *StructuredList, |
| 258 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 259 | bool FinishSubobjectInit, |
| 260 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 261 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 262 | QualType CurrentObjectType, |
| 263 | InitListExpr *StructuredList, |
| 264 | unsigned StructuredIndex, |
| 265 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 266 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 267 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 268 | Expr *expr); |
| 269 | int numArrayElements(QualType DeclType); |
| 270 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 272 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 273 | const InitializedEntity &ParentEntity, |
| 274 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 275 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 276 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 277 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 278 | Expr *InitExpr, FieldDecl *Field, |
| 279 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 280 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 281 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 282 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 283 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 284 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 285 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 286 | bool HadError() { return hadError; } |
| 287 | |
| 288 | // @brief Retrieves the fully-structured initializer list used for |
| 289 | // semantic analysis and code generation. |
| 290 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 291 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 292 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 293 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 294 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 295 | assert(VerifyOnly && |
| 296 | "CheckValueInitializable is only inteded for verification mode."); |
| 297 | |
| 298 | SourceLocation Loc; |
| 299 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 300 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 301 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 302 | if (InitSeq.Failed()) |
| 303 | hadError = true; |
| 304 | } |
| 305 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 306 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 307 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 308 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 309 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 310 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 311 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 312 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 313 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 314 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 315 | // If there's no explicit initializer but we have a default initializer, use |
| 316 | // that. This only happens in C++1y, since classes with default |
| 317 | // initializers are not aggregates in C++11. |
| 318 | if (Field->hasInClassInitializer()) { |
| 319 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 320 | ILE->getRBraceLoc(), Field); |
| 321 | if (Init < NumInits) |
| 322 | ILE->setInit(Init, DIE); |
| 323 | else { |
| 324 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 325 | RequiresSecondPass = true; |
| 326 | } |
| 327 | return; |
| 328 | } |
| 329 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 330 | // FIXME: We probably don't need to handle references |
| 331 | // specially here, since value-initialization of references is |
| 332 | // handled in InitializationSequence. |
| 333 | if (Field->getType()->isReferenceType()) { |
| 334 | // C++ [dcl.init.aggr]p9: |
| 335 | // If an incomplete or empty initializer-list leaves a |
| 336 | // member of reference type uninitialized, the program is |
| 337 | // ill-formed. |
| 338 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 339 | << Field->getType() |
| 340 | << ILE->getSyntacticForm()->getSourceRange(); |
| 341 | SemaRef.Diag(Field->getLocation(), |
| 342 | diag::note_uninit_reference_member); |
| 343 | hadError = true; |
| 344 | return; |
| 345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 347 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 348 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 349 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 350 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 351 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 352 | hadError = true; |
| 353 | return; |
| 354 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 356 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 357 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 358 | if (MemberInit.isInvalid()) { |
| 359 | hadError = true; |
| 360 | return; |
| 361 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 363 | if (hadError) { |
| 364 | // Do nothing |
| 365 | } else if (Init < NumInits) { |
| 366 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 367 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 368 | // Value-initialization requires a constructor call, so |
| 369 | // extend the initializer list to include the constructor |
| 370 | // call and make a note that we'll need to take another pass |
| 371 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 372 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 373 | RequiresSecondPass = true; |
| 374 | } |
| 375 | } else if (InitListExpr *InnerILE |
| 376 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 377 | FillInValueInitializations(MemberEntity, InnerILE, |
| 378 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 381 | /// Recursively replaces NULL values within the given initializer list |
| 382 | /// with expressions that perform value-initialization of the |
| 383 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 385 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 386 | InitListExpr *ILE, |
| 387 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 389 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 390 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 391 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 392 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 394 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 395 | const RecordDecl *RDecl = RType->getDecl(); |
| 396 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 397 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 398 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 399 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 400 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 401 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 402 | FieldEnd = RDecl->field_end(); |
| 403 | Field != FieldEnd; ++Field) { |
| 404 | if (Field->hasInClassInitializer()) { |
| 405 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 410 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 411 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 412 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 413 | Field != FieldEnd; ++Field) { |
| 414 | if (Field->isUnnamedBitfield()) |
| 415 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 417 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 418 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 419 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 420 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 421 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 422 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 423 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 424 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 425 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 426 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 427 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 434 | |
| 435 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 437 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 438 | unsigned NumInits = ILE->getNumInits(); |
| 439 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 440 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 441 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 442 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 443 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 444 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 445 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 446 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 447 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 448 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 449 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 450 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 452 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 455 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 456 | if (hadError) |
| 457 | return; |
| 458 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 459 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 460 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 461 | ElementEntity.setElementIndex(Init); |
| 462 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 463 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 464 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 465 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 466 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 467 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 468 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 469 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 470 | hadError = true; |
| 471 | return; |
| 472 | } |
| 473 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 474 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 475 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 476 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 477 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
| 481 | if (hadError) { |
| 482 | // Do nothing |
| 483 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 484 | // For arrays, just set the expression used for value-initialization |
| 485 | // of the "holes" in the array. |
| 486 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 487 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 488 | else |
| 489 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 490 | } else { |
| 491 | // For arrays, just set the expression used for value-initialization |
| 492 | // of the rest of elements and exit. |
| 493 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 494 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 495 | return; |
| 496 | } |
| 497 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 498 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 499 | // Value-initialization requires a constructor call, so |
| 500 | // extend the initializer list to include the constructor |
| 501 | // call and make a note that we'll need to take another pass |
| 502 | // through the initializer list. |
| 503 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 504 | RequiresSecondPass = true; |
| 505 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 506 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 507 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 508 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 509 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 514 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 515 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 516 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 517 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 518 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 519 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 520 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 521 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 523 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 524 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 525 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 526 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 527 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 528 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 529 | bool RequiresSecondPass = false; |
| 530 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 531 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 532 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 533 | RequiresSecondPass); |
| 534 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 538 | // FIXME: use a proper constant |
| 539 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 540 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 541 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 542 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 543 | } |
| 544 | return maxElements; |
| 545 | } |
| 546 | |
| 547 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 548 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 549 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 551 | Field = structDecl->field_begin(), |
| 552 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 553 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 554 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 555 | ++InitializableMembers; |
| 556 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 557 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 558 | return std::min(InitializableMembers, 1); |
| 559 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 562 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 563 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 564 | QualType T, unsigned &Index, |
| 565 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 566 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 567 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 569 | if (T->isArrayType()) |
| 570 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 571 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 572 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 573 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 574 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 575 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 576 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 577 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 578 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 579 | if (!VerifyOnly) |
| 580 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 581 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 582 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 583 | hadError = true; |
| 584 | return; |
| 585 | } |
| 586 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 587 | // Build a structured initializer list corresponding to this subobject. |
| 588 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 590 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 591 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 592 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 593 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 595 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 596 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 597 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 598 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 600 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 601 | |
| 602 | if (VerifyOnly) { |
| 603 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 604 | hadError = true; |
| 605 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 606 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 607 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 608 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 609 | // Update the structured sub-object initializer so that it's ending |
| 610 | // range corresponds with the end of the last initializer it used. |
| 611 | if (EndIndex < ParentIList->getNumInits()) { |
| 612 | SourceLocation EndLoc |
| 613 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 614 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 615 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 617 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 618 | if (T->isArrayType() || T->isRecordType()) { |
| 619 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 620 | AllowBraceElision ? diag::warn_missing_braces : |
| 621 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 622 | << StructuredSubobjectInitList->getSourceRange() |
| 623 | << FixItHint::CreateInsertion( |
| 624 | StructuredSubobjectInitList->getLocStart(), "{") |
| 625 | << FixItHint::CreateInsertion( |
| 626 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 628 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 629 | if (!AllowBraceElision) |
| 630 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 631 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 632 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 635 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 636 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 637 | unsigned &Index, |
| 638 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 639 | unsigned &StructuredIndex, |
| 640 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 641 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 642 | if (!VerifyOnly) { |
| 643 | SyntacticToSemantic[IList] = StructuredList; |
| 644 | StructuredList->setSyntacticForm(IList); |
| 645 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 646 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 647 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 648 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 649 | QualType ExprTy = T; |
| 650 | if (!ExprTy->isArrayType()) |
| 651 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 652 | IList->setType(ExprTy); |
| 653 | StructuredList->setType(ExprTy); |
| 654 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 655 | if (hadError) |
| 656 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 657 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 658 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 659 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 660 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 661 | if (SemaRef.getLangOpts().CPlusPlus || |
| 662 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 663 | IList->getType()->isVectorType())) { |
| 664 | hadError = true; |
| 665 | } |
| 666 | return; |
| 667 | } |
| 668 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 669 | if (StructuredIndex == 1 && |
| 670 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 671 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 672 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 673 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 674 | hadError = true; |
| 675 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 676 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 677 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 678 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 679 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 680 | // Don't complain for incomplete types, since we'll get an error |
| 681 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 682 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 684 | CurrentObjectType->isArrayType()? 0 : |
| 685 | CurrentObjectType->isVectorType()? 1 : |
| 686 | CurrentObjectType->isScalarType()? 2 : |
| 687 | CurrentObjectType->isUnionType()? 3 : |
| 688 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 689 | |
| 690 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 691 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 692 | DK = diag::err_excess_initializers; |
| 693 | hadError = true; |
| 694 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 695 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 696 | DK = diag::err_excess_initializers; |
| 697 | hadError = true; |
| 698 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 700 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 701 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 704 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 705 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 706 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 707 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 708 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 709 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 710 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 713 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 714 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 716 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 717 | unsigned &Index, |
| 718 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 719 | unsigned &StructuredIndex, |
| 720 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 721 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 722 | // Explicitly braced initializer for complex type can be real+imaginary |
| 723 | // parts. |
| 724 | CheckComplexType(Entity, IList, DeclType, Index, |
| 725 | StructuredList, StructuredIndex); |
| 726 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 727 | CheckScalarType(Entity, IList, DeclType, Index, |
| 728 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 729 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 730 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 731 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 732 | } else if (DeclType->isRecordType()) { |
| 733 | assert(DeclType->isAggregateType() && |
| 734 | "non-aggregate records should be handed in CheckSubElementType"); |
| 735 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 736 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 737 | SubobjectIsDesignatorContext, Index, |
| 738 | StructuredList, StructuredIndex, |
| 739 | TopLevelObject); |
| 740 | } else if (DeclType->isArrayType()) { |
| 741 | llvm::APSInt Zero( |
| 742 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 743 | false); |
| 744 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 745 | SubobjectIsDesignatorContext, Index, |
| 746 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 747 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 748 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 749 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 750 | if (!VerifyOnly) |
| 751 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 752 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 753 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 754 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 755 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 756 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 757 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 758 | if (!VerifyOnly) |
| 759 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 760 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 761 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 762 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 763 | if (!VerifyOnly) |
| 764 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 765 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 766 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 770 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 771 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 773 | unsigned &Index, |
| 774 | InitListExpr *StructuredList, |
| 775 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 776 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 777 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 778 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 779 | unsigned newIndex = 0; |
| 780 | unsigned newStructuredIndex = 0; |
| 781 | InitListExpr *newStructuredList |
| 782 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 783 | StructuredList, StructuredIndex, |
| 784 | SubInitList->getSourceRange()); |
| 785 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 786 | newStructuredList, newStructuredIndex); |
| 787 | ++StructuredIndex; |
| 788 | ++Index; |
| 789 | return; |
| 790 | } |
| 791 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 792 | "non-aggregate records are only possible in C++"); |
| 793 | // C++ initialization is handled later. |
| 794 | } |
| 795 | |
| 796 | if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 797 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 798 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 799 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 800 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 801 | StructuredList, StructuredIndex); |
| 802 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 803 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 804 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 805 | // arrayType can be incomplete if we're initializing a flexible |
| 806 | // array member. There's nothing we can do with the completed |
| 807 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 809 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 810 | if (!VerifyOnly) { |
| 811 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 812 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 813 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 814 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 815 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 816 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 817 | |
| 818 | // Fall through for subaggregate initialization. |
| 819 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 820 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 821 | // C++ [dcl.init.aggr]p12: |
| 822 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 823 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 824 | // an initializer-list. If the initializer can initialize a |
| 825 | // member, the member is initialized. [...] |
| 826 | |
| 827 | // FIXME: Better EqualLoc? |
| 828 | InitializationKind Kind = |
| 829 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 830 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 831 | |
| 832 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 833 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 834 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 835 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 836 | if (Result.isInvalid()) |
| 837 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 838 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 839 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 840 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 841 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 842 | ++Index; |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | // Fall through for subaggregate initialization |
| 847 | } else { |
| 848 | // C99 6.7.8p13: |
| 849 | // |
| 850 | // The initializer for a structure or union object that has |
| 851 | // automatic storage duration shall be either an initializer |
| 852 | // list as described below, or a single expression that has |
| 853 | // compatible structure or union type. In the latter case, the |
| 854 | // initial value of the object, including unnamed members, is |
| 855 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 856 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 857 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 858 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 859 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 860 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 861 | if (ExprRes.isInvalid()) |
| 862 | hadError = true; |
| 863 | else { |
| 864 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 865 | if (ExprRes.isInvalid()) |
| 866 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 867 | } |
| 868 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 869 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 870 | ++Index; |
| 871 | return; |
| 872 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 873 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 874 | // Fall through for subaggregate initialization |
| 875 | } |
| 876 | |
| 877 | // C++ [dcl.init.aggr]p12: |
| 878 | // |
| 879 | // [...] Otherwise, if the member is itself a non-empty |
| 880 | // subaggregate, brace elision is assumed and the initializer is |
| 881 | // considered for the initialization of the first member of |
| 882 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 883 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 884 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 885 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 886 | StructuredIndex); |
| 887 | ++StructuredIndex; |
| 888 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 889 | if (!VerifyOnly) { |
| 890 | // We cannot initialize this element, so let |
| 891 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 892 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 893 | SemaRef.Owned(expr), |
| 894 | /*TopLevelOfInitList=*/true); |
| 895 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 896 | hadError = true; |
| 897 | ++Index; |
| 898 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 899 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 902 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 903 | InitListExpr *IList, QualType DeclType, |
| 904 | unsigned &Index, |
| 905 | InitListExpr *StructuredList, |
| 906 | unsigned &StructuredIndex) { |
| 907 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 908 | |
| 909 | // As an extension, clang supports complex initializers, which initialize |
| 910 | // a complex number component-wise. When an explicit initializer list for |
| 911 | // a complex number contains two two initializers, this extension kicks in: |
| 912 | // it exepcts the initializer list to contain two elements convertible to |
| 913 | // the element type of the complex type. The first element initializes |
| 914 | // the real part, and the second element intitializes the imaginary part. |
| 915 | |
| 916 | if (IList->getNumInits() != 2) |
| 917 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 918 | StructuredIndex); |
| 919 | |
| 920 | // This is an extension in C. (The builtin _Complex type does not exist |
| 921 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 922 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 923 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 924 | << IList->getSourceRange(); |
| 925 | |
| 926 | // Initialize the complex number. |
| 927 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 928 | InitializedEntity ElementEntity = |
| 929 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 930 | |
| 931 | for (unsigned i = 0; i < 2; ++i) { |
| 932 | ElementEntity.setElementIndex(Index); |
| 933 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 934 | StructuredList, StructuredIndex); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 939 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 940 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 941 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 942 | InitListExpr *StructuredList, |
| 943 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 944 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 945 | if (!VerifyOnly) |
| 946 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 947 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 948 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 949 | diag::err_empty_scalar_initializer) |
| 950 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 951 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 952 | ++Index; |
| 953 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 954 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 955 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 956 | |
| 957 | Expr *expr = IList->getInit(Index); |
| 958 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 959 | if (!VerifyOnly) |
| 960 | SemaRef.Diag(SubIList->getLocStart(), |
| 961 | diag::warn_many_braces_around_scalar_init) |
| 962 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 963 | |
| 964 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 965 | StructuredIndex); |
| 966 | return; |
| 967 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 968 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 969 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 970 | diag::err_designator_for_scalar_init) |
| 971 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 972 | hadError = true; |
| 973 | ++Index; |
| 974 | ++StructuredIndex; |
| 975 | return; |
| 976 | } |
| 977 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 978 | if (VerifyOnly) { |
| 979 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 980 | hadError = true; |
| 981 | ++Index; |
| 982 | return; |
| 983 | } |
| 984 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 985 | ExprResult Result = |
| 986 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 987 | SemaRef.Owned(expr), |
| 988 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 989 | |
| 990 | Expr *ResultExpr = 0; |
| 991 | |
| 992 | if (Result.isInvalid()) |
| 993 | hadError = true; // types weren't compatible. |
| 994 | else { |
| 995 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 997 | if (ResultExpr != expr) { |
| 998 | // The type was promoted, update initializer list. |
| 999 | IList->setInit(Index, ResultExpr); |
| 1000 | } |
| 1001 | } |
| 1002 | if (hadError) |
| 1003 | ++StructuredIndex; |
| 1004 | else |
| 1005 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1006 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1009 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1010 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1011 | unsigned &Index, |
| 1012 | InitListExpr *StructuredList, |
| 1013 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1014 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1015 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1016 | // general, it would be useful to pass location information down the stack, |
| 1017 | // so that we know the location (or decl) of the "current object" being |
| 1018 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1019 | if (!VerifyOnly) |
| 1020 | SemaRef.Diag(IList->getLocStart(), |
| 1021 | diag::err_init_reference_member_uninitialized) |
| 1022 | << DeclType |
| 1023 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1024 | hadError = true; |
| 1025 | ++Index; |
| 1026 | ++StructuredIndex; |
| 1027 | return; |
| 1028 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1029 | |
| 1030 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1031 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1032 | if (!VerifyOnly) |
| 1033 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1034 | << DeclType << IList->getSourceRange(); |
| 1035 | hadError = true; |
| 1036 | ++Index; |
| 1037 | ++StructuredIndex; |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | if (VerifyOnly) { |
| 1042 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1043 | hadError = true; |
| 1044 | ++Index; |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | ExprResult Result = |
| 1049 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1050 | SemaRef.Owned(expr), |
| 1051 | /*TopLevelOfInitList=*/true); |
| 1052 | |
| 1053 | if (Result.isInvalid()) |
| 1054 | hadError = true; |
| 1055 | |
| 1056 | expr = Result.takeAs<Expr>(); |
| 1057 | IList->setInit(Index, expr); |
| 1058 | |
| 1059 | if (hadError) |
| 1060 | ++StructuredIndex; |
| 1061 | else |
| 1062 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1063 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1066 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1067 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1068 | unsigned &Index, |
| 1069 | InitListExpr *StructuredList, |
| 1070 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1071 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1072 | unsigned maxElements = VT->getNumElements(); |
| 1073 | unsigned numEltsInit = 0; |
| 1074 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1075 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1076 | if (Index >= IList->getNumInits()) { |
| 1077 | // Make sure the element type can be value-initialized. |
| 1078 | if (VerifyOnly) |
| 1079 | CheckValueInitializable( |
| 1080 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1084 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1085 | // If the initializing element is a vector, try to copy-initialize |
| 1086 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1087 | Expr *Init = IList->getInit(Index); |
| 1088 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1089 | if (VerifyOnly) { |
| 1090 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1091 | hadError = true; |
| 1092 | ++Index; |
| 1093 | return; |
| 1094 | } |
| 1095 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1096 | ExprResult Result = |
| 1097 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1098 | SemaRef.Owned(Init), |
| 1099 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1100 | |
| 1101 | Expr *ResultExpr = 0; |
| 1102 | if (Result.isInvalid()) |
| 1103 | hadError = true; // types weren't compatible. |
| 1104 | else { |
| 1105 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1106 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1107 | if (ResultExpr != Init) { |
| 1108 | // The type was promoted, update initializer list. |
| 1109 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1112 | if (hadError) |
| 1113 | ++StructuredIndex; |
| 1114 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1115 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1116 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1117 | ++Index; |
| 1118 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1121 | InitializedEntity ElementEntity = |
| 1122 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1124 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1125 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1126 | if (Index >= IList->getNumInits()) { |
| 1127 | if (VerifyOnly) |
| 1128 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1129 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1130 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1132 | ElementEntity.setElementIndex(Index); |
| 1133 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1134 | StructuredList, StructuredIndex); |
| 1135 | } |
| 1136 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1137 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1138 | |
| 1139 | InitializedEntity ElementEntity = |
| 1140 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1141 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1142 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1143 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1144 | // Don't attempt to go past the end of the init list |
| 1145 | if (Index >= IList->getNumInits()) |
| 1146 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1147 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1148 | ElementEntity.setElementIndex(Index); |
| 1149 | |
| 1150 | QualType IType = IList->getInit(Index)->getType(); |
| 1151 | if (!IType->isVectorType()) { |
| 1152 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1153 | StructuredList, StructuredIndex); |
| 1154 | ++numEltsInit; |
| 1155 | } else { |
| 1156 | QualType VecType; |
| 1157 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1158 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1159 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1160 | if (IType->isExtVectorType()) |
| 1161 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1162 | else |
| 1163 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1164 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1165 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1166 | StructuredList, StructuredIndex); |
| 1167 | numEltsInit += numIElts; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1172 | if (numEltsInit != maxElements) { |
| 1173 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1174 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1175 | diag::err_vector_incorrect_num_initializers) |
| 1176 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1177 | hadError = true; |
| 1178 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1181 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1182 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1183 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1185 | unsigned &Index, |
| 1186 | InitListExpr *StructuredList, |
| 1187 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1188 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1189 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1190 | // Check for the special-case of initializing an array with a string. |
| 1191 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1192 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1193 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1194 | // We place the string literal directly into the resulting |
| 1195 | // initializer list. This is the only place where the structure |
| 1196 | // of the structured initializer list doesn't match exactly, |
| 1197 | // because doing so would involve allocating one character |
| 1198 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1199 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1200 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1201 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1202 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1203 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1204 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1205 | return; |
| 1206 | } |
| 1207 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1208 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1209 | // Check for VLAs; in standard C it would be possible to check this |
| 1210 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1211 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1212 | if (!VerifyOnly) |
| 1213 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1214 | diag::err_variable_object_no_init) |
| 1215 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1216 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1217 | ++Index; |
| 1218 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1219 | return; |
| 1220 | } |
| 1221 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1222 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1223 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1224 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1225 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1226 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1227 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1228 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1229 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1230 | maxElementsKnown = true; |
| 1231 | } |
| 1232 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1233 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1234 | while (Index < IList->getNumInits()) { |
| 1235 | Expr *Init = IList->getInit(Index); |
| 1236 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1237 | // If we're not the subobject that matches up with the '{' for |
| 1238 | // the designator, we shouldn't be handling the |
| 1239 | // designator. Return immediately. |
| 1240 | if (!SubobjectIsDesignatorContext) |
| 1241 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1243 | // Handle this designated initializer. elementIndex will be |
| 1244 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1245 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1246 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1247 | StructuredList, StructuredIndex, true, |
| 1248 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1249 | hadError = true; |
| 1250 | continue; |
| 1251 | } |
| 1252 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1253 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1254 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1255 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1256 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1257 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1258 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1259 | // If the array is of incomplete type, keep track of the number of |
| 1260 | // elements in the initializer. |
| 1261 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1262 | maxElements = elementIndex; |
| 1263 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1264 | continue; |
| 1265 | } |
| 1266 | |
| 1267 | // If we know the maximum number of elements, and we've already |
| 1268 | // hit it, stop consuming elements in the initializer list. |
| 1269 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1270 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1272 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1273 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1274 | Entity); |
| 1275 | // Check this element. |
| 1276 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1277 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1278 | ++elementIndex; |
| 1279 | |
| 1280 | // If the array is of incomplete type, keep track of the number of |
| 1281 | // elements in the initializer. |
| 1282 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1283 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1284 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1285 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1286 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1287 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1288 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1289 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1290 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1291 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1292 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1293 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1294 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1295 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1297 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1298 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1299 | if (!hadError && VerifyOnly) { |
| 1300 | // Check if there are any members of the array that get value-initialized. |
| 1301 | // If so, check if doing that is possible. |
| 1302 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1303 | if (maxElementsKnown && elementIndex < maxElements) |
| 1304 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1305 | SemaRef.Context, 0, Entity)); |
| 1306 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1309 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1310 | Expr *InitExpr, |
| 1311 | FieldDecl *Field, |
| 1312 | bool TopLevelObject) { |
| 1313 | // Handle GNU flexible array initializers. |
| 1314 | unsigned FlexArrayDiag; |
| 1315 | if (isa<InitListExpr>(InitExpr) && |
| 1316 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1317 | // Empty flexible array init always allowed as an extension |
| 1318 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1319 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1320 | // Disallow flexible array init in C++; it is not required for gcc |
| 1321 | // compatibility, and it needs work to IRGen correctly in general. |
| 1322 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1323 | } else if (!TopLevelObject) { |
| 1324 | // Disallow flexible array init on non-top-level object |
| 1325 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1326 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1327 | // Disallow flexible array init on anything which is not a variable. |
| 1328 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1329 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1330 | // Disallow flexible array init on local variables. |
| 1331 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1332 | } else { |
| 1333 | // Allow other cases. |
| 1334 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1335 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1336 | |
| 1337 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1338 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1339 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1340 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1341 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1342 | << Field; |
| 1343 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1344 | |
| 1345 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1346 | } |
| 1347 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1348 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1349 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1351 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1353 | unsigned &Index, |
| 1354 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1355 | unsigned &StructuredIndex, |
| 1356 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1357 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1359 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1360 | // confusion, we forgo checking the intializer for the entire record. |
| 1361 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1362 | // Assume it was supposed to consume a single initializer. |
| 1363 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1364 | hadError = true; |
| 1365 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1367 | |
| 1368 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1369 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1370 | |
| 1371 | // If there's a default initializer, use it. |
| 1372 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1373 | if (VerifyOnly) |
| 1374 | return; |
| 1375 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1376 | Field != FieldEnd; ++Field) { |
| 1377 | if (Field->hasInClassInitializer()) { |
| 1378 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1379 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1380 | return; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1386 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1387 | Field != FieldEnd; ++Field) { |
| 1388 | if (Field->getDeclName()) { |
| 1389 | if (VerifyOnly) |
| 1390 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1391 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1392 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1393 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1394 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | return; |
| 1398 | } |
| 1399 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1400 | // If structDecl is a forward declaration, this loop won't do |
| 1401 | // anything except look at designated initializers; That's okay, |
| 1402 | // because an error should get printed out elsewhere. It might be |
| 1403 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1404 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1405 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1406 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1407 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1408 | while (Index < IList->getNumInits()) { |
| 1409 | Expr *Init = IList->getInit(Index); |
| 1410 | |
| 1411 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1412 | // If we're not the subobject that matches up with the '{' for |
| 1413 | // the designator, we shouldn't be handling the |
| 1414 | // designator. Return immediately. |
| 1415 | if (!SubobjectIsDesignatorContext) |
| 1416 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1418 | // Handle this designated initializer. Field will be updated to |
| 1419 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1420 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1421 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1422 | StructuredList, StructuredIndex, |
| 1423 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1424 | hadError = true; |
| 1425 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1426 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Disable check for missing fields when designators are used. |
| 1429 | // This matches gcc behaviour. |
| 1430 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1431 | continue; |
| 1432 | } |
| 1433 | |
| 1434 | if (Field == FieldEnd) { |
| 1435 | // We've run out of fields. We're done. |
| 1436 | break; |
| 1437 | } |
| 1438 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1439 | // We've already initialized a member of a union. We're done. |
| 1440 | if (InitializedSomething && DeclType->isUnionType()) |
| 1441 | break; |
| 1442 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1443 | // If we've hit the flexible array member at the end, we're done. |
| 1444 | if (Field->getType()->isIncompleteArrayType()) |
| 1445 | break; |
| 1446 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1447 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1448 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1449 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1450 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1451 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1452 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1453 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1454 | bool InvalidUse; |
| 1455 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1456 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1457 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1458 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1459 | IList->getInit(Index)->getLocStart()); |
| 1460 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1461 | ++Index; |
| 1462 | ++Field; |
| 1463 | hadError = true; |
| 1464 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1465 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1466 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1467 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1468 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1469 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1470 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1471 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1472 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1473 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1474 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1475 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1476 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1477 | |
| 1478 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1479 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1480 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1481 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1482 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1483 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1484 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1485 | // It is possible we have one or more unnamed bitfields remaining. |
| 1486 | // Find first (if any) named field and emit warning. |
| 1487 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1488 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1489 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1490 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1491 | diag::warn_missing_field_initializers) << it->getName(); |
| 1492 | break; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1497 | // Check that any remaining fields can be value-initialized. |
| 1498 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1499 | !Field->getType()->isIncompleteArrayType()) { |
| 1500 | // FIXME: Should check for holes left by designated initializers too. |
| 1501 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1502 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1503 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1504 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1509 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1512 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1513 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1514 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1515 | ++Index; |
| 1516 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1519 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1520 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1521 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1522 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1523 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1524 | StructuredList, StructuredIndex); |
| 1525 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1526 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1527 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1528 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1530 | /// \brief Expand a field designator that refers to a member of an |
| 1531 | /// anonymous struct or union into a series of field designators that |
| 1532 | /// refers to the field within the appropriate subobject. |
| 1533 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1534 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | DesignatedInitExpr *DIE, |
| 1536 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1537 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1538 | typedef DesignatedInitExpr::Designator Designator; |
| 1539 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1540 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1541 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1542 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1543 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1544 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1546 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1547 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1548 | else |
| 1549 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1550 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1551 | assert(isa<FieldDecl>(*PI)); |
| 1552 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | // Expand the current designator into the set of replacement |
| 1556 | // designators, so we have a full subobject path down to where the |
| 1557 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1558 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1559 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1560 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1562 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1563 | /// corresponds to FieldName. |
| 1564 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1565 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1566 | if (!FieldName) |
| 1567 | return 0; |
| 1568 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1569 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1570 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1571 | while (IndirectFieldDecl *IF = |
| 1572 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1573 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1574 | return IF; |
| 1575 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1576 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1577 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1580 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1581 | DesignatedInitExpr *DIE) { |
| 1582 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1583 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1584 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1585 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1586 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1587 | DIE->size(), IndexExprs, |
| 1588 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1589 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1590 | } |
| 1591 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1592 | namespace { |
| 1593 | |
| 1594 | // Callback to only accept typo corrections that are for field members of |
| 1595 | // the given struct or union. |
| 1596 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1597 | public: |
| 1598 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1599 | : Record(RD) {} |
| 1600 | |
| 1601 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1602 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1603 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1604 | } |
| 1605 | |
| 1606 | private: |
| 1607 | RecordDecl *Record; |
| 1608 | }; |
| 1609 | |
| 1610 | } |
| 1611 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1612 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1613 | /// |
| 1614 | /// Determines whether the designated initializer @p DIE, which |
| 1615 | /// resides at the given @p Index within the initializer list @p |
| 1616 | /// IList, is well-formed for a current object of type @p DeclType |
| 1617 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1619 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1620 | /// |
| 1621 | /// @param IList The initializer list in which this designated |
| 1622 | /// initializer occurs. |
| 1623 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1624 | /// @param DIE The designated initializer expression. |
| 1625 | /// |
| 1626 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1627 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1628 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1629 | /// into which the designation in @p DIE should refer. |
| 1630 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1631 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1632 | /// a field, this will be set to the field declaration corresponding |
| 1633 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1634 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1635 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1636 | /// DIE is an array designator or GNU array-range designator, this |
| 1637 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1638 | /// |
| 1639 | /// @param Index Index into @p IList where the designated initializer |
| 1640 | /// @p DIE occurs. |
| 1641 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1642 | /// @param StructuredList The initializer list expression that |
| 1643 | /// describes all of the subobject initializers in the order they'll |
| 1644 | /// actually be initialized. |
| 1645 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1646 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1648 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1649 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1650 | DesignatedInitExpr *DIE, |
| 1651 | unsigned DesigIdx, |
| 1652 | QualType &CurrentObjectType, |
| 1653 | RecordDecl::field_iterator *NextField, |
| 1654 | llvm::APSInt *NextElementIndex, |
| 1655 | unsigned &Index, |
| 1656 | InitListExpr *StructuredList, |
| 1657 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1658 | bool FinishSubobjectInit, |
| 1659 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1660 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1661 | // Check the actual initialization for the designated object type. |
| 1662 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1663 | |
| 1664 | // Temporarily remove the designator expression from the |
| 1665 | // initializer list that the child calls see, so that we don't try |
| 1666 | // to re-process the designator. |
| 1667 | unsigned OldIndex = Index; |
| 1668 | IList->setInit(OldIndex, DIE->getInit()); |
| 1669 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1670 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1671 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Restore the designated initializer expression in the syntactic |
| 1674 | // form of the initializer list. |
| 1675 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1676 | DIE->setInit(IList->getInit(OldIndex)); |
| 1677 | IList->setInit(OldIndex, DIE); |
| 1678 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1679 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1682 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1683 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1684 | if (!VerifyOnly) { |
| 1685 | assert((IsFirstDesignator || StructuredList) && |
| 1686 | "Need a non-designated initializer list to start from"); |
| 1687 | |
| 1688 | // Determine the structural initializer list that corresponds to the |
| 1689 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1690 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1691 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1692 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1693 | SourceRange(D->getLocStart(), |
| 1694 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1695 | assert(StructuredList && "Expected a structured initializer list"); |
| 1696 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1698 | if (D->isFieldDesignator()) { |
| 1699 | // C99 6.7.8p7: |
| 1700 | // |
| 1701 | // If a designator has the form |
| 1702 | // |
| 1703 | // . identifier |
| 1704 | // |
| 1705 | // then the current object (defined below) shall have |
| 1706 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1708 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1709 | if (!RT) { |
| 1710 | SourceLocation Loc = D->getDotLoc(); |
| 1711 | if (Loc.isInvalid()) |
| 1712 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1713 | if (!VerifyOnly) |
| 1714 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1715 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1716 | ++Index; |
| 1717 | return true; |
| 1718 | } |
| 1719 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1720 | // Note: we perform a linear search of the fields here, despite |
| 1721 | // the fact that we have a faster lookup method, because we always |
| 1722 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1723 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1724 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1725 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1727 | Field = RT->getDecl()->field_begin(), |
| 1728 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1729 | for (; Field != FieldEnd; ++Field) { |
| 1730 | if (Field->isUnnamedBitfield()) |
| 1731 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1732 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1733 | // If we find a field representing an anonymous field, look in the |
| 1734 | // IndirectFieldDecl that follow for the designated initializer. |
| 1735 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1736 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1737 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1738 | // In verify mode, don't modify the original. |
| 1739 | if (VerifyOnly) |
| 1740 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1741 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1742 | D = DIE->getDesignator(DesigIdx); |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1746 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1747 | break; |
| 1748 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1749 | break; |
| 1750 | |
| 1751 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1754 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1755 | if (VerifyOnly) { |
| 1756 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1757 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1758 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1760 | // There was no normal field in the struct with the designated |
| 1761 | // name. Perform another lookup for this name, which may find |
| 1762 | // something that we can't designate (e.g., a member function), |
| 1763 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1765 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1766 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1767 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1768 | // Name lookup didn't find anything. Determine whether this |
| 1769 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1770 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1771 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1772 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1773 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1774 | RT->getDecl()); |
| 1775 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1776 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1777 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1778 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1779 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1780 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1782 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1783 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1784 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1785 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1786 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1787 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1788 | } else { |
| 1789 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1790 | << FieldName << CurrentObjectType; |
| 1791 | ++Index; |
| 1792 | return true; |
| 1793 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1794 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1796 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1797 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1798 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1799 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1800 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1801 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1802 | ++Index; |
| 1803 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1804 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1805 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1806 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1807 | // The replacement field comes from typo correction; find it |
| 1808 | // in the list of fields. |
| 1809 | FieldIndex = 0; |
| 1810 | Field = RT->getDecl()->field_begin(); |
| 1811 | for (; Field != FieldEnd; ++Field) { |
| 1812 | if (Field->isUnnamedBitfield()) |
| 1813 | continue; |
| 1814 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1815 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1816 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1817 | break; |
| 1818 | |
| 1819 | ++FieldIndex; |
| 1820 | } |
| 1821 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1822 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1823 | |
| 1824 | // All of the fields of a union are located at the same place in |
| 1825 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1826 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1827 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1828 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1829 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1830 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1832 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1833 | bool InvalidUse; |
| 1834 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1835 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1836 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1837 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1838 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1839 | ++Index; |
| 1840 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1841 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1842 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1843 | if (!VerifyOnly) { |
| 1844 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1845 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1847 | // Make sure that our non-designated initializer list has space |
| 1848 | // for a subobject corresponding to this field. |
| 1849 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1850 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1851 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1853 | // This designator names a flexible array member. |
| 1854 | if (Field->getType()->isIncompleteArrayType()) { |
| 1855 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1856 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1857 | // We can't designate an object within the flexible array |
| 1858 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1859 | if (!VerifyOnly) { |
| 1860 | DesignatedInitExpr::Designator *NextD |
| 1861 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1862 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1863 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1864 | << SourceRange(NextD->getLocStart(), |
| 1865 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1866 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1867 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1868 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1869 | Invalid = true; |
| 1870 | } |
| 1871 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1872 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1873 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1874 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1875 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1876 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1877 | diag::err_flexible_array_init_needs_braces) |
| 1878 | << DIE->getInit()->getSourceRange(); |
| 1879 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1880 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1881 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1882 | Invalid = true; |
| 1883 | } |
| 1884 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1885 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1886 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1887 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1888 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1889 | |
| 1890 | if (Invalid) { |
| 1891 | ++Index; |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | // Initialize the array. |
| 1896 | bool prevHadError = hadError; |
| 1897 | unsigned newStructuredIndex = FieldIndex; |
| 1898 | unsigned OldIndex = Index; |
| 1899 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1900 | |
| 1901 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1902 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1903 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1904 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1906 | IList->setInit(OldIndex, DIE); |
| 1907 | if (hadError && !prevHadError) { |
| 1908 | ++Field; |
| 1909 | ++FieldIndex; |
| 1910 | if (NextField) |
| 1911 | *NextField = Field; |
| 1912 | StructuredIndex = FieldIndex; |
| 1913 | return true; |
| 1914 | } |
| 1915 | } else { |
| 1916 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1917 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1918 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1919 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1920 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1921 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1922 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1923 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1924 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1925 | true, false)) |
| 1926 | return true; |
| 1927 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1928 | |
| 1929 | // Find the position of the next field to be initialized in this |
| 1930 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1931 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1932 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1933 | |
| 1934 | // If this the first designator, our caller will continue checking |
| 1935 | // the rest of this struct/class/union subobject. |
| 1936 | if (IsFirstDesignator) { |
| 1937 | if (NextField) |
| 1938 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1939 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1940 | return false; |
| 1941 | } |
| 1942 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1943 | if (!FinishSubobjectInit) |
| 1944 | return false; |
| 1945 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1946 | // We've already initialized something in the union; we're done. |
| 1947 | if (RT->getDecl()->isUnion()) |
| 1948 | return hadError; |
| 1949 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1950 | // Check the remaining fields within this class/struct/union subobject. |
| 1951 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1952 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1953 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1954 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1955 | return hadError && !prevHadError; |
| 1956 | } |
| 1957 | |
| 1958 | // C99 6.7.8p6: |
| 1959 | // |
| 1960 | // If a designator has the form |
| 1961 | // |
| 1962 | // [ constant-expression ] |
| 1963 | // |
| 1964 | // then the current object (defined below) shall have array |
| 1965 | // type and the expression shall be an integer constant |
| 1966 | // expression. If the array is of unknown size, any |
| 1967 | // nonnegative value is valid. |
| 1968 | // |
| 1969 | // Additionally, cope with the GNU extension that permits |
| 1970 | // designators of the form |
| 1971 | // |
| 1972 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1973 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1974 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1975 | if (!VerifyOnly) |
| 1976 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1977 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1978 | ++Index; |
| 1979 | return true; |
| 1980 | } |
| 1981 | |
| 1982 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1983 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1984 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1985 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1986 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1987 | DesignatedEndIndex = DesignatedStartIndex; |
| 1988 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1990 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1992 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1994 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1995 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1997 | // Codegen can't handle evaluating array range designators that have side |
| 1998 | // effects, because we replicate the AST value for each initialized element. |
| 1999 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2000 | // elements with something that has a side effect, so codegen can emit an |
| 2001 | // "error unsupported" error instead of miscompiling the app. |
| 2002 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2003 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2004 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2007 | if (isa<ConstantArrayType>(AT)) { |
| 2008 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2009 | DesignatedStartIndex |
| 2010 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2011 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2012 | DesignatedEndIndex |
| 2013 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2014 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2015 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2016 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2017 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2018 | diag::err_array_designator_too_large) |
| 2019 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2020 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2021 | ++Index; |
| 2022 | return true; |
| 2023 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2024 | } else { |
| 2025 | // Make sure the bit-widths and signedness match. |
| 2026 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2027 | DesignatedEndIndex |
| 2028 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2029 | else if (DesignatedStartIndex.getBitWidth() < |
| 2030 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2031 | DesignatedStartIndex |
| 2032 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2033 | DesignatedStartIndex.setIsUnsigned(true); |
| 2034 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2035 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2037 | // Make sure that our non-designated initializer list has space |
| 2038 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2039 | if (!VerifyOnly && |
| 2040 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2042 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2043 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2044 | // Repeatedly perform subobject initializations in the range |
| 2045 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2046 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2047 | // Move to the next designator |
| 2048 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2049 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2050 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2051 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2052 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2054 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2055 | // Recurse to check later designated subobjects. |
| 2056 | QualType ElementType = AT->getElementType(); |
| 2057 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2058 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2059 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2060 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2061 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2062 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2063 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2064 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2065 | return true; |
| 2066 | |
| 2067 | // Move to the next index in the array that we'll be initializing. |
| 2068 | ++DesignatedStartIndex; |
| 2069 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2070 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2071 | |
| 2072 | // If this the first designator, our caller will continue checking |
| 2073 | // the rest of this array subobject. |
| 2074 | if (IsFirstDesignator) { |
| 2075 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2076 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2077 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2078 | return false; |
| 2079 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2081 | if (!FinishSubobjectInit) |
| 2082 | return false; |
| 2083 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2084 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2085 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2087 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2088 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2092 | // Get the structured initializer list for a subobject of type |
| 2093 | // @p CurrentObjectType. |
| 2094 | InitListExpr * |
| 2095 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2096 | QualType CurrentObjectType, |
| 2097 | InitListExpr *StructuredList, |
| 2098 | unsigned StructuredIndex, |
| 2099 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2100 | if (VerifyOnly) |
| 2101 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2102 | Expr *ExistingInit = 0; |
| 2103 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2104 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2105 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2106 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2108 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2109 | return Result; |
| 2110 | |
| 2111 | if (ExistingInit) { |
| 2112 | // We are creating an initializer list that initializes the |
| 2113 | // subobjects of the current object, but there was already an |
| 2114 | // initialization that completely initialized the current |
| 2115 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2117 | // struct X { int a, b; }; |
| 2118 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2120 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2121 | // designated initializer re-initializes the whole |
| 2122 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2124 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2125 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2126 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2127 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2128 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2129 | << ExistingInit->getSourceRange(); |
| 2130 | } |
| 2131 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2133 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2134 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2135 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2136 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2137 | QualType ResultType = CurrentObjectType; |
| 2138 | if (!ResultType->isArrayType()) |
| 2139 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2140 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2142 | // Pre-allocate storage for the structured initializer list. |
| 2143 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2144 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2145 | bool GotNumInits = false; |
| 2146 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2147 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2148 | GotNumInits = true; |
| 2149 | } else if (Index < IList->getNumInits()) { |
| 2150 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2151 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2152 | GotNumInits = true; |
| 2153 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2157 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2158 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2159 | NumElements = CAType->getSize().getZExtValue(); |
| 2160 | // Simple heuristic so that we don't allocate a very large |
| 2161 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2162 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2163 | NumElements = 0; |
| 2164 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2165 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2166 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2167 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2168 | RecordDecl *RDecl = RType->getDecl(); |
| 2169 | if (RDecl->isUnion()) |
| 2170 | NumElements = 1; |
| 2171 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2173 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2176 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2178 | // Link this new initializer list into the structured initializer |
| 2179 | // lists. |
| 2180 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2181 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2182 | else { |
| 2183 | Result->setSyntacticForm(IList); |
| 2184 | SyntacticToSemantic[IList] = Result; |
| 2185 | } |
| 2186 | |
| 2187 | return Result; |
| 2188 | } |
| 2189 | |
| 2190 | /// Update the initializer at index @p StructuredIndex within the |
| 2191 | /// structured initializer list to the value @p expr. |
| 2192 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2193 | unsigned &StructuredIndex, |
| 2194 | Expr *expr) { |
| 2195 | // No structured initializer list to update |
| 2196 | if (!StructuredList) |
| 2197 | return; |
| 2198 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2199 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2200 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2201 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2202 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2203 | diag::warn_initializer_overrides) |
| 2204 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2205 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2206 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2207 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2208 | << PrevInit->getSourceRange(); |
| 2209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2211 | ++StructuredIndex; |
| 2212 | } |
| 2213 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2214 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2215 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2216 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2217 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2218 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2219 | /// added, on success. If everything went okay, Value will receive the |
| 2220 | /// value of the constant expression. |
| 2221 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2222 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2223 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2226 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2227 | if (Result.isInvalid()) |
| 2228 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2230 | if (Value.isSigned() && Value.isNegative()) |
| 2231 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2232 | << Value.toString(10) << Index->getSourceRange(); |
| 2233 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2234 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2235 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2238 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2239 | SourceLocation Loc, |
| 2240 | bool GNUSyntax, |
| 2241 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2242 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2243 | |
| 2244 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2245 | SmallVector<ASTDesignator, 32> Designators; |
| 2246 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2247 | |
| 2248 | // Build designators and check array designator expressions. |
| 2249 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2250 | const Designator &D = Desig.getDesignator(Idx); |
| 2251 | switch (D.getKind()) { |
| 2252 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2254 | D.getFieldLoc())); |
| 2255 | break; |
| 2256 | |
| 2257 | case Designator::ArrayDesignator: { |
| 2258 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2259 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2260 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2261 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2262 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2263 | Invalid = true; |
| 2264 | else { |
| 2265 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2267 | D.getRBracketLoc())); |
| 2268 | InitExpressions.push_back(Index); |
| 2269 | } |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | case Designator::ArrayRangeDesignator: { |
| 2274 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2275 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2276 | llvm::APSInt StartValue; |
| 2277 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2278 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2279 | StartIndex->isValueDependent(); |
| 2280 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2281 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2282 | if (!StartDependent) |
| 2283 | StartIndex = |
| 2284 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2285 | if (!EndDependent) |
| 2286 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2287 | |
| 2288 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2289 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2290 | else { |
| 2291 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2292 | if (StartDependent || EndDependent) { |
| 2293 | // Nothing to compute. |
| 2294 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2295 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2296 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2297 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2298 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2299 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2300 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2302 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2303 | Invalid = true; |
| 2304 | } else { |
| 2305 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2306 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2307 | D.getEllipsisLoc(), |
| 2308 | D.getRBracketLoc())); |
| 2309 | InitExpressions.push_back(StartIndex); |
| 2310 | InitExpressions.push_back(EndIndex); |
| 2311 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2312 | } |
| 2313 | break; |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | if (Invalid || Init.isInvalid()) |
| 2319 | return ExprError(); |
| 2320 | |
| 2321 | // Clear out the expressions within the designation. |
| 2322 | Desig.ClearExprs(*this); |
| 2323 | |
| 2324 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2325 | = DesignatedInitExpr::Create(Context, |
| 2326 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2327 | InitExpressions, Loc, GNUSyntax, |
| 2328 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2329 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2330 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2331 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2332 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2333 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2334 | return Owned(DIE); |
| 2335 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2337 | //===----------------------------------------------------------------------===// |
| 2338 | // Initialization entity |
| 2339 | //===----------------------------------------------------------------------===// |
| 2340 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2342 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2344 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2345 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2346 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2347 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2348 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2349 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2350 | Type = VT->getElementType(); |
| 2351 | } else { |
| 2352 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2353 | assert(CT && "Unexpected type"); |
| 2354 | Kind = EK_ComplexElement; |
| 2355 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2356 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2359 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2360 | CXXBaseSpecifier *Base, |
| 2361 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2362 | { |
| 2363 | InitializedEntity Result; |
| 2364 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2365 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2366 | if (IsInheritedVirtualBase) |
| 2367 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2369 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2370 | return Result; |
| 2371 | } |
| 2372 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2373 | DeclarationName InitializedEntity::getName() const { |
| 2374 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2375 | case EK_Parameter: { |
| 2376 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2377 | return (D ? D->getDeclName() : DeclarationName()); |
| 2378 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2379 | |
| 2380 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2381 | case EK_Member: |
| 2382 | return VariableOrMember->getDeclName(); |
| 2383 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2384 | case EK_LambdaCapture: |
| 2385 | return Capture.Var->getDeclName(); |
| 2386 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2387 | case EK_Result: |
| 2388 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2389 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2390 | case EK_Temporary: |
| 2391 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2392 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2393 | case EK_ArrayElement: |
| 2394 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2395 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2396 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2397 | return DeclarationName(); |
| 2398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2399 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2400 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2403 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2404 | switch (getKind()) { |
| 2405 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2406 | case EK_Member: |
| 2407 | return VariableOrMember; |
| 2408 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2409 | case EK_Parameter: |
| 2410 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2411 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2412 | case EK_Result: |
| 2413 | case EK_Exception: |
| 2414 | case EK_New: |
| 2415 | case EK_Temporary: |
| 2416 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2417 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2418 | case EK_ArrayElement: |
| 2419 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2420 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2421 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2422 | case EK_LambdaCapture: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2423 | return 0; |
| 2424 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2425 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2426 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2429 | bool InitializedEntity::allowsNRVO() const { |
| 2430 | switch (getKind()) { |
| 2431 | case EK_Result: |
| 2432 | case EK_Exception: |
| 2433 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2434 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2435 | case EK_Variable: |
| 2436 | case EK_Parameter: |
| 2437 | case EK_Member: |
| 2438 | case EK_New: |
| 2439 | case EK_Temporary: |
| 2440 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2441 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2442 | case EK_ArrayElement: |
| 2443 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2444 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2445 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2446 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | return false; |
| 2451 | } |
| 2452 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2453 | //===----------------------------------------------------------------------===// |
| 2454 | // Initialization sequence |
| 2455 | //===----------------------------------------------------------------------===// |
| 2456 | |
| 2457 | void InitializationSequence::Step::Destroy() { |
| 2458 | switch (Kind) { |
| 2459 | case SK_ResolveAddressOfOverloadedFunction: |
| 2460 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2461 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2462 | case SK_CastDerivedToBaseLValue: |
| 2463 | case SK_BindReference: |
| 2464 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2465 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2466 | case SK_UserConversion: |
| 2467 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2468 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2469 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2470 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2471 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2472 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2473 | case SK_UnwrapInitList: |
| 2474 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2475 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2476 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2477 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2478 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2479 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2480 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2481 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2482 | case SK_PassByIndirectCopyRestore: |
| 2483 | case SK_PassByIndirectRestore: |
| 2484 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2485 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2486 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2487 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2488 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2490 | case SK_ConversionSequence: |
| 2491 | delete ICS; |
| 2492 | } |
| 2493 | } |
| 2494 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2495 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2496 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2500 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2501 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2502 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2503 | switch (getFailureKind()) { |
| 2504 | case FK_TooManyInitsForReference: |
| 2505 | case FK_ArrayNeedsInitList: |
| 2506 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2507 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2508 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2509 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2510 | case FK_RValueReferenceBindingToLValue: |
| 2511 | case FK_ReferenceInitDropsQualifiers: |
| 2512 | case FK_ReferenceInitFailed: |
| 2513 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2514 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2515 | case FK_TooManyInitsForScalar: |
| 2516 | case FK_ReferenceBindingToInitList: |
| 2517 | case FK_InitListBadDestinationType: |
| 2518 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2519 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2520 | case FK_ArrayTypeMismatch: |
| 2521 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2522 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2523 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2524 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2525 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2526 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2527 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2529 | case FK_ReferenceInitOverloadFailed: |
| 2530 | case FK_UserConversionOverloadFailed: |
| 2531 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2532 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2533 | return FailedOverloadResult == OR_Ambiguous; |
| 2534 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2536 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2539 | bool InitializationSequence::isConstructorInitialization() const { |
| 2540 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2541 | } |
| 2542 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2543 | void |
| 2544 | InitializationSequence |
| 2545 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2546 | DeclAccessPair Found, |
| 2547 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2548 | Step S; |
| 2549 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2550 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2551 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2552 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2553 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2554 | Steps.push_back(S); |
| 2555 | } |
| 2556 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2558 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2559 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2560 | switch (VK) { |
| 2561 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2562 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2563 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2564 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2565 | S.Type = BaseType; |
| 2566 | Steps.push_back(S); |
| 2567 | } |
| 2568 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2570 | bool BindingTemporary) { |
| 2571 | Step S; |
| 2572 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2573 | S.Type = T; |
| 2574 | Steps.push_back(S); |
| 2575 | } |
| 2576 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2577 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2578 | Step S; |
| 2579 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2580 | S.Type = T; |
| 2581 | Steps.push_back(S); |
| 2582 | } |
| 2583 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2584 | void |
| 2585 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2586 | DeclAccessPair FoundDecl, |
| 2587 | QualType T, |
| 2588 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2589 | Step S; |
| 2590 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2591 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2592 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2593 | S.Function.Function = Function; |
| 2594 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2595 | Steps.push_back(S); |
| 2596 | } |
| 2597 | |
| 2598 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2599 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2600 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2601 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2602 | switch (VK) { |
| 2603 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2604 | S.Kind = SK_QualificationConversionRValue; |
| 2605 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2606 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2607 | S.Kind = SK_QualificationConversionXValue; |
| 2608 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2609 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2610 | S.Kind = SK_QualificationConversionLValue; |
| 2611 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2612 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2613 | S.Type = Ty; |
| 2614 | Steps.push_back(S); |
| 2615 | } |
| 2616 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2617 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2618 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2619 | |
| 2620 | Step S; |
| 2621 | S.Kind = SK_LValueToRValue; |
| 2622 | S.Type = Ty; |
| 2623 | Steps.push_back(S); |
| 2624 | } |
| 2625 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2626 | void InitializationSequence::AddConversionSequenceStep( |
| 2627 | const ImplicitConversionSequence &ICS, |
| 2628 | QualType T) { |
| 2629 | Step S; |
| 2630 | S.Kind = SK_ConversionSequence; |
| 2631 | S.Type = T; |
| 2632 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2633 | Steps.push_back(S); |
| 2634 | } |
| 2635 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2636 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2637 | Step S; |
| 2638 | S.Kind = SK_ListInitialization; |
| 2639 | S.Type = T; |
| 2640 | Steps.push_back(S); |
| 2641 | } |
| 2642 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2644 | InitializationSequence |
| 2645 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2646 | AccessSpecifier Access, |
| 2647 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2648 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2649 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2650 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2651 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2652 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2653 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2654 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2655 | S.Function.Function = Constructor; |
| 2656 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2657 | Steps.push_back(S); |
| 2658 | } |
| 2659 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2660 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2661 | Step S; |
| 2662 | S.Kind = SK_ZeroInitialization; |
| 2663 | S.Type = T; |
| 2664 | Steps.push_back(S); |
| 2665 | } |
| 2666 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2667 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2668 | Step S; |
| 2669 | S.Kind = SK_CAssignment; |
| 2670 | S.Type = T; |
| 2671 | Steps.push_back(S); |
| 2672 | } |
| 2673 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2674 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2675 | Step S; |
| 2676 | S.Kind = SK_StringInit; |
| 2677 | S.Type = T; |
| 2678 | Steps.push_back(S); |
| 2679 | } |
| 2680 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2681 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2682 | Step S; |
| 2683 | S.Kind = SK_ObjCObjectConversion; |
| 2684 | S.Type = T; |
| 2685 | Steps.push_back(S); |
| 2686 | } |
| 2687 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2688 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2689 | Step S; |
| 2690 | S.Kind = SK_ArrayInit; |
| 2691 | S.Type = T; |
| 2692 | Steps.push_back(S); |
| 2693 | } |
| 2694 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2695 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2696 | Step S; |
| 2697 | S.Kind = SK_ParenthesizedArrayInit; |
| 2698 | S.Type = T; |
| 2699 | Steps.push_back(S); |
| 2700 | } |
| 2701 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2702 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2703 | bool shouldCopy) { |
| 2704 | Step s; |
| 2705 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2706 | : SK_PassByIndirectRestore); |
| 2707 | s.Type = type; |
| 2708 | Steps.push_back(s); |
| 2709 | } |
| 2710 | |
| 2711 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2712 | Step S; |
| 2713 | S.Kind = SK_ProduceObjCObject; |
| 2714 | S.Type = T; |
| 2715 | Steps.push_back(S); |
| 2716 | } |
| 2717 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2718 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2719 | Step S; |
| 2720 | S.Kind = SK_StdInitializerList; |
| 2721 | S.Type = T; |
| 2722 | Steps.push_back(S); |
| 2723 | } |
| 2724 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2725 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2726 | Step S; |
| 2727 | S.Kind = SK_OCLSamplerInit; |
| 2728 | S.Type = T; |
| 2729 | Steps.push_back(S); |
| 2730 | } |
| 2731 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2732 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2733 | Step S; |
| 2734 | S.Kind = SK_OCLZeroEvent; |
| 2735 | S.Type = T; |
| 2736 | Steps.push_back(S); |
| 2737 | } |
| 2738 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2739 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2740 | InitListExpr *Syntactic) { |
| 2741 | assert(Syntactic->getNumInits() == 1 && |
| 2742 | "Can only rewrap trivial init lists."); |
| 2743 | Step S; |
| 2744 | S.Kind = SK_UnwrapInitList; |
| 2745 | S.Type = Syntactic->getInit(0)->getType(); |
| 2746 | Steps.insert(Steps.begin(), S); |
| 2747 | |
| 2748 | S.Kind = SK_RewrapInitList; |
| 2749 | S.Type = T; |
| 2750 | S.WrappingSyntacticList = Syntactic; |
| 2751 | Steps.push_back(S); |
| 2752 | } |
| 2753 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2754 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2755 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2756 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2757 | this->Failure = Failure; |
| 2758 | this->FailedOverloadResult = Result; |
| 2759 | } |
| 2760 | |
| 2761 | //===----------------------------------------------------------------------===// |
| 2762 | // Attempt initialization |
| 2763 | //===----------------------------------------------------------------------===// |
| 2764 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2765 | static void MaybeProduceObjCObject(Sema &S, |
| 2766 | InitializationSequence &Sequence, |
| 2767 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2768 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2769 | |
| 2770 | /// When initializing a parameter, produce the value if it's marked |
| 2771 | /// __attribute__((ns_consumed)). |
| 2772 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2773 | if (!Entity.isParameterConsumed()) |
| 2774 | return; |
| 2775 | |
| 2776 | assert(Entity.getType()->isObjCRetainableType() && |
| 2777 | "consuming an object of unretainable type?"); |
| 2778 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2779 | |
| 2780 | /// When initializing a return value, if the return type is a |
| 2781 | /// retainable type, then returns need to immediately retain the |
| 2782 | /// object. If an autorelease is required, it will be done at the |
| 2783 | /// last instant. |
| 2784 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2785 | if (!Entity.getType()->isObjCRetainableType()) |
| 2786 | return; |
| 2787 | |
| 2788 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2789 | } |
| 2790 | } |
| 2791 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2792 | /// \brief When initializing from init list via constructor, handle |
| 2793 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2794 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2795 | /// \return true if we have handled initialization of an object of type |
| 2796 | /// std::initializer_list<T>, false otherwise. |
| 2797 | static bool TryInitializerListConstruction(Sema &S, |
| 2798 | InitListExpr *List, |
| 2799 | QualType DestType, |
| 2800 | InitializationSequence &Sequence) { |
| 2801 | QualType E; |
| 2802 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2803 | return false; |
| 2804 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2805 | // Check that each individual element can be copy-constructed. But since we |
| 2806 | // have no place to store further information, we'll recalculate everything |
| 2807 | // later. |
| 2808 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2809 | S.Context.getConstantArrayType(E, |
| 2810 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2811 | List->getNumInits()), |
| 2812 | ArrayType::Normal, 0)); |
| 2813 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2814 | 0, HiddenArray); |
| 2815 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2816 | Element.setElementIndex(i); |
| 2817 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2818 | Sequence.SetFailed( |
| 2819 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2820 | return true; |
| 2821 | } |
| 2822 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2823 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2824 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2827 | static OverloadingResult |
| 2828 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2829 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2830 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2831 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2832 | OverloadCandidateSet::iterator &Best, |
| 2833 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2834 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2835 | CandidateSet.clear(); |
| 2836 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2837 | for (ArrayRef<NamedDecl *>::iterator |
| 2838 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2839 | NamedDecl *D = *Con; |
| 2840 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2841 | bool SuppressUserConversions = false; |
| 2842 | |
| 2843 | // Find the constructor (which may be a template). |
| 2844 | CXXConstructorDecl *Constructor = 0; |
| 2845 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2846 | if (ConstructorTmpl) |
| 2847 | Constructor = cast<CXXConstructorDecl>( |
| 2848 | ConstructorTmpl->getTemplatedDecl()); |
| 2849 | else { |
| 2850 | Constructor = cast<CXXConstructorDecl>(D); |
| 2851 | |
| 2852 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2853 | // suppress user-defined conversions on the arguments. We do the same for |
| 2854 | // move constructors. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2855 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2856 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2857 | SuppressUserConversions = true; |
| 2858 | } |
| 2859 | |
| 2860 | if (!Constructor->isInvalidDecl() && |
| 2861 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2862 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2863 | if (ConstructorTmpl) |
| 2864 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2865 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2866 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2867 | else { |
| 2868 | // C++ [over.match.copy]p1: |
| 2869 | // - When initializing a temporary to be bound to the first parameter |
| 2870 | // of a constructor that takes a reference to possibly cv-qualified |
| 2871 | // T as its first argument, called with a single argument in the |
| 2872 | // context of direct-initialization, explicit conversion functions |
| 2873 | // are also considered. |
| 2874 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2875 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2876 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2877 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2878 | SuppressUserConversions, |
| 2879 | /*PartialOverloading=*/false, |
| 2880 | /*AllowExplicit=*/AllowExplicitConv); |
| 2881 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | // Perform overload resolution and return the result. |
| 2886 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2887 | } |
| 2888 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2889 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2890 | /// enumerates the constructors of the initialized entity and performs overload |
| 2891 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2892 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2893 | /// class type. |
| 2894 | static void TryConstructorInitialization(Sema &S, |
| 2895 | const InitializedEntity &Entity, |
| 2896 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2897 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2898 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2899 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2900 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2901 | "InitListSyntax must come with a single initializer list argument."); |
| 2902 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2903 | // The type we're constructing needs to be complete. |
| 2904 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2905 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2906 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2910 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2911 | CXXRecordDecl *DestRecordDecl |
| 2912 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2913 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2914 | // Build the candidate set directly in the initialization sequence |
| 2915 | // structure, so that it will persist if we fail. |
| 2916 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2917 | |
| 2918 | // Determine whether we are allowed to call explicit constructors or |
| 2919 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2920 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2921 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2922 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2923 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2924 | // applicable constructors are enumerated, and the best one is chosen |
| 2925 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2926 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2927 | // The container holding the constructors can under certain conditions |
| 2928 | // be changed while iterating (e.g. because of deserialization). |
| 2929 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2930 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2931 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2932 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2933 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2934 | bool AsInitializerList = false; |
| 2935 | |
| 2936 | // C++11 [over.match.list]p1: |
| 2937 | // When objects of non-aggregate type T are list-initialized, overload |
| 2938 | // resolution selects the constructor in two phases: |
| 2939 | // - Initially, the candidate functions are the initializer-list |
| 2940 | // constructors of the class T and the argument list consists of the |
| 2941 | // initializer list as a single argument. |
| 2942 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2943 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2944 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2945 | |
| 2946 | // If the initializer list has no elements and T has a default constructor, |
| 2947 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2948 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2949 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2950 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2951 | CopyInitialization, AllowExplicit, |
| 2952 | /*OnlyListConstructor=*/true, |
| 2953 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2954 | |
| 2955 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2956 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | // C++11 [over.match.list]p1: |
| 2960 | // - If no viable initializer-list constructor is found, overload resolution |
| 2961 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2962 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2963 | // elements of the initializer list. |
| 2964 | if (Result == OR_No_Viable_Function) { |
| 2965 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2966 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2967 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2968 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2969 | /*OnlyListConstructors=*/false, |
| 2970 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2971 | } |
| 2972 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2973 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2974 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2975 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2976 | Result); |
| 2977 | return; |
| 2978 | } |
| 2979 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2980 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2981 | // If a program calls for the default initialization of an object |
| 2982 | // of a const-qualified type T, T shall be a class type with a |
| 2983 | // user-provided default constructor. |
| 2984 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2985 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 2986 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2987 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2988 | return; |
| 2989 | } |
| 2990 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2991 | // C++11 [over.match.list]p1: |
| 2992 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 2993 | // initializer is ill-formed. |
| 2994 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2995 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 2996 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 2997 | return; |
| 2998 | } |
| 2999 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3000 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3001 | // subsumed by the initialization. |
| 3002 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3003 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3004 | Best->FoundDecl.getAccess(), |
| 3005 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3006 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3009 | static bool |
| 3010 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3011 | Expr *Initializer, |
| 3012 | QualType &SourceType, |
| 3013 | QualType &UnqualifiedSourceType, |
| 3014 | QualType UnqualifiedTargetType, |
| 3015 | InitializationSequence &Sequence) { |
| 3016 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3017 | S.Context.OverloadTy) { |
| 3018 | DeclAccessPair Found; |
| 3019 | bool HadMultipleCandidates = false; |
| 3020 | if (FunctionDecl *Fn |
| 3021 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3022 | UnqualifiedTargetType, |
| 3023 | false, Found, |
| 3024 | &HadMultipleCandidates)) { |
| 3025 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3026 | HadMultipleCandidates); |
| 3027 | SourceType = Fn->getType(); |
| 3028 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3029 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3030 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3031 | return true; |
| 3032 | } |
| 3033 | } |
| 3034 | return false; |
| 3035 | } |
| 3036 | |
| 3037 | static void TryReferenceInitializationCore(Sema &S, |
| 3038 | const InitializedEntity &Entity, |
| 3039 | const InitializationKind &Kind, |
| 3040 | Expr *Initializer, |
| 3041 | QualType cv1T1, QualType T1, |
| 3042 | Qualifiers T1Quals, |
| 3043 | QualType cv2T2, QualType T2, |
| 3044 | Qualifiers T2Quals, |
| 3045 | InitializationSequence &Sequence); |
| 3046 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3047 | static void TryValueInitialization(Sema &S, |
| 3048 | const InitializedEntity &Entity, |
| 3049 | const InitializationKind &Kind, |
| 3050 | InitializationSequence &Sequence, |
| 3051 | InitListExpr *InitList = 0); |
| 3052 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3053 | static void TryListInitialization(Sema &S, |
| 3054 | const InitializedEntity &Entity, |
| 3055 | const InitializationKind &Kind, |
| 3056 | InitListExpr *InitList, |
| 3057 | InitializationSequence &Sequence); |
| 3058 | |
| 3059 | /// \brief Attempt list initialization of a reference. |
| 3060 | static void TryReferenceListInitialization(Sema &S, |
| 3061 | const InitializedEntity &Entity, |
| 3062 | const InitializationKind &Kind, |
| 3063 | InitListExpr *InitList, |
| 3064 | InitializationSequence &Sequence) |
| 3065 | { |
| 3066 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3067 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3068 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3069 | return; |
| 3070 | } |
| 3071 | |
| 3072 | QualType DestType = Entity.getType(); |
| 3073 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3074 | Qualifiers T1Quals; |
| 3075 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3076 | |
| 3077 | // Reference initialization via an initializer list works thus: |
| 3078 | // If the initializer list consists of a single element that is |
| 3079 | // reference-related to the referenced type, bind directly to that element |
| 3080 | // (possibly creating temporaries). |
| 3081 | // Otherwise, initialize a temporary with the initializer list and |
| 3082 | // bind to that. |
| 3083 | if (InitList->getNumInits() == 1) { |
| 3084 | Expr *Initializer = InitList->getInit(0); |
| 3085 | QualType cv2T2 = Initializer->getType(); |
| 3086 | Qualifiers T2Quals; |
| 3087 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3088 | |
| 3089 | // If this fails, creating a temporary wouldn't work either. |
| 3090 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3091 | T1, Sequence)) |
| 3092 | return; |
| 3093 | |
| 3094 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3095 | bool dummy1, dummy2, dummy3; |
| 3096 | Sema::ReferenceCompareResult RefRelationship |
| 3097 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3098 | dummy2, dummy3); |
| 3099 | if (RefRelationship >= Sema::Ref_Related) { |
| 3100 | // Try to bind the reference here. |
| 3101 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3102 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3103 | if (Sequence) |
| 3104 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3105 | return; |
| 3106 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Update the initializer if we've resolved an overloaded function. |
| 3109 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3110 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | // Not reference-related. Create a temporary and bind to that. |
| 3114 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3115 | |
| 3116 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3117 | if (Sequence) { |
| 3118 | if (DestType->isRValueReferenceType() || |
| 3119 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3120 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3121 | else |
| 3122 | Sequence.SetFailed( |
| 3123 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3124 | } |
| 3125 | } |
| 3126 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3127 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3128 | static void TryListInitialization(Sema &S, |
| 3129 | const InitializedEntity &Entity, |
| 3130 | const InitializationKind &Kind, |
| 3131 | InitListExpr *InitList, |
| 3132 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3133 | QualType DestType = Entity.getType(); |
| 3134 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3135 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3136 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3137 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3138 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3139 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3140 | return; |
| 3141 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3142 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3143 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3144 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3145 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3146 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3147 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3148 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3149 | return; |
| 3150 | } |
| 3151 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3152 | // C++11 [dcl.init.list]p3: |
| 3153 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3154 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3155 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3156 | // - Otherwise, if the initializer list has no elements and T is a |
| 3157 | // class type with a default constructor, the object is |
| 3158 | // value-initialized. |
| 3159 | if (InitList->getNumInits() == 0) { |
| 3160 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3161 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3162 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3163 | return; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3168 | // an initializer_list object constructed [...] |
| 3169 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3170 | return; |
| 3171 | |
| 3172 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3173 | Expr *InitListAsExpr = InitList; |
| 3174 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3175 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3176 | } else |
| 3177 | Sequence.SetFailed( |
| 3178 | InitializationSequence::FK_InitListBadDestinationType); |
| 3179 | return; |
| 3180 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3183 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3184 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3185 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3186 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3187 | if (CheckInitList.HadError()) { |
| 3188 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3189 | return; |
| 3190 | } |
| 3191 | |
| 3192 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3193 | Sequence.AddListInitializationStep(DestType); |
| 3194 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3195 | |
| 3196 | /// \brief Try a reference initialization that involves calling a conversion |
| 3197 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3198 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3199 | const InitializedEntity &Entity, |
| 3200 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3201 | Expr *Initializer, |
| 3202 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3203 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3204 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3205 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3206 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3207 | QualType cv2T2 = Initializer->getType(); |
| 3208 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3209 | |
| 3210 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3211 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3212 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3214 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3215 | ObjCConversion, |
| 3216 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3217 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3218 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3219 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3220 | (void)ObjCLifetimeConversion; |
| 3221 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3222 | // Build the candidate set directly in the initialization sequence |
| 3223 | // structure, so that it will persist if we fail. |
| 3224 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3225 | CandidateSet.clear(); |
| 3226 | |
| 3227 | // Determine whether we are allowed to call explicit constructors or |
| 3228 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3229 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3230 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3231 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3232 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3233 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3234 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3235 | // The type we're converting to is a class type. Enumerate its constructors |
| 3236 | // to see if there is a suitable conversion. |
| 3237 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3238 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3239 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3240 | // The container holding the constructors can under certain conditions |
| 3241 | // be changed while iterating (e.g. because of deserialization). |
| 3242 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3243 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3244 | for (SmallVector<NamedDecl*, 16>::iterator |
| 3245 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3246 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3247 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3248 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3249 | // Find the constructor (which may be a template). |
| 3250 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3251 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3252 | if (ConstructorTmpl) |
| 3253 | Constructor = cast<CXXConstructorDecl>( |
| 3254 | ConstructorTmpl->getTemplatedDecl()); |
| 3255 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3256 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3258 | if (!Constructor->isInvalidDecl() && |
| 3259 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3260 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3261 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3262 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3263 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3264 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3265 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3266 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3267 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3268 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3269 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3270 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3271 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3272 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3273 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3275 | const RecordType *T2RecordType = 0; |
| 3276 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3277 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3278 | // The type we're converting from is a class type, enumerate its conversion |
| 3279 | // functions. |
| 3280 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3281 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3282 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3283 | CXXRecordDecl::conversion_iterator> |
| 3284 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3285 | for (CXXRecordDecl::conversion_iterator |
| 3286 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3287 | NamedDecl *D = *I; |
| 3288 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3289 | if (isa<UsingShadowDecl>(D)) |
| 3290 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3292 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3293 | CXXConversionDecl *Conv; |
| 3294 | if (ConvTemplate) |
| 3295 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3296 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3297 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3299 | // If the conversion function doesn't return a reference type, |
| 3300 | // it can't be considered for this conversion unless we're allowed to |
| 3301 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | // FIXME: Do we need to make sure that we only consider conversion |
| 3303 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3304 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3305 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3306 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3307 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3308 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3309 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3310 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3311 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3312 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3313 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3317 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3318 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3320 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3321 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3323 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3325 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3326 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3328 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3329 | // This is the overload that will be used for this initialization step if we |
| 3330 | // use this initialization. Mark it as referenced. |
| 3331 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3332 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3333 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3334 | if (isa<CXXConversionDecl>(Function)) |
| 3335 | T2 = Function->getResultType(); |
| 3336 | else |
| 3337 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3338 | |
| 3339 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3340 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3341 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3342 | T2.getNonLValueExprType(S.Context), |
| 3343 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3344 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3345 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3346 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3347 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3348 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3349 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3350 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3351 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3353 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3354 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3355 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3356 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3358 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3359 | NewDerivedToBase, NewObjCConversion, |
| 3360 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3361 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3362 | // If the type we've converted to is not reference-related to the |
| 3363 | // type we're looking for, then there is another conversion step |
| 3364 | // we need to perform to produce a temporary of the right type |
| 3365 | // that we'll be binding to. |
| 3366 | ImplicitConversionSequence ICS; |
| 3367 | ICS.setStandard(); |
| 3368 | ICS.Standard = Best->FinalConversion; |
| 3369 | T2 = ICS.Standard.getToType(2); |
| 3370 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3371 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3372 | Sequence.AddDerivedToBaseCastStep( |
| 3373 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3375 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3376 | else if (NewObjCConversion) |
| 3377 | Sequence.AddObjCObjectConversionStep( |
| 3378 | S.Context.getQualifiedType(T1, |
| 3379 | T2.getNonReferenceType().getQualifiers())); |
| 3380 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3381 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3382 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3384 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3385 | return OR_Success; |
| 3386 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3388 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3389 | const InitializedEntity &Entity, |
| 3390 | Expr *CurInitExpr); |
| 3391 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3393 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3394 | const InitializedEntity &Entity, |
| 3395 | const InitializationKind &Kind, |
| 3396 | Expr *Initializer, |
| 3397 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3398 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3399 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3400 | Qualifiers T1Quals; |
| 3401 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3403 | Qualifiers T2Quals; |
| 3404 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3405 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3406 | // If the initializer is the address of an overloaded function, try |
| 3407 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3408 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3409 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3410 | T1, Sequence)) |
| 3411 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3412 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3413 | // Delegate everything else to a subfunction. |
| 3414 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3415 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3416 | } |
| 3417 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3418 | /// Converts the target of reference initialization so that it has the |
| 3419 | /// appropriate qualifiers and value kind. |
| 3420 | /// |
| 3421 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3422 | /// \code |
| 3423 | /// int x; |
| 3424 | /// const int &r = x; |
| 3425 | /// \endcode |
| 3426 | /// |
| 3427 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3428 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3429 | /// \code |
| 3430 | /// const int &r = someStruct.bitfield; |
| 3431 | /// \endcode |
| 3432 | static ExprValueKind |
| 3433 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3434 | InitializationSequence &Sequence, |
| 3435 | Expr *Initializer, |
| 3436 | QualType cv1T1, |
| 3437 | Qualifiers T1Quals, |
| 3438 | Qualifiers T2Quals, |
| 3439 | bool IsLValueRef) { |
| 3440 | bool IsNonAddressableType = Initializer->getBitField() || |
| 3441 | Initializer->refersToVectorElement(); |
| 3442 | |
| 3443 | if (IsNonAddressableType) { |
| 3444 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3445 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3446 | // an rvalue reference. |
| 3447 | // |
| 3448 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3449 | // error to be diagnosed later. |
| 3450 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3451 | assert(Initializer->isGLValue()); |
| 3452 | return Initializer->getValueKind(); |
| 3453 | } |
| 3454 | |
| 3455 | // Force a load so we can materialize a temporary. |
| 3456 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3457 | return VK_RValue; |
| 3458 | } |
| 3459 | |
| 3460 | if (T1Quals != T2Quals) { |
| 3461 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3462 | Initializer->getValueKind()); |
| 3463 | } |
| 3464 | |
| 3465 | return Initializer->getValueKind(); |
| 3466 | } |
| 3467 | |
| 3468 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3469 | /// \brief Reference initialization without resolving overloaded functions. |
| 3470 | static void TryReferenceInitializationCore(Sema &S, |
| 3471 | const InitializedEntity &Entity, |
| 3472 | const InitializationKind &Kind, |
| 3473 | Expr *Initializer, |
| 3474 | QualType cv1T1, QualType T1, |
| 3475 | Qualifiers T1Quals, |
| 3476 | QualType cv2T2, QualType T2, |
| 3477 | Qualifiers T2Quals, |
| 3478 | InitializationSequence &Sequence) { |
| 3479 | QualType DestType = Entity.getType(); |
| 3480 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3481 | // Compute some basic properties of the types and the initializer. |
| 3482 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3483 | bool isRValueRef = !isLValueRef; |
| 3484 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3485 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3486 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3487 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3488 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3489 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3490 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3492 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | // 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] | 3494 | // "cv2 T2" as follows: |
| 3495 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3497 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3498 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3499 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3500 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3501 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3502 | bool T1Function = T1->isFunctionType(); |
| 3503 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3505 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3507 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | // - 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] | 3509 | // reference-compatible with "cv2 T2," or |
| 3510 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3511 | // 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] | 3512 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3513 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3514 | // to decide whether we're actually binding to a temporary created from |
| 3515 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3516 | if (DerivedToBase) |
| 3517 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3519 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3520 | else if (ObjCConversion) |
| 3521 | Sequence.AddObjCObjectConversionStep( |
| 3522 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3523 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3524 | ExprValueKind ValueKind = |
| 3525 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3526 | cv1T1, T1Quals, T2Quals, |
| 3527 | isLValueRef); |
| 3528 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | return; |
| 3530 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3531 | |
| 3532 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3533 | // reference-related to T2, and can be implicitly converted to an |
| 3534 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3535 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3537 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3538 | // If we have an rvalue ref to function type here, the rhs must be |
| 3539 | // an rvalue. |
| 3540 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3541 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3542 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3544 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3545 | Sequence); |
| 3546 | if (ConvOvlResult == OR_Success) |
| 3547 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3548 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3549 | Sequence.SetOverloadFailure( |
| 3550 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3551 | ConvOvlResult); |
| 3552 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3555 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3556 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3557 | // 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] | 3558 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3559 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3560 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3561 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3562 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3563 | Sequence.SetOverloadFailure( |
| 3564 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3565 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3566 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3567 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3568 | ? (RefRelationship == Sema::Ref_Related |
| 3569 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3570 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3571 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3573 | return; |
| 3574 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3575 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3576 | // - If the initializer expression |
| 3577 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3578 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3579 | // Note: functions are handled below. |
| 3580 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3581 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3583 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3584 | (InitCategory.isXValue() || |
| 3585 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3586 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3587 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3588 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3589 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3590 | // compiler the freedom to perform a copy here or bind to the |
| 3591 | // object, while C++0x requires that we bind directly to the |
| 3592 | // object. Hence, we always bind to the object without making an |
| 3593 | // extra copy. However, in C++03 requires that we check for the |
| 3594 | // presence of a suitable copy constructor: |
| 3595 | // |
| 3596 | // The constructor that would be used to make the copy shall |
| 3597 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3598 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3599 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3600 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3601 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3602 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3604 | if (DerivedToBase) |
| 3605 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3606 | ValueKind); |
| 3607 | else if (ObjCConversion) |
| 3608 | Sequence.AddObjCObjectConversionStep( |
| 3609 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3610 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3611 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3612 | Initializer, cv1T1, |
| 3613 | T1Quals, T2Quals, |
| 3614 | isLValueRef); |
| 3615 | |
| 3616 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3617 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3618 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3619 | |
| 3620 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3621 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3622 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3623 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3624 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3625 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3626 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3627 | Kind, Initializer, |
| 3628 | /*AllowRValues=*/true, |
| 3629 | Sequence); |
| 3630 | if (ConvOvlResult) |
| 3631 | Sequence.SetOverloadFailure( |
| 3632 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3633 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3634 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3635 | return; |
| 3636 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3638 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3639 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3640 | isRValueRef && InitCategory.isLValue()) { |
| 3641 | Sequence.SetFailed( |
| 3642 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3643 | return; |
| 3644 | } |
| 3645 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3646 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3647 | return; |
| 3648 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3649 | |
| 3650 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3651 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3652 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | // Determine whether we are allowed to call explicit constructors or |
| 3656 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3657 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3658 | |
| 3659 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3660 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3661 | ImplicitConversionSequence ICS |
| 3662 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3663 | /*SuppressUserConversions*/ false, |
| 3664 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3665 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3666 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3667 | /*AllowObjCWritebackConversion=*/false); |
| 3668 | |
| 3669 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3670 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3671 | // this into an overloading ambiguity diagnostic. However, we need |
| 3672 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3673 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3674 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3675 | Sequence.SetOverloadFailure( |
| 3676 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3677 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3678 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3679 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3680 | else |
| 3681 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3682 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3683 | } else { |
| 3684 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3688 | // same cv-qualification as, or greater cv-qualification |
| 3689 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3690 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3691 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3693 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3694 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3695 | return; |
| 3696 | } |
| 3697 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | // [...] 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] | 3699 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3701 | InitCategory.isLValue()) { |
| 3702 | Sequence.SetFailed( |
| 3703 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3704 | return; |
| 3705 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3707 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3708 | return; |
| 3709 | } |
| 3710 | |
| 3711 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3713 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3714 | const InitializedEntity &Entity, |
| 3715 | const InitializationKind &Kind, |
| 3716 | Expr *Initializer, |
| 3717 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3718 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3721 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3722 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3723 | const InitializedEntity &Entity, |
| 3724 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3725 | InitializationSequence &Sequence, |
| 3726 | InitListExpr *InitList) { |
| 3727 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3728 | "Shouldn't use value-init for non-empty init lists"); |
| 3729 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3730 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3731 | // |
| 3732 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3733 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3734 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3735 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3736 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3737 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3738 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3739 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3740 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3741 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3742 | // C++98: |
| 3743 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3744 | // (12.1), then the default constructor for T is called (and the |
| 3745 | // initialization is ill-formed if T has no accessible default |
| 3746 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3747 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3748 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3749 | } else { |
| 3750 | // C++11: |
| 3751 | // -- if T is a class type (clause 9) with either no default constructor |
| 3752 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3753 | // or deleted, then the object is default-initialized; |
| 3754 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3755 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3756 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3757 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3759 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3760 | // user-provided or deleted default constructor, then the object is |
| 3761 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3762 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3763 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3764 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3765 | if (NeedZeroInitialization) |
| 3766 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3767 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3768 | // C++03: |
| 3769 | // -- if T is a non-union class type without a user-declared constructor, |
| 3770 | // then every non-static data member and base class component of T is |
| 3771 | // value-initialized; |
| 3772 | // [...] A program that calls for [...] value-initialization of an |
| 3773 | // entity of reference type is ill-formed. |
| 3774 | // |
| 3775 | // C++11 doesn't need this handling, because value-initialization does not |
| 3776 | // occur recursively there, and the implicit default constructor is |
| 3777 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3778 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3779 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3780 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3781 | return; |
| 3782 | } |
| 3783 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3784 | // If this is list-value-initialization, pass the empty init list on when |
| 3785 | // building the constructor call. This affects the semantics of a few |
| 3786 | // things (such as whether an explicit default constructor can be called). |
| 3787 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3788 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3789 | bool InitListSyntax = InitList; |
| 3790 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3791 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3792 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3796 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3799 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3800 | static void TryDefaultInitialization(Sema &S, |
| 3801 | const InitializedEntity &Entity, |
| 3802 | const InitializationKind &Kind, |
| 3803 | InitializationSequence &Sequence) { |
| 3804 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3806 | // C++ [dcl.init]p6: |
| 3807 | // To default-initialize an object of type T means: |
| 3808 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3809 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3810 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3811 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3812 | // constructor for T is called (and the initialization is ill-formed if |
| 3813 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3814 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3815 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3816 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3817 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3818 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3819 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3821 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3822 | // 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] | 3823 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3824 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3825 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3826 | return; |
| 3827 | } |
| 3828 | |
| 3829 | // If the destination type has a lifetime property, zero-initialize it. |
| 3830 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3831 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3832 | return; |
| 3833 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3836 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3837 | /// which enumerates all conversion functions and performs overload resolution |
| 3838 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3840 | const InitializedEntity &Entity, |
| 3841 | const InitializationKind &Kind, |
| 3842 | Expr *Initializer, |
| 3843 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3844 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3845 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3846 | QualType SourceType = Initializer->getType(); |
| 3847 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3848 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3850 | // Build the candidate set directly in the initialization sequence |
| 3851 | // structure, so that it will persist if we fail. |
| 3852 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3853 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3855 | // Determine whether we are allowed to call explicit constructors or |
| 3856 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3857 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3858 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3859 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3860 | // The type we're converting to is a class type. Enumerate its constructors |
| 3861 | // to see if there is a suitable conversion. |
| 3862 | CXXRecordDecl *DestRecordDecl |
| 3863 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3865 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3867 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3868 | // The container holding the constructors can under certain conditions |
| 3869 | // be changed while iterating. To be safe we copy the lookup results |
| 3870 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3871 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3872 | for (SmallVector<NamedDecl*, 8>::iterator |
| 3873 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3874 | Con != ConEnd; ++Con) { |
| 3875 | NamedDecl *D = *Con; |
| 3876 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3877 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3878 | // Find the constructor (which may be a template). |
| 3879 | CXXConstructorDecl *Constructor = 0; |
| 3880 | FunctionTemplateDecl *ConstructorTmpl |
| 3881 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3882 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3883 | Constructor = cast<CXXConstructorDecl>( |
| 3884 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3885 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3886 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3888 | if (!Constructor->isInvalidDecl() && |
| 3889 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3890 | if (ConstructorTmpl) |
| 3891 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3892 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3893 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3894 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3895 | else |
| 3896 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3897 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3898 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3899 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3901 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3902 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3903 | |
| 3904 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3905 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3906 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3907 | // The type we're converting from is a class type, enumerate its conversion |
| 3908 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3909 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3910 | // We can only enumerate the conversion functions for a complete type; if |
| 3911 | // the type isn't complete, simply skip this step. |
| 3912 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3913 | CXXRecordDecl *SourceRecordDecl |
| 3914 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3916 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3917 | CXXRecordDecl::conversion_iterator> |
| 3918 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 3919 | for (CXXRecordDecl::conversion_iterator |
| 3920 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3921 | NamedDecl *D = *I; |
| 3922 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3923 | if (isa<UsingShadowDecl>(D)) |
| 3924 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3925 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3926 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3927 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3928 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3929 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3930 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3931 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3932 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3933 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3934 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3935 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3936 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3937 | CandidateSet); |
| 3938 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3939 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3940 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3941 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3942 | } |
| 3943 | } |
| 3944 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | |
| 3946 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3947 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3948 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3949 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3950 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3952 | Result); |
| 3953 | return; |
| 3954 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3956 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3957 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3958 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3959 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3960 | if (isa<CXXConstructorDecl>(Function)) { |
| 3961 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3962 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 3963 | // cv-unqualified type of the destination. |
| 3964 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 3965 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3966 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3967 | return; |
| 3968 | } |
| 3969 | |
| 3970 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3971 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3972 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3973 | // 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] | 3974 | // the resulting temporary object (possible to create an object of |
| 3975 | // a base class type). That copy is not a separate conversion, so |
| 3976 | // we just make a note of the actual destination type (possibly a |
| 3977 | // base class of the type returned by the conversion function) and |
| 3978 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3979 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3980 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3981 | return; |
| 3982 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3983 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3984 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3985 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3987 | // If the conversion following the call to the conversion function |
| 3988 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3989 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3990 | Best->FinalConversion.Third) { |
| 3991 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3992 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3993 | ICS.Standard = Best->FinalConversion; |
| 3994 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3995 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3998 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3999 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4000 | |
| 4001 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4002 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4003 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4004 | // Skip parens. |
| 4005 | e = e->IgnoreParens(); |
| 4006 | |
| 4007 | // Skip address-of nodes. |
| 4008 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4009 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4010 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4011 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4012 | |
| 4013 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4014 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4015 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4016 | case CK_Dependent: |
| 4017 | case CK_BitCast: |
| 4018 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4019 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4020 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4021 | |
| 4022 | case CK_ArrayToPointerDecay: |
| 4023 | return IIK_nonscalar; |
| 4024 | |
| 4025 | case CK_NullToPointer: |
| 4026 | return IIK_okay; |
| 4027 | |
| 4028 | default: |
| 4029 | break; |
| 4030 | } |
| 4031 | |
| 4032 | // 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] | 4033 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4034 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4035 | // load which requires a cleanup. |
| 4036 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4037 | isWeakAccess = true; |
| 4038 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4039 | if (!isAddressOf) return IIK_nonlocal; |
| 4040 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4041 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4042 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4043 | |
| 4044 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4045 | |
| 4046 | // If we have a conditional operator, check both sides. |
| 4047 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4048 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4049 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4050 | return iik; |
| 4051 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4052 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4053 | |
| 4054 | // These are never scalar. |
| 4055 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4056 | return IIK_nonscalar; |
| 4057 | |
| 4058 | // Otherwise, it needs to be a null pointer constant. |
| 4059 | } else { |
| 4060 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4061 | ? IIK_okay : IIK_nonlocal); |
| 4062 | } |
| 4063 | |
| 4064 | return IIK_nonlocal; |
| 4065 | } |
| 4066 | |
| 4067 | /// Check whether the given expression is a valid operand for an |
| 4068 | /// indirect copy/restore. |
| 4069 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4070 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4071 | bool isWeakAccess = false; |
| 4072 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4073 | // If isWeakAccess to true, there will be an implicit |
| 4074 | // load which requires a cleanup. |
| 4075 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4076 | S.ExprNeedsCleanups = true; |
| 4077 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4078 | if (iik == IIK_okay) return; |
| 4079 | |
| 4080 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4081 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4082 | << src->getSourceRange(); |
| 4083 | } |
| 4084 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4085 | /// \brief Determine whether we have compatible array types for the |
| 4086 | /// purposes of GNU by-copy array initialization. |
| 4087 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4088 | const ArrayType *Dest, |
| 4089 | const ArrayType *Source) { |
| 4090 | // If the source and destination array types are equivalent, we're |
| 4091 | // done. |
| 4092 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4093 | return true; |
| 4094 | |
| 4095 | // Make sure that the element types are the same. |
| 4096 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4097 | return false; |
| 4098 | |
| 4099 | // The only mismatch we allow is when the destination is an |
| 4100 | // incomplete array type and the source is a constant array type. |
| 4101 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4102 | } |
| 4103 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4104 | static bool tryObjCWritebackConversion(Sema &S, |
| 4105 | InitializationSequence &Sequence, |
| 4106 | const InitializedEntity &Entity, |
| 4107 | Expr *Initializer) { |
| 4108 | bool ArrayDecay = false; |
| 4109 | QualType ArgType = Initializer->getType(); |
| 4110 | QualType ArgPointee; |
| 4111 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4112 | ArrayDecay = true; |
| 4113 | ArgPointee = ArgArrayType->getElementType(); |
| 4114 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4115 | } |
| 4116 | |
| 4117 | // Handle write-back conversion. |
| 4118 | QualType ConvertedArgType; |
| 4119 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4120 | ConvertedArgType)) |
| 4121 | return false; |
| 4122 | |
| 4123 | // We should copy unless we're passing to an argument explicitly |
| 4124 | // marked 'out'. |
| 4125 | bool ShouldCopy = true; |
| 4126 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4127 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4128 | |
| 4129 | // Do we need an lvalue conversion? |
| 4130 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4131 | ImplicitConversionSequence ICS; |
| 4132 | ICS.setStandard(); |
| 4133 | ICS.Standard.setAsIdentityConversion(); |
| 4134 | |
| 4135 | QualType ResultType; |
| 4136 | if (ArrayDecay) { |
| 4137 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4138 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4139 | } else { |
| 4140 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4141 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4142 | } |
| 4143 | |
| 4144 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4145 | } |
| 4146 | |
| 4147 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4148 | return true; |
| 4149 | } |
| 4150 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4151 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4152 | InitializationSequence &Sequence, |
| 4153 | QualType DestType, |
| 4154 | Expr *Initializer) { |
| 4155 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4156 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4157 | return false; |
| 4158 | |
| 4159 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4160 | return true; |
| 4161 | } |
| 4162 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4163 | // |
| 4164 | // OpenCL 1.2 spec, s6.12.10 |
| 4165 | // |
| 4166 | // The event argument can also be used to associate the |
| 4167 | // async_work_group_copy with a previous async copy allowing |
| 4168 | // an event to be shared by multiple async copies; otherwise |
| 4169 | // event should be zero. |
| 4170 | // |
| 4171 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4172 | InitializationSequence &Sequence, |
| 4173 | QualType DestType, |
| 4174 | Expr *Initializer) { |
| 4175 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4176 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4177 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4178 | return false; |
| 4179 | |
| 4180 | Sequence.AddOCLZeroEventStep(DestType); |
| 4181 | return true; |
| 4182 | } |
| 4183 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | InitializationSequence::InitializationSequence(Sema &S, |
| 4185 | const InitializedEntity &Entity, |
| 4186 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4187 | MultiExprArg Args) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4188 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4189 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4190 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4191 | // Eliminate non-overload placeholder types in the arguments. We |
| 4192 | // need to do this before checking whether types are dependent |
| 4193 | // because lowering a pseudo-object expression might well give us |
| 4194 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4195 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4196 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4197 | // FIXME: should we be doing this here? |
| 4198 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4199 | if (result.isInvalid()) { |
| 4200 | SetFailed(FK_PlaceholderType); |
| 4201 | return; |
| 4202 | } |
| 4203 | Args[I] = result.take(); |
| 4204 | } |
| 4205 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4206 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | // The semantics of initializers are as follows. The destination type is |
| 4208 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4209 | // 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] | 4210 | // 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] | 4211 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4212 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4213 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4214 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4215 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4216 | SequenceKind = DependentSequence; |
| 4217 | return; |
| 4218 | } |
| 4219 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4220 | // Almost everything is a normal sequence. |
| 4221 | setSequenceKind(NormalSequence); |
| 4222 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4223 | QualType SourceType; |
| 4224 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4225 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4226 | Initializer = Args[0]; |
| 4227 | if (!isa<InitListExpr>(Initializer)) |
| 4228 | SourceType = Initializer->getType(); |
| 4229 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4231 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4232 | // object is list-initialized (8.5.4). |
| 4233 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4234 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4235 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4236 | return; |
| 4237 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4238 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4239 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4240 | // - If the destination type is a reference type, see 8.5.3. |
| 4241 | if (DestType->isReferenceType()) { |
| 4242 | // C++0x [dcl.init.ref]p1: |
| 4243 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4244 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4245 | // by an object that can be converted into a T. |
| 4246 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4247 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4248 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4249 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4250 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4251 | return; |
| 4252 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4254 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4255 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4256 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4257 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4258 | return; |
| 4259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4261 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4262 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4263 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4264 | return; |
| 4265 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4266 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4267 | // - If the destination type is an array of characters, an array of |
| 4268 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4269 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4271 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4272 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4273 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4274 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4275 | return; |
| 4276 | } |
| 4277 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4278 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4279 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4280 | return; |
| 4281 | } |
| 4282 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4283 | // Note: as an GNU C extension, we allow initialization of an |
| 4284 | // array from a compound literal that creates an array of the same |
| 4285 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4286 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4287 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4288 | Initializer->getType()->isArrayType()) { |
| 4289 | const ArrayType *SourceAT |
| 4290 | = Context.getAsArrayType(Initializer->getType()); |
| 4291 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4292 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4293 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4294 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4295 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4296 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4297 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4298 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4299 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4300 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4301 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4302 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4303 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4304 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4305 | *this); |
| 4306 | AddParenthesizedArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4307 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4308 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4309 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4310 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4311 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4312 | return; |
| 4313 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4314 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4315 | // Determine whether we should consider writeback conversions for |
| 4316 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4317 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4318 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4319 | |
| 4320 | // We're at the end of the line for C: it's either a write-back conversion |
| 4321 | // 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] | 4322 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4323 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4324 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4325 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4326 | return; |
| 4327 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4328 | |
| 4329 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4330 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4331 | |
| 4332 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4333 | return; |
| 4334 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4335 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4336 | AddCAssignmentStep(DestType); |
| 4337 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4338 | return; |
| 4339 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4341 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4342 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4343 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4344 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | // - If the initialization is direct-initialization, or if it is |
| 4346 | // copy-initialization where the cv-unqualified version of the |
| 4347 | // 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] | 4348 | // class of the destination, constructors are considered. [...] |
| 4349 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4350 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4351 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4352 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4353 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4354 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4356 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4358 | // used) to a derived class thereof are enumerated as described in |
| 4359 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4360 | // (13.3). |
| 4361 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4362 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4363 | return; |
| 4364 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4366 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4367 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4368 | return; |
| 4369 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4370 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | |
| 4372 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4374 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4375 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4376 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4377 | return; |
| 4378 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4380 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4381 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4382 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4384 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4385 | |
| 4386 | ImplicitConversionSequence ICS |
| 4387 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4388 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4389 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4390 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4391 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4392 | allowObjCWritebackConversion); |
| 4393 | |
| 4394 | if (ICS.isStandard() && |
| 4395 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4396 | // Objective-C ARC writeback conversion. |
| 4397 | |
| 4398 | // We should copy unless we're passing to an argument explicitly |
| 4399 | // marked 'out'. |
| 4400 | bool ShouldCopy = true; |
| 4401 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4402 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4403 | |
| 4404 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4405 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4406 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4407 | ImplicitConversionSequence LvalueICS; |
| 4408 | LvalueICS.setStandard(); |
| 4409 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4410 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4411 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4412 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4413 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4414 | |
| 4415 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4416 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4417 | DeclAccessPair dap; |
| 4418 | if (Initializer->getType() == Context.OverloadTy && |
| 4419 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4420 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4421 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4422 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4423 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4424 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4425 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4426 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4427 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4428 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4432 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4433 | StepEnd = Steps.end(); |
| 4434 | Step != StepEnd; ++Step) |
| 4435 | Step->Destroy(); |
| 4436 | } |
| 4437 | |
| 4438 | //===----------------------------------------------------------------------===// |
| 4439 | // Perform initialization |
| 4440 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4442 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4443 | switch(Entity.getKind()) { |
| 4444 | case InitializedEntity::EK_Variable: |
| 4445 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4446 | case InitializedEntity::EK_Exception: |
| 4447 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4448 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4449 | return Sema::AA_Initializing; |
| 4450 | |
| 4451 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4453 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4454 | return Sema::AA_Sending; |
| 4455 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4456 | return Sema::AA_Passing; |
| 4457 | |
| 4458 | case InitializedEntity::EK_Result: |
| 4459 | return Sema::AA_Returning; |
| 4460 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4461 | case InitializedEntity::EK_Temporary: |
| 4462 | // FIXME: Can we tell apart casting vs. converting? |
| 4463 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4465 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4466 | case InitializedEntity::EK_ArrayElement: |
| 4467 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4468 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4469 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4470 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4471 | return Sema::AA_Initializing; |
| 4472 | } |
| 4473 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4474 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4477 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4478 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4479 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4480 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4481 | case InitializedEntity::EK_ArrayElement: |
| 4482 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4483 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4484 | case InitializedEntity::EK_New: |
| 4485 | case InitializedEntity::EK_Variable: |
| 4486 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4487 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4488 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4489 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4490 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4491 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4492 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4493 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4495 | case InitializedEntity::EK_Parameter: |
| 4496 | case InitializedEntity::EK_Temporary: |
| 4497 | return true; |
| 4498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4499 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4500 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4501 | } |
| 4502 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4503 | /// \brief Whether the given entity, when initialized with an object |
| 4504 | /// created for that initialization, requires destruction. |
| 4505 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4506 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4507 | case InitializedEntity::EK_Result: |
| 4508 | case InitializedEntity::EK_New: |
| 4509 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4510 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4511 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4512 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4513 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4514 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4515 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4516 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4517 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4518 | case InitializedEntity::EK_Variable: |
| 4519 | case InitializedEntity::EK_Parameter: |
| 4520 | case InitializedEntity::EK_Temporary: |
| 4521 | case InitializedEntity::EK_ArrayElement: |
| 4522 | case InitializedEntity::EK_Exception: |
| 4523 | return true; |
| 4524 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4525 | |
| 4526 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4529 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4530 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4531 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4532 | OverloadCandidateSet &CandidateSet, |
| 4533 | CXXRecordDecl *Class, |
| 4534 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4535 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4536 | // The container holding the constructors can under certain conditions |
| 4537 | // be changed while iterating (e.g. because of deserialization). |
| 4538 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4539 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4540 | for (SmallVector<NamedDecl*, 16>::iterator |
| 4541 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4542 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4543 | CXXConstructorDecl *Constructor = 0; |
| 4544 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4545 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4546 | // Handle copy/moveconstructors, only. |
| 4547 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4548 | !Constructor->isCopyOrMoveConstructor() || |
| 4549 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4550 | continue; |
| 4551 | |
| 4552 | DeclAccessPair FoundDecl |
| 4553 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4554 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4555 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4556 | continue; |
| 4557 | } |
| 4558 | |
| 4559 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4560 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4561 | if (ConstructorTmpl->isInvalidDecl()) |
| 4562 | continue; |
| 4563 | |
| 4564 | Constructor = cast<CXXConstructorDecl>( |
| 4565 | ConstructorTmpl->getTemplatedDecl()); |
| 4566 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4567 | continue; |
| 4568 | |
| 4569 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4570 | // candidates? |
| 4571 | DeclAccessPair FoundDecl |
| 4572 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4573 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4574 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4579 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4580 | Expr *Initializer) { |
| 4581 | switch (Entity.getKind()) { |
| 4582 | case InitializedEntity::EK_Result: |
| 4583 | return Entity.getReturnLoc(); |
| 4584 | |
| 4585 | case InitializedEntity::EK_Exception: |
| 4586 | return Entity.getThrowLoc(); |
| 4587 | |
| 4588 | case InitializedEntity::EK_Variable: |
| 4589 | return Entity.getDecl()->getLocation(); |
| 4590 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4591 | case InitializedEntity::EK_LambdaCapture: |
| 4592 | return Entity.getCaptureLoc(); |
| 4593 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4594 | case InitializedEntity::EK_ArrayElement: |
| 4595 | case InitializedEntity::EK_Member: |
| 4596 | case InitializedEntity::EK_Parameter: |
| 4597 | case InitializedEntity::EK_Temporary: |
| 4598 | case InitializedEntity::EK_New: |
| 4599 | case InitializedEntity::EK_Base: |
| 4600 | case InitializedEntity::EK_Delegating: |
| 4601 | case InitializedEntity::EK_VectorElement: |
| 4602 | case InitializedEntity::EK_ComplexElement: |
| 4603 | case InitializedEntity::EK_BlockElement: |
| 4604 | return Initializer->getLocStart(); |
| 4605 | } |
| 4606 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4607 | } |
| 4608 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4609 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4610 | /// provided by the given initializer by calling the appropriate copy |
| 4611 | /// constructor. |
| 4612 | /// |
| 4613 | /// \param S The Sema object used for type-checking. |
| 4614 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4615 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4616 | /// the type of the initializer expression or a superclass thereof. |
| 4617 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4618 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4619 | /// |
| 4620 | /// \param CurInit The initializer expression. |
| 4621 | /// |
| 4622 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4623 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4624 | /// an rvalue. |
| 4625 | /// |
| 4626 | /// \returns An expression that copies the initializer expression into |
| 4627 | /// a temporary object, or an error expression if a copy could not be |
| 4628 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4629 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4630 | QualType T, |
| 4631 | const InitializedEntity &Entity, |
| 4632 | ExprResult CurInit, |
| 4633 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4634 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4635 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4636 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4637 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4638 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4639 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4640 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4641 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4642 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4643 | // When certain criteria are met, an implementation is allowed to |
| 4644 | // omit the copy/move construction of a class object, even if the |
| 4645 | // copy/move constructor and/or destructor for the object have |
| 4646 | // side effects. [...] |
| 4647 | // - when a temporary class object that has not been bound to a |
| 4648 | // reference (12.2) would be copied/moved to a class object |
| 4649 | // with the same cv-unqualified type, the copy/move operation |
| 4650 | // can be omitted by constructing the temporary object |
| 4651 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4652 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4653 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4654 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4655 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4656 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4657 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4658 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4659 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4661 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4662 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4663 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4664 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4665 | // Only consider constructors and constructor templates. Per |
| 4666 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4667 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4668 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4669 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4670 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4671 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4672 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4673 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4674 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4675 | case OR_Success: |
| 4676 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4678 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4679 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4680 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4681 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4682 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4683 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4684 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4685 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4686 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4687 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4689 | case OR_Ambiguous: |
| 4690 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4691 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4692 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4693 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4694 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4695 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4696 | case OR_Deleted: |
| 4697 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4698 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4699 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4700 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4701 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4704 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4705 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4706 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4707 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4708 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4709 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4710 | |
| 4711 | if (IsExtraneousCopy) { |
| 4712 | // If this is a totally extraneous copy for C++03 reference |
| 4713 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4714 | // expression. We don't generate an (elided) copy operation here |
| 4715 | // because doing so would require us to pass down a flag to avoid |
| 4716 | // infinite recursion, where each step adds another extraneous, |
| 4717 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4719 | // Instantiate the default arguments of any extra parameters in |
| 4720 | // the selected copy constructor, as if we were going to create a |
| 4721 | // proper call to the copy constructor. |
| 4722 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4723 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4724 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4725 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4726 | break; |
| 4727 | |
| 4728 | // Build the default argument expression; we don't actually care |
| 4729 | // if this succeeds or not, because this routine will complain |
| 4730 | // if there was a problem. |
| 4731 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4732 | } |
| 4733 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4734 | return S.Owned(CurInitExpr); |
| 4735 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4737 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4738 | // constructor call (we might have derived-to-base conversions, or |
| 4739 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4740 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4741 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4743 | // Actually perform the constructor call. |
| 4744 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4745 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4746 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4747 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4748 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4749 | CXXConstructExpr::CK_Complete, |
| 4750 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4752 | // If we're supposed to bind temporaries, do so. |
| 4753 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4754 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4755 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4756 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4757 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4758 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4759 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4760 | /// -Wc++98-compat. |
| 4761 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4762 | const InitializedEntity &Entity, |
| 4763 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4764 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4765 | |
| 4766 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4767 | if (!Record) |
| 4768 | return; |
| 4769 | |
| 4770 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4771 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4772 | == DiagnosticsEngine::Ignored) |
| 4773 | return; |
| 4774 | |
| 4775 | // Find constructors which would have been considered. |
| 4776 | OverloadCandidateSet CandidateSet(Loc); |
| 4777 | LookupCopyAndMoveConstructors( |
| 4778 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4779 | |
| 4780 | // Perform overload resolution. |
| 4781 | OverloadCandidateSet::iterator Best; |
| 4782 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4783 | |
| 4784 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4785 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4786 | << CurInitExpr->getSourceRange(); |
| 4787 | |
| 4788 | switch (OR) { |
| 4789 | case OR_Success: |
| 4790 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4791 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4792 | // FIXME: Check default arguments as far as that's possible. |
| 4793 | break; |
| 4794 | |
| 4795 | case OR_No_Viable_Function: |
| 4796 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4797 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4798 | break; |
| 4799 | |
| 4800 | case OR_Ambiguous: |
| 4801 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4802 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4803 | break; |
| 4804 | |
| 4805 | case OR_Deleted: |
| 4806 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4807 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4808 | break; |
| 4809 | } |
| 4810 | } |
| 4811 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4812 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4813 | const InitializedEntity &Entity) { |
| 4814 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4815 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4816 | return; |
| 4817 | |
| 4818 | if (Entity.getDecl()->getDeclName()) |
| 4819 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4820 | << Entity.getDecl()->getDeclName(); |
| 4821 | else |
| 4822 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4823 | } |
| 4824 | } |
| 4825 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4826 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4827 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4828 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4829 | } |
| 4830 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4831 | static ExprResult |
| 4832 | PerformConstructorInitialization(Sema &S, |
| 4833 | const InitializedEntity &Entity, |
| 4834 | const InitializationKind &Kind, |
| 4835 | MultiExprArg Args, |
| 4836 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4837 | bool &ConstructorInitRequiresZeroInit, |
| 4838 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4839 | unsigned NumArgs = Args.size(); |
| 4840 | CXXConstructorDecl *Constructor |
| 4841 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4842 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4843 | |
| 4844 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4845 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4846 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4847 | ? Kind.getEqualLoc() |
| 4848 | : Kind.getLocation(); |
| 4849 | |
| 4850 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4851 | // Force even a trivial, implicit default constructor to be |
| 4852 | // semantically checked. We do this explicitly because we don't build |
| 4853 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4854 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4855 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4856 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4857 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4858 | } |
| 4859 | |
| 4860 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4861 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4862 | // C++ [over.match.copy]p1: |
| 4863 | // - When initializing a temporary to be bound to the first parameter |
| 4864 | // of a constructor that takes a reference to possibly cv-qualified |
| 4865 | // T as its first argument, called with a single argument in the |
| 4866 | // context of direct-initialization, explicit conversion functions |
| 4867 | // are also considered. |
| 4868 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4869 | Args.size() == 1 && |
| 4870 | Constructor->isCopyOrMoveConstructor(); |
| 4871 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4872 | // Determine the arguments required to actually perform the constructor |
| 4873 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4874 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4875 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 4876 | AllowExplicitConv, |
| 4877 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4878 | return ExprError(); |
| 4879 | |
| 4880 | |
| 4881 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4882 | (Kind.getKind() == InitializationKind::IK_DirectList || |
| 4883 | (NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4884 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4885 | Kind.getKind() == InitializationKind::IK_Value)))) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4886 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4887 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 4888 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 4889 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4890 | |
| 4891 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4892 | if (!TSInfo) |
| 4893 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4894 | SourceRange ParenRange; |
| 4895 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4896 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4897 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4898 | CurInit = S.Owned( |
| 4899 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 4900 | TSInfo, ConstructorArgs, |
| 4901 | ParenRange, IsListInitialization, |
| 4902 | HadMultipleCandidates, |
| 4903 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4904 | } else { |
| 4905 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4906 | CXXConstructExpr::CK_Complete; |
| 4907 | |
| 4908 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4909 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4910 | CXXConstructExpr::CK_VirtualBase : |
| 4911 | CXXConstructExpr::CK_NonVirtualBase; |
| 4912 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4913 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4914 | } |
| 4915 | |
| 4916 | // Only get the parenthesis range if it is a direct construction. |
| 4917 | SourceRange parenRange = |
| 4918 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4919 | Kind.getParenRange() : SourceRange(); |
| 4920 | |
| 4921 | // If the entity allows NRVO, mark the construction as elidable |
| 4922 | // unconditionally. |
| 4923 | if (Entity.allowsNRVO()) |
| 4924 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4925 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4926 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4927 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4928 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4929 | ConstructorInitRequiresZeroInit, |
| 4930 | ConstructKind, |
| 4931 | parenRange); |
| 4932 | else |
| 4933 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4934 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4935 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4936 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4937 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4938 | ConstructorInitRequiresZeroInit, |
| 4939 | ConstructKind, |
| 4940 | parenRange); |
| 4941 | } |
| 4942 | if (CurInit.isInvalid()) |
| 4943 | return ExprError(); |
| 4944 | |
| 4945 | // Only check access if all of that succeeded. |
| 4946 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4947 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 4948 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 4949 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4950 | |
| 4951 | if (shouldBindAsTemporary(Entity)) |
| 4952 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4953 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4954 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4957 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 4958 | /// longer than the current full-expression. Conservatively returns false if |
| 4959 | /// it's unclear. |
| 4960 | static bool |
| 4961 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 4962 | const InitializedEntity *Top = &Entity; |
| 4963 | while (Top->getParent()) |
| 4964 | Top = Top->getParent(); |
| 4965 | |
| 4966 | switch (Top->getKind()) { |
| 4967 | case InitializedEntity::EK_Variable: |
| 4968 | case InitializedEntity::EK_Result: |
| 4969 | case InitializedEntity::EK_Exception: |
| 4970 | case InitializedEntity::EK_Member: |
| 4971 | case InitializedEntity::EK_New: |
| 4972 | case InitializedEntity::EK_Base: |
| 4973 | case InitializedEntity::EK_Delegating: |
| 4974 | return true; |
| 4975 | |
| 4976 | case InitializedEntity::EK_ArrayElement: |
| 4977 | case InitializedEntity::EK_VectorElement: |
| 4978 | case InitializedEntity::EK_BlockElement: |
| 4979 | case InitializedEntity::EK_ComplexElement: |
| 4980 | // Could not determine what the full initialization is. Assume it might not |
| 4981 | // outlive the full-expression. |
| 4982 | return false; |
| 4983 | |
| 4984 | case InitializedEntity::EK_Parameter: |
| 4985 | case InitializedEntity::EK_Temporary: |
| 4986 | case InitializedEntity::EK_LambdaCapture: |
| 4987 | // The entity being initialized might not outlive the full-expression. |
| 4988 | return false; |
| 4989 | } |
| 4990 | |
| 4991 | llvm_unreachable("unknown entity kind"); |
| 4992 | } |
| 4993 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4994 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4995 | InitializationSequence::Perform(Sema &S, |
| 4996 | const InitializedEntity &Entity, |
| 4997 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4998 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4999 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5000 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5001 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5002 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5003 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5005 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5006 | // If the declaration is a non-dependent, incomplete array type |
| 5007 | // that has an initializer, then its type will be completed once |
| 5008 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5009 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5010 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5011 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5012 | if (const IncompleteArrayType *ArrayT |
| 5013 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5014 | // FIXME: We don't currently have the ability to accurately |
| 5015 | // compute the length of an initializer list without |
| 5016 | // performing full type-checking of the initializer list |
| 5017 | // (since we have to determine where braces are implicitly |
| 5018 | // introduced and such). So, we fall back to making the array |
| 5019 | // type a dependently-sized array type with no specified |
| 5020 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5021 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5022 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5024 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5025 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5026 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5027 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5028 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5029 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5030 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5031 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | *ResultType |
| 5035 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5036 | /*NumElts=*/0, |
| 5037 | ArrayT->getSizeModifier(), |
| 5038 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5039 | Brackets); |
| 5040 | } |
| 5041 | |
| 5042 | } |
| 5043 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5044 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5045 | !Kind.isExplicitCast()) { |
| 5046 | // Rebuild the ParenListExpr. |
| 5047 | SourceRange ParenRange = Kind.getParenRange(); |
| 5048 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5049 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5050 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5051 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5052 | Kind.isExplicitCast() || |
| 5053 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5054 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5057 | // No steps means no initialization. |
| 5058 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5059 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5061 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5062 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5063 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 5064 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5065 | // from an initializer list. For parameters, we produce a better warning |
| 5066 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5067 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5068 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5069 | << Init->getSourceRange(); |
| 5070 | } |
| 5071 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5072 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5073 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5074 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5075 | Entity.getType()->isPointerType() && |
| 5076 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5077 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5078 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5079 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5080 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5081 | << Init->getSourceRange(); |
| 5082 | } |
| 5083 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5084 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5085 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5086 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5087 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5088 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5089 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5090 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5091 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5092 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5093 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5095 | // grab the only argument out the Args and place it into the "current" |
| 5096 | // initializer. |
| 5097 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5098 | case SK_ResolveAddressOfOverloadedFunction: |
| 5099 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5100 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5101 | case SK_CastDerivedToBaseLValue: |
| 5102 | case SK_BindReference: |
| 5103 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5104 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5105 | case SK_UserConversion: |
| 5106 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5107 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5108 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5109 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5110 | case SK_ConversionSequence: |
| 5111 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5112 | case SK_UnwrapInitList: |
| 5113 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5114 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5115 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5116 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5117 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5118 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5119 | case SK_PassByIndirectCopyRestore: |
| 5120 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5121 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5122 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5123 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5124 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5125 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5126 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5127 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5128 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5129 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5131 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5132 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5133 | case SK_ZeroInitialization: |
| 5134 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5135 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | |
| 5137 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5138 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5139 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5140 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5141 | Step != StepEnd; ++Step) { |
| 5142 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5143 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5144 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5145 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5146 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5147 | switch (Step->Kind) { |
| 5148 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5150 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5151 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5152 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5153 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5154 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5155 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5156 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5157 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5158 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5159 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5160 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5161 | case SK_CastDerivedToBaseLValue: { |
| 5162 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5163 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5165 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5166 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5167 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5168 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5169 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5170 | CurInit.get()->getLocStart(), |
| 5171 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5172 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5173 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5174 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5175 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5176 | QualType T = SourceType; |
| 5177 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5178 | T = Pointer->getPointeeType(); |
| 5179 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5180 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5181 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5182 | } |
| 5183 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5184 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5185 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5186 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5187 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5188 | VK_XValue : |
| 5189 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5190 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5191 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5192 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5193 | CurInit.get(), |
| 5194 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5195 | break; |
| 5196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5197 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5198 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5199 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5200 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 5201 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5202 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5203 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5204 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5205 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5206 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5207 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5208 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5209 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5210 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5211 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5212 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5213 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5214 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5215 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5216 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5217 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5218 | // Reference binding does not have any corresponding ASTs. |
| 5219 | |
| 5220 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5221 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5222 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5223 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5224 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5225 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5226 | case SK_BindReferenceToTemporary: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5227 | // Make sure the "temporary" is actually an rvalue. |
| 5228 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5229 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5230 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5231 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5232 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5233 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5234 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5235 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5236 | Entity.getType().getNonReferenceType(), |
| 5237 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5238 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5239 | |
| 5240 | // If we're binding to an Objective-C object that has lifetime, we |
| 5241 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5242 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5243 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5244 | S.ExprNeedsCleanups = true; |
| 5245 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5246 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5247 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5248 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5249 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5250 | /*IsExtraneousCopy=*/true); |
| 5251 | break; |
| 5252 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5253 | case SK_UserConversion: { |
| 5254 | // We have a user-defined conversion that invokes either a constructor |
| 5255 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5256 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5257 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5258 | FunctionDecl *Fn = Step->Function.Function; |
| 5259 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5260 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5261 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5262 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5263 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5264 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5265 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5266 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5268 | // Determine the arguments required to actually perform the constructor |
| 5269 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5270 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5271 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5272 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5273 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5274 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5276 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5277 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5278 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5279 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5280 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5281 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5282 | CXXConstructExpr::CK_Complete, |
| 5283 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5284 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5285 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5286 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5287 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5288 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5289 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5290 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5292 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5293 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5294 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5295 | S.IsDerivedFrom(SourceType, Class)) |
| 5296 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5297 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5298 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5299 | } else { |
| 5300 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5301 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5302 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5303 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5304 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5305 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | |
| 5307 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5308 | // derived-to-base conversion? I believe the answer is "no", because |
| 5309 | // 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] | 5310 | ExprResult CurInitExprRes = |
| 5311 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5312 | FoundFn, Conversion); |
| 5313 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5314 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5315 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5317 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5318 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5319 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5320 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5321 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5322 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5323 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5324 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5325 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5326 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5327 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5328 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5329 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5330 | |
| 5331 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5332 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5333 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5335 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5336 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5337 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5338 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5339 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5340 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5341 | } |
| 5342 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5343 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5344 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5345 | CurInit.get()->getType(), |
| 5346 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5347 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5348 | if (MaybeBindToTemp) |
| 5349 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5350 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5351 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5352 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5353 | break; |
| 5354 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5355 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5356 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5357 | case SK_QualificationConversionXValue: |
| 5358 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5359 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5360 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5361 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5362 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5363 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5364 | VK_XValue : |
| 5365 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5366 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5367 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5370 | case SK_LValueToRValue: { |
| 5371 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5372 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5373 | CK_LValueToRValue, |
| 5374 | CurInit.take(), |
| 5375 | /*BasePath=*/0, |
| 5376 | VK_RValue)); |
| 5377 | break; |
| 5378 | } |
| 5379 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5380 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5381 | Sema::CheckedConversionKind CCK |
| 5382 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5383 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5384 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5385 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5386 | ExprResult CurInitExprRes = |
| 5387 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5388 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5389 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5390 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5391 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5392 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5393 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5394 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5395 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5396 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5397 | // Hack: We must pass *ResultType if available in order to set the type |
| 5398 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5399 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5400 | // temporary, not a reference, so we should pass Ty. |
| 5401 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5402 | // Since this step is never used for a reference directly, we explicitly |
| 5403 | // unwrap references here and rewrap them afterwards. |
| 5404 | // We also need to create a InitializeTemporary entity for this. |
| 5405 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5406 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5407 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5408 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5409 | InitListChecker PerformInitList(S, InitEntity, |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5410 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5411 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5412 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5413 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5414 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5415 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5416 | if (ResultType) { |
| 5417 | if ((*ResultType)->isRValueReferenceType()) |
| 5418 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5419 | else if ((*ResultType)->isLValueReferenceType()) |
| 5420 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5421 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5422 | *ResultType = Ty; |
| 5423 | } |
| 5424 | |
| 5425 | InitListExpr *StructuredInitList = |
| 5426 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5427 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5428 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5429 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5430 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5431 | break; |
| 5432 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5433 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5434 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5435 | // When an initializer list is passed for a parameter of type "reference |
| 5436 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5437 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5438 | // FIXME: This is a hack. What we really should do is create a user |
| 5439 | // conversion step for this case, but this makes it considerably more |
| 5440 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5441 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5442 | Entity.getType().getNonReferenceType()); |
| 5443 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5444 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5445 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5446 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5447 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5448 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5449 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5450 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5451 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5452 | ConstructorInitRequiresZeroInit, |
| 5453 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5456 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5457 | case SK_UnwrapInitList: |
| 5458 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5459 | break; |
| 5460 | |
| 5461 | case SK_RewrapInitList: { |
| 5462 | Expr *E = CurInit.take(); |
| 5463 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5464 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5465 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5466 | ILE->setSyntacticForm(Syntactic); |
| 5467 | ILE->setType(E->getType()); |
| 5468 | ILE->setValueKind(E->getValueKind()); |
| 5469 | CurInit = S.Owned(ILE); |
| 5470 | break; |
| 5471 | } |
| 5472 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5473 | case SK_ConstructorInitialization: { |
| 5474 | // When an initializer list is passed for a parameter of type "reference |
| 5475 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5476 | // EK_Parameter entity with reference type. |
| 5477 | // FIXME: This is a hack. What we really should do is create a user |
| 5478 | // conversion step for this case, but this makes it considerably more |
| 5479 | // complicated. For now, this will do. |
| 5480 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5481 | Entity.getType().getNonReferenceType()); |
| 5482 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5483 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5484 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5485 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5486 | ConstructorInitRequiresZeroInit, |
| 5487 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5488 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5489 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5490 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5491 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5492 | step_iterator NextStep = Step; |
| 5493 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5494 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5495 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5496 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5497 | // The need for zero-initialization is recorded directly into |
| 5498 | // the call to the object's constructor within the next step. |
| 5499 | ConstructorInitRequiresZeroInit = true; |
| 5500 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5501 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5502 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5503 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5504 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5505 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5506 | Kind.getRange().getBegin()); |
| 5507 | |
| 5508 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5509 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5510 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5511 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5512 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5513 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5514 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5515 | break; |
| 5516 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5517 | |
| 5518 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5519 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5520 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5521 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5522 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5523 | if (Result.isInvalid()) |
| 5524 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5525 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5526 | |
| 5527 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5528 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5529 | if (ConvTy != Sema::Compatible && |
| 5530 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5531 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5532 | == Sema::Compatible) |
| 5533 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5534 | if (CurInitExprRes.isInvalid()) |
| 5535 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5536 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5537 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5538 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5539 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5540 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5541 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5542 | getAssignmentAction(Entity), |
| 5543 | &Complained)) { |
| 5544 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5545 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5546 | } else if (Complained) |
| 5547 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5548 | break; |
| 5549 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5550 | |
| 5551 | case SK_StringInit: { |
| 5552 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5553 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5554 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5555 | break; |
| 5556 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5557 | |
| 5558 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5559 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5560 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5561 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5562 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5563 | |
| 5564 | case SK_ArrayInit: |
| 5565 | // Okay: we checked everything before creating this step. Note that |
| 5566 | // this is a GNU extension. |
| 5567 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5568 | << Step->Type << CurInit.get()->getType() |
| 5569 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5570 | |
| 5571 | // If the destination type is an incomplete array type, update the |
| 5572 | // type accordingly. |
| 5573 | if (ResultType) { |
| 5574 | if (const IncompleteArrayType *IncompleteDest |
| 5575 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5576 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5577 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5578 | *ResultType = S.Context.getConstantArrayType( |
| 5579 | IncompleteDest->getElementType(), |
| 5580 | ConstantSource->getSize(), |
| 5581 | ArrayType::Normal, 0); |
| 5582 | } |
| 5583 | } |
| 5584 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5585 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5586 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5587 | case SK_ParenthesizedArrayInit: |
| 5588 | // Okay: we checked everything before creating this step. Note that |
| 5589 | // this is a GNU extension. |
| 5590 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5591 | << CurInit.get()->getSourceRange(); |
| 5592 | break; |
| 5593 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5594 | case SK_PassByIndirectCopyRestore: |
| 5595 | case SK_PassByIndirectRestore: |
| 5596 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5597 | CurInit = S.Owned(new (S.Context) |
| 5598 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5599 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5600 | break; |
| 5601 | |
| 5602 | case SK_ProduceObjCObject: |
| 5603 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5604 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5605 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5606 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5607 | |
| 5608 | case SK_StdInitializerList: { |
| 5609 | QualType Dest = Step->Type; |
| 5610 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 5611 | bool Success = S.isStdInitializerList(Dest.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5612 | (void)Success; |
| 5613 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5614 | |
| 5615 | // If the element type has a destructor, check it. |
| 5616 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5617 | if (!RD->hasIrrelevantDestructor()) { |
| 5618 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5619 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5620 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5621 | S.PDiag(diag::err_access_dtor_temp) << E); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5622 | if (S.DiagnoseUseOfDecl(Destructor, Kind.getLocation())) |
| 5623 | return ExprError(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5624 | } |
| 5625 | } |
| 5626 | } |
| 5627 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5628 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5629 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5630 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5631 | unsigned NumInits = ILE->getNumInits(); |
| 5632 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5633 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5634 | S.Context.getConstantArrayType(E, |
| 5635 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5636 | NumInits), |
| 5637 | ArrayType::Normal, 0)); |
| 5638 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5639 | 0, HiddenArray); |
| 5640 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5641 | Element.setElementIndex(i); |
| 5642 | ExprResult Init = S.Owned(ILE->getInit(i)); |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5643 | ExprResult Res = S.PerformCopyInitialization( |
| 5644 | Element, Init.get()->getExprLoc(), Init, |
| 5645 | /*TopLevelOfInitList=*/ true); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5646 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5647 | Converted[i] = Res.take(); |
| 5648 | } |
| 5649 | InitListExpr *Semantic = new (S.Context) |
| 5650 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5651 | Converted, ILE->getRBraceLoc()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5652 | Semantic->setSyntacticForm(ILE); |
| 5653 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5654 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5655 | CurInit = S.Owned(Semantic); |
| 5656 | break; |
| 5657 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5658 | case SK_OCLSamplerInit: { |
| 5659 | assert(Step->Type->isSamplerT() && |
| 5660 | "Sampler initialization on non sampler type."); |
| 5661 | |
| 5662 | QualType SourceType = CurInit.get()->getType(); |
| 5663 | InitializedEntity::EntityKind EntityKind = Entity.getKind(); |
| 5664 | |
| 5665 | if (EntityKind == InitializedEntity::EK_Parameter) { |
| 5666 | if (!SourceType->isSamplerT()) |
| 5667 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 5668 | << SourceType; |
| 5669 | } else if (EntityKind != InitializedEntity::EK_Variable) { |
| 5670 | llvm_unreachable("Invalid EntityKind!"); |
| 5671 | } |
| 5672 | |
| 5673 | break; |
| 5674 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5675 | case SK_OCLZeroEvent: { |
| 5676 | assert(Step->Type->isEventT() && |
| 5677 | "Event initialization on non event type."); |
| 5678 | |
| 5679 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 5680 | CK_ZeroToOCLEvent, |
| 5681 | CurInit.get()->getValueKind()); |
| 5682 | break; |
| 5683 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5684 | } |
| 5685 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5686 | |
| 5687 | // Diagnose non-fatal problems with the completed initialization. |
| 5688 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5689 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5690 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5691 | cast<FieldDecl>(Entity.getDecl()), |
| 5692 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5693 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5694 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5697 | /// Somewhere within T there is an uninitialized reference subobject. |
| 5698 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5699 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 5700 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5701 | if (T->isReferenceType()) { |
| 5702 | S.Diag(Loc, diag::err_reference_without_init) |
| 5703 | << T.getNonReferenceType(); |
| 5704 | return true; |
| 5705 | } |
| 5706 | |
| 5707 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 5708 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 5709 | return false; |
| 5710 | |
| 5711 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5712 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5713 | if (FI->isUnnamedBitfield()) |
| 5714 | continue; |
| 5715 | |
| 5716 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 5717 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5718 | return true; |
| 5719 | } |
| 5720 | } |
| 5721 | |
| 5722 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5723 | BE = RD->bases_end(); |
| 5724 | BI != BE; ++BI) { |
| 5725 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 5726 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5727 | return true; |
| 5728 | } |
| 5729 | } |
| 5730 | |
| 5731 | return false; |
| 5732 | } |
| 5733 | |
| 5734 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5735 | //===----------------------------------------------------------------------===// |
| 5736 | // Diagnose initialization failures |
| 5737 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5738 | |
| 5739 | /// Emit notes associated with an initialization that failed due to a |
| 5740 | /// "simple" conversion failure. |
| 5741 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 5742 | Expr *op) { |
| 5743 | QualType destType = entity.getType(); |
| 5744 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5745 | op->getType()->isObjCObjectPointerType()) { |
| 5746 | |
| 5747 | // Emit a possible note about the conversion failing because the |
| 5748 | // operand is a message send with a related result type. |
| 5749 | S.EmitRelatedResultTypeNote(op); |
| 5750 | |
| 5751 | // Emit a possible note about a return failing because we're |
| 5752 | // expecting a related result type. |
| 5753 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 5754 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 5755 | } |
| 5756 | } |
| 5757 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5758 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5759 | const InitializedEntity &Entity, |
| 5760 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5761 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5762 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5763 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5764 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5765 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5766 | switch (Failure) { |
| 5767 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5768 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5769 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5770 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 5771 | // If this is value-initialization, this could be nested some way within |
| 5772 | // the target type. |
| 5773 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 5774 | DestType->isReferenceType()); |
| 5775 | bool Diagnosed = |
| 5776 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 5777 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 5778 | (void)Diagnosed; |
| 5779 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5780 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5781 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5782 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5783 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5784 | case FK_ArrayNeedsInitList: |
| 5785 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5786 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5787 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5788 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5790 | case FK_ArrayTypeMismatch: |
| 5791 | case FK_NonConstantArrayInit: |
| 5792 | S.Diag(Kind.getLocation(), |
| 5793 | (Failure == FK_ArrayTypeMismatch |
| 5794 | ? diag::err_array_init_different_type |
| 5795 | : diag::err_array_init_non_constant_array)) |
| 5796 | << DestType.getNonReferenceType() |
| 5797 | << Args[0]->getType() |
| 5798 | << Args[0]->getSourceRange(); |
| 5799 | break; |
| 5800 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5801 | case FK_VariableLengthArrayHasInitializer: |
| 5802 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5803 | << Args[0]->getSourceRange(); |
| 5804 | break; |
| 5805 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5806 | case FK_AddressOfOverloadFailed: { |
| 5807 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5809 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5810 | true, |
| 5811 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5812 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5813 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5814 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5815 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5816 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5817 | switch (FailedOverloadResult) { |
| 5818 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5819 | if (Failure == FK_UserConversionOverloadFailed) |
| 5820 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5821 | << Args[0]->getType() << DestType |
| 5822 | << Args[0]->getSourceRange(); |
| 5823 | else |
| 5824 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5825 | << DestType << Args[0]->getType() |
| 5826 | << Args[0]->getSourceRange(); |
| 5827 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5828 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5829 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5831 | case OR_No_Viable_Function: |
| 5832 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5833 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5834 | << Args[0]->getSourceRange(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5835 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5836 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5838 | case OR_Deleted: { |
| 5839 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5840 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5841 | << Args[0]->getSourceRange(); |
| 5842 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5843 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5844 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5845 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5846 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5847 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5848 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5849 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5850 | } |
| 5851 | break; |
| 5852 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5853 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5854 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5855 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5856 | } |
| 5857 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5859 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5860 | if (isa<InitListExpr>(Args[0])) { |
| 5861 | S.Diag(Kind.getLocation(), |
| 5862 | diag::err_lvalue_reference_bind_to_initlist) |
| 5863 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5864 | << DestType.getNonReferenceType() |
| 5865 | << Args[0]->getSourceRange(); |
| 5866 | break; |
| 5867 | } |
| 5868 | // Intentional fallthrough |
| 5869 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5870 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5872 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5873 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5874 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5875 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5876 | << DestType.getNonReferenceType() |
| 5877 | << Args[0]->getType() |
| 5878 | << Args[0]->getSourceRange(); |
| 5879 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5880 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5881 | case FK_RValueReferenceBindingToLValue: |
| 5882 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5883 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5884 | << Args[0]->getSourceRange(); |
| 5885 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5887 | case FK_ReferenceInitDropsQualifiers: |
| 5888 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5889 | << DestType.getNonReferenceType() |
| 5890 | << Args[0]->getType() |
| 5891 | << Args[0]->getSourceRange(); |
| 5892 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5893 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5894 | case FK_ReferenceInitFailed: |
| 5895 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5896 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5897 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5898 | << Args[0]->getType() |
| 5899 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5900 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5901 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5902 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5903 | case FK_ConversionFailed: { |
| 5904 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5905 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5906 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5907 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5908 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5909 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5910 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5911 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5912 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5913 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5914 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5915 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5916 | |
| 5917 | case FK_ConversionFromPropertyFailed: |
| 5918 | // No-op. This error has already been reported. |
| 5919 | break; |
| 5920 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5921 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5922 | SourceRange R; |
| 5923 | |
| 5924 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5925 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5926 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5928 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5929 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5930 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5931 | if (Kind.isCStyleOrFunctionalCast()) |
| 5932 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5933 | << R; |
| 5934 | else |
| 5935 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5936 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5937 | break; |
| 5938 | } |
| 5939 | |
| 5940 | case FK_ReferenceBindingToInitList: |
| 5941 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5942 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5943 | break; |
| 5944 | |
| 5945 | case FK_InitListBadDestinationType: |
| 5946 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5947 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5948 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5950 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5951 | case FK_ConstructorOverloadFailed: { |
| 5952 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5953 | if (Args.size()) |
| 5954 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 5955 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5956 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5957 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5958 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5959 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5960 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5963 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5964 | // bad. |
| 5965 | switch (FailedOverloadResult) { |
| 5966 | case OR_Ambiguous: |
| 5967 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5968 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5969 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5970 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5972 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5973 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5974 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5975 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5976 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5977 | // This is implicit default initialization of a member or |
| 5978 | // base within a constructor. If no viable function was |
| 5979 | // found, notify the user that she needs to explicitly |
| 5980 | // initialize this base/member. |
| 5981 | CXXConstructorDecl *Constructor |
| 5982 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5983 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5984 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5985 | << (Constructor->getInheritedConstructor() ? 2 : |
| 5986 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5987 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5988 | << /*base=*/0 |
| 5989 | << Entity.getType(); |
| 5990 | |
| 5991 | RecordDecl *BaseDecl |
| 5992 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5993 | ->getDecl(); |
| 5994 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5995 | << S.Context.getTagDeclType(BaseDecl); |
| 5996 | } else { |
| 5997 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5998 | << (Constructor->getInheritedConstructor() ? 2 : |
| 5999 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6000 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6001 | << /*member=*/1 |
| 6002 | << Entity.getName(); |
| 6003 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6004 | |
| 6005 | if (const RecordType *Record |
| 6006 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6007 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6008 | diag::note_previous_decl) |
| 6009 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6010 | } |
| 6011 | break; |
| 6012 | } |
| 6013 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6014 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6015 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6016 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6017 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6019 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6020 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6021 | OverloadingResult Ovl |
| 6022 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6023 | if (Ovl != OR_Deleted) { |
| 6024 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6025 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6026 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6027 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6028 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6029 | |
| 6030 | // If this is a defaulted or implicitly-declared function, then |
| 6031 | // it was implicitly deleted. Make it clear that the deletion was |
| 6032 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6033 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6034 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6035 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6036 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6037 | else |
| 6038 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6039 | << true << DestType << ArgsRange; |
| 6040 | |
| 6041 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6042 | break; |
| 6043 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6045 | case OR_Success: |
| 6046 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6047 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6048 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6049 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6050 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6051 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6052 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6053 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6054 | // This is implicit default-initialization of a const member in |
| 6055 | // a constructor. Complain that it needs to be explicitly |
| 6056 | // initialized. |
| 6057 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6058 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6059 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6060 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6061 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6062 | << /*const=*/1 |
| 6063 | << Entity.getName(); |
| 6064 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6065 | << Entity.getName(); |
| 6066 | } else { |
| 6067 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6068 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6069 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6070 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6072 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6073 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6074 | diag::err_init_incomplete_type); |
| 6075 | break; |
| 6076 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6077 | case FK_ListInitializationFailed: { |
| 6078 | // Run the init list checker again to emit diagnostics. |
| 6079 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6080 | QualType DestType = Entity.getType(); |
| 6081 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 6082 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6083 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6084 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6085 | assert(DiagnoseInitList.HadError() && |
| 6086 | "Inconsistent init list check result."); |
| 6087 | break; |
| 6088 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6089 | |
| 6090 | case FK_PlaceholderType: { |
| 6091 | // FIXME: Already diagnosed! |
| 6092 | break; |
| 6093 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6094 | |
| 6095 | case FK_InitListElementCopyFailure: { |
| 6096 | // Try to perform all copies again. |
| 6097 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6098 | unsigned NumInits = InitList->getNumInits(); |
| 6099 | QualType DestType = Entity.getType(); |
| 6100 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 6101 | bool Success = S.isStdInitializerList(DestType.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6102 | (void)Success; |
| 6103 | assert(Success && "Where did the std::initializer_list go?"); |
| 6104 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 6105 | S.Context.getConstantArrayType(E, |
| 6106 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6107 | NumInits), |
| 6108 | ArrayType::Normal, 0)); |
| 6109 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 6110 | 0, HiddenArray); |
| 6111 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 6112 | // where the init list type is wrong, e.g. |
| 6113 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 6114 | // FIXME: Emit a note if we hit the limit? |
| 6115 | int ErrorCount = 0; |
| 6116 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 6117 | Element.setElementIndex(i); |
| 6118 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 6119 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 6120 | .isInvalid()) |
| 6121 | ++ErrorCount; |
| 6122 | } |
| 6123 | break; |
| 6124 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6125 | |
| 6126 | case FK_ExplicitConstructor: { |
| 6127 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6128 | << Args[0]->getSourceRange(); |
| 6129 | OverloadCandidateSet::iterator Best; |
| 6130 | OverloadingResult Ovl |
| 6131 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6132 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6133 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6134 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6135 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6136 | break; |
| 6137 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6138 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6139 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6140 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6141 | return true; |
| 6142 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6143 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6144 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6145 | switch (SequenceKind) { |
| 6146 | case FailedSequence: { |
| 6147 | OS << "Failed sequence: "; |
| 6148 | switch (Failure) { |
| 6149 | case FK_TooManyInitsForReference: |
| 6150 | OS << "too many initializers for reference"; |
| 6151 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6153 | case FK_ArrayNeedsInitList: |
| 6154 | OS << "array requires initializer list"; |
| 6155 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6156 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6157 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6158 | OS << "array requires initializer list or string literal"; |
| 6159 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6161 | case FK_ArrayTypeMismatch: |
| 6162 | OS << "array type mismatch"; |
| 6163 | break; |
| 6164 | |
| 6165 | case FK_NonConstantArrayInit: |
| 6166 | OS << "non-constant array initializer"; |
| 6167 | break; |
| 6168 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6169 | case FK_AddressOfOverloadFailed: |
| 6170 | OS << "address of overloaded function failed"; |
| 6171 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6172 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6173 | case FK_ReferenceInitOverloadFailed: |
| 6174 | OS << "overload resolution for reference initialization failed"; |
| 6175 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6176 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6177 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6178 | OS << "non-const lvalue reference bound to temporary"; |
| 6179 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6180 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6181 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6182 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6183 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6185 | case FK_RValueReferenceBindingToLValue: |
| 6186 | OS << "rvalue reference bound to an lvalue"; |
| 6187 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6189 | case FK_ReferenceInitDropsQualifiers: |
| 6190 | OS << "reference initialization drops qualifiers"; |
| 6191 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6193 | case FK_ReferenceInitFailed: |
| 6194 | OS << "reference initialization failed"; |
| 6195 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6197 | case FK_ConversionFailed: |
| 6198 | OS << "conversion failed"; |
| 6199 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6200 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6201 | case FK_ConversionFromPropertyFailed: |
| 6202 | OS << "conversion from property failed"; |
| 6203 | break; |
| 6204 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6205 | case FK_TooManyInitsForScalar: |
| 6206 | OS << "too many initializers for scalar"; |
| 6207 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6209 | case FK_ReferenceBindingToInitList: |
| 6210 | OS << "referencing binding to initializer list"; |
| 6211 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6213 | case FK_InitListBadDestinationType: |
| 6214 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6215 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6217 | case FK_UserConversionOverloadFailed: |
| 6218 | OS << "overloading failed for user-defined conversion"; |
| 6219 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6220 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6221 | case FK_ConstructorOverloadFailed: |
| 6222 | OS << "constructor overloading failed"; |
| 6223 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6224 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6225 | case FK_DefaultInitOfConst: |
| 6226 | OS << "default initialization of a const variable"; |
| 6227 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6228 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6229 | case FK_Incomplete: |
| 6230 | OS << "initialization of incomplete type"; |
| 6231 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6232 | |
| 6233 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6234 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6235 | break; |
| 6236 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6237 | case FK_VariableLengthArrayHasInitializer: |
| 6238 | OS << "variable length array has an initializer"; |
| 6239 | break; |
| 6240 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6241 | case FK_PlaceholderType: |
| 6242 | OS << "initializer expression isn't contextually valid"; |
| 6243 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6244 | |
| 6245 | case FK_ListConstructorOverloadFailed: |
| 6246 | OS << "list constructor overloading failed"; |
| 6247 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6248 | |
| 6249 | case FK_InitListElementCopyFailure: |
| 6250 | OS << "copy construction of initializer list element failed"; |
| 6251 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6252 | |
| 6253 | case FK_ExplicitConstructor: |
| 6254 | OS << "list copy initialization chose explicit constructor"; |
| 6255 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6257 | OS << '\n'; |
| 6258 | return; |
| 6259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6261 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6262 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6263 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6264 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6265 | case NormalSequence: |
| 6266 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6267 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6268 | } |
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 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6271 | if (S != step_begin()) { |
| 6272 | OS << " -> "; |
| 6273 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6275 | switch (S->Kind) { |
| 6276 | case SK_ResolveAddressOfOverloadedFunction: |
| 6277 | OS << "resolve address of overloaded function"; |
| 6278 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6280 | case SK_CastDerivedToBaseRValue: |
| 6281 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6282 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6284 | case SK_CastDerivedToBaseXValue: |
| 6285 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6286 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6288 | case SK_CastDerivedToBaseLValue: |
| 6289 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6290 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6291 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6292 | case SK_BindReference: |
| 6293 | OS << "bind reference to lvalue"; |
| 6294 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6296 | case SK_BindReferenceToTemporary: |
| 6297 | OS << "bind reference to a temporary"; |
| 6298 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6299 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6300 | case SK_ExtraneousCopyToTemporary: |
| 6301 | OS << "extraneous C++03 copy to temporary"; |
| 6302 | break; |
| 6303 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6304 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6305 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6306 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6307 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6308 | case SK_QualificationConversionRValue: |
| 6309 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6310 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6311 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6312 | case SK_QualificationConversionXValue: |
| 6313 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6314 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6316 | case SK_QualificationConversionLValue: |
| 6317 | OS << "qualification conversion (lvalue)"; |
| 6318 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6319 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6320 | case SK_LValueToRValue: |
| 6321 | OS << "load (lvalue to rvalue)"; |
| 6322 | break; |
| 6323 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6324 | case SK_ConversionSequence: |
| 6325 | OS << "implicit conversion sequence ("; |
| 6326 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6327 | OS << ")"; |
| 6328 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6329 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6330 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6331 | OS << "list aggregate initialization"; |
| 6332 | break; |
| 6333 | |
| 6334 | case SK_ListConstructorCall: |
| 6335 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6336 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6338 | case SK_UnwrapInitList: |
| 6339 | OS << "unwrap reference initializer list"; |
| 6340 | break; |
| 6341 | |
| 6342 | case SK_RewrapInitList: |
| 6343 | OS << "rewrap reference initializer list"; |
| 6344 | break; |
| 6345 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6346 | case SK_ConstructorInitialization: |
| 6347 | OS << "constructor initialization"; |
| 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 SK_ZeroInitialization: |
| 6351 | OS << "zero initialization"; |
| 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 SK_CAssignment: |
| 6355 | OS << "C assignment"; |
| 6356 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6357 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6358 | case SK_StringInit: |
| 6359 | OS << "string initialization"; |
| 6360 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6361 | |
| 6362 | case SK_ObjCObjectConversion: |
| 6363 | OS << "Objective-C object conversion"; |
| 6364 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6365 | |
| 6366 | case SK_ArrayInit: |
| 6367 | OS << "array initialization"; |
| 6368 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6369 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6370 | case SK_ParenthesizedArrayInit: |
| 6371 | OS << "parenthesized array initialization"; |
| 6372 | break; |
| 6373 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6374 | case SK_PassByIndirectCopyRestore: |
| 6375 | OS << "pass by indirect copy and restore"; |
| 6376 | break; |
| 6377 | |
| 6378 | case SK_PassByIndirectRestore: |
| 6379 | OS << "pass by indirect restore"; |
| 6380 | break; |
| 6381 | |
| 6382 | case SK_ProduceObjCObject: |
| 6383 | OS << "Objective-C object retension"; |
| 6384 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6385 | |
| 6386 | case SK_StdInitializerList: |
| 6387 | OS << "std::initializer_list from initializer list"; |
| 6388 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6389 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6390 | case SK_OCLSamplerInit: |
| 6391 | OS << "OpenCL sampler_t from integer constant"; |
| 6392 | break; |
| 6393 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6394 | case SK_OCLZeroEvent: |
| 6395 | OS << "OpenCL event_t from zero"; |
| 6396 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6397 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6398 | |
| 6399 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6400 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6401 | |
| 6402 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | void InitializationSequence::dump() const { |
| 6406 | dump(llvm::errs()); |
| 6407 | } |
| 6408 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6409 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6410 | QualType EntityType, |
| 6411 | const Expr *PreInit, |
| 6412 | const Expr *PostInit) { |
| 6413 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6414 | return; |
| 6415 | |
| 6416 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6417 | // an initialization sequence. |
| 6418 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6419 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6420 | return; |
| 6421 | |
| 6422 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6423 | const StandardConversionSequence *SCS = 0; |
| 6424 | switch (ICS.getKind()) { |
| 6425 | case ImplicitConversionSequence::StandardConversion: |
| 6426 | SCS = &ICS.Standard; |
| 6427 | break; |
| 6428 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6429 | SCS = &ICS.UserDefined.After; |
| 6430 | break; |
| 6431 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6432 | case ImplicitConversionSequence::EllipsisConversion: |
| 6433 | case ImplicitConversionSequence::BadConversion: |
| 6434 | return; |
| 6435 | } |
| 6436 | |
| 6437 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6438 | // operator was used, this may be different from both the type of the entity |
| 6439 | // and of the pre-initialization expression. |
| 6440 | QualType PreNarrowingType = PreInit->getType(); |
| 6441 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6442 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6443 | |
| 6444 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6445 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6446 | QualType ConstantType; |
| 6447 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6448 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6449 | case NK_Not_Narrowing: |
| 6450 | // No narrowing occurred. |
| 6451 | return; |
| 6452 | |
| 6453 | case NK_Type_Narrowing: |
| 6454 | // This was a floating-to-integer conversion, which is always considered a |
| 6455 | // narrowing conversion even if the value is a constant and can be |
| 6456 | // represented exactly as an integer. |
| 6457 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6458 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6459 | diag::warn_init_list_type_narrowing |
| 6460 | : S.isSFINAEContext()? |
| 6461 | diag::err_init_list_type_narrowing_sfinae |
| 6462 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6463 | << PostInit->getSourceRange() |
| 6464 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6465 | << EntityType.getLocalUnqualifiedType(); |
| 6466 | break; |
| 6467 | |
| 6468 | case NK_Constant_Narrowing: |
| 6469 | // A constant value was narrowed. |
| 6470 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6471 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6472 | diag::warn_init_list_constant_narrowing |
| 6473 | : S.isSFINAEContext()? |
| 6474 | diag::err_init_list_constant_narrowing_sfinae |
| 6475 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6476 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6477 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6478 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6479 | break; |
| 6480 | |
| 6481 | case NK_Variable_Narrowing: |
| 6482 | // A variable's value may have been narrowed. |
| 6483 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6484 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6485 | diag::warn_init_list_variable_narrowing |
| 6486 | : S.isSFINAEContext()? |
| 6487 | diag::err_init_list_variable_narrowing_sfinae |
| 6488 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6489 | << PostInit->getSourceRange() |
| 6490 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6491 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6492 | break; |
| 6493 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6494 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6495 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6496 | llvm::raw_svector_ostream OS(StaticCast); |
| 6497 | OS << "static_cast<"; |
| 6498 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6499 | // It's important to use the typedef's name if there is one so that the |
| 6500 | // fixit doesn't break code using types like int64_t. |
| 6501 | // |
| 6502 | // FIXME: This will break if the typedef requires qualification. But |
| 6503 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6504 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6505 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6506 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6507 | else { |
| 6508 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6509 | // with a broken cast. |
| 6510 | return; |
| 6511 | } |
| 6512 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6513 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6514 | << PostInit->getSourceRange() |
| 6515 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6516 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6517 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6520 | //===----------------------------------------------------------------------===// |
| 6521 | // Initialization helper functions |
| 6522 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6523 | bool |
| 6524 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6525 | ExprResult Init) { |
| 6526 | if (Init.isInvalid()) |
| 6527 | return false; |
| 6528 | |
| 6529 | Expr *InitE = Init.get(); |
| 6530 | assert(InitE && "No initialization expression"); |
| 6531 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6532 | InitializationKind Kind |
| 6533 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6534 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6535 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6539 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6540 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6541 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6542 | bool TopLevelOfInitList, |
| 6543 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6544 | if (Init.isInvalid()) |
| 6545 | return ExprError(); |
| 6546 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6547 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6548 | assert(InitE && "No initialization expression?"); |
| 6549 | |
| 6550 | if (EqualLoc.isInvalid()) |
| 6551 | EqualLoc = InitE->getLocStart(); |
| 6552 | |
| 6553 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6554 | EqualLoc, |
| 6555 | AllowExplicit); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6556 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6557 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6558 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6559 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6560 | |
| 6561 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6562 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6563 | InitE, Result.get()); |
| 6564 | |
| 6565 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6566 | } |