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 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 85 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 86 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | // Get the length of the string as parsed. |
| 88 | uint64_t StrLength = |
| 89 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 90 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | // 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] | 94 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 95 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 96 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 97 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 98 | ConstVal, |
| 99 | ArrayType::Normal, 0); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame^] | 100 | Str->setType(DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 101 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 104 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 107 | // the size may be smaller or larger than the string we are initializing. |
| 108 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 109 | if (S.getLangOpts().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 111 | // For Pascal strings it's OK to strip off the terminating null character, |
| 112 | // so the example below is valid: |
| 113 | // |
| 114 | // unsigned char a[2] = "\pa"; |
| 115 | if (SL->isPascal()) |
| 116 | StrLength--; |
| 117 | } |
| 118 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 119 | // [dcl.init.string]p2 |
| 120 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 121 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 122 | diag::err_initializer_string_for_char_array_too_long) |
| 123 | << Str->getSourceRange(); |
| 124 | } else { |
| 125 | // C99 6.7.8p14. |
| 126 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 127 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 128 | diag::warn_initializer_string_for_char_array_too_long) |
| 129 | << Str->getSourceRange(); |
| 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 132 | // Set the type to the actual size that we are initializing. If we have |
| 133 | // something like: |
| 134 | // char x[1] = "foo"; |
| 135 | // then this will set the string literal's type to char[1]. |
| 136 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Semantic checking for initializer lists. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 143 | /// @brief Semantic checking for initializer lists. |
| 144 | /// |
| 145 | /// The InitListChecker class contains a set of routines that each |
| 146 | /// handle the initialization of a certain kind of entity, e.g., |
| 147 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 148 | /// InitListChecker itself performs a recursive walk of the subobject |
| 149 | /// structure of the type to be initialized, while stepping through |
| 150 | /// the initializer list one element at a time. The IList and Index |
| 151 | /// parameters to each of the Check* routines contain the active |
| 152 | /// (syntactic) initializer list and the index into that initializer |
| 153 | /// list that represents the current initializer. Each routine is |
| 154 | /// responsible for moving that Index forward as it consumes elements. |
| 155 | /// |
| 156 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 157 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 158 | /// initializer list and the index into that initializer list where we |
| 159 | /// are copying initializers as we map them over to the semantic |
| 160 | /// list. Once we have completed our recursive walk of the subobject |
| 161 | /// structure, we will have constructed a full semantic initializer |
| 162 | /// list. |
| 163 | /// |
| 164 | /// C99 designators cause changes in the initializer list traversal, |
| 165 | /// because they make the initialization "jump" into a specific |
| 166 | /// subobject and then continue the initialization from that |
| 167 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 168 | /// designated subobject and manages backing out the recursion to |
| 169 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 170 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 172 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 174 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 175 | bool AllowBraceElision; |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 176 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 177 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 179 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 180 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 181 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 182 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 183 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 184 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 185 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 186 | unsigned &StructuredIndex, |
| 187 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 188 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 189 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 191 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 192 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 193 | unsigned &StructuredIndex, |
| 194 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 195 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 196 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 197 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 198 | InitListExpr *StructuredList, |
| 199 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 200 | void CheckComplexType(const InitializedEntity &Entity, |
| 201 | InitListExpr *IList, QualType DeclType, |
| 202 | unsigned &Index, |
| 203 | InitListExpr *StructuredList, |
| 204 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 205 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 206 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 207 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 208 | InitListExpr *StructuredList, |
| 209 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 210 | void CheckReferenceType(const InitializedEntity &Entity, |
| 211 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 212 | unsigned &Index, |
| 213 | InitListExpr *StructuredList, |
| 214 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 215 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 216 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 217 | InitListExpr *StructuredList, |
| 218 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 219 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 220 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 223 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 224 | unsigned &StructuredIndex, |
| 225 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 226 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 227 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 229 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 230 | InitListExpr *StructuredList, |
| 231 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 232 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 233 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 234 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | RecordDecl::field_iterator *NextField, |
| 237 | llvm::APSInt *NextElementIndex, |
| 238 | unsigned &Index, |
| 239 | InitListExpr *StructuredList, |
| 240 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 241 | bool FinishSubobjectInit, |
| 242 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 244 | QualType CurrentObjectType, |
| 245 | InitListExpr *StructuredList, |
| 246 | unsigned StructuredIndex, |
| 247 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 248 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 249 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 250 | Expr *expr); |
| 251 | int numArrayElements(QualType DeclType); |
| 252 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 254 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 255 | const InitializedEntity &ParentEntity, |
| 256 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 257 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 258 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 259 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 260 | Expr *InitExpr, FieldDecl *Field, |
| 261 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 262 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 263 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 264 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 265 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 266 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 267 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 268 | bool HadError() { return hadError; } |
| 269 | |
| 270 | // @brief Retrieves the fully-structured initializer list used for |
| 271 | // semantic analysis and code generation. |
| 272 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 273 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 274 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 275 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 276 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 277 | assert(VerifyOnly && |
| 278 | "CheckValueInitializable is only inteded for verification mode."); |
| 279 | |
| 280 | SourceLocation Loc; |
| 281 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 282 | true); |
| 283 | InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0); |
| 284 | if (InitSeq.Failed()) |
| 285 | hadError = true; |
| 286 | } |
| 287 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 288 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 289 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 291 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 292 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 293 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 295 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 296 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 297 | // If there's no explicit initializer but we have a default initializer, use |
| 298 | // that. This only happens in C++1y, since classes with default |
| 299 | // initializers are not aggregates in C++11. |
| 300 | if (Field->hasInClassInitializer()) { |
| 301 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 302 | ILE->getRBraceLoc(), Field); |
| 303 | if (Init < NumInits) |
| 304 | ILE->setInit(Init, DIE); |
| 305 | else { |
| 306 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 307 | RequiresSecondPass = true; |
| 308 | } |
| 309 | return; |
| 310 | } |
| 311 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 312 | // FIXME: We probably don't need to handle references |
| 313 | // specially here, since value-initialization of references is |
| 314 | // handled in InitializationSequence. |
| 315 | if (Field->getType()->isReferenceType()) { |
| 316 | // C++ [dcl.init.aggr]p9: |
| 317 | // If an incomplete or empty initializer-list leaves a |
| 318 | // member of reference type uninitialized, the program is |
| 319 | // ill-formed. |
| 320 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 321 | << Field->getType() |
| 322 | << ILE->getSyntacticForm()->getSourceRange(); |
| 323 | SemaRef.Diag(Field->getLocation(), |
| 324 | diag::note_uninit_reference_member); |
| 325 | hadError = true; |
| 326 | return; |
| 327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 329 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 330 | true); |
| 331 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 332 | if (!InitSeq) { |
| 333 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 334 | hadError = true; |
| 335 | return; |
| 336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 337 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 338 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 339 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 340 | if (MemberInit.isInvalid()) { |
| 341 | hadError = true; |
| 342 | return; |
| 343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 345 | if (hadError) { |
| 346 | // Do nothing |
| 347 | } else if (Init < NumInits) { |
| 348 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 349 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 350 | // Value-initialization requires a constructor call, so |
| 351 | // extend the initializer list to include the constructor |
| 352 | // call and make a note that we'll need to take another pass |
| 353 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 354 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 355 | RequiresSecondPass = true; |
| 356 | } |
| 357 | } else if (InitListExpr *InnerILE |
| 358 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 359 | FillInValueInitializations(MemberEntity, InnerILE, |
| 360 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 363 | /// Recursively replaces NULL values within the given initializer list |
| 364 | /// with expressions that perform value-initialization of the |
| 365 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 366 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 367 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 368 | InitListExpr *ILE, |
| 369 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 371 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 372 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 373 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 374 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 376 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 377 | const RecordDecl *RDecl = RType->getDecl(); |
| 378 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 379 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 380 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 381 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 382 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 383 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 384 | FieldEnd = RDecl->field_end(); |
| 385 | Field != FieldEnd; ++Field) { |
| 386 | if (Field->hasInClassInitializer()) { |
| 387 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 392 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 393 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 394 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 395 | Field != FieldEnd; ++Field) { |
| 396 | if (Field->isUnnamedBitfield()) |
| 397 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 398 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 399 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 400 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 401 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 402 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 403 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 404 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 406 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 408 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 409 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 410 | break; |
| 411 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 416 | |
| 417 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 419 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 420 | unsigned NumInits = ILE->getNumInits(); |
| 421 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 422 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 423 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 424 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 425 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 426 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 427 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 428 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 429 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 430 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 431 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 432 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 434 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 437 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 438 | if (hadError) |
| 439 | return; |
| 440 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 441 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 442 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 443 | ElementEntity.setElementIndex(Init); |
| 444 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 445 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 446 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 447 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 448 | true); |
| 449 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 450 | if (!InitSeq) { |
| 451 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 452 | hadError = true; |
| 453 | return; |
| 454 | } |
| 455 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 456 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 457 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 458 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 459 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | if (hadError) { |
| 464 | // Do nothing |
| 465 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 466 | // For arrays, just set the expression used for value-initialization |
| 467 | // of the "holes" in the array. |
| 468 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 469 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 470 | else |
| 471 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 472 | } else { |
| 473 | // For arrays, just set the expression used for value-initialization |
| 474 | // of the rest of elements and exit. |
| 475 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 476 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 477 | return; |
| 478 | } |
| 479 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 480 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 481 | // Value-initialization requires a constructor call, so |
| 482 | // extend the initializer list to include the constructor |
| 483 | // call and make a note that we'll need to take another pass |
| 484 | // through the initializer list. |
| 485 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 486 | RequiresSecondPass = true; |
| 487 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 488 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 489 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 490 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 491 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 496 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 497 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 498 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 499 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 500 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 501 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 502 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 503 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 505 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 507 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 508 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 509 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 510 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 511 | bool RequiresSecondPass = false; |
| 512 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 513 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 514 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 515 | RequiresSecondPass); |
| 516 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 520 | // FIXME: use a proper constant |
| 521 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 522 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 523 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 524 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 525 | } |
| 526 | return maxElements; |
| 527 | } |
| 528 | |
| 529 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 530 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 531 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 533 | Field = structDecl->field_begin(), |
| 534 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 535 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 536 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 537 | ++InitializableMembers; |
| 538 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 539 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 540 | return std::min(InitializableMembers, 1); |
| 541 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 544 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 545 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 546 | QualType T, unsigned &Index, |
| 547 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 548 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 549 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 551 | if (T->isArrayType()) |
| 552 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 553 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 554 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 555 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 556 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 557 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 558 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 559 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 560 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 561 | if (!VerifyOnly) |
| 562 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 563 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 564 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 565 | hadError = true; |
| 566 | return; |
| 567 | } |
| 568 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 569 | // Build a structured initializer list corresponding to this subobject. |
| 570 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 572 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 573 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 574 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 575 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 577 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 578 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 579 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 580 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 582 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 583 | |
| 584 | if (VerifyOnly) { |
| 585 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 586 | hadError = true; |
| 587 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 588 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 589 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 590 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 591 | // Update the structured sub-object initializer so that it's ending |
| 592 | // range corresponds with the end of the last initializer it used. |
| 593 | if (EndIndex < ParentIList->getNumInits()) { |
| 594 | SourceLocation EndLoc |
| 595 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 596 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 597 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 599 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 600 | if (T->isArrayType() || T->isRecordType()) { |
| 601 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 602 | AllowBraceElision ? diag::warn_missing_braces : |
| 603 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 604 | << StructuredSubobjectInitList->getSourceRange() |
| 605 | << FixItHint::CreateInsertion( |
| 606 | StructuredSubobjectInitList->getLocStart(), "{") |
| 607 | << FixItHint::CreateInsertion( |
| 608 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 610 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 611 | if (!AllowBraceElision) |
| 612 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 613 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 614 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 617 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 618 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 619 | unsigned &Index, |
| 620 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 621 | unsigned &StructuredIndex, |
| 622 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 623 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 624 | if (!VerifyOnly) { |
| 625 | SyntacticToSemantic[IList] = StructuredList; |
| 626 | StructuredList->setSyntacticForm(IList); |
| 627 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 628 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 629 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 630 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 631 | QualType ExprTy = T; |
| 632 | if (!ExprTy->isArrayType()) |
| 633 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 634 | IList->setType(ExprTy); |
| 635 | StructuredList->setType(ExprTy); |
| 636 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 637 | if (hadError) |
| 638 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 639 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 640 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 641 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 642 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 643 | if (SemaRef.getLangOpts().CPlusPlus || |
| 644 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 645 | IList->getType()->isVectorType())) { |
| 646 | hadError = true; |
| 647 | } |
| 648 | return; |
| 649 | } |
| 650 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 651 | if (StructuredIndex == 1 && |
| 652 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 653 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 654 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 655 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 656 | hadError = true; |
| 657 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 658 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 659 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 660 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 661 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 662 | // Don't complain for incomplete types, since we'll get an error |
| 663 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 664 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 666 | CurrentObjectType->isArrayType()? 0 : |
| 667 | CurrentObjectType->isVectorType()? 1 : |
| 668 | CurrentObjectType->isScalarType()? 2 : |
| 669 | CurrentObjectType->isUnionType()? 3 : |
| 670 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 671 | |
| 672 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 673 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 674 | DK = diag::err_excess_initializers; |
| 675 | hadError = true; |
| 676 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 677 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 678 | DK = diag::err_excess_initializers; |
| 679 | hadError = true; |
| 680 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 682 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 683 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 686 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 687 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 688 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 689 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 690 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 691 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 692 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 695 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 696 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 698 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 699 | unsigned &Index, |
| 700 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 701 | unsigned &StructuredIndex, |
| 702 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 703 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 704 | // Explicitly braced initializer for complex type can be real+imaginary |
| 705 | // parts. |
| 706 | CheckComplexType(Entity, IList, DeclType, Index, |
| 707 | StructuredList, StructuredIndex); |
| 708 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 709 | CheckScalarType(Entity, IList, DeclType, Index, |
| 710 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 711 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 712 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 713 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 714 | } else if (DeclType->isRecordType()) { |
| 715 | assert(DeclType->isAggregateType() && |
| 716 | "non-aggregate records should be handed in CheckSubElementType"); |
| 717 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 718 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 719 | SubobjectIsDesignatorContext, Index, |
| 720 | StructuredList, StructuredIndex, |
| 721 | TopLevelObject); |
| 722 | } else if (DeclType->isArrayType()) { |
| 723 | llvm::APSInt Zero( |
| 724 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 725 | false); |
| 726 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 727 | SubobjectIsDesignatorContext, Index, |
| 728 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 729 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 730 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 732 | if (!VerifyOnly) |
| 733 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 734 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 735 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 736 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 737 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 738 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 739 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 740 | if (!VerifyOnly) |
| 741 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 742 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 743 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 744 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 745 | if (!VerifyOnly) |
| 746 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 747 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 748 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 752 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 753 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 755 | unsigned &Index, |
| 756 | InitListExpr *StructuredList, |
| 757 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 758 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 759 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 760 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 761 | unsigned newIndex = 0; |
| 762 | unsigned newStructuredIndex = 0; |
| 763 | InitListExpr *newStructuredList |
| 764 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 765 | StructuredList, StructuredIndex, |
| 766 | SubInitList->getSourceRange()); |
| 767 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 768 | newStructuredList, newStructuredIndex); |
| 769 | ++StructuredIndex; |
| 770 | ++Index; |
| 771 | return; |
| 772 | } |
| 773 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 774 | "non-aggregate records are only possible in C++"); |
| 775 | // C++ initialization is handled later. |
| 776 | } |
| 777 | |
| 778 | if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 779 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 780 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 781 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 782 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 783 | StructuredList, StructuredIndex); |
| 784 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 785 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 786 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 787 | // arrayType can be incomplete if we're initializing a flexible |
| 788 | // array member. There's nothing we can do with the completed |
| 789 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 790 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 791 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 792 | if (!VerifyOnly) { |
| 793 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 794 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 795 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 796 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 797 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 798 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 799 | |
| 800 | // Fall through for subaggregate initialization. |
| 801 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 802 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 803 | // C++ [dcl.init.aggr]p12: |
| 804 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 805 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 806 | // an initializer-list. If the initializer can initialize a |
| 807 | // member, the member is initialized. [...] |
| 808 | |
| 809 | // FIXME: Better EqualLoc? |
| 810 | InitializationKind Kind = |
| 811 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 812 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 813 | |
| 814 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 815 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 816 | ExprResult Result = |
| 817 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 818 | if (Result.isInvalid()) |
| 819 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 820 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 821 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 822 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 823 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 824 | ++Index; |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | // Fall through for subaggregate initialization |
| 829 | } else { |
| 830 | // C99 6.7.8p13: |
| 831 | // |
| 832 | // The initializer for a structure or union object that has |
| 833 | // automatic storage duration shall be either an initializer |
| 834 | // list as described below, or a single expression that has |
| 835 | // compatible structure or union type. In the latter case, the |
| 836 | // initial value of the object, including unnamed members, is |
| 837 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 838 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 839 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 840 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 841 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 842 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 843 | if (ExprRes.isInvalid()) |
| 844 | hadError = true; |
| 845 | else { |
| 846 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 847 | if (ExprRes.isInvalid()) |
| 848 | hadError = true; |
| 849 | } |
| 850 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 851 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 852 | ++Index; |
| 853 | return; |
| 854 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 855 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 856 | // Fall through for subaggregate initialization |
| 857 | } |
| 858 | |
| 859 | // C++ [dcl.init.aggr]p12: |
| 860 | // |
| 861 | // [...] Otherwise, if the member is itself a non-empty |
| 862 | // subaggregate, brace elision is assumed and the initializer is |
| 863 | // considered for the initialization of the first member of |
| 864 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 865 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 866 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 867 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 868 | StructuredIndex); |
| 869 | ++StructuredIndex; |
| 870 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 871 | if (!VerifyOnly) { |
| 872 | // We cannot initialize this element, so let |
| 873 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 874 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 875 | SemaRef.Owned(expr), |
| 876 | /*TopLevelOfInitList=*/true); |
| 877 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 878 | hadError = true; |
| 879 | ++Index; |
| 880 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 881 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 884 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 885 | InitListExpr *IList, QualType DeclType, |
| 886 | unsigned &Index, |
| 887 | InitListExpr *StructuredList, |
| 888 | unsigned &StructuredIndex) { |
| 889 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 890 | |
| 891 | // As an extension, clang supports complex initializers, which initialize |
| 892 | // a complex number component-wise. When an explicit initializer list for |
| 893 | // a complex number contains two two initializers, this extension kicks in: |
| 894 | // it exepcts the initializer list to contain two elements convertible to |
| 895 | // the element type of the complex type. The first element initializes |
| 896 | // the real part, and the second element intitializes the imaginary part. |
| 897 | |
| 898 | if (IList->getNumInits() != 2) |
| 899 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 900 | StructuredIndex); |
| 901 | |
| 902 | // This is an extension in C. (The builtin _Complex type does not exist |
| 903 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 904 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 905 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 906 | << IList->getSourceRange(); |
| 907 | |
| 908 | // Initialize the complex number. |
| 909 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 910 | InitializedEntity ElementEntity = |
| 911 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 912 | |
| 913 | for (unsigned i = 0; i < 2; ++i) { |
| 914 | ElementEntity.setElementIndex(Index); |
| 915 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 916 | StructuredList, StructuredIndex); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 921 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 922 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 923 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 924 | InitListExpr *StructuredList, |
| 925 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 926 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 927 | if (!VerifyOnly) |
| 928 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 929 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 930 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 931 | diag::err_empty_scalar_initializer) |
| 932 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 933 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 934 | ++Index; |
| 935 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 936 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 937 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 938 | |
| 939 | Expr *expr = IList->getInit(Index); |
| 940 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 941 | if (!VerifyOnly) |
| 942 | SemaRef.Diag(SubIList->getLocStart(), |
| 943 | diag::warn_many_braces_around_scalar_init) |
| 944 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 945 | |
| 946 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 947 | StructuredIndex); |
| 948 | return; |
| 949 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 950 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 951 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 952 | diag::err_designator_for_scalar_init) |
| 953 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 954 | hadError = true; |
| 955 | ++Index; |
| 956 | ++StructuredIndex; |
| 957 | return; |
| 958 | } |
| 959 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 960 | if (VerifyOnly) { |
| 961 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 962 | hadError = true; |
| 963 | ++Index; |
| 964 | return; |
| 965 | } |
| 966 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 967 | ExprResult Result = |
| 968 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 969 | SemaRef.Owned(expr), |
| 970 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 971 | |
| 972 | Expr *ResultExpr = 0; |
| 973 | |
| 974 | if (Result.isInvalid()) |
| 975 | hadError = true; // types weren't compatible. |
| 976 | else { |
| 977 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 978 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 979 | if (ResultExpr != expr) { |
| 980 | // The type was promoted, update initializer list. |
| 981 | IList->setInit(Index, ResultExpr); |
| 982 | } |
| 983 | } |
| 984 | if (hadError) |
| 985 | ++StructuredIndex; |
| 986 | else |
| 987 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 988 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 991 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 992 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 993 | unsigned &Index, |
| 994 | InitListExpr *StructuredList, |
| 995 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 996 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 997 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 998 | // general, it would be useful to pass location information down the stack, |
| 999 | // so that we know the location (or decl) of the "current object" being |
| 1000 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1001 | if (!VerifyOnly) |
| 1002 | SemaRef.Diag(IList->getLocStart(), |
| 1003 | diag::err_init_reference_member_uninitialized) |
| 1004 | << DeclType |
| 1005 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1006 | hadError = true; |
| 1007 | ++Index; |
| 1008 | ++StructuredIndex; |
| 1009 | return; |
| 1010 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1011 | |
| 1012 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1013 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1014 | if (!VerifyOnly) |
| 1015 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1016 | << DeclType << IList->getSourceRange(); |
| 1017 | hadError = true; |
| 1018 | ++Index; |
| 1019 | ++StructuredIndex; |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | if (VerifyOnly) { |
| 1024 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1025 | hadError = true; |
| 1026 | ++Index; |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | ExprResult Result = |
| 1031 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1032 | SemaRef.Owned(expr), |
| 1033 | /*TopLevelOfInitList=*/true); |
| 1034 | |
| 1035 | if (Result.isInvalid()) |
| 1036 | hadError = true; |
| 1037 | |
| 1038 | expr = Result.takeAs<Expr>(); |
| 1039 | IList->setInit(Index, expr); |
| 1040 | |
| 1041 | if (hadError) |
| 1042 | ++StructuredIndex; |
| 1043 | else |
| 1044 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1045 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1048 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1049 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1050 | unsigned &Index, |
| 1051 | InitListExpr *StructuredList, |
| 1052 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1053 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1054 | unsigned maxElements = VT->getNumElements(); |
| 1055 | unsigned numEltsInit = 0; |
| 1056 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1057 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1058 | if (Index >= IList->getNumInits()) { |
| 1059 | // Make sure the element type can be value-initialized. |
| 1060 | if (VerifyOnly) |
| 1061 | CheckValueInitializable( |
| 1062 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1066 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1067 | // If the initializing element is a vector, try to copy-initialize |
| 1068 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1069 | Expr *Init = IList->getInit(Index); |
| 1070 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1071 | if (VerifyOnly) { |
| 1072 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1073 | hadError = true; |
| 1074 | ++Index; |
| 1075 | return; |
| 1076 | } |
| 1077 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1078 | ExprResult Result = |
| 1079 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1080 | SemaRef.Owned(Init), |
| 1081 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1082 | |
| 1083 | Expr *ResultExpr = 0; |
| 1084 | if (Result.isInvalid()) |
| 1085 | hadError = true; // types weren't compatible. |
| 1086 | else { |
| 1087 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1089 | if (ResultExpr != Init) { |
| 1090 | // The type was promoted, update initializer list. |
| 1091 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1094 | if (hadError) |
| 1095 | ++StructuredIndex; |
| 1096 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1097 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1098 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1099 | ++Index; |
| 1100 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1103 | InitializedEntity ElementEntity = |
| 1104 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1106 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1107 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1108 | if (Index >= IList->getNumInits()) { |
| 1109 | if (VerifyOnly) |
| 1110 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1111 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1112 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1113 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1114 | ElementEntity.setElementIndex(Index); |
| 1115 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1116 | StructuredList, StructuredIndex); |
| 1117 | } |
| 1118 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1119 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1120 | |
| 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 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1125 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1126 | // Don't attempt to go past the end of the init list |
| 1127 | if (Index >= IList->getNumInits()) |
| 1128 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1129 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1130 | ElementEntity.setElementIndex(Index); |
| 1131 | |
| 1132 | QualType IType = IList->getInit(Index)->getType(); |
| 1133 | if (!IType->isVectorType()) { |
| 1134 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1135 | StructuredList, StructuredIndex); |
| 1136 | ++numEltsInit; |
| 1137 | } else { |
| 1138 | QualType VecType; |
| 1139 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1140 | unsigned numIElts = IVT->getNumElements(); |
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 | if (IType->isExtVectorType()) |
| 1143 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1144 | else |
| 1145 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1146 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1147 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1148 | StructuredList, StructuredIndex); |
| 1149 | numEltsInit += numIElts; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1154 | if (numEltsInit != maxElements) { |
| 1155 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1156 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1157 | diag::err_vector_incorrect_num_initializers) |
| 1158 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1159 | hadError = true; |
| 1160 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1163 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1164 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1165 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1167 | unsigned &Index, |
| 1168 | InitListExpr *StructuredList, |
| 1169 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1170 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1171 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1172 | // Check for the special-case of initializing an array with a string. |
| 1173 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1174 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1175 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1176 | // We place the string literal directly into the resulting |
| 1177 | // initializer list. This is the only place where the structure |
| 1178 | // of the structured initializer list doesn't match exactly, |
| 1179 | // because doing so would involve allocating one character |
| 1180 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1181 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1182 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1183 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1184 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1185 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1186 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1187 | return; |
| 1188 | } |
| 1189 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1190 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1191 | // Check for VLAs; in standard C it would be possible to check this |
| 1192 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1193 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1194 | if (!VerifyOnly) |
| 1195 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1196 | diag::err_variable_object_no_init) |
| 1197 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1198 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1199 | ++Index; |
| 1200 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1201 | return; |
| 1202 | } |
| 1203 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1204 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1205 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1206 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1207 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1208 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1209 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1210 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1211 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1212 | maxElementsKnown = true; |
| 1213 | } |
| 1214 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1215 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1216 | while (Index < IList->getNumInits()) { |
| 1217 | Expr *Init = IList->getInit(Index); |
| 1218 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1219 | // If we're not the subobject that matches up with the '{' for |
| 1220 | // the designator, we shouldn't be handling the |
| 1221 | // designator. Return immediately. |
| 1222 | if (!SubobjectIsDesignatorContext) |
| 1223 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1225 | // Handle this designated initializer. elementIndex will be |
| 1226 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1227 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1228 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1229 | StructuredList, StructuredIndex, true, |
| 1230 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1231 | hadError = true; |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1235 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1236 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1237 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1238 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1239 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1241 | // If the array is of incomplete type, keep track of the number of |
| 1242 | // elements in the initializer. |
| 1243 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1244 | maxElements = elementIndex; |
| 1245 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1246 | continue; |
| 1247 | } |
| 1248 | |
| 1249 | // If we know the maximum number of elements, and we've already |
| 1250 | // hit it, stop consuming elements in the initializer list. |
| 1251 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1252 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1253 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1254 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1255 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1256 | Entity); |
| 1257 | // Check this element. |
| 1258 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1259 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1260 | ++elementIndex; |
| 1261 | |
| 1262 | // If the array is of incomplete type, keep track of the number of |
| 1263 | // elements in the initializer. |
| 1264 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1265 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1266 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1267 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1268 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1269 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1270 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1272 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1273 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1274 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1275 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1276 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1277 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1279 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1280 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1281 | if (!hadError && VerifyOnly) { |
| 1282 | // Check if there are any members of the array that get value-initialized. |
| 1283 | // If so, check if doing that is possible. |
| 1284 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1285 | if (maxElementsKnown && elementIndex < maxElements) |
| 1286 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1287 | SemaRef.Context, 0, Entity)); |
| 1288 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1291 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1292 | Expr *InitExpr, |
| 1293 | FieldDecl *Field, |
| 1294 | bool TopLevelObject) { |
| 1295 | // Handle GNU flexible array initializers. |
| 1296 | unsigned FlexArrayDiag; |
| 1297 | if (isa<InitListExpr>(InitExpr) && |
| 1298 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1299 | // Empty flexible array init always allowed as an extension |
| 1300 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1301 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1302 | // Disallow flexible array init in C++; it is not required for gcc |
| 1303 | // compatibility, and it needs work to IRGen correctly in general. |
| 1304 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1305 | } else if (!TopLevelObject) { |
| 1306 | // Disallow flexible array init on non-top-level object |
| 1307 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1308 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1309 | // Disallow flexible array init on anything which is not a variable. |
| 1310 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1311 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1312 | // Disallow flexible array init on local variables. |
| 1313 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1314 | } else { |
| 1315 | // Allow other cases. |
| 1316 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1317 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1318 | |
| 1319 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1320 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1321 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1322 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1323 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1324 | << Field; |
| 1325 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1326 | |
| 1327 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1328 | } |
| 1329 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1330 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1331 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1333 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1335 | unsigned &Index, |
| 1336 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1337 | unsigned &StructuredIndex, |
| 1338 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1339 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1341 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1342 | // confusion, we forgo checking the intializer for the entire record. |
| 1343 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1344 | // Assume it was supposed to consume a single initializer. |
| 1345 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1346 | hadError = true; |
| 1347 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1351 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1352 | |
| 1353 | // If there's a default initializer, use it. |
| 1354 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1355 | if (VerifyOnly) |
| 1356 | return; |
| 1357 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1358 | Field != FieldEnd; ++Field) { |
| 1359 | if (Field->hasInClassInitializer()) { |
| 1360 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1361 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1362 | return; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1368 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1369 | Field != FieldEnd; ++Field) { |
| 1370 | if (Field->getDeclName()) { |
| 1371 | if (VerifyOnly) |
| 1372 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1373 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1374 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1375 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1376 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | return; |
| 1380 | } |
| 1381 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1382 | // If structDecl is a forward declaration, this loop won't do |
| 1383 | // anything except look at designated initializers; That's okay, |
| 1384 | // because an error should get printed out elsewhere. It might be |
| 1385 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1386 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1387 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1388 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1389 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1390 | while (Index < IList->getNumInits()) { |
| 1391 | Expr *Init = IList->getInit(Index); |
| 1392 | |
| 1393 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1394 | // If we're not the subobject that matches up with the '{' for |
| 1395 | // the designator, we shouldn't be handling the |
| 1396 | // designator. Return immediately. |
| 1397 | if (!SubobjectIsDesignatorContext) |
| 1398 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1400 | // Handle this designated initializer. Field will be updated to |
| 1401 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1402 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1403 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1404 | StructuredList, StructuredIndex, |
| 1405 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1406 | hadError = true; |
| 1407 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1408 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Disable check for missing fields when designators are used. |
| 1411 | // This matches gcc behaviour. |
| 1412 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1413 | continue; |
| 1414 | } |
| 1415 | |
| 1416 | if (Field == FieldEnd) { |
| 1417 | // We've run out of fields. We're done. |
| 1418 | break; |
| 1419 | } |
| 1420 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1421 | // We've already initialized a member of a union. We're done. |
| 1422 | if (InitializedSomething && DeclType->isUnionType()) |
| 1423 | break; |
| 1424 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1425 | // If we've hit the flexible array member at the end, we're done. |
| 1426 | if (Field->getType()->isIncompleteArrayType()) |
| 1427 | break; |
| 1428 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1429 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1430 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1431 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1432 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1433 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1435 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1436 | bool InvalidUse; |
| 1437 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1438 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1439 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1440 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1441 | IList->getInit(Index)->getLocStart()); |
| 1442 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1443 | ++Index; |
| 1444 | ++Field; |
| 1445 | hadError = true; |
| 1446 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1447 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1448 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1449 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1450 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1451 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1452 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1453 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1454 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1455 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1456 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1457 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1458 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1459 | |
| 1460 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1461 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1462 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1463 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1464 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1465 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1466 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1467 | // It is possible we have one or more unnamed bitfields remaining. |
| 1468 | // Find first (if any) named field and emit warning. |
| 1469 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1470 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1471 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1472 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1473 | diag::warn_missing_field_initializers) << it->getName(); |
| 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1479 | // Check that any remaining fields can be value-initialized. |
| 1480 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1481 | !Field->getType()->isIncompleteArrayType()) { |
| 1482 | // FIXME: Should check for holes left by designated initializers too. |
| 1483 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1484 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1485 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1486 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1491 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1494 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1495 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1496 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1497 | ++Index; |
| 1498 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1501 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1502 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1503 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1504 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1505 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1506 | StructuredList, StructuredIndex); |
| 1507 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1508 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1509 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1510 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1512 | /// \brief Expand a field designator that refers to a member of an |
| 1513 | /// anonymous struct or union into a series of field designators that |
| 1514 | /// refers to the field within the appropriate subobject. |
| 1515 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1516 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | DesignatedInitExpr *DIE, |
| 1518 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1519 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1520 | typedef DesignatedInitExpr::Designator Designator; |
| 1521 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1522 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1523 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1524 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1525 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1526 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1528 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1529 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1530 | else |
| 1531 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1532 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1533 | assert(isa<FieldDecl>(*PI)); |
| 1534 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | // Expand the current designator into the set of replacement |
| 1538 | // designators, so we have a full subobject path down to where the |
| 1539 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1540 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1541 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1544 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1545 | /// corresponds to FieldName. |
| 1546 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1547 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1548 | if (!FieldName) |
| 1549 | return 0; |
| 1550 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1551 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1552 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1553 | while (IndirectFieldDecl *IF = |
| 1554 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1555 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1556 | return IF; |
| 1557 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1558 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1559 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1562 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1563 | DesignatedInitExpr *DIE) { |
| 1564 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1565 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1566 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1567 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1568 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1569 | DIE->size(), IndexExprs, |
| 1570 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1571 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1572 | } |
| 1573 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1574 | namespace { |
| 1575 | |
| 1576 | // Callback to only accept typo corrections that are for field members of |
| 1577 | // the given struct or union. |
| 1578 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1579 | public: |
| 1580 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1581 | : Record(RD) {} |
| 1582 | |
| 1583 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1584 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1585 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1586 | } |
| 1587 | |
| 1588 | private: |
| 1589 | RecordDecl *Record; |
| 1590 | }; |
| 1591 | |
| 1592 | } |
| 1593 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1594 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1595 | /// |
| 1596 | /// Determines whether the designated initializer @p DIE, which |
| 1597 | /// resides at the given @p Index within the initializer list @p |
| 1598 | /// IList, is well-formed for a current object of type @p DeclType |
| 1599 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1601 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1602 | /// |
| 1603 | /// @param IList The initializer list in which this designated |
| 1604 | /// initializer occurs. |
| 1605 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1606 | /// @param DIE The designated initializer expression. |
| 1607 | /// |
| 1608 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1609 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1610 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1611 | /// into which the designation in @p DIE should refer. |
| 1612 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1613 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1614 | /// a field, this will be set to the field declaration corresponding |
| 1615 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1616 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1617 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1618 | /// DIE is an array designator or GNU array-range designator, this |
| 1619 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1620 | /// |
| 1621 | /// @param Index Index into @p IList where the designated initializer |
| 1622 | /// @p DIE occurs. |
| 1623 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1624 | /// @param StructuredList The initializer list expression that |
| 1625 | /// describes all of the subobject initializers in the order they'll |
| 1626 | /// actually be initialized. |
| 1627 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1628 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1630 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1631 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1632 | DesignatedInitExpr *DIE, |
| 1633 | unsigned DesigIdx, |
| 1634 | QualType &CurrentObjectType, |
| 1635 | RecordDecl::field_iterator *NextField, |
| 1636 | llvm::APSInt *NextElementIndex, |
| 1637 | unsigned &Index, |
| 1638 | InitListExpr *StructuredList, |
| 1639 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1640 | bool FinishSubobjectInit, |
| 1641 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1642 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1643 | // Check the actual initialization for the designated object type. |
| 1644 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Temporarily remove the designator expression from the |
| 1647 | // initializer list that the child calls see, so that we don't try |
| 1648 | // to re-process the designator. |
| 1649 | unsigned OldIndex = Index; |
| 1650 | IList->setInit(OldIndex, DIE->getInit()); |
| 1651 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1652 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1653 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1654 | |
| 1655 | // Restore the designated initializer expression in the syntactic |
| 1656 | // form of the initializer list. |
| 1657 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1658 | DIE->setInit(IList->getInit(OldIndex)); |
| 1659 | IList->setInit(OldIndex, DIE); |
| 1660 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1661 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1664 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1665 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1666 | if (!VerifyOnly) { |
| 1667 | assert((IsFirstDesignator || StructuredList) && |
| 1668 | "Need a non-designated initializer list to start from"); |
| 1669 | |
| 1670 | // Determine the structural initializer list that corresponds to the |
| 1671 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1672 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1673 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1674 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1675 | SourceRange(D->getLocStart(), |
| 1676 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1677 | assert(StructuredList && "Expected a structured initializer list"); |
| 1678 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1680 | if (D->isFieldDesignator()) { |
| 1681 | // C99 6.7.8p7: |
| 1682 | // |
| 1683 | // If a designator has the form |
| 1684 | // |
| 1685 | // . identifier |
| 1686 | // |
| 1687 | // then the current object (defined below) shall have |
| 1688 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1690 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1691 | if (!RT) { |
| 1692 | SourceLocation Loc = D->getDotLoc(); |
| 1693 | if (Loc.isInvalid()) |
| 1694 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1695 | if (!VerifyOnly) |
| 1696 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1697 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1698 | ++Index; |
| 1699 | return true; |
| 1700 | } |
| 1701 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1702 | // Note: we perform a linear search of the fields here, despite |
| 1703 | // the fact that we have a faster lookup method, because we always |
| 1704 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1705 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1706 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1707 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1709 | Field = RT->getDecl()->field_begin(), |
| 1710 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1711 | for (; Field != FieldEnd; ++Field) { |
| 1712 | if (Field->isUnnamedBitfield()) |
| 1713 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1714 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1715 | // If we find a field representing an anonymous field, look in the |
| 1716 | // IndirectFieldDecl that follow for the designated initializer. |
| 1717 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1718 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1719 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1720 | // In verify mode, don't modify the original. |
| 1721 | if (VerifyOnly) |
| 1722 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1723 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1724 | D = DIE->getDesignator(DesigIdx); |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1728 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1729 | break; |
| 1730 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1731 | break; |
| 1732 | |
| 1733 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1736 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1737 | if (VerifyOnly) { |
| 1738 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1739 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1740 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1742 | // There was no normal field in the struct with the designated |
| 1743 | // name. Perform another lookup for this name, which may find |
| 1744 | // something that we can't designate (e.g., a member function), |
| 1745 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1747 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1748 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1749 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1750 | // Name lookup didn't find anything. Determine whether this |
| 1751 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1752 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1753 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1754 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1755 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1756 | RT->getDecl()); |
| 1757 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1758 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1759 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1760 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1761 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1762 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1763 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1764 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1765 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1766 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1768 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1769 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1770 | } else { |
| 1771 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1772 | << FieldName << CurrentObjectType; |
| 1773 | ++Index; |
| 1774 | return true; |
| 1775 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1776 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1778 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1779 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1780 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1781 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1782 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1783 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1784 | ++Index; |
| 1785 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1786 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1787 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1788 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1789 | // The replacement field comes from typo correction; find it |
| 1790 | // in the list of fields. |
| 1791 | FieldIndex = 0; |
| 1792 | Field = RT->getDecl()->field_begin(); |
| 1793 | for (; Field != FieldEnd; ++Field) { |
| 1794 | if (Field->isUnnamedBitfield()) |
| 1795 | continue; |
| 1796 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1797 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1798 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1799 | break; |
| 1800 | |
| 1801 | ++FieldIndex; |
| 1802 | } |
| 1803 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1804 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1805 | |
| 1806 | // All of the fields of a union are located at the same place in |
| 1807 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1808 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1809 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1810 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1811 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1812 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1814 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1815 | bool InvalidUse; |
| 1816 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1817 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1818 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1819 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1820 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1821 | ++Index; |
| 1822 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1823 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1824 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1825 | if (!VerifyOnly) { |
| 1826 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1827 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1829 | // Make sure that our non-designated initializer list has space |
| 1830 | // for a subobject corresponding to this field. |
| 1831 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1832 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1833 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1834 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1835 | // This designator names a flexible array member. |
| 1836 | if (Field->getType()->isIncompleteArrayType()) { |
| 1837 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1838 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1839 | // We can't designate an object within the flexible array |
| 1840 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1841 | if (!VerifyOnly) { |
| 1842 | DesignatedInitExpr::Designator *NextD |
| 1843 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1844 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1845 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1846 | << SourceRange(NextD->getLocStart(), |
| 1847 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1848 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1849 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1850 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1851 | Invalid = true; |
| 1852 | } |
| 1853 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1854 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1855 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1856 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1857 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1858 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1859 | diag::err_flexible_array_init_needs_braces) |
| 1860 | << DIE->getInit()->getSourceRange(); |
| 1861 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1862 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1863 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1864 | Invalid = true; |
| 1865 | } |
| 1866 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1867 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1868 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1869 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1870 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1871 | |
| 1872 | if (Invalid) { |
| 1873 | ++Index; |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | // Initialize the array. |
| 1878 | bool prevHadError = hadError; |
| 1879 | unsigned newStructuredIndex = FieldIndex; |
| 1880 | unsigned OldIndex = Index; |
| 1881 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1882 | |
| 1883 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1884 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1885 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1886 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1888 | IList->setInit(OldIndex, DIE); |
| 1889 | if (hadError && !prevHadError) { |
| 1890 | ++Field; |
| 1891 | ++FieldIndex; |
| 1892 | if (NextField) |
| 1893 | *NextField = Field; |
| 1894 | StructuredIndex = FieldIndex; |
| 1895 | return true; |
| 1896 | } |
| 1897 | } else { |
| 1898 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1899 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1900 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1901 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1902 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1903 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1904 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1905 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1906 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1907 | true, false)) |
| 1908 | return true; |
| 1909 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Find the position of the next field to be initialized in this |
| 1912 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1913 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1914 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1915 | |
| 1916 | // If this the first designator, our caller will continue checking |
| 1917 | // the rest of this struct/class/union subobject. |
| 1918 | if (IsFirstDesignator) { |
| 1919 | if (NextField) |
| 1920 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1921 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1922 | return false; |
| 1923 | } |
| 1924 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1925 | if (!FinishSubobjectInit) |
| 1926 | return false; |
| 1927 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1928 | // We've already initialized something in the union; we're done. |
| 1929 | if (RT->getDecl()->isUnion()) |
| 1930 | return hadError; |
| 1931 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1932 | // Check the remaining fields within this class/struct/union subobject. |
| 1933 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1934 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1935 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1936 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1937 | return hadError && !prevHadError; |
| 1938 | } |
| 1939 | |
| 1940 | // C99 6.7.8p6: |
| 1941 | // |
| 1942 | // If a designator has the form |
| 1943 | // |
| 1944 | // [ constant-expression ] |
| 1945 | // |
| 1946 | // then the current object (defined below) shall have array |
| 1947 | // type and the expression shall be an integer constant |
| 1948 | // expression. If the array is of unknown size, any |
| 1949 | // nonnegative value is valid. |
| 1950 | // |
| 1951 | // Additionally, cope with the GNU extension that permits |
| 1952 | // designators of the form |
| 1953 | // |
| 1954 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1955 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1956 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1957 | if (!VerifyOnly) |
| 1958 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1959 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1960 | ++Index; |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
| 1964 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1965 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1966 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1967 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1968 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1969 | DesignatedEndIndex = DesignatedStartIndex; |
| 1970 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1971 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1972 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1974 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1976 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1977 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1979 | // Codegen can't handle evaluating array range designators that have side |
| 1980 | // effects, because we replicate the AST value for each initialized element. |
| 1981 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1982 | // elements with something that has a side effect, so codegen can emit an |
| 1983 | // "error unsupported" error instead of miscompiling the app. |
| 1984 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1985 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1986 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | if (isa<ConstantArrayType>(AT)) { |
| 1990 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1991 | DesignatedStartIndex |
| 1992 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1993 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1994 | DesignatedEndIndex |
| 1995 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1996 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1997 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1998 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1999 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2000 | diag::err_array_designator_too_large) |
| 2001 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2002 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2003 | ++Index; |
| 2004 | return true; |
| 2005 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2006 | } else { |
| 2007 | // Make sure the bit-widths and signedness match. |
| 2008 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2009 | DesignatedEndIndex |
| 2010 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2011 | else if (DesignatedStartIndex.getBitWidth() < |
| 2012 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2013 | DesignatedStartIndex |
| 2014 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2015 | DesignatedStartIndex.setIsUnsigned(true); |
| 2016 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2017 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2019 | // Make sure that our non-designated initializer list has space |
| 2020 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2021 | if (!VerifyOnly && |
| 2022 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2024 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2026 | // Repeatedly perform subobject initializations in the range |
| 2027 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2028 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2029 | // Move to the next designator |
| 2030 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2031 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2032 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2033 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2034 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2035 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2036 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2037 | // Recurse to check later designated subobjects. |
| 2038 | QualType ElementType = AT->getElementType(); |
| 2039 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2040 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2041 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2043 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2044 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2045 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2046 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2047 | return true; |
| 2048 | |
| 2049 | // Move to the next index in the array that we'll be initializing. |
| 2050 | ++DesignatedStartIndex; |
| 2051 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2052 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2053 | |
| 2054 | // If this the first designator, our caller will continue checking |
| 2055 | // the rest of this array subobject. |
| 2056 | if (IsFirstDesignator) { |
| 2057 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2058 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2059 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2060 | return false; |
| 2061 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2063 | if (!FinishSubobjectInit) |
| 2064 | return false; |
| 2065 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2066 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2067 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2069 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2070 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2074 | // Get the structured initializer list for a subobject of type |
| 2075 | // @p CurrentObjectType. |
| 2076 | InitListExpr * |
| 2077 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2078 | QualType CurrentObjectType, |
| 2079 | InitListExpr *StructuredList, |
| 2080 | unsigned StructuredIndex, |
| 2081 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2082 | if (VerifyOnly) |
| 2083 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2084 | Expr *ExistingInit = 0; |
| 2085 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2086 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2087 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2088 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2090 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2091 | return Result; |
| 2092 | |
| 2093 | if (ExistingInit) { |
| 2094 | // We are creating an initializer list that initializes the |
| 2095 | // subobjects of the current object, but there was already an |
| 2096 | // initialization that completely initialized the current |
| 2097 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2099 | // struct X { int a, b; }; |
| 2100 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2102 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2103 | // designated initializer re-initializes the whole |
| 2104 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2106 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2107 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2108 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2109 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2110 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2111 | << ExistingInit->getSourceRange(); |
| 2112 | } |
| 2113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2115 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2116 | InitRange.getBegin(), MultiExprArg(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2117 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2118 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2119 | QualType ResultType = CurrentObjectType; |
| 2120 | if (!ResultType->isArrayType()) |
| 2121 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2122 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2124 | // Pre-allocate storage for the structured initializer list. |
| 2125 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2126 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2127 | bool GotNumInits = false; |
| 2128 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2129 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2130 | GotNumInits = true; |
| 2131 | } else if (Index < IList->getNumInits()) { |
| 2132 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2133 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2134 | GotNumInits = true; |
| 2135 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2139 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2140 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2141 | NumElements = CAType->getSize().getZExtValue(); |
| 2142 | // Simple heuristic so that we don't allocate a very large |
| 2143 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2144 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2145 | NumElements = 0; |
| 2146 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2147 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2148 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2149 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2150 | RecordDecl *RDecl = RType->getDecl(); |
| 2151 | if (RDecl->isUnion()) |
| 2152 | NumElements = 1; |
| 2153 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2155 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2158 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2160 | // Link this new initializer list into the structured initializer |
| 2161 | // lists. |
| 2162 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2163 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2164 | else { |
| 2165 | Result->setSyntacticForm(IList); |
| 2166 | SyntacticToSemantic[IList] = Result; |
| 2167 | } |
| 2168 | |
| 2169 | return Result; |
| 2170 | } |
| 2171 | |
| 2172 | /// Update the initializer at index @p StructuredIndex within the |
| 2173 | /// structured initializer list to the value @p expr. |
| 2174 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2175 | unsigned &StructuredIndex, |
| 2176 | Expr *expr) { |
| 2177 | // No structured initializer list to update |
| 2178 | if (!StructuredList) |
| 2179 | return; |
| 2180 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2181 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2182 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2183 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2184 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2185 | diag::warn_initializer_overrides) |
| 2186 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2187 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2188 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2189 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2190 | << PrevInit->getSourceRange(); |
| 2191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2193 | ++StructuredIndex; |
| 2194 | } |
| 2195 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2196 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2197 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2198 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2199 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2200 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2201 | /// added, on success. If everything went okay, Value will receive the |
| 2202 | /// value of the constant expression. |
| 2203 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2204 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2205 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2208 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2209 | if (Result.isInvalid()) |
| 2210 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2211 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2212 | if (Value.isSigned() && Value.isNegative()) |
| 2213 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2214 | << Value.toString(10) << Index->getSourceRange(); |
| 2215 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2216 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2217 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2220 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2221 | SourceLocation Loc, |
| 2222 | bool GNUSyntax, |
| 2223 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2224 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2225 | |
| 2226 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2227 | SmallVector<ASTDesignator, 32> Designators; |
| 2228 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2229 | |
| 2230 | // Build designators and check array designator expressions. |
| 2231 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2232 | const Designator &D = Desig.getDesignator(Idx); |
| 2233 | switch (D.getKind()) { |
| 2234 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2236 | D.getFieldLoc())); |
| 2237 | break; |
| 2238 | |
| 2239 | case Designator::ArrayDesignator: { |
| 2240 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2241 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2242 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2243 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2244 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2245 | Invalid = true; |
| 2246 | else { |
| 2247 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2249 | D.getRBracketLoc())); |
| 2250 | InitExpressions.push_back(Index); |
| 2251 | } |
| 2252 | break; |
| 2253 | } |
| 2254 | |
| 2255 | case Designator::ArrayRangeDesignator: { |
| 2256 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2257 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2258 | llvm::APSInt StartValue; |
| 2259 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2260 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2261 | StartIndex->isValueDependent(); |
| 2262 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2263 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2264 | if (!StartDependent) |
| 2265 | StartIndex = |
| 2266 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2267 | if (!EndDependent) |
| 2268 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2269 | |
| 2270 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2271 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2272 | else { |
| 2273 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2274 | if (StartDependent || EndDependent) { |
| 2275 | // Nothing to compute. |
| 2276 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2277 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2278 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2279 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2281 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2282 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2284 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2285 | Invalid = true; |
| 2286 | } else { |
| 2287 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2289 | D.getEllipsisLoc(), |
| 2290 | D.getRBracketLoc())); |
| 2291 | InitExpressions.push_back(StartIndex); |
| 2292 | InitExpressions.push_back(EndIndex); |
| 2293 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2294 | } |
| 2295 | break; |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | if (Invalid || Init.isInvalid()) |
| 2301 | return ExprError(); |
| 2302 | |
| 2303 | // Clear out the expressions within the designation. |
| 2304 | Desig.ClearExprs(*this); |
| 2305 | |
| 2306 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2307 | = DesignatedInitExpr::Create(Context, |
| 2308 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2309 | InitExpressions, Loc, GNUSyntax, |
| 2310 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2311 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2312 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2313 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2314 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2316 | return Owned(DIE); |
| 2317 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2319 | //===----------------------------------------------------------------------===// |
| 2320 | // Initialization entity |
| 2321 | //===----------------------------------------------------------------------===// |
| 2322 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2323 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2324 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2325 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2326 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2327 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2328 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2329 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2330 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2331 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2332 | Type = VT->getElementType(); |
| 2333 | } else { |
| 2334 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2335 | assert(CT && "Unexpected type"); |
| 2336 | Kind = EK_ComplexElement; |
| 2337 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2338 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2342 | CXXBaseSpecifier *Base, |
| 2343 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2344 | { |
| 2345 | InitializedEntity Result; |
| 2346 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2347 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2348 | if (IsInheritedVirtualBase) |
| 2349 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2351 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2352 | return Result; |
| 2353 | } |
| 2354 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2355 | DeclarationName InitializedEntity::getName() const { |
| 2356 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2357 | case EK_Parameter: { |
| 2358 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2359 | return (D ? D->getDeclName() : DeclarationName()); |
| 2360 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2361 | |
| 2362 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2363 | case EK_Member: |
| 2364 | return VariableOrMember->getDeclName(); |
| 2365 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2366 | case EK_LambdaCapture: |
| 2367 | return Capture.Var->getDeclName(); |
| 2368 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2369 | case EK_Result: |
| 2370 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2371 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2372 | case EK_Temporary: |
| 2373 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2374 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2375 | case EK_ArrayElement: |
| 2376 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2377 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2378 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2379 | return DeclarationName(); |
| 2380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2382 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2385 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2386 | switch (getKind()) { |
| 2387 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2388 | case EK_Member: |
| 2389 | return VariableOrMember; |
| 2390 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2391 | case EK_Parameter: |
| 2392 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2393 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2394 | case EK_Result: |
| 2395 | case EK_Exception: |
| 2396 | case EK_New: |
| 2397 | case EK_Temporary: |
| 2398 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2399 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2400 | case EK_ArrayElement: |
| 2401 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2402 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2403 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2404 | case EK_LambdaCapture: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2405 | return 0; |
| 2406 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2407 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2408 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2411 | bool InitializedEntity::allowsNRVO() const { |
| 2412 | switch (getKind()) { |
| 2413 | case EK_Result: |
| 2414 | case EK_Exception: |
| 2415 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2417 | case EK_Variable: |
| 2418 | case EK_Parameter: |
| 2419 | case EK_Member: |
| 2420 | case EK_New: |
| 2421 | case EK_Temporary: |
| 2422 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2423 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2424 | case EK_ArrayElement: |
| 2425 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2426 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2427 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2428 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2429 | break; |
| 2430 | } |
| 2431 | |
| 2432 | return false; |
| 2433 | } |
| 2434 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2435 | //===----------------------------------------------------------------------===// |
| 2436 | // Initialization sequence |
| 2437 | //===----------------------------------------------------------------------===// |
| 2438 | |
| 2439 | void InitializationSequence::Step::Destroy() { |
| 2440 | switch (Kind) { |
| 2441 | case SK_ResolveAddressOfOverloadedFunction: |
| 2442 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2443 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2444 | case SK_CastDerivedToBaseLValue: |
| 2445 | case SK_BindReference: |
| 2446 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2447 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2448 | case SK_UserConversion: |
| 2449 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2450 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2451 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2452 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2453 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2454 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2455 | case SK_UnwrapInitList: |
| 2456 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2457 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2458 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2459 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2460 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2461 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2462 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2463 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2464 | case SK_PassByIndirectCopyRestore: |
| 2465 | case SK_PassByIndirectRestore: |
| 2466 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2467 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2468 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2469 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2470 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2472 | case SK_ConversionSequence: |
| 2473 | delete ICS; |
| 2474 | } |
| 2475 | } |
| 2476 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2477 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2478 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2482 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2483 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2485 | switch (getFailureKind()) { |
| 2486 | case FK_TooManyInitsForReference: |
| 2487 | case FK_ArrayNeedsInitList: |
| 2488 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2489 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2490 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2491 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2492 | case FK_RValueReferenceBindingToLValue: |
| 2493 | case FK_ReferenceInitDropsQualifiers: |
| 2494 | case FK_ReferenceInitFailed: |
| 2495 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2496 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2497 | case FK_TooManyInitsForScalar: |
| 2498 | case FK_ReferenceBindingToInitList: |
| 2499 | case FK_InitListBadDestinationType: |
| 2500 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2501 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2502 | case FK_ArrayTypeMismatch: |
| 2503 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2504 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2505 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2506 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2507 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2508 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2509 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2511 | case FK_ReferenceInitOverloadFailed: |
| 2512 | case FK_UserConversionOverloadFailed: |
| 2513 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2514 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2515 | return FailedOverloadResult == OR_Ambiguous; |
| 2516 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2518 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2521 | bool InitializationSequence::isConstructorInitialization() const { |
| 2522 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2523 | } |
| 2524 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2525 | void |
| 2526 | InitializationSequence |
| 2527 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2528 | DeclAccessPair Found, |
| 2529 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2530 | Step S; |
| 2531 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2532 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2533 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2534 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2535 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2536 | Steps.push_back(S); |
| 2537 | } |
| 2538 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2539 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2540 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2541 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2542 | switch (VK) { |
| 2543 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2544 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2545 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2546 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2547 | S.Type = BaseType; |
| 2548 | Steps.push_back(S); |
| 2549 | } |
| 2550 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2551 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2552 | bool BindingTemporary) { |
| 2553 | Step S; |
| 2554 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2555 | S.Type = T; |
| 2556 | Steps.push_back(S); |
| 2557 | } |
| 2558 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2559 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2560 | Step S; |
| 2561 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2562 | S.Type = T; |
| 2563 | Steps.push_back(S); |
| 2564 | } |
| 2565 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2566 | void |
| 2567 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2568 | DeclAccessPair FoundDecl, |
| 2569 | QualType T, |
| 2570 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2571 | Step S; |
| 2572 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2573 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2574 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2575 | S.Function.Function = Function; |
| 2576 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2577 | Steps.push_back(S); |
| 2578 | } |
| 2579 | |
| 2580 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2581 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2582 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2583 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2584 | switch (VK) { |
| 2585 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2586 | S.Kind = SK_QualificationConversionRValue; |
| 2587 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2588 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2589 | S.Kind = SK_QualificationConversionXValue; |
| 2590 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2591 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2592 | S.Kind = SK_QualificationConversionLValue; |
| 2593 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2594 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2595 | S.Type = Ty; |
| 2596 | Steps.push_back(S); |
| 2597 | } |
| 2598 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2599 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2600 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2601 | |
| 2602 | Step S; |
| 2603 | S.Kind = SK_LValueToRValue; |
| 2604 | S.Type = Ty; |
| 2605 | Steps.push_back(S); |
| 2606 | } |
| 2607 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | void InitializationSequence::AddConversionSequenceStep( |
| 2609 | const ImplicitConversionSequence &ICS, |
| 2610 | QualType T) { |
| 2611 | Step S; |
| 2612 | S.Kind = SK_ConversionSequence; |
| 2613 | S.Type = T; |
| 2614 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2615 | Steps.push_back(S); |
| 2616 | } |
| 2617 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2618 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2619 | Step S; |
| 2620 | S.Kind = SK_ListInitialization; |
| 2621 | S.Type = T; |
| 2622 | Steps.push_back(S); |
| 2623 | } |
| 2624 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2625 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2626 | InitializationSequence |
| 2627 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2628 | AccessSpecifier Access, |
| 2629 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2630 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2631 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2632 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2633 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2634 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2635 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2636 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2637 | S.Function.Function = Constructor; |
| 2638 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2639 | Steps.push_back(S); |
| 2640 | } |
| 2641 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2642 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2643 | Step S; |
| 2644 | S.Kind = SK_ZeroInitialization; |
| 2645 | S.Type = T; |
| 2646 | Steps.push_back(S); |
| 2647 | } |
| 2648 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2649 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2650 | Step S; |
| 2651 | S.Kind = SK_CAssignment; |
| 2652 | S.Type = T; |
| 2653 | Steps.push_back(S); |
| 2654 | } |
| 2655 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2656 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2657 | Step S; |
| 2658 | S.Kind = SK_StringInit; |
| 2659 | S.Type = T; |
| 2660 | Steps.push_back(S); |
| 2661 | } |
| 2662 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2663 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2664 | Step S; |
| 2665 | S.Kind = SK_ObjCObjectConversion; |
| 2666 | S.Type = T; |
| 2667 | Steps.push_back(S); |
| 2668 | } |
| 2669 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2670 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2671 | Step S; |
| 2672 | S.Kind = SK_ArrayInit; |
| 2673 | S.Type = T; |
| 2674 | Steps.push_back(S); |
| 2675 | } |
| 2676 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2677 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2678 | Step S; |
| 2679 | S.Kind = SK_ParenthesizedArrayInit; |
| 2680 | S.Type = T; |
| 2681 | Steps.push_back(S); |
| 2682 | } |
| 2683 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2684 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2685 | bool shouldCopy) { |
| 2686 | Step s; |
| 2687 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2688 | : SK_PassByIndirectRestore); |
| 2689 | s.Type = type; |
| 2690 | Steps.push_back(s); |
| 2691 | } |
| 2692 | |
| 2693 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2694 | Step S; |
| 2695 | S.Kind = SK_ProduceObjCObject; |
| 2696 | S.Type = T; |
| 2697 | Steps.push_back(S); |
| 2698 | } |
| 2699 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2700 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2701 | Step S; |
| 2702 | S.Kind = SK_StdInitializerList; |
| 2703 | S.Type = T; |
| 2704 | Steps.push_back(S); |
| 2705 | } |
| 2706 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2707 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2708 | Step S; |
| 2709 | S.Kind = SK_OCLSamplerInit; |
| 2710 | S.Type = T; |
| 2711 | Steps.push_back(S); |
| 2712 | } |
| 2713 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2714 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2715 | Step S; |
| 2716 | S.Kind = SK_OCLZeroEvent; |
| 2717 | S.Type = T; |
| 2718 | Steps.push_back(S); |
| 2719 | } |
| 2720 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2721 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2722 | InitListExpr *Syntactic) { |
| 2723 | assert(Syntactic->getNumInits() == 1 && |
| 2724 | "Can only rewrap trivial init lists."); |
| 2725 | Step S; |
| 2726 | S.Kind = SK_UnwrapInitList; |
| 2727 | S.Type = Syntactic->getInit(0)->getType(); |
| 2728 | Steps.insert(Steps.begin(), S); |
| 2729 | |
| 2730 | S.Kind = SK_RewrapInitList; |
| 2731 | S.Type = T; |
| 2732 | S.WrappingSyntacticList = Syntactic; |
| 2733 | Steps.push_back(S); |
| 2734 | } |
| 2735 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2736 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2737 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2738 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2739 | this->Failure = Failure; |
| 2740 | this->FailedOverloadResult = Result; |
| 2741 | } |
| 2742 | |
| 2743 | //===----------------------------------------------------------------------===// |
| 2744 | // Attempt initialization |
| 2745 | //===----------------------------------------------------------------------===// |
| 2746 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2747 | static void MaybeProduceObjCObject(Sema &S, |
| 2748 | InitializationSequence &Sequence, |
| 2749 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2750 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2751 | |
| 2752 | /// When initializing a parameter, produce the value if it's marked |
| 2753 | /// __attribute__((ns_consumed)). |
| 2754 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2755 | if (!Entity.isParameterConsumed()) |
| 2756 | return; |
| 2757 | |
| 2758 | assert(Entity.getType()->isObjCRetainableType() && |
| 2759 | "consuming an object of unretainable type?"); |
| 2760 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2761 | |
| 2762 | /// When initializing a return value, if the return type is a |
| 2763 | /// retainable type, then returns need to immediately retain the |
| 2764 | /// object. If an autorelease is required, it will be done at the |
| 2765 | /// last instant. |
| 2766 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2767 | if (!Entity.getType()->isObjCRetainableType()) |
| 2768 | return; |
| 2769 | |
| 2770 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2771 | } |
| 2772 | } |
| 2773 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2774 | /// \brief When initializing from init list via constructor, handle |
| 2775 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2776 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2777 | /// \return true if we have handled initialization of an object of type |
| 2778 | /// std::initializer_list<T>, false otherwise. |
| 2779 | static bool TryInitializerListConstruction(Sema &S, |
| 2780 | InitListExpr *List, |
| 2781 | QualType DestType, |
| 2782 | InitializationSequence &Sequence) { |
| 2783 | QualType E; |
| 2784 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2785 | return false; |
| 2786 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2787 | // Check that each individual element can be copy-constructed. But since we |
| 2788 | // have no place to store further information, we'll recalculate everything |
| 2789 | // later. |
| 2790 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2791 | S.Context.getConstantArrayType(E, |
| 2792 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2793 | List->getNumInits()), |
| 2794 | ArrayType::Normal, 0)); |
| 2795 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2796 | 0, HiddenArray); |
| 2797 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2798 | Element.setElementIndex(i); |
| 2799 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2800 | Sequence.SetFailed( |
| 2801 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2802 | return true; |
| 2803 | } |
| 2804 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2805 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2806 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2809 | static OverloadingResult |
| 2810 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
| 2811 | Expr **Args, unsigned NumArgs, |
| 2812 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2813 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2814 | OverloadCandidateSet::iterator &Best, |
| 2815 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2816 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2817 | CandidateSet.clear(); |
| 2818 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2819 | for (ArrayRef<NamedDecl *>::iterator |
| 2820 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2821 | NamedDecl *D = *Con; |
| 2822 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2823 | bool SuppressUserConversions = false; |
| 2824 | |
| 2825 | // Find the constructor (which may be a template). |
| 2826 | CXXConstructorDecl *Constructor = 0; |
| 2827 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2828 | if (ConstructorTmpl) |
| 2829 | Constructor = cast<CXXConstructorDecl>( |
| 2830 | ConstructorTmpl->getTemplatedDecl()); |
| 2831 | else { |
| 2832 | Constructor = cast<CXXConstructorDecl>(D); |
| 2833 | |
| 2834 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2835 | // suppress user-defined conversions on the arguments. We do the same for |
| 2836 | // move constructors. |
| 2837 | if ((CopyInitializing || (InitListSyntax && NumArgs == 1)) && |
| 2838 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2839 | SuppressUserConversions = true; |
| 2840 | } |
| 2841 | |
| 2842 | if (!Constructor->isInvalidDecl() && |
| 2843 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2844 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2845 | if (ConstructorTmpl) |
| 2846 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2847 | /*ExplicitArgs*/ 0, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2848 | llvm::makeArrayRef(Args, NumArgs), |
| 2849 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2850 | else { |
| 2851 | // C++ [over.match.copy]p1: |
| 2852 | // - When initializing a temporary to be bound to the first parameter |
| 2853 | // of a constructor that takes a reference to possibly cv-qualified |
| 2854 | // T as its first argument, called with a single argument in the |
| 2855 | // context of direct-initialization, explicit conversion functions |
| 2856 | // are also considered. |
| 2857 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 2858 | NumArgs == 1 && |
| 2859 | Constructor->isCopyOrMoveConstructor(); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2860 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2861 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2862 | SuppressUserConversions, |
| 2863 | /*PartialOverloading=*/false, |
| 2864 | /*AllowExplicit=*/AllowExplicitConv); |
| 2865 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | // Perform overload resolution and return the result. |
| 2870 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2871 | } |
| 2872 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2873 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2874 | /// enumerates the constructors of the initialized entity and performs overload |
| 2875 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2876 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2877 | /// class type. |
| 2878 | static void TryConstructorInitialization(Sema &S, |
| 2879 | const InitializedEntity &Entity, |
| 2880 | const InitializationKind &Kind, |
| 2881 | Expr **Args, unsigned NumArgs, |
| 2882 | QualType DestType, |
| 2883 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2884 | bool InitListSyntax = false) { |
| 2885 | assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) && |
| 2886 | "InitListSyntax must come with a single initializer list argument."); |
| 2887 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2888 | // The type we're constructing needs to be complete. |
| 2889 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2890 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2891 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2895 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2896 | CXXRecordDecl *DestRecordDecl |
| 2897 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2898 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2899 | // Build the candidate set directly in the initialization sequence |
| 2900 | // structure, so that it will persist if we fail. |
| 2901 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2902 | |
| 2903 | // Determine whether we are allowed to call explicit constructors or |
| 2904 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2905 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2906 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2907 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2908 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2909 | // applicable constructors are enumerated, and the best one is chosen |
| 2910 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2911 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2912 | // The container holding the constructors can under certain conditions |
| 2913 | // be changed while iterating (e.g. because of deserialization). |
| 2914 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2915 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2916 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2917 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2918 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2919 | bool AsInitializerList = false; |
| 2920 | |
| 2921 | // C++11 [over.match.list]p1: |
| 2922 | // When objects of non-aggregate type T are list-initialized, overload |
| 2923 | // resolution selects the constructor in two phases: |
| 2924 | // - Initially, the candidate functions are the initializer-list |
| 2925 | // constructors of the class T and the argument list consists of the |
| 2926 | // initializer list as a single argument. |
| 2927 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2928 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2929 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2930 | |
| 2931 | // If the initializer list has no elements and T has a default constructor, |
| 2932 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2933 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2934 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2935 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2936 | CopyInitialization, AllowExplicit, |
| 2937 | /*OnlyListConstructor=*/true, |
| 2938 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2939 | |
| 2940 | // Time to unwrap the init list. |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2941 | Args = ILE->getInits(); |
| 2942 | NumArgs = ILE->getNumInits(); |
| 2943 | } |
| 2944 | |
| 2945 | // C++11 [over.match.list]p1: |
| 2946 | // - If no viable initializer-list constructor is found, overload resolution |
| 2947 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2948 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2949 | // elements of the initializer list. |
| 2950 | if (Result == OR_No_Viable_Function) { |
| 2951 | AsInitializerList = false; |
| 2952 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2953 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2954 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2955 | /*OnlyListConstructors=*/false, |
| 2956 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2957 | } |
| 2958 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2959 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2960 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2961 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2962 | Result); |
| 2963 | return; |
| 2964 | } |
| 2965 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2966 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2967 | // If a program calls for the default initialization of an object |
| 2968 | // of a const-qualified type T, T shall be a class type with a |
| 2969 | // user-provided default constructor. |
| 2970 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2971 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 2972 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2973 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2977 | // C++11 [over.match.list]p1: |
| 2978 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 2979 | // initializer is ill-formed. |
| 2980 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2981 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 2982 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 2983 | return; |
| 2984 | } |
| 2985 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2986 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2987 | // subsumed by the initialization. |
| 2988 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2989 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 2990 | Best->FoundDecl.getAccess(), |
| 2991 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2992 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2995 | static bool |
| 2996 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 2997 | Expr *Initializer, |
| 2998 | QualType &SourceType, |
| 2999 | QualType &UnqualifiedSourceType, |
| 3000 | QualType UnqualifiedTargetType, |
| 3001 | InitializationSequence &Sequence) { |
| 3002 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3003 | S.Context.OverloadTy) { |
| 3004 | DeclAccessPair Found; |
| 3005 | bool HadMultipleCandidates = false; |
| 3006 | if (FunctionDecl *Fn |
| 3007 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3008 | UnqualifiedTargetType, |
| 3009 | false, Found, |
| 3010 | &HadMultipleCandidates)) { |
| 3011 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3012 | HadMultipleCandidates); |
| 3013 | SourceType = Fn->getType(); |
| 3014 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3015 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3016 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3017 | return true; |
| 3018 | } |
| 3019 | } |
| 3020 | return false; |
| 3021 | } |
| 3022 | |
| 3023 | static void TryReferenceInitializationCore(Sema &S, |
| 3024 | const InitializedEntity &Entity, |
| 3025 | const InitializationKind &Kind, |
| 3026 | Expr *Initializer, |
| 3027 | QualType cv1T1, QualType T1, |
| 3028 | Qualifiers T1Quals, |
| 3029 | QualType cv2T2, QualType T2, |
| 3030 | Qualifiers T2Quals, |
| 3031 | InitializationSequence &Sequence); |
| 3032 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3033 | static void TryValueInitialization(Sema &S, |
| 3034 | const InitializedEntity &Entity, |
| 3035 | const InitializationKind &Kind, |
| 3036 | InitializationSequence &Sequence, |
| 3037 | InitListExpr *InitList = 0); |
| 3038 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3039 | static void TryListInitialization(Sema &S, |
| 3040 | const InitializedEntity &Entity, |
| 3041 | const InitializationKind &Kind, |
| 3042 | InitListExpr *InitList, |
| 3043 | InitializationSequence &Sequence); |
| 3044 | |
| 3045 | /// \brief Attempt list initialization of a reference. |
| 3046 | static void TryReferenceListInitialization(Sema &S, |
| 3047 | const InitializedEntity &Entity, |
| 3048 | const InitializationKind &Kind, |
| 3049 | InitListExpr *InitList, |
| 3050 | InitializationSequence &Sequence) |
| 3051 | { |
| 3052 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3053 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3054 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | QualType DestType = Entity.getType(); |
| 3059 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3060 | Qualifiers T1Quals; |
| 3061 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3062 | |
| 3063 | // Reference initialization via an initializer list works thus: |
| 3064 | // If the initializer list consists of a single element that is |
| 3065 | // reference-related to the referenced type, bind directly to that element |
| 3066 | // (possibly creating temporaries). |
| 3067 | // Otherwise, initialize a temporary with the initializer list and |
| 3068 | // bind to that. |
| 3069 | if (InitList->getNumInits() == 1) { |
| 3070 | Expr *Initializer = InitList->getInit(0); |
| 3071 | QualType cv2T2 = Initializer->getType(); |
| 3072 | Qualifiers T2Quals; |
| 3073 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3074 | |
| 3075 | // If this fails, creating a temporary wouldn't work either. |
| 3076 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3077 | T1, Sequence)) |
| 3078 | return; |
| 3079 | |
| 3080 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3081 | bool dummy1, dummy2, dummy3; |
| 3082 | Sema::ReferenceCompareResult RefRelationship |
| 3083 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3084 | dummy2, dummy3); |
| 3085 | if (RefRelationship >= Sema::Ref_Related) { |
| 3086 | // Try to bind the reference here. |
| 3087 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3088 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3089 | if (Sequence) |
| 3090 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3091 | return; |
| 3092 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Update the initializer if we've resolved an overloaded function. |
| 3095 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3096 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | // Not reference-related. Create a temporary and bind to that. |
| 3100 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3101 | |
| 3102 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3103 | if (Sequence) { |
| 3104 | if (DestType->isRValueReferenceType() || |
| 3105 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3106 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3107 | else |
| 3108 | Sequence.SetFailed( |
| 3109 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3110 | } |
| 3111 | } |
| 3112 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3113 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3114 | static void TryListInitialization(Sema &S, |
| 3115 | const InitializedEntity &Entity, |
| 3116 | const InitializationKind &Kind, |
| 3117 | InitListExpr *InitList, |
| 3118 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3119 | QualType DestType = Entity.getType(); |
| 3120 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3121 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3122 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3123 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3124 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3125 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3126 | return; |
| 3127 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3128 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3129 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3130 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3131 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3132 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3133 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3134 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3135 | return; |
| 3136 | } |
| 3137 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3138 | // C++11 [dcl.init.list]p3: |
| 3139 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3140 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3141 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3142 | // - Otherwise, if the initializer list has no elements and T is a |
| 3143 | // class type with a default constructor, the object is |
| 3144 | // value-initialized. |
| 3145 | if (InitList->getNumInits() == 0) { |
| 3146 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3147 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3148 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3149 | return; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3154 | // an initializer_list object constructed [...] |
| 3155 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3156 | return; |
| 3157 | |
| 3158 | // - Otherwise, if T is a class type, constructors are considered. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3159 | Expr *Arg = InitList; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3160 | TryConstructorInitialization(S, Entity, Kind, &Arg, 1, DestType, |
| 3161 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3162 | } else |
| 3163 | Sequence.SetFailed( |
| 3164 | InitializationSequence::FK_InitListBadDestinationType); |
| 3165 | return; |
| 3166 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3169 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3170 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3171 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3172 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3173 | if (CheckInitList.HadError()) { |
| 3174 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3175 | return; |
| 3176 | } |
| 3177 | |
| 3178 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3179 | Sequence.AddListInitializationStep(DestType); |
| 3180 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | |
| 3182 | /// \brief Try a reference initialization that involves calling a conversion |
| 3183 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3184 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3185 | const InitializedEntity &Entity, |
| 3186 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3187 | Expr *Initializer, |
| 3188 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3189 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3190 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3191 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3192 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3193 | QualType cv2T2 = Initializer->getType(); |
| 3194 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3195 | |
| 3196 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3197 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3198 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3200 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3201 | ObjCConversion, |
| 3202 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3203 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3204 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3205 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3206 | (void)ObjCLifetimeConversion; |
| 3207 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3208 | // Build the candidate set directly in the initialization sequence |
| 3209 | // structure, so that it will persist if we fail. |
| 3210 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3211 | CandidateSet.clear(); |
| 3212 | |
| 3213 | // Determine whether we are allowed to call explicit constructors or |
| 3214 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3215 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3216 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3217 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3218 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3219 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3220 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3221 | // The type we're converting to is a class type. Enumerate its constructors |
| 3222 | // to see if there is a suitable conversion. |
| 3223 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3224 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3225 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3226 | // The container holding the constructors can under certain conditions |
| 3227 | // be changed while iterating (e.g. because of deserialization). |
| 3228 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3229 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3230 | for (SmallVector<NamedDecl*, 16>::iterator |
| 3231 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3232 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3233 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3234 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3235 | // Find the constructor (which may be a template). |
| 3236 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3237 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3238 | if (ConstructorTmpl) |
| 3239 | Constructor = cast<CXXConstructorDecl>( |
| 3240 | ConstructorTmpl->getTemplatedDecl()); |
| 3241 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3242 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3243 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3244 | if (!Constructor->isInvalidDecl() && |
| 3245 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3246 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3247 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3248 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3249 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3250 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3251 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3252 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3253 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3254 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3255 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3257 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3258 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3259 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3261 | const RecordType *T2RecordType = 0; |
| 3262 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3263 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3264 | // The type we're converting from is a class type, enumerate its conversion |
| 3265 | // functions. |
| 3266 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3267 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3268 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3269 | CXXRecordDecl::conversion_iterator> |
| 3270 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3271 | for (CXXRecordDecl::conversion_iterator |
| 3272 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3273 | NamedDecl *D = *I; |
| 3274 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3275 | if (isa<UsingShadowDecl>(D)) |
| 3276 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3278 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3279 | CXXConversionDecl *Conv; |
| 3280 | if (ConvTemplate) |
| 3281 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3282 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3283 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3285 | // If the conversion function doesn't return a reference type, |
| 3286 | // it can't be considered for this conversion unless we're allowed to |
| 3287 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | // FIXME: Do we need to make sure that we only consider conversion |
| 3289 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3290 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3291 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3292 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3293 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3294 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3295 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3296 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3298 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3299 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3303 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3304 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3306 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3307 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3308 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3309 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3311 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3312 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3313 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3314 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3315 | // This is the overload that will be used for this initialization step if we |
| 3316 | // use this initialization. Mark it as referenced. |
| 3317 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3318 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3319 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3320 | if (isa<CXXConversionDecl>(Function)) |
| 3321 | T2 = Function->getResultType(); |
| 3322 | else |
| 3323 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3324 | |
| 3325 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3326 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3327 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3328 | T2.getNonLValueExprType(S.Context), |
| 3329 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3330 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3332 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3333 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3334 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3335 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3336 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3337 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3338 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3339 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3340 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3341 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3342 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3344 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3345 | NewDerivedToBase, NewObjCConversion, |
| 3346 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3347 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3348 | // If the type we've converted to is not reference-related to the |
| 3349 | // type we're looking for, then there is another conversion step |
| 3350 | // we need to perform to produce a temporary of the right type |
| 3351 | // that we'll be binding to. |
| 3352 | ImplicitConversionSequence ICS; |
| 3353 | ICS.setStandard(); |
| 3354 | ICS.Standard = Best->FinalConversion; |
| 3355 | T2 = ICS.Standard.getToType(2); |
| 3356 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3357 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3358 | Sequence.AddDerivedToBaseCastStep( |
| 3359 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3361 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3362 | else if (NewObjCConversion) |
| 3363 | Sequence.AddObjCObjectConversionStep( |
| 3364 | S.Context.getQualifiedType(T1, |
| 3365 | T2.getNonReferenceType().getQualifiers())); |
| 3366 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3367 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3368 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3370 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3371 | return OR_Success; |
| 3372 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3374 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3375 | const InitializedEntity &Entity, |
| 3376 | Expr *CurInitExpr); |
| 3377 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3378 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3379 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3380 | const InitializedEntity &Entity, |
| 3381 | const InitializationKind &Kind, |
| 3382 | Expr *Initializer, |
| 3383 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3384 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3385 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3386 | Qualifiers T1Quals; |
| 3387 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3388 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3389 | Qualifiers T2Quals; |
| 3390 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3392 | // If the initializer is the address of an overloaded function, try |
| 3393 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3394 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3395 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3396 | T1, Sequence)) |
| 3397 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3398 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3399 | // Delegate everything else to a subfunction. |
| 3400 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3401 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3402 | } |
| 3403 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3404 | /// Converts the target of reference initialization so that it has the |
| 3405 | /// appropriate qualifiers and value kind. |
| 3406 | /// |
| 3407 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3408 | /// \code |
| 3409 | /// int x; |
| 3410 | /// const int &r = x; |
| 3411 | /// \endcode |
| 3412 | /// |
| 3413 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3414 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3415 | /// \code |
| 3416 | /// const int &r = someStruct.bitfield; |
| 3417 | /// \endcode |
| 3418 | static ExprValueKind |
| 3419 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3420 | InitializationSequence &Sequence, |
| 3421 | Expr *Initializer, |
| 3422 | QualType cv1T1, |
| 3423 | Qualifiers T1Quals, |
| 3424 | Qualifiers T2Quals, |
| 3425 | bool IsLValueRef) { |
| 3426 | bool IsNonAddressableType = Initializer->getBitField() || |
| 3427 | Initializer->refersToVectorElement(); |
| 3428 | |
| 3429 | if (IsNonAddressableType) { |
| 3430 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3431 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3432 | // an rvalue reference. |
| 3433 | // |
| 3434 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3435 | // error to be diagnosed later. |
| 3436 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3437 | assert(Initializer->isGLValue()); |
| 3438 | return Initializer->getValueKind(); |
| 3439 | } |
| 3440 | |
| 3441 | // Force a load so we can materialize a temporary. |
| 3442 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3443 | return VK_RValue; |
| 3444 | } |
| 3445 | |
| 3446 | if (T1Quals != T2Quals) { |
| 3447 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3448 | Initializer->getValueKind()); |
| 3449 | } |
| 3450 | |
| 3451 | return Initializer->getValueKind(); |
| 3452 | } |
| 3453 | |
| 3454 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3455 | /// \brief Reference initialization without resolving overloaded functions. |
| 3456 | static void TryReferenceInitializationCore(Sema &S, |
| 3457 | const InitializedEntity &Entity, |
| 3458 | const InitializationKind &Kind, |
| 3459 | Expr *Initializer, |
| 3460 | QualType cv1T1, QualType T1, |
| 3461 | Qualifiers T1Quals, |
| 3462 | QualType cv2T2, QualType T2, |
| 3463 | Qualifiers T2Quals, |
| 3464 | InitializationSequence &Sequence) { |
| 3465 | QualType DestType = Entity.getType(); |
| 3466 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3467 | // Compute some basic properties of the types and the initializer. |
| 3468 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3469 | bool isRValueRef = !isLValueRef; |
| 3470 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3471 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3472 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3473 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3474 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3475 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3476 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3478 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3479 | // 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] | 3480 | // "cv2 T2" as follows: |
| 3481 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3482 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3483 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3484 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3485 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3486 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3487 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3488 | bool T1Function = T1->isFunctionType(); |
| 3489 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3491 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3493 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | // - 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] | 3495 | // reference-compatible with "cv2 T2," or |
| 3496 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | // 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] | 3498 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3499 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3500 | // to decide whether we're actually binding to a temporary created from |
| 3501 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3502 | if (DerivedToBase) |
| 3503 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3505 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3506 | else if (ObjCConversion) |
| 3507 | Sequence.AddObjCObjectConversionStep( |
| 3508 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3509 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3510 | ExprValueKind ValueKind = |
| 3511 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3512 | cv1T1, T1Quals, T2Quals, |
| 3513 | isLValueRef); |
| 3514 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3515 | return; |
| 3516 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3517 | |
| 3518 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3519 | // reference-related to T2, and can be implicitly converted to an |
| 3520 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3521 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3522 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3523 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3524 | // If we have an rvalue ref to function type here, the rhs must be |
| 3525 | // an rvalue. |
| 3526 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3527 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3528 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3530 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3531 | Sequence); |
| 3532 | if (ConvOvlResult == OR_Success) |
| 3533 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3534 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3535 | Sequence.SetOverloadFailure( |
| 3536 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3537 | ConvOvlResult); |
| 3538 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3539 | } |
| 3540 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3541 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3542 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | // 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] | 3544 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3545 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3546 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3547 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3548 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3549 | Sequence.SetOverloadFailure( |
| 3550 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3551 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3552 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3553 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3554 | ? (RefRelationship == Sema::Ref_Related |
| 3555 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3556 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3557 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3558 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3559 | return; |
| 3560 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3562 | // - If the initializer expression |
| 3563 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3564 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3565 | // Note: functions are handled below. |
| 3566 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3567 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3568 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3569 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3570 | (InitCategory.isXValue() || |
| 3571 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3572 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3573 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3574 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3575 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3576 | // compiler the freedom to perform a copy here or bind to the |
| 3577 | // object, while C++0x requires that we bind directly to the |
| 3578 | // object. Hence, we always bind to the object without making an |
| 3579 | // extra copy. However, in C++03 requires that we check for the |
| 3580 | // presence of a suitable copy constructor: |
| 3581 | // |
| 3582 | // The constructor that would be used to make the copy shall |
| 3583 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3584 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3585 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3586 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3587 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3590 | if (DerivedToBase) |
| 3591 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3592 | ValueKind); |
| 3593 | else if (ObjCConversion) |
| 3594 | Sequence.AddObjCObjectConversionStep( |
| 3595 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3597 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3598 | Initializer, cv1T1, |
| 3599 | T1Quals, T2Quals, |
| 3600 | isLValueRef); |
| 3601 | |
| 3602 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3604 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | |
| 3606 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3607 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3608 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3609 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3610 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3611 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3612 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3613 | Kind, Initializer, |
| 3614 | /*AllowRValues=*/true, |
| 3615 | Sequence); |
| 3616 | if (ConvOvlResult) |
| 3617 | Sequence.SetOverloadFailure( |
| 3618 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3619 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3620 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3621 | return; |
| 3622 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3623 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3624 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3625 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3626 | isRValueRef && InitCategory.isLValue()) { |
| 3627 | Sequence.SetFailed( |
| 3628 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3629 | return; |
| 3630 | } |
| 3631 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3632 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3633 | return; |
| 3634 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3635 | |
| 3636 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3637 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3639 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3640 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3641 | // Determine whether we are allowed to call explicit constructors or |
| 3642 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3643 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3644 | |
| 3645 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3646 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3647 | ImplicitConversionSequence ICS |
| 3648 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3649 | /*SuppressUserConversions*/ false, |
| 3650 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3651 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3652 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3653 | /*AllowObjCWritebackConversion=*/false); |
| 3654 | |
| 3655 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3656 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3657 | // this into an overloading ambiguity diagnostic. However, we need |
| 3658 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3659 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3660 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3661 | Sequence.SetOverloadFailure( |
| 3662 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3663 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3664 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3665 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3666 | else |
| 3667 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3668 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3669 | } else { |
| 3670 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3674 | // same cv-qualification as, or greater cv-qualification |
| 3675 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3676 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3677 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3679 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3681 | return; |
| 3682 | } |
| 3683 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3684 | // [...] 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] | 3685 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3687 | InitCategory.isLValue()) { |
| 3688 | Sequence.SetFailed( |
| 3689 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3690 | return; |
| 3691 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3693 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3694 | return; |
| 3695 | } |
| 3696 | |
| 3697 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3699 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3700 | const InitializedEntity &Entity, |
| 3701 | const InitializationKind &Kind, |
| 3702 | Expr *Initializer, |
| 3703 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3704 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3707 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3708 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3709 | const InitializedEntity &Entity, |
| 3710 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3711 | InitializationSequence &Sequence, |
| 3712 | InitListExpr *InitList) { |
| 3713 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3714 | "Shouldn't use value-init for non-empty init lists"); |
| 3715 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3716 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3717 | // |
| 3718 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3719 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3721 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3722 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3724 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3725 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3726 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3727 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3728 | // C++98: |
| 3729 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3730 | // (12.1), then the default constructor for T is called (and the |
| 3731 | // initialization is ill-formed if T has no accessible default |
| 3732 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3733 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3734 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3735 | } else { |
| 3736 | // C++11: |
| 3737 | // -- if T is a class type (clause 9) with either no default constructor |
| 3738 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3739 | // or deleted, then the object is default-initialized; |
| 3740 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3741 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3742 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3743 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3745 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3746 | // user-provided or deleted default constructor, then the object is |
| 3747 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3748 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3749 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3750 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3751 | if (NeedZeroInitialization) |
| 3752 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3753 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3754 | // C++03: |
| 3755 | // -- if T is a non-union class type without a user-declared constructor, |
| 3756 | // then every non-static data member and base class component of T is |
| 3757 | // value-initialized; |
| 3758 | // [...] A program that calls for [...] value-initialization of an |
| 3759 | // entity of reference type is ill-formed. |
| 3760 | // |
| 3761 | // C++11 doesn't need this handling, because value-initialization does not |
| 3762 | // occur recursively there, and the implicit default constructor is |
| 3763 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3764 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3765 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3766 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3767 | return; |
| 3768 | } |
| 3769 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3770 | // If this is list-value-initialization, pass the empty init list on when |
| 3771 | // building the constructor call. This affects the semantics of a few |
| 3772 | // things (such as whether an explicit default constructor can be called). |
| 3773 | Expr *InitListAsExpr = InitList; |
| 3774 | Expr **Args = InitList ? &InitListAsExpr : 0; |
| 3775 | unsigned NumArgs = InitList ? 1 : 0; |
| 3776 | bool InitListSyntax = InitList; |
| 3777 | |
| 3778 | return TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, T, |
| 3779 | Sequence, InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3783 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3786 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3787 | static void TryDefaultInitialization(Sema &S, |
| 3788 | const InitializedEntity &Entity, |
| 3789 | const InitializationKind &Kind, |
| 3790 | InitializationSequence &Sequence) { |
| 3791 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3793 | // C++ [dcl.init]p6: |
| 3794 | // To default-initialize an object of type T means: |
| 3795 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3796 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3797 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3798 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3799 | // constructor for T is called (and the initialization is ill-formed if |
| 3800 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3801 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3802 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3803 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3804 | } |
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 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3808 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | // 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] | 3810 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3811 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3812 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3813 | return; |
| 3814 | } |
| 3815 | |
| 3816 | // If the destination type has a lifetime property, zero-initialize it. |
| 3817 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3818 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3819 | return; |
| 3820 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3823 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3824 | /// which enumerates all conversion functions and performs overload resolution |
| 3825 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3827 | const InitializedEntity &Entity, |
| 3828 | const InitializationKind &Kind, |
| 3829 | Expr *Initializer, |
| 3830 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3831 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3832 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3833 | QualType SourceType = Initializer->getType(); |
| 3834 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3835 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3837 | // Build the candidate set directly in the initialization sequence |
| 3838 | // structure, so that it will persist if we fail. |
| 3839 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3840 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3842 | // Determine whether we are allowed to call explicit constructors or |
| 3843 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3844 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3845 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3846 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3847 | // The type we're converting to is a class type. Enumerate its constructors |
| 3848 | // to see if there is a suitable conversion. |
| 3849 | CXXRecordDecl *DestRecordDecl |
| 3850 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3852 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3854 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3855 | // The container holding the constructors can under certain conditions |
| 3856 | // be changed while iterating. To be safe we copy the lookup results |
| 3857 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3858 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3859 | for (SmallVector<NamedDecl*, 8>::iterator |
| 3860 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3861 | Con != ConEnd; ++Con) { |
| 3862 | NamedDecl *D = *Con; |
| 3863 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
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 | // Find the constructor (which may be a template). |
| 3866 | CXXConstructorDecl *Constructor = 0; |
| 3867 | FunctionTemplateDecl *ConstructorTmpl |
| 3868 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3869 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3870 | Constructor = cast<CXXConstructorDecl>( |
| 3871 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3872 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3873 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3875 | if (!Constructor->isInvalidDecl() && |
| 3876 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3877 | if (ConstructorTmpl) |
| 3878 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3879 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3880 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3881 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3882 | else |
| 3883 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3884 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3885 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3886 | } |
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 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3889 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3890 | |
| 3891 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3892 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3893 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3894 | // The type we're converting from is a class type, enumerate its conversion |
| 3895 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3896 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3897 | // We can only enumerate the conversion functions for a complete type; if |
| 3898 | // the type isn't complete, simply skip this step. |
| 3899 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3900 | CXXRecordDecl *SourceRecordDecl |
| 3901 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3903 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3904 | CXXRecordDecl::conversion_iterator> |
| 3905 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 3906 | for (CXXRecordDecl::conversion_iterator |
| 3907 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3908 | NamedDecl *D = *I; |
| 3909 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3910 | if (isa<UsingShadowDecl>(D)) |
| 3911 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3913 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3914 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3915 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3916 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3917 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3918 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3920 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3921 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3922 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3923 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3924 | CandidateSet); |
| 3925 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3926 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3927 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3928 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3932 | |
| 3933 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3934 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3935 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3936 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3937 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3939 | Result); |
| 3940 | return; |
| 3941 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3943 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3944 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3945 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3946 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3947 | if (isa<CXXConstructorDecl>(Function)) { |
| 3948 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3949 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 3950 | // cv-unqualified type of the destination. |
| 3951 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 3952 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3953 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3958 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3959 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3960 | // 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] | 3961 | // the resulting temporary object (possible to create an object of |
| 3962 | // a base class type). That copy is not a separate conversion, so |
| 3963 | // we just make a note of the actual destination type (possibly a |
| 3964 | // base class of the type returned by the conversion function) and |
| 3965 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3966 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3967 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3968 | return; |
| 3969 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3970 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3971 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3972 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3974 | // If the conversion following the call to the conversion function |
| 3975 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3976 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3977 | Best->FinalConversion.Third) { |
| 3978 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3979 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3980 | ICS.Standard = Best->FinalConversion; |
| 3981 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3982 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3985 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3986 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3987 | |
| 3988 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3989 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3990 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | // Skip parens. |
| 3992 | e = e->IgnoreParens(); |
| 3993 | |
| 3994 | // Skip address-of nodes. |
| 3995 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3996 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3997 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 3998 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3999 | |
| 4000 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4001 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4002 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4003 | case CK_Dependent: |
| 4004 | case CK_BitCast: |
| 4005 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4006 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4007 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4008 | |
| 4009 | case CK_ArrayToPointerDecay: |
| 4010 | return IIK_nonscalar; |
| 4011 | |
| 4012 | case CK_NullToPointer: |
| 4013 | return IIK_okay; |
| 4014 | |
| 4015 | default: |
| 4016 | break; |
| 4017 | } |
| 4018 | |
| 4019 | // 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] | 4020 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4021 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4022 | // load which requires a cleanup. |
| 4023 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4024 | isWeakAccess = true; |
| 4025 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4026 | if (!isAddressOf) return IIK_nonlocal; |
| 4027 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4028 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4029 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4030 | |
| 4031 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4032 | |
| 4033 | // If we have a conditional operator, check both sides. |
| 4034 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4035 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4036 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4037 | return iik; |
| 4038 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4039 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4040 | |
| 4041 | // These are never scalar. |
| 4042 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4043 | return IIK_nonscalar; |
| 4044 | |
| 4045 | // Otherwise, it needs to be a null pointer constant. |
| 4046 | } else { |
| 4047 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4048 | ? IIK_okay : IIK_nonlocal); |
| 4049 | } |
| 4050 | |
| 4051 | return IIK_nonlocal; |
| 4052 | } |
| 4053 | |
| 4054 | /// Check whether the given expression is a valid operand for an |
| 4055 | /// indirect copy/restore. |
| 4056 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4057 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4058 | bool isWeakAccess = false; |
| 4059 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4060 | // If isWeakAccess to true, there will be an implicit |
| 4061 | // load which requires a cleanup. |
| 4062 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4063 | S.ExprNeedsCleanups = true; |
| 4064 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | if (iik == IIK_okay) return; |
| 4066 | |
| 4067 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4068 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4069 | << src->getSourceRange(); |
| 4070 | } |
| 4071 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4072 | /// \brief Determine whether we have compatible array types for the |
| 4073 | /// purposes of GNU by-copy array initialization. |
| 4074 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4075 | const ArrayType *Dest, |
| 4076 | const ArrayType *Source) { |
| 4077 | // If the source and destination array types are equivalent, we're |
| 4078 | // done. |
| 4079 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4080 | return true; |
| 4081 | |
| 4082 | // Make sure that the element types are the same. |
| 4083 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4084 | return false; |
| 4085 | |
| 4086 | // The only mismatch we allow is when the destination is an |
| 4087 | // incomplete array type and the source is a constant array type. |
| 4088 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4089 | } |
| 4090 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | static bool tryObjCWritebackConversion(Sema &S, |
| 4092 | InitializationSequence &Sequence, |
| 4093 | const InitializedEntity &Entity, |
| 4094 | Expr *Initializer) { |
| 4095 | bool ArrayDecay = false; |
| 4096 | QualType ArgType = Initializer->getType(); |
| 4097 | QualType ArgPointee; |
| 4098 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4099 | ArrayDecay = true; |
| 4100 | ArgPointee = ArgArrayType->getElementType(); |
| 4101 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4102 | } |
| 4103 | |
| 4104 | // Handle write-back conversion. |
| 4105 | QualType ConvertedArgType; |
| 4106 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4107 | ConvertedArgType)) |
| 4108 | return false; |
| 4109 | |
| 4110 | // We should copy unless we're passing to an argument explicitly |
| 4111 | // marked 'out'. |
| 4112 | bool ShouldCopy = true; |
| 4113 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4114 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4115 | |
| 4116 | // Do we need an lvalue conversion? |
| 4117 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4118 | ImplicitConversionSequence ICS; |
| 4119 | ICS.setStandard(); |
| 4120 | ICS.Standard.setAsIdentityConversion(); |
| 4121 | |
| 4122 | QualType ResultType; |
| 4123 | if (ArrayDecay) { |
| 4124 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4125 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4126 | } else { |
| 4127 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4128 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4129 | } |
| 4130 | |
| 4131 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4132 | } |
| 4133 | |
| 4134 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4135 | return true; |
| 4136 | } |
| 4137 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4138 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4139 | InitializationSequence &Sequence, |
| 4140 | QualType DestType, |
| 4141 | Expr *Initializer) { |
| 4142 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4143 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4144 | return false; |
| 4145 | |
| 4146 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4147 | return true; |
| 4148 | } |
| 4149 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4150 | // |
| 4151 | // OpenCL 1.2 spec, s6.12.10 |
| 4152 | // |
| 4153 | // The event argument can also be used to associate the |
| 4154 | // async_work_group_copy with a previous async copy allowing |
| 4155 | // an event to be shared by multiple async copies; otherwise |
| 4156 | // event should be zero. |
| 4157 | // |
| 4158 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4159 | InitializationSequence &Sequence, |
| 4160 | QualType DestType, |
| 4161 | Expr *Initializer) { |
| 4162 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4163 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4164 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4165 | return false; |
| 4166 | |
| 4167 | Sequence.AddOCLZeroEventStep(DestType); |
| 4168 | return true; |
| 4169 | } |
| 4170 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4171 | InitializationSequence::InitializationSequence(Sema &S, |
| 4172 | const InitializedEntity &Entity, |
| 4173 | const InitializationKind &Kind, |
| 4174 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4175 | unsigned NumArgs) |
| 4176 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4177 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4178 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4179 | // Eliminate non-overload placeholder types in the arguments. We |
| 4180 | // need to do this before checking whether types are dependent |
| 4181 | // because lowering a pseudo-object expression might well give us |
| 4182 | // something of dependent type. |
| 4183 | for (unsigned I = 0; I != NumArgs; ++I) |
| 4184 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4185 | // FIXME: should we be doing this here? |
| 4186 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4187 | if (result.isInvalid()) { |
| 4188 | SetFailed(FK_PlaceholderType); |
| 4189 | return; |
| 4190 | } |
| 4191 | Args[I] = result.take(); |
| 4192 | } |
| 4193 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4194 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4195 | // The semantics of initializers are as follows. The destination type is |
| 4196 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4197 | // 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] | 4198 | // 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] | 4199 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4200 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4201 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4202 | if (DestType->isDependentType() || |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4203 | Expr::hasAnyTypeDependentArguments(llvm::makeArrayRef(Args, NumArgs))) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4204 | SequenceKind = DependentSequence; |
| 4205 | return; |
| 4206 | } |
| 4207 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4208 | // Almost everything is a normal sequence. |
| 4209 | setSequenceKind(NormalSequence); |
| 4210 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4211 | QualType SourceType; |
| 4212 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4213 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4214 | Initializer = Args[0]; |
| 4215 | if (!isa<InitListExpr>(Initializer)) |
| 4216 | SourceType = Initializer->getType(); |
| 4217 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4219 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4220 | // object is list-initialized (8.5.4). |
| 4221 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4222 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4223 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4224 | return; |
| 4225 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4226 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4228 | // - If the destination type is a reference type, see 8.5.3. |
| 4229 | if (DestType->isReferenceType()) { |
| 4230 | // C++0x [dcl.init.ref]p1: |
| 4231 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4232 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4233 | // by an object that can be converted into a T. |
| 4234 | // (Therefore, multiple arguments are not permitted.) |
| 4235 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4236 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4237 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4238 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4239 | return; |
| 4240 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4242 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4243 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 4244 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4245 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4246 | return; |
| 4247 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4248 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4249 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4250 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4251 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4252 | return; |
| 4253 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4254 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4255 | // - If the destination type is an array of characters, an array of |
| 4256 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4257 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4259 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4260 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4261 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4262 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4263 | return; |
| 4264 | } |
| 4265 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4266 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4267 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4268 | return; |
| 4269 | } |
| 4270 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4271 | // Note: as an GNU C extension, we allow initialization of an |
| 4272 | // array from a compound literal that creates an array of the same |
| 4273 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4274 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4275 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4276 | Initializer->getType()->isArrayType()) { |
| 4277 | const ArrayType *SourceAT |
| 4278 | = Context.getAsArrayType(Initializer->getType()); |
| 4279 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4280 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4281 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4282 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4283 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4284 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4285 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4286 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4287 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4288 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4289 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4290 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4291 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4292 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4293 | *this); |
| 4294 | AddParenthesizedArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4295 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4296 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4297 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4298 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4300 | return; |
| 4301 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4302 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4303 | // Determine whether we should consider writeback conversions for |
| 4304 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4305 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4306 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4307 | |
| 4308 | // We're at the end of the line for C: it's either a write-back conversion |
| 4309 | // 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] | 4310 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4311 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4312 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4313 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4314 | return; |
| 4315 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4316 | |
| 4317 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4318 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4319 | |
| 4320 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4321 | return; |
| 4322 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4323 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4324 | AddCAssignmentStep(DestType); |
| 4325 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4326 | return; |
| 4327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4329 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4331 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4332 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4333 | // - If the initialization is direct-initialization, or if it is |
| 4334 | // copy-initialization where the cv-unqualified version of the |
| 4335 | // 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] | 4336 | // class of the destination, constructors are considered. [...] |
| 4337 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4338 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4339 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4340 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4341 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4342 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4343 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4344 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4346 | // used) to a derived class thereof are enumerated as described in |
| 4347 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4348 | // (13.3). |
| 4349 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4350 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4351 | return; |
| 4352 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4354 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4355 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4356 | return; |
| 4357 | } |
| 4358 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
| 4360 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4361 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4362 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4363 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4364 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4365 | return; |
| 4366 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4368 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4369 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4370 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4372 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4373 | |
| 4374 | ImplicitConversionSequence ICS |
| 4375 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4376 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4377 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4378 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4379 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4380 | allowObjCWritebackConversion); |
| 4381 | |
| 4382 | if (ICS.isStandard() && |
| 4383 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4384 | // Objective-C ARC writeback conversion. |
| 4385 | |
| 4386 | // We should copy unless we're passing to an argument explicitly |
| 4387 | // marked 'out'. |
| 4388 | bool ShouldCopy = true; |
| 4389 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4390 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4391 | |
| 4392 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4393 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4394 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4395 | ImplicitConversionSequence LvalueICS; |
| 4396 | LvalueICS.setStandard(); |
| 4397 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4398 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4399 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4400 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4401 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4402 | |
| 4403 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4404 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4405 | DeclAccessPair dap; |
| 4406 | if (Initializer->getType() == Context.OverloadTy && |
| 4407 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4408 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4409 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4410 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4411 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4412 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4413 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4414 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4415 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4416 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4420 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4421 | StepEnd = Steps.end(); |
| 4422 | Step != StepEnd; ++Step) |
| 4423 | Step->Destroy(); |
| 4424 | } |
| 4425 | |
| 4426 | //===----------------------------------------------------------------------===// |
| 4427 | // Perform initialization |
| 4428 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4430 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4431 | switch(Entity.getKind()) { |
| 4432 | case InitializedEntity::EK_Variable: |
| 4433 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4434 | case InitializedEntity::EK_Exception: |
| 4435 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4436 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4437 | return Sema::AA_Initializing; |
| 4438 | |
| 4439 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4440 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4441 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4442 | return Sema::AA_Sending; |
| 4443 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4444 | return Sema::AA_Passing; |
| 4445 | |
| 4446 | case InitializedEntity::EK_Result: |
| 4447 | return Sema::AA_Returning; |
| 4448 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4449 | case InitializedEntity::EK_Temporary: |
| 4450 | // FIXME: Can we tell apart casting vs. converting? |
| 4451 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4453 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4454 | case InitializedEntity::EK_ArrayElement: |
| 4455 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4456 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4457 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4458 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4459 | return Sema::AA_Initializing; |
| 4460 | } |
| 4461 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4462 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4465 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4466 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4467 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4468 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4469 | case InitializedEntity::EK_ArrayElement: |
| 4470 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4471 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4472 | case InitializedEntity::EK_New: |
| 4473 | case InitializedEntity::EK_Variable: |
| 4474 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4475 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4476 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4477 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4478 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4479 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4480 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4481 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4483 | case InitializedEntity::EK_Parameter: |
| 4484 | case InitializedEntity::EK_Temporary: |
| 4485 | return true; |
| 4486 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4488 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4489 | } |
| 4490 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4491 | /// \brief Whether the given entity, when initialized with an object |
| 4492 | /// created for that initialization, requires destruction. |
| 4493 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4494 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4495 | case InitializedEntity::EK_Result: |
| 4496 | case InitializedEntity::EK_New: |
| 4497 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4498 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4499 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4500 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4501 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4502 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4503 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4504 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4505 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4506 | case InitializedEntity::EK_Variable: |
| 4507 | case InitializedEntity::EK_Parameter: |
| 4508 | case InitializedEntity::EK_Temporary: |
| 4509 | case InitializedEntity::EK_ArrayElement: |
| 4510 | case InitializedEntity::EK_Exception: |
| 4511 | return true; |
| 4512 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
| 4514 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4517 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4518 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4519 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4520 | OverloadCandidateSet &CandidateSet, |
| 4521 | CXXRecordDecl *Class, |
| 4522 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4523 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4524 | // The container holding the constructors can under certain conditions |
| 4525 | // be changed while iterating (e.g. because of deserialization). |
| 4526 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4527 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4528 | for (SmallVector<NamedDecl*, 16>::iterator |
| 4529 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4530 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4531 | CXXConstructorDecl *Constructor = 0; |
| 4532 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4533 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4534 | // Handle copy/moveconstructors, only. |
| 4535 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4536 | !Constructor->isCopyOrMoveConstructor() || |
| 4537 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4538 | continue; |
| 4539 | |
| 4540 | DeclAccessPair FoundDecl |
| 4541 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4542 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4543 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4544 | continue; |
| 4545 | } |
| 4546 | |
| 4547 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4548 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4549 | if (ConstructorTmpl->isInvalidDecl()) |
| 4550 | continue; |
| 4551 | |
| 4552 | Constructor = cast<CXXConstructorDecl>( |
| 4553 | ConstructorTmpl->getTemplatedDecl()); |
| 4554 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4555 | continue; |
| 4556 | |
| 4557 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4558 | // candidates? |
| 4559 | DeclAccessPair FoundDecl |
| 4560 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4561 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4562 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4567 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4568 | Expr *Initializer) { |
| 4569 | switch (Entity.getKind()) { |
| 4570 | case InitializedEntity::EK_Result: |
| 4571 | return Entity.getReturnLoc(); |
| 4572 | |
| 4573 | case InitializedEntity::EK_Exception: |
| 4574 | return Entity.getThrowLoc(); |
| 4575 | |
| 4576 | case InitializedEntity::EK_Variable: |
| 4577 | return Entity.getDecl()->getLocation(); |
| 4578 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4579 | case InitializedEntity::EK_LambdaCapture: |
| 4580 | return Entity.getCaptureLoc(); |
| 4581 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4582 | case InitializedEntity::EK_ArrayElement: |
| 4583 | case InitializedEntity::EK_Member: |
| 4584 | case InitializedEntity::EK_Parameter: |
| 4585 | case InitializedEntity::EK_Temporary: |
| 4586 | case InitializedEntity::EK_New: |
| 4587 | case InitializedEntity::EK_Base: |
| 4588 | case InitializedEntity::EK_Delegating: |
| 4589 | case InitializedEntity::EK_VectorElement: |
| 4590 | case InitializedEntity::EK_ComplexElement: |
| 4591 | case InitializedEntity::EK_BlockElement: |
| 4592 | return Initializer->getLocStart(); |
| 4593 | } |
| 4594 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4595 | } |
| 4596 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4597 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4598 | /// provided by the given initializer by calling the appropriate copy |
| 4599 | /// constructor. |
| 4600 | /// |
| 4601 | /// \param S The Sema object used for type-checking. |
| 4602 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4603 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4604 | /// the type of the initializer expression or a superclass thereof. |
| 4605 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4606 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4607 | /// |
| 4608 | /// \param CurInit The initializer expression. |
| 4609 | /// |
| 4610 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4611 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4612 | /// an rvalue. |
| 4613 | /// |
| 4614 | /// \returns An expression that copies the initializer expression into |
| 4615 | /// a temporary object, or an error expression if a copy could not be |
| 4616 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4617 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4618 | QualType T, |
| 4619 | const InitializedEntity &Entity, |
| 4620 | ExprResult CurInit, |
| 4621 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4622 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4623 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4625 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4626 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4627 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4628 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4629 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4630 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4631 | // When certain criteria are met, an implementation is allowed to |
| 4632 | // omit the copy/move construction of a class object, even if the |
| 4633 | // copy/move constructor and/or destructor for the object have |
| 4634 | // side effects. [...] |
| 4635 | // - when a temporary class object that has not been bound to a |
| 4636 | // reference (12.2) would be copied/moved to a class object |
| 4637 | // with the same cv-unqualified type, the copy/move operation |
| 4638 | // can be omitted by constructing the temporary object |
| 4639 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4641 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4642 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4644 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4645 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4646 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4647 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4649 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4650 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4651 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4652 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4653 | // Only consider constructors and constructor templates. Per |
| 4654 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4655 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4656 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4657 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4659 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4660 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4661 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4662 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4663 | case OR_Success: |
| 4664 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4666 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4667 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4668 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4669 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4670 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4671 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4672 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4673 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4674 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4675 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4677 | case OR_Ambiguous: |
| 4678 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4679 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4680 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4681 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4682 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4683 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4684 | case OR_Deleted: |
| 4685 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4686 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4687 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4688 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4689 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4692 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4693 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4694 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4695 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4696 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4697 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4698 | |
| 4699 | if (IsExtraneousCopy) { |
| 4700 | // If this is a totally extraneous copy for C++03 reference |
| 4701 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4702 | // expression. We don't generate an (elided) copy operation here |
| 4703 | // because doing so would require us to pass down a flag to avoid |
| 4704 | // infinite recursion, where each step adds another extraneous, |
| 4705 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4707 | // Instantiate the default arguments of any extra parameters in |
| 4708 | // the selected copy constructor, as if we were going to create a |
| 4709 | // proper call to the copy constructor. |
| 4710 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4711 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4712 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4713 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | |
| 4716 | // Build the default argument expression; we don't actually care |
| 4717 | // if this succeeds or not, because this routine will complain |
| 4718 | // if there was a problem. |
| 4719 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4720 | } |
| 4721 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4722 | return S.Owned(CurInitExpr); |
| 4723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4724 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4725 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4726 | // constructor call (we might have derived-to-base conversions, or |
| 4727 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4728 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4729 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4730 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4731 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4732 | // Actually perform the constructor call. |
| 4733 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4734 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4735 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4736 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4737 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4738 | CXXConstructExpr::CK_Complete, |
| 4739 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4741 | // If we're supposed to bind temporaries, do so. |
| 4742 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4743 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4744 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4745 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4746 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4747 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4748 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4749 | /// -Wc++98-compat. |
| 4750 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4751 | const InitializedEntity &Entity, |
| 4752 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4753 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4754 | |
| 4755 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4756 | if (!Record) |
| 4757 | return; |
| 4758 | |
| 4759 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4760 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4761 | == DiagnosticsEngine::Ignored) |
| 4762 | return; |
| 4763 | |
| 4764 | // Find constructors which would have been considered. |
| 4765 | OverloadCandidateSet CandidateSet(Loc); |
| 4766 | LookupCopyAndMoveConstructors( |
| 4767 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4768 | |
| 4769 | // Perform overload resolution. |
| 4770 | OverloadCandidateSet::iterator Best; |
| 4771 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4772 | |
| 4773 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4774 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4775 | << CurInitExpr->getSourceRange(); |
| 4776 | |
| 4777 | switch (OR) { |
| 4778 | case OR_Success: |
| 4779 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4780 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4781 | // FIXME: Check default arguments as far as that's possible. |
| 4782 | break; |
| 4783 | |
| 4784 | case OR_No_Viable_Function: |
| 4785 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4786 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4787 | break; |
| 4788 | |
| 4789 | case OR_Ambiguous: |
| 4790 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4791 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4792 | break; |
| 4793 | |
| 4794 | case OR_Deleted: |
| 4795 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4796 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4797 | break; |
| 4798 | } |
| 4799 | } |
| 4800 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4801 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4802 | const InitializedEntity &Entity) { |
| 4803 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4804 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4805 | return; |
| 4806 | |
| 4807 | if (Entity.getDecl()->getDeclName()) |
| 4808 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4809 | << Entity.getDecl()->getDeclName(); |
| 4810 | else |
| 4811 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4812 | } |
| 4813 | } |
| 4814 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4815 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4816 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4817 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4818 | } |
| 4819 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4820 | static ExprResult |
| 4821 | PerformConstructorInitialization(Sema &S, |
| 4822 | const InitializedEntity &Entity, |
| 4823 | const InitializationKind &Kind, |
| 4824 | MultiExprArg Args, |
| 4825 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4826 | bool &ConstructorInitRequiresZeroInit, |
| 4827 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4828 | unsigned NumArgs = Args.size(); |
| 4829 | CXXConstructorDecl *Constructor |
| 4830 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4831 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4832 | |
| 4833 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4834 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4835 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4836 | ? Kind.getEqualLoc() |
| 4837 | : Kind.getLocation(); |
| 4838 | |
| 4839 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4840 | // Force even a trivial, implicit default constructor to be |
| 4841 | // semantically checked. We do this explicitly because we don't build |
| 4842 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4843 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4844 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4845 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4846 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4847 | } |
| 4848 | |
| 4849 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4850 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4851 | // C++ [over.match.copy]p1: |
| 4852 | // - When initializing a temporary to be bound to the first parameter |
| 4853 | // of a constructor that takes a reference to possibly cv-qualified |
| 4854 | // T as its first argument, called with a single argument in the |
| 4855 | // context of direct-initialization, explicit conversion functions |
| 4856 | // are also considered. |
| 4857 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4858 | Args.size() == 1 && |
| 4859 | Constructor->isCopyOrMoveConstructor(); |
| 4860 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4861 | // Determine the arguments required to actually perform the constructor |
| 4862 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4863 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4864 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 4865 | AllowExplicitConv, |
| 4866 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4867 | return ExprError(); |
| 4868 | |
| 4869 | |
| 4870 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4871 | (Kind.getKind() == InitializationKind::IK_DirectList || |
| 4872 | (NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4873 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4874 | Kind.getKind() == InitializationKind::IK_Value)))) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4875 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4876 | S.MarkFunctionReferenced(Loc, Constructor); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4877 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 4878 | |
| 4879 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4880 | if (!TSInfo) |
| 4881 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4882 | SourceRange ParenRange; |
| 4883 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4884 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4885 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4886 | CurInit = S.Owned( |
| 4887 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 4888 | TSInfo, ConstructorArgs, |
| 4889 | ParenRange, IsListInitialization, |
| 4890 | HadMultipleCandidates, |
| 4891 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4892 | } else { |
| 4893 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4894 | CXXConstructExpr::CK_Complete; |
| 4895 | |
| 4896 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4897 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4898 | CXXConstructExpr::CK_VirtualBase : |
| 4899 | CXXConstructExpr::CK_NonVirtualBase; |
| 4900 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4901 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4902 | } |
| 4903 | |
| 4904 | // Only get the parenthesis range if it is a direct construction. |
| 4905 | SourceRange parenRange = |
| 4906 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4907 | Kind.getParenRange() : SourceRange(); |
| 4908 | |
| 4909 | // If the entity allows NRVO, mark the construction as elidable |
| 4910 | // unconditionally. |
| 4911 | if (Entity.allowsNRVO()) |
| 4912 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4913 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4914 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4915 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4916 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4917 | ConstructorInitRequiresZeroInit, |
| 4918 | ConstructKind, |
| 4919 | parenRange); |
| 4920 | else |
| 4921 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4922 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4923 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4924 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4925 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4926 | ConstructorInitRequiresZeroInit, |
| 4927 | ConstructKind, |
| 4928 | parenRange); |
| 4929 | } |
| 4930 | if (CurInit.isInvalid()) |
| 4931 | return ExprError(); |
| 4932 | |
| 4933 | // Only check access if all of that succeeded. |
| 4934 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4935 | Step.Function.FoundDecl.getAccess()); |
| 4936 | S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc); |
| 4937 | |
| 4938 | if (shouldBindAsTemporary(Entity)) |
| 4939 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4940 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4941 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4944 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 4945 | /// longer than the current full-expression. Conservatively returns false if |
| 4946 | /// it's unclear. |
| 4947 | static bool |
| 4948 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 4949 | const InitializedEntity *Top = &Entity; |
| 4950 | while (Top->getParent()) |
| 4951 | Top = Top->getParent(); |
| 4952 | |
| 4953 | switch (Top->getKind()) { |
| 4954 | case InitializedEntity::EK_Variable: |
| 4955 | case InitializedEntity::EK_Result: |
| 4956 | case InitializedEntity::EK_Exception: |
| 4957 | case InitializedEntity::EK_Member: |
| 4958 | case InitializedEntity::EK_New: |
| 4959 | case InitializedEntity::EK_Base: |
| 4960 | case InitializedEntity::EK_Delegating: |
| 4961 | return true; |
| 4962 | |
| 4963 | case InitializedEntity::EK_ArrayElement: |
| 4964 | case InitializedEntity::EK_VectorElement: |
| 4965 | case InitializedEntity::EK_BlockElement: |
| 4966 | case InitializedEntity::EK_ComplexElement: |
| 4967 | // Could not determine what the full initialization is. Assume it might not |
| 4968 | // outlive the full-expression. |
| 4969 | return false; |
| 4970 | |
| 4971 | case InitializedEntity::EK_Parameter: |
| 4972 | case InitializedEntity::EK_Temporary: |
| 4973 | case InitializedEntity::EK_LambdaCapture: |
| 4974 | // The entity being initialized might not outlive the full-expression. |
| 4975 | return false; |
| 4976 | } |
| 4977 | |
| 4978 | llvm_unreachable("unknown entity kind"); |
| 4979 | } |
| 4980 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4981 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4982 | InitializationSequence::Perform(Sema &S, |
| 4983 | const InitializedEntity &Entity, |
| 4984 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4985 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4986 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4987 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4988 | unsigned NumArgs = Args.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4989 | Diagnose(S, Entity, Kind, Args.data(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4990 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4991 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4992 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4993 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4994 | // If the declaration is a non-dependent, incomplete array type |
| 4995 | // that has an initializer, then its type will be completed once |
| 4996 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4997 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4998 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4999 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5000 | if (const IncompleteArrayType *ArrayT |
| 5001 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5002 | // FIXME: We don't currently have the ability to accurately |
| 5003 | // compute the length of an initializer list without |
| 5004 | // performing full type-checking of the initializer list |
| 5005 | // (since we have to determine where braces are implicitly |
| 5006 | // introduced and such). So, we fall back to making the array |
| 5007 | // type a dependently-sized array type with no specified |
| 5008 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5009 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5010 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5011 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5012 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5013 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5014 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5015 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5016 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5017 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5018 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5019 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | *ResultType |
| 5023 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5024 | /*NumElts=*/0, |
| 5025 | ArrayT->getSizeModifier(), |
| 5026 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5027 | Brackets); |
| 5028 | } |
| 5029 | |
| 5030 | } |
| 5031 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5032 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5033 | !Kind.isExplicitCast()) { |
| 5034 | // Rebuild the ParenListExpr. |
| 5035 | SourceRange ParenRange = Kind.getParenRange(); |
| 5036 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5037 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5038 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5039 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5040 | Kind.isExplicitCast() || |
| 5041 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5042 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5045 | // No steps means no initialization. |
| 5046 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5047 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5049 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5050 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5051 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 5052 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5053 | // from an initializer list. For parameters, we produce a better warning |
| 5054 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5055 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5056 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5057 | << Init->getSourceRange(); |
| 5058 | } |
| 5059 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5060 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5061 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5062 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5063 | Entity.getType()->isPointerType() && |
| 5064 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5065 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5066 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5067 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5068 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5069 | << Init->getSourceRange(); |
| 5070 | } |
| 5071 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5072 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5073 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5074 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5075 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5076 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5077 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5078 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5079 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5080 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5081 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5083 | // grab the only argument out the Args and place it into the "current" |
| 5084 | // initializer. |
| 5085 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5086 | case SK_ResolveAddressOfOverloadedFunction: |
| 5087 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5088 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5089 | case SK_CastDerivedToBaseLValue: |
| 5090 | case SK_BindReference: |
| 5091 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5092 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5093 | case SK_UserConversion: |
| 5094 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5095 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5096 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5097 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5098 | case SK_ConversionSequence: |
| 5099 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5100 | case SK_UnwrapInitList: |
| 5101 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5102 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5103 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5104 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5105 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5106 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5107 | case SK_PassByIndirectCopyRestore: |
| 5108 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5109 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5110 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5111 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5112 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5113 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5114 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5115 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5116 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5117 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5118 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5119 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5120 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5121 | case SK_ZeroInitialization: |
| 5122 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5123 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5124 | |
| 5125 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5126 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5127 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5128 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5129 | Step != StepEnd; ++Step) { |
| 5130 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5131 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5133 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5135 | switch (Step->Kind) { |
| 5136 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5137 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5138 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5139 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5140 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5141 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5142 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5143 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5144 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5146 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5147 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5148 | case SK_CastDerivedToBaseLValue: { |
| 5149 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5150 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5151 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5152 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5154 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5155 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5156 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5157 | CurInit.get()->getLocStart(), |
| 5158 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5159 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5160 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5162 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5163 | QualType T = SourceType; |
| 5164 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5165 | T = Pointer->getPointeeType(); |
| 5166 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5167 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5168 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5169 | } |
| 5170 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5171 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5172 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5173 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5174 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5175 | VK_XValue : |
| 5176 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5177 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5178 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5179 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5180 | CurInit.get(), |
| 5181 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5182 | break; |
| 5183 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5184 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5185 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5186 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5187 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 5188 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5189 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5190 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5191 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5192 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5193 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5194 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5195 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5196 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5197 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5198 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5199 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5200 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5201 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5202 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5203 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5204 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5205 | // Reference binding does not have any corresponding ASTs. |
| 5206 | |
| 5207 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5208 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5209 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5210 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5211 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5212 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5213 | case SK_BindReferenceToTemporary: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5214 | // Make sure the "temporary" is actually an rvalue. |
| 5215 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5216 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5217 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5218 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5219 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5220 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5221 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5222 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5223 | Entity.getType().getNonReferenceType(), |
| 5224 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5225 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5226 | |
| 5227 | // If we're binding to an Objective-C object that has lifetime, we |
| 5228 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5229 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5230 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5231 | S.ExprNeedsCleanups = true; |
| 5232 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5233 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5234 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5235 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5236 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5237 | /*IsExtraneousCopy=*/true); |
| 5238 | break; |
| 5239 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5240 | case SK_UserConversion: { |
| 5241 | // We have a user-defined conversion that invokes either a constructor |
| 5242 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5243 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5244 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5245 | FunctionDecl *Fn = Step->Function.Function; |
| 5246 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5247 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5248 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5249 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5250 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5251 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5252 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5253 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5255 | // Determine the arguments required to actually perform the constructor |
| 5256 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5257 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5258 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5259 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5260 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5261 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5263 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5264 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5265 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5266 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5267 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5268 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5269 | CXXConstructExpr::CK_Complete, |
| 5270 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5271 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5272 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5273 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5274 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5275 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5276 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5277 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5278 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5279 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5280 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5281 | S.IsDerivedFrom(SourceType, Class)) |
| 5282 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5284 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5285 | } else { |
| 5286 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5287 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5288 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5289 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5290 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | |
| 5292 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5293 | // derived-to-base conversion? I believe the answer is "no", because |
| 5294 | // 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] | 5295 | ExprResult CurInitExprRes = |
| 5296 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5297 | FoundFn, Conversion); |
| 5298 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5299 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5300 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5302 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5303 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5304 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5305 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5306 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5308 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5309 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5310 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5312 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5313 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5314 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5315 | |
| 5316 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5317 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5318 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5319 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5320 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5321 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5322 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5323 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5324 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5325 | } |
| 5326 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5327 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5328 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5329 | CurInit.get()->getType(), |
| 5330 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5331 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5332 | if (MaybeBindToTemp) |
| 5333 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5334 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5335 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5336 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5339 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5340 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5341 | case SK_QualificationConversionXValue: |
| 5342 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5343 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5344 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5345 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5346 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5347 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5348 | VK_XValue : |
| 5349 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5350 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5351 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5354 | case SK_LValueToRValue: { |
| 5355 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5356 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5357 | CK_LValueToRValue, |
| 5358 | CurInit.take(), |
| 5359 | /*BasePath=*/0, |
| 5360 | VK_RValue)); |
| 5361 | break; |
| 5362 | } |
| 5363 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5364 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5365 | Sema::CheckedConversionKind CCK |
| 5366 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5367 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5368 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5369 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5370 | ExprResult CurInitExprRes = |
| 5371 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5372 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5373 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5374 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5375 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5376 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5377 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5378 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5379 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5380 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5381 | // Hack: We must pass *ResultType if available in order to set the type |
| 5382 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5383 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5384 | // temporary, not a reference, so we should pass Ty. |
| 5385 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5386 | // Since this step is never used for a reference directly, we explicitly |
| 5387 | // unwrap references here and rewrap them afterwards. |
| 5388 | // We also need to create a InitializeTemporary entity for this. |
| 5389 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5390 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5391 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5392 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5393 | InitListChecker PerformInitList(S, InitEntity, |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5394 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5395 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5396 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5397 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5398 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5399 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5400 | if (ResultType) { |
| 5401 | if ((*ResultType)->isRValueReferenceType()) |
| 5402 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5403 | else if ((*ResultType)->isLValueReferenceType()) |
| 5404 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5405 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5406 | *ResultType = Ty; |
| 5407 | } |
| 5408 | |
| 5409 | InitListExpr *StructuredInitList = |
| 5410 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5411 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5412 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5413 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5414 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5415 | break; |
| 5416 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5417 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5418 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5419 | // When an initializer list is passed for a parameter of type "reference |
| 5420 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5421 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5422 | // FIXME: This is a hack. What we really should do is create a user |
| 5423 | // conversion step for this case, but this makes it considerably more |
| 5424 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5425 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5426 | Entity.getType().getNonReferenceType()); |
| 5427 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5428 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5429 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5430 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5431 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5432 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5433 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5434 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5435 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5436 | ConstructorInitRequiresZeroInit, |
| 5437 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5438 | break; |
| 5439 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5440 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5441 | case SK_UnwrapInitList: |
| 5442 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5443 | break; |
| 5444 | |
| 5445 | case SK_RewrapInitList: { |
| 5446 | Expr *E = CurInit.take(); |
| 5447 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5448 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5449 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5450 | ILE->setSyntacticForm(Syntactic); |
| 5451 | ILE->setType(E->getType()); |
| 5452 | ILE->setValueKind(E->getValueKind()); |
| 5453 | CurInit = S.Owned(ILE); |
| 5454 | break; |
| 5455 | } |
| 5456 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5457 | case SK_ConstructorInitialization: { |
| 5458 | // When an initializer list is passed for a parameter of type "reference |
| 5459 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5460 | // EK_Parameter entity with reference type. |
| 5461 | // FIXME: This is a hack. What we really should do is create a user |
| 5462 | // conversion step for this case, but this makes it considerably more |
| 5463 | // complicated. For now, this will do. |
| 5464 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5465 | Entity.getType().getNonReferenceType()); |
| 5466 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5467 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5468 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5469 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5470 | ConstructorInitRequiresZeroInit, |
| 5471 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5472 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5475 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5476 | step_iterator NextStep = Step; |
| 5477 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5478 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5479 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5480 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5481 | // The need for zero-initialization is recorded directly into |
| 5482 | // the call to the object's constructor within the next step. |
| 5483 | ConstructorInitRequiresZeroInit = true; |
| 5484 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5485 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5486 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5487 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5488 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5490 | Kind.getRange().getBegin()); |
| 5491 | |
| 5492 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5493 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5494 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5495 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5496 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5497 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5498 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5499 | break; |
| 5500 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5501 | |
| 5502 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5503 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5504 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5505 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5506 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5507 | if (Result.isInvalid()) |
| 5508 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5509 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5510 | |
| 5511 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5512 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5513 | if (ConvTy != Sema::Compatible && |
| 5514 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5515 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5516 | == Sema::Compatible) |
| 5517 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5518 | if (CurInitExprRes.isInvalid()) |
| 5519 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5520 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5521 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5522 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5523 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5524 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5525 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5526 | getAssignmentAction(Entity), |
| 5527 | &Complained)) { |
| 5528 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5529 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5530 | } else if (Complained) |
| 5531 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5532 | break; |
| 5533 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5534 | |
| 5535 | case SK_StringInit: { |
| 5536 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5537 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5538 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5539 | break; |
| 5540 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5541 | |
| 5542 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5543 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5544 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5545 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5546 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5547 | |
| 5548 | case SK_ArrayInit: |
| 5549 | // Okay: we checked everything before creating this step. Note that |
| 5550 | // this is a GNU extension. |
| 5551 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5552 | << Step->Type << CurInit.get()->getType() |
| 5553 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5554 | |
| 5555 | // If the destination type is an incomplete array type, update the |
| 5556 | // type accordingly. |
| 5557 | if (ResultType) { |
| 5558 | if (const IncompleteArrayType *IncompleteDest |
| 5559 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5560 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5561 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5562 | *ResultType = S.Context.getConstantArrayType( |
| 5563 | IncompleteDest->getElementType(), |
| 5564 | ConstantSource->getSize(), |
| 5565 | ArrayType::Normal, 0); |
| 5566 | } |
| 5567 | } |
| 5568 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5569 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5570 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5571 | case SK_ParenthesizedArrayInit: |
| 5572 | // Okay: we checked everything before creating this step. Note that |
| 5573 | // this is a GNU extension. |
| 5574 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5575 | << CurInit.get()->getSourceRange(); |
| 5576 | break; |
| 5577 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5578 | case SK_PassByIndirectCopyRestore: |
| 5579 | case SK_PassByIndirectRestore: |
| 5580 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5581 | CurInit = S.Owned(new (S.Context) |
| 5582 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5583 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5584 | break; |
| 5585 | |
| 5586 | case SK_ProduceObjCObject: |
| 5587 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5588 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5589 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5590 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5591 | |
| 5592 | case SK_StdInitializerList: { |
| 5593 | QualType Dest = Step->Type; |
| 5594 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 5595 | bool Success = S.isStdInitializerList(Dest.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5596 | (void)Success; |
| 5597 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5598 | |
| 5599 | // If the element type has a destructor, check it. |
| 5600 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5601 | if (!RD->hasIrrelevantDestructor()) { |
| 5602 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5603 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5604 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5605 | S.PDiag(diag::err_access_dtor_temp) << E); |
| 5606 | S.DiagnoseUseOfDecl(Destructor, Kind.getLocation()); |
| 5607 | } |
| 5608 | } |
| 5609 | } |
| 5610 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5611 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5612 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5613 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5614 | unsigned NumInits = ILE->getNumInits(); |
| 5615 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5616 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5617 | S.Context.getConstantArrayType(E, |
| 5618 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5619 | NumInits), |
| 5620 | ArrayType::Normal, 0)); |
| 5621 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5622 | 0, HiddenArray); |
| 5623 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5624 | Element.setElementIndex(i); |
| 5625 | ExprResult Init = S.Owned(ILE->getInit(i)); |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5626 | ExprResult Res = S.PerformCopyInitialization( |
| 5627 | Element, Init.get()->getExprLoc(), Init, |
| 5628 | /*TopLevelOfInitList=*/ true); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5629 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5630 | Converted[i] = Res.take(); |
| 5631 | } |
| 5632 | InitListExpr *Semantic = new (S.Context) |
| 5633 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5634 | Converted, ILE->getRBraceLoc()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5635 | Semantic->setSyntacticForm(ILE); |
| 5636 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5637 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5638 | CurInit = S.Owned(Semantic); |
| 5639 | break; |
| 5640 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5641 | case SK_OCLSamplerInit: { |
| 5642 | assert(Step->Type->isSamplerT() && |
| 5643 | "Sampler initialization on non sampler type."); |
| 5644 | |
| 5645 | QualType SourceType = CurInit.get()->getType(); |
| 5646 | InitializedEntity::EntityKind EntityKind = Entity.getKind(); |
| 5647 | |
| 5648 | if (EntityKind == InitializedEntity::EK_Parameter) { |
| 5649 | if (!SourceType->isSamplerT()) |
| 5650 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 5651 | << SourceType; |
| 5652 | } else if (EntityKind != InitializedEntity::EK_Variable) { |
| 5653 | llvm_unreachable("Invalid EntityKind!"); |
| 5654 | } |
| 5655 | |
| 5656 | break; |
| 5657 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5658 | case SK_OCLZeroEvent: { |
| 5659 | assert(Step->Type->isEventT() && |
| 5660 | "Event initialization on non event type."); |
| 5661 | |
| 5662 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 5663 | CK_ZeroToOCLEvent, |
| 5664 | CurInit.get()->getValueKind()); |
| 5665 | break; |
| 5666 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5667 | } |
| 5668 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5669 | |
| 5670 | // Diagnose non-fatal problems with the completed initialization. |
| 5671 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5672 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5673 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5674 | cast<FieldDecl>(Entity.getDecl()), |
| 5675 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5677 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5680 | /// Somewhere within T there is an uninitialized reference subobject. |
| 5681 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5682 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 5683 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5684 | if (T->isReferenceType()) { |
| 5685 | S.Diag(Loc, diag::err_reference_without_init) |
| 5686 | << T.getNonReferenceType(); |
| 5687 | return true; |
| 5688 | } |
| 5689 | |
| 5690 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 5691 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 5692 | return false; |
| 5693 | |
| 5694 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5695 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5696 | if (FI->isUnnamedBitfield()) |
| 5697 | continue; |
| 5698 | |
| 5699 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 5700 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5701 | return true; |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5706 | BE = RD->bases_end(); |
| 5707 | BI != BE; ++BI) { |
| 5708 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 5709 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5710 | return true; |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | return false; |
| 5715 | } |
| 5716 | |
| 5717 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5718 | //===----------------------------------------------------------------------===// |
| 5719 | // Diagnose initialization failures |
| 5720 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5721 | |
| 5722 | /// Emit notes associated with an initialization that failed due to a |
| 5723 | /// "simple" conversion failure. |
| 5724 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 5725 | Expr *op) { |
| 5726 | QualType destType = entity.getType(); |
| 5727 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5728 | op->getType()->isObjCObjectPointerType()) { |
| 5729 | |
| 5730 | // Emit a possible note about the conversion failing because the |
| 5731 | // operand is a message send with a related result type. |
| 5732 | S.EmitRelatedResultTypeNote(op); |
| 5733 | |
| 5734 | // Emit a possible note about a return failing because we're |
| 5735 | // expecting a related result type. |
| 5736 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 5737 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 5738 | } |
| 5739 | } |
| 5740 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5742 | const InitializedEntity &Entity, |
| 5743 | const InitializationKind &Kind, |
| 5744 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5745 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5746 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5747 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5748 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5749 | switch (Failure) { |
| 5750 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5751 | // FIXME: Customize for the initialized entity? |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5752 | if (NumArgs == 0) { |
| 5753 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 5754 | // If this is value-initialization, this could be nested some way within |
| 5755 | // the target type. |
| 5756 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 5757 | DestType->isReferenceType()); |
| 5758 | bool Diagnosed = |
| 5759 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 5760 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 5761 | (void)Diagnosed; |
| 5762 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5763 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5764 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5765 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5767 | case FK_ArrayNeedsInitList: |
| 5768 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5769 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5770 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5771 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5772 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5773 | case FK_ArrayTypeMismatch: |
| 5774 | case FK_NonConstantArrayInit: |
| 5775 | S.Diag(Kind.getLocation(), |
| 5776 | (Failure == FK_ArrayTypeMismatch |
| 5777 | ? diag::err_array_init_different_type |
| 5778 | : diag::err_array_init_non_constant_array)) |
| 5779 | << DestType.getNonReferenceType() |
| 5780 | << Args[0]->getType() |
| 5781 | << Args[0]->getSourceRange(); |
| 5782 | break; |
| 5783 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5784 | case FK_VariableLengthArrayHasInitializer: |
| 5785 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5786 | << Args[0]->getSourceRange(); |
| 5787 | break; |
| 5788 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5789 | case FK_AddressOfOverloadFailed: { |
| 5790 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5791 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5792 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5793 | true, |
| 5794 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5795 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5796 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5798 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5799 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5800 | switch (FailedOverloadResult) { |
| 5801 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5802 | if (Failure == FK_UserConversionOverloadFailed) |
| 5803 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5804 | << Args[0]->getType() << DestType |
| 5805 | << Args[0]->getSourceRange(); |
| 5806 | else |
| 5807 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5808 | << DestType << Args[0]->getType() |
| 5809 | << Args[0]->getSourceRange(); |
| 5810 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5811 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5812 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5813 | break; |
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 OR_No_Viable_Function: |
| 5816 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5817 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5818 | << Args[0]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5819 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5820 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5821 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5823 | case OR_Deleted: { |
| 5824 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5825 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5826 | << Args[0]->getSourceRange(); |
| 5827 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5828 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5829 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5830 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5831 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5832 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5833 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5834 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5835 | } |
| 5836 | break; |
| 5837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5838 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5839 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5840 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5841 | } |
| 5842 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5843 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5844 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5845 | if (isa<InitListExpr>(Args[0])) { |
| 5846 | S.Diag(Kind.getLocation(), |
| 5847 | diag::err_lvalue_reference_bind_to_initlist) |
| 5848 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5849 | << DestType.getNonReferenceType() |
| 5850 | << Args[0]->getSourceRange(); |
| 5851 | break; |
| 5852 | } |
| 5853 | // Intentional fallthrough |
| 5854 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5855 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5857 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5858 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5859 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5860 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5861 | << DestType.getNonReferenceType() |
| 5862 | << Args[0]->getType() |
| 5863 | << Args[0]->getSourceRange(); |
| 5864 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5866 | case FK_RValueReferenceBindingToLValue: |
| 5867 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5868 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5869 | << Args[0]->getSourceRange(); |
| 5870 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5872 | case FK_ReferenceInitDropsQualifiers: |
| 5873 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5874 | << DestType.getNonReferenceType() |
| 5875 | << Args[0]->getType() |
| 5876 | << Args[0]->getSourceRange(); |
| 5877 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5878 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5879 | case FK_ReferenceInitFailed: |
| 5880 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5881 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5882 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5883 | << Args[0]->getType() |
| 5884 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5885 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5886 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5888 | case FK_ConversionFailed: { |
| 5889 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5890 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5891 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5892 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5893 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5894 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5895 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5896 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5897 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5898 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5899 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5900 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5901 | |
| 5902 | case FK_ConversionFromPropertyFailed: |
| 5903 | // No-op. This error has already been reported. |
| 5904 | break; |
| 5905 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5906 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5907 | SourceRange R; |
| 5908 | |
| 5909 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5910 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5911 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5912 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5913 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5915 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5916 | if (Kind.isCStyleOrFunctionalCast()) |
| 5917 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5918 | << R; |
| 5919 | else |
| 5920 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5921 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5922 | break; |
| 5923 | } |
| 5924 | |
| 5925 | case FK_ReferenceBindingToInitList: |
| 5926 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5927 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5928 | break; |
| 5929 | |
| 5930 | case FK_InitListBadDestinationType: |
| 5931 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5932 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5933 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5934 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5935 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5936 | case FK_ConstructorOverloadFailed: { |
| 5937 | SourceRange ArgsRange; |
| 5938 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5940 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5942 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 5943 | assert(NumArgs == 1 && "List construction from other than 1 argument."); |
| 5944 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 5945 | Args = InitList->getInits(); |
| 5946 | NumArgs = InitList->getNumInits(); |
| 5947 | } |
| 5948 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5949 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5950 | // bad. |
| 5951 | switch (FailedOverloadResult) { |
| 5952 | case OR_Ambiguous: |
| 5953 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5954 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5955 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5956 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5957 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5958 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5959 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5960 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5961 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5962 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5963 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5964 | // This is implicit default initialization of a member or |
| 5965 | // base within a constructor. If no viable function was |
| 5966 | // found, notify the user that she needs to explicitly |
| 5967 | // initialize this base/member. |
| 5968 | CXXConstructorDecl *Constructor |
| 5969 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5970 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5971 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5972 | << (Constructor->getInheritedConstructor() ? 2 : |
| 5973 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5974 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5975 | << /*base=*/0 |
| 5976 | << Entity.getType(); |
| 5977 | |
| 5978 | RecordDecl *BaseDecl |
| 5979 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5980 | ->getDecl(); |
| 5981 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5982 | << S.Context.getTagDeclType(BaseDecl); |
| 5983 | } else { |
| 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 | << /*member=*/1 |
| 5989 | << Entity.getName(); |
| 5990 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5991 | |
| 5992 | if (const RecordType *Record |
| 5993 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5994 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5995 | diag::note_previous_decl) |
| 5996 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5997 | } |
| 5998 | break; |
| 5999 | } |
| 6000 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6001 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6002 | << DestType << ArgsRange; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6003 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 6004 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6005 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6007 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6008 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6009 | OverloadingResult Ovl |
| 6010 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6011 | if (Ovl != OR_Deleted) { |
| 6012 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6013 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6014 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6015 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6016 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6017 | |
| 6018 | // If this is a defaulted or implicitly-declared function, then |
| 6019 | // it was implicitly deleted. Make it clear that the deletion was |
| 6020 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6021 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6022 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6023 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6024 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6025 | else |
| 6026 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6027 | << true << DestType << ArgsRange; |
| 6028 | |
| 6029 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6030 | break; |
| 6031 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6032 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6033 | case OR_Success: |
| 6034 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6035 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6036 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6037 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6039 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6040 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6041 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6042 | // This is implicit default-initialization of a const member in |
| 6043 | // a constructor. Complain that it needs to be explicitly |
| 6044 | // initialized. |
| 6045 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6046 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6047 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6048 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6049 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6050 | << /*const=*/1 |
| 6051 | << Entity.getName(); |
| 6052 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6053 | << Entity.getName(); |
| 6054 | } else { |
| 6055 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6056 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6057 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6058 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6059 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6060 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6061 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6062 | diag::err_init_incomplete_type); |
| 6063 | break; |
| 6064 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6065 | case FK_ListInitializationFailed: { |
| 6066 | // Run the init list checker again to emit diagnostics. |
| 6067 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6068 | QualType DestType = Entity.getType(); |
| 6069 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 6070 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6071 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6072 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6073 | assert(DiagnoseInitList.HadError() && |
| 6074 | "Inconsistent init list check result."); |
| 6075 | break; |
| 6076 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6077 | |
| 6078 | case FK_PlaceholderType: { |
| 6079 | // FIXME: Already diagnosed! |
| 6080 | break; |
| 6081 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6082 | |
| 6083 | case FK_InitListElementCopyFailure: { |
| 6084 | // Try to perform all copies again. |
| 6085 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6086 | unsigned NumInits = InitList->getNumInits(); |
| 6087 | QualType DestType = Entity.getType(); |
| 6088 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 6089 | bool Success = S.isStdInitializerList(DestType.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6090 | (void)Success; |
| 6091 | assert(Success && "Where did the std::initializer_list go?"); |
| 6092 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 6093 | S.Context.getConstantArrayType(E, |
| 6094 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6095 | NumInits), |
| 6096 | ArrayType::Normal, 0)); |
| 6097 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 6098 | 0, HiddenArray); |
| 6099 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 6100 | // where the init list type is wrong, e.g. |
| 6101 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 6102 | // FIXME: Emit a note if we hit the limit? |
| 6103 | int ErrorCount = 0; |
| 6104 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 6105 | Element.setElementIndex(i); |
| 6106 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 6107 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 6108 | .isInvalid()) |
| 6109 | ++ErrorCount; |
| 6110 | } |
| 6111 | break; |
| 6112 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6113 | |
| 6114 | case FK_ExplicitConstructor: { |
| 6115 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6116 | << Args[0]->getSourceRange(); |
| 6117 | OverloadCandidateSet::iterator Best; |
| 6118 | OverloadingResult Ovl |
| 6119 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6120 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6121 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6122 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6123 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6124 | break; |
| 6125 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6126 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6127 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6128 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6129 | return true; |
| 6130 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6131 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6132 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6133 | switch (SequenceKind) { |
| 6134 | case FailedSequence: { |
| 6135 | OS << "Failed sequence: "; |
| 6136 | switch (Failure) { |
| 6137 | case FK_TooManyInitsForReference: |
| 6138 | OS << "too many initializers for reference"; |
| 6139 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6140 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6141 | case FK_ArrayNeedsInitList: |
| 6142 | OS << "array requires initializer list"; |
| 6143 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6145 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6146 | OS << "array requires initializer list or string literal"; |
| 6147 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6148 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6149 | case FK_ArrayTypeMismatch: |
| 6150 | OS << "array type mismatch"; |
| 6151 | break; |
| 6152 | |
| 6153 | case FK_NonConstantArrayInit: |
| 6154 | OS << "non-constant array initializer"; |
| 6155 | break; |
| 6156 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6157 | case FK_AddressOfOverloadFailed: |
| 6158 | OS << "address of overloaded function failed"; |
| 6159 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6161 | case FK_ReferenceInitOverloadFailed: |
| 6162 | OS << "overload resolution for reference initialization failed"; |
| 6163 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6164 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6165 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6166 | OS << "non-const lvalue reference bound to temporary"; |
| 6167 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6169 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6170 | OS << "non-const lvalue reference bound to unrelated type"; |
| 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_RValueReferenceBindingToLValue: |
| 6174 | OS << "rvalue reference bound to an lvalue"; |
| 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_ReferenceInitDropsQualifiers: |
| 6178 | OS << "reference initialization drops qualifiers"; |
| 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_ReferenceInitFailed: |
| 6182 | OS << "reference initialization failed"; |
| 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_ConversionFailed: |
| 6186 | OS << "conversion failed"; |
| 6187 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6189 | case FK_ConversionFromPropertyFailed: |
| 6190 | OS << "conversion from property failed"; |
| 6191 | break; |
| 6192 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6193 | case FK_TooManyInitsForScalar: |
| 6194 | OS << "too many initializers for scalar"; |
| 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_ReferenceBindingToInitList: |
| 6198 | OS << "referencing binding to initializer list"; |
| 6199 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6201 | case FK_InitListBadDestinationType: |
| 6202 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6203 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6205 | case FK_UserConversionOverloadFailed: |
| 6206 | OS << "overloading failed for user-defined conversion"; |
| 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_ConstructorOverloadFailed: |
| 6210 | OS << "constructor overloading failed"; |
| 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_DefaultInitOfConst: |
| 6214 | OS << "default initialization of a const variable"; |
| 6215 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6217 | case FK_Incomplete: |
| 6218 | OS << "initialization of incomplete type"; |
| 6219 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6220 | |
| 6221 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6222 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6223 | break; |
| 6224 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6225 | case FK_VariableLengthArrayHasInitializer: |
| 6226 | OS << "variable length array has an initializer"; |
| 6227 | break; |
| 6228 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6229 | case FK_PlaceholderType: |
| 6230 | OS << "initializer expression isn't contextually valid"; |
| 6231 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6232 | |
| 6233 | case FK_ListConstructorOverloadFailed: |
| 6234 | OS << "list constructor overloading failed"; |
| 6235 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6236 | |
| 6237 | case FK_InitListElementCopyFailure: |
| 6238 | OS << "copy construction of initializer list element failed"; |
| 6239 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6240 | |
| 6241 | case FK_ExplicitConstructor: |
| 6242 | OS << "list copy initialization chose explicit constructor"; |
| 6243 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6244 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6245 | OS << '\n'; |
| 6246 | return; |
| 6247 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6248 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6249 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6250 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6251 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6252 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6253 | case NormalSequence: |
| 6254 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6255 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6258 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6259 | if (S != step_begin()) { |
| 6260 | OS << " -> "; |
| 6261 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6262 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6263 | switch (S->Kind) { |
| 6264 | case SK_ResolveAddressOfOverloadedFunction: |
| 6265 | OS << "resolve address of overloaded function"; |
| 6266 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6267 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6268 | case SK_CastDerivedToBaseRValue: |
| 6269 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6270 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6272 | case SK_CastDerivedToBaseXValue: |
| 6273 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6274 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6275 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6276 | case SK_CastDerivedToBaseLValue: |
| 6277 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 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_BindReference: |
| 6281 | OS << "bind reference to lvalue"; |
| 6282 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6284 | case SK_BindReferenceToTemporary: |
| 6285 | OS << "bind reference to a temporary"; |
| 6286 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6288 | case SK_ExtraneousCopyToTemporary: |
| 6289 | OS << "extraneous C++03 copy to temporary"; |
| 6290 | break; |
| 6291 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6292 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6293 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6294 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6296 | case SK_QualificationConversionRValue: |
| 6297 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6298 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6299 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6300 | case SK_QualificationConversionXValue: |
| 6301 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6302 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6303 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6304 | case SK_QualificationConversionLValue: |
| 6305 | OS << "qualification conversion (lvalue)"; |
| 6306 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6307 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6308 | case SK_LValueToRValue: |
| 6309 | OS << "load (lvalue to rvalue)"; |
| 6310 | break; |
| 6311 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6312 | case SK_ConversionSequence: |
| 6313 | OS << "implicit conversion sequence ("; |
| 6314 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6315 | OS << ")"; |
| 6316 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6317 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6318 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6319 | OS << "list aggregate initialization"; |
| 6320 | break; |
| 6321 | |
| 6322 | case SK_ListConstructorCall: |
| 6323 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6324 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6325 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6326 | case SK_UnwrapInitList: |
| 6327 | OS << "unwrap reference initializer list"; |
| 6328 | break; |
| 6329 | |
| 6330 | case SK_RewrapInitList: |
| 6331 | OS << "rewrap reference initializer list"; |
| 6332 | break; |
| 6333 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6334 | case SK_ConstructorInitialization: |
| 6335 | OS << "constructor initialization"; |
| 6336 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6338 | case SK_ZeroInitialization: |
| 6339 | OS << "zero initialization"; |
| 6340 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6342 | case SK_CAssignment: |
| 6343 | OS << "C assignment"; |
| 6344 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6346 | case SK_StringInit: |
| 6347 | OS << "string initialization"; |
| 6348 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6349 | |
| 6350 | case SK_ObjCObjectConversion: |
| 6351 | OS << "Objective-C object conversion"; |
| 6352 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6353 | |
| 6354 | case SK_ArrayInit: |
| 6355 | OS << "array initialization"; |
| 6356 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6357 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6358 | case SK_ParenthesizedArrayInit: |
| 6359 | OS << "parenthesized array initialization"; |
| 6360 | break; |
| 6361 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6362 | case SK_PassByIndirectCopyRestore: |
| 6363 | OS << "pass by indirect copy and restore"; |
| 6364 | break; |
| 6365 | |
| 6366 | case SK_PassByIndirectRestore: |
| 6367 | OS << "pass by indirect restore"; |
| 6368 | break; |
| 6369 | |
| 6370 | case SK_ProduceObjCObject: |
| 6371 | OS << "Objective-C object retension"; |
| 6372 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6373 | |
| 6374 | case SK_StdInitializerList: |
| 6375 | OS << "std::initializer_list from initializer list"; |
| 6376 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6377 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6378 | case SK_OCLSamplerInit: |
| 6379 | OS << "OpenCL sampler_t from integer constant"; |
| 6380 | break; |
| 6381 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6382 | case SK_OCLZeroEvent: |
| 6383 | OS << "OpenCL event_t from zero"; |
| 6384 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6385 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6386 | |
| 6387 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6388 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6389 | |
| 6390 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
| 6393 | void InitializationSequence::dump() const { |
| 6394 | dump(llvm::errs()); |
| 6395 | } |
| 6396 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6397 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6398 | QualType EntityType, |
| 6399 | const Expr *PreInit, |
| 6400 | const Expr *PostInit) { |
| 6401 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6402 | return; |
| 6403 | |
| 6404 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6405 | // an initialization sequence. |
| 6406 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6407 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6408 | return; |
| 6409 | |
| 6410 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6411 | const StandardConversionSequence *SCS = 0; |
| 6412 | switch (ICS.getKind()) { |
| 6413 | case ImplicitConversionSequence::StandardConversion: |
| 6414 | SCS = &ICS.Standard; |
| 6415 | break; |
| 6416 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6417 | SCS = &ICS.UserDefined.After; |
| 6418 | break; |
| 6419 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6420 | case ImplicitConversionSequence::EllipsisConversion: |
| 6421 | case ImplicitConversionSequence::BadConversion: |
| 6422 | return; |
| 6423 | } |
| 6424 | |
| 6425 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6426 | // operator was used, this may be different from both the type of the entity |
| 6427 | // and of the pre-initialization expression. |
| 6428 | QualType PreNarrowingType = PreInit->getType(); |
| 6429 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6430 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6431 | |
| 6432 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6433 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6434 | QualType ConstantType; |
| 6435 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6436 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6437 | case NK_Not_Narrowing: |
| 6438 | // No narrowing occurred. |
| 6439 | return; |
| 6440 | |
| 6441 | case NK_Type_Narrowing: |
| 6442 | // This was a floating-to-integer conversion, which is always considered a |
| 6443 | // narrowing conversion even if the value is a constant and can be |
| 6444 | // represented exactly as an integer. |
| 6445 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6446 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6447 | diag::warn_init_list_type_narrowing |
| 6448 | : S.isSFINAEContext()? |
| 6449 | diag::err_init_list_type_narrowing_sfinae |
| 6450 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6451 | << PostInit->getSourceRange() |
| 6452 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6453 | << EntityType.getLocalUnqualifiedType(); |
| 6454 | break; |
| 6455 | |
| 6456 | case NK_Constant_Narrowing: |
| 6457 | // A constant value was narrowed. |
| 6458 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6459 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6460 | diag::warn_init_list_constant_narrowing |
| 6461 | : S.isSFINAEContext()? |
| 6462 | diag::err_init_list_constant_narrowing_sfinae |
| 6463 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6464 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6465 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6466 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6467 | break; |
| 6468 | |
| 6469 | case NK_Variable_Narrowing: |
| 6470 | // A variable's value may have been narrowed. |
| 6471 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6472 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6473 | diag::warn_init_list_variable_narrowing |
| 6474 | : S.isSFINAEContext()? |
| 6475 | diag::err_init_list_variable_narrowing_sfinae |
| 6476 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6477 | << PostInit->getSourceRange() |
| 6478 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6479 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6480 | break; |
| 6481 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6482 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6483 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6484 | llvm::raw_svector_ostream OS(StaticCast); |
| 6485 | OS << "static_cast<"; |
| 6486 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6487 | // It's important to use the typedef's name if there is one so that the |
| 6488 | // fixit doesn't break code using types like int64_t. |
| 6489 | // |
| 6490 | // FIXME: This will break if the typedef requires qualification. But |
| 6491 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6492 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6493 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6494 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6495 | else { |
| 6496 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6497 | // with a broken cast. |
| 6498 | return; |
| 6499 | } |
| 6500 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6501 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6502 | << PostInit->getSourceRange() |
| 6503 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6504 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6505 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6508 | //===----------------------------------------------------------------------===// |
| 6509 | // Initialization helper functions |
| 6510 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6511 | bool |
| 6512 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6513 | ExprResult Init) { |
| 6514 | if (Init.isInvalid()) |
| 6515 | return false; |
| 6516 | |
| 6517 | Expr *InitE = Init.get(); |
| 6518 | assert(InitE && "No initialization expression"); |
| 6519 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6520 | InitializationKind Kind |
| 6521 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6522 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6523 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6526 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6527 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6528 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6529 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6530 | bool TopLevelOfInitList, |
| 6531 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6532 | if (Init.isInvalid()) |
| 6533 | return ExprError(); |
| 6534 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6535 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6536 | assert(InitE && "No initialization expression?"); |
| 6537 | |
| 6538 | if (EqualLoc.isInvalid()) |
| 6539 | EqualLoc = InitE->getLocStart(); |
| 6540 | |
| 6541 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6542 | EqualLoc, |
| 6543 | AllowExplicit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6544 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 6545 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6546 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6547 | ExprResult Result = Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
| 6548 | |
| 6549 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6550 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6551 | InitE, Result.get()); |
| 6552 | |
| 6553 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6554 | } |