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 | // |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
| 13 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Designator.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 27 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 28 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Sema Initialization Checking |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 34 | static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) { |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 35 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
| 36 | if (!AT) return 0; |
| 37 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 38 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 39 | return 0; |
| 40 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // See if this is a string literal or @encode. |
| 42 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 44 | // Handle @encode, which is a narrow string. |
| 45 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 46 | return Init; |
| 47 | |
| 48 | // Otherwise we can only handle string literals. |
| 49 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 50 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 51 | |
| 52 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 53 | // char array can be initialized with a narrow string. |
| 54 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 55 | if (!SL->isWide()) |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 57 | |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 59 | // correction from DR343): "An array with element type compatible with a |
| 60 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 61 | // string literal, optionally enclosed in braces." |
| 62 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 63 | ElemTy.getUnqualifiedType())) |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | return Init; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 69 | static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) { |
| 70 | // Get the length of the string as parsed. |
| 71 | uint64_t StrLength = |
| 72 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 75 | const ArrayType *AT = S.Context.getAsArrayType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | // 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] | 78 | // being initialized to a string literal. |
| 79 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 80 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 81 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 82 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 83 | ConstVal, |
| 84 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 85 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 88 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 90 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 91 | // the size may be smaller or larger than the string we are initializing. |
| 92 | // FIXME: Avoid truncation for 64-bit length strings. |
| 93 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 94 | S.Diag(Str->getSourceRange().getBegin(), |
| 95 | diag::warn_initializer_string_for_char_array_too_long) |
| 96 | << Str->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 98 | // Set the type to the actual size that we are initializing. If we have |
| 99 | // something like: |
| 100 | // char x[1] = "foo"; |
| 101 | // then this will set the string literal's type to char[1]. |
| 102 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
| 106 | // Semantic checking for initializer lists. |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 109 | /// @brief Semantic checking for initializer lists. |
| 110 | /// |
| 111 | /// The InitListChecker class contains a set of routines that each |
| 112 | /// handle the initialization of a certain kind of entity, e.g., |
| 113 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 114 | /// InitListChecker itself performs a recursive walk of the subobject |
| 115 | /// structure of the type to be initialized, while stepping through |
| 116 | /// the initializer list one element at a time. The IList and Index |
| 117 | /// parameters to each of the Check* routines contain the active |
| 118 | /// (syntactic) initializer list and the index into that initializer |
| 119 | /// list that represents the current initializer. Each routine is |
| 120 | /// responsible for moving that Index forward as it consumes elements. |
| 121 | /// |
| 122 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 123 | /// arguments, which contains the current the "structured" (semantic) |
| 124 | /// initializer list and the index into that initializer list where we |
| 125 | /// are copying initializers as we map them over to the semantic |
| 126 | /// list. Once we have completed our recursive walk of the subobject |
| 127 | /// structure, we will have constructed a full semantic initializer |
| 128 | /// list. |
| 129 | /// |
| 130 | /// C99 designators cause changes in the initializer list traversal, |
| 131 | /// because they make the initialization "jump" into a specific |
| 132 | /// subobject and then continue the initialization from that |
| 133 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 134 | /// designated subobject and manages backing out the recursion to |
| 135 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 136 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 137 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 138 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 139 | bool hadError; |
| 140 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 141 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 143 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 144 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 145 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 146 | unsigned &StructuredIndex, |
| 147 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 148 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 149 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 150 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 151 | unsigned &StructuredIndex, |
| 152 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 153 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 154 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 156 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 157 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 158 | unsigned &StructuredIndex, |
| 159 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 160 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 161 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 162 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 163 | InitListExpr *StructuredList, |
| 164 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 165 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 166 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 167 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 168 | InitListExpr *StructuredList, |
| 169 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 170 | void CheckReferenceType(const InitializedEntity &Entity, |
| 171 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 172 | unsigned &Index, |
| 173 | InitListExpr *StructuredList, |
| 174 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 175 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 176 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 177 | InitListExpr *StructuredList, |
| 178 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 179 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 180 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 182 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 184 | unsigned &StructuredIndex, |
| 185 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
| 191 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 192 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 193 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 194 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 196 | RecordDecl::field_iterator *NextField, |
| 197 | llvm::APSInt *NextElementIndex, |
| 198 | unsigned &Index, |
| 199 | InitListExpr *StructuredList, |
| 200 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 201 | bool FinishSubobjectInit, |
| 202 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 203 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 204 | QualType CurrentObjectType, |
| 205 | InitListExpr *StructuredList, |
| 206 | unsigned StructuredIndex, |
| 207 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 208 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 209 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 210 | Expr *expr); |
| 211 | int numArrayElements(QualType DeclType); |
| 212 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 214 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 215 | const InitializedEntity &ParentEntity, |
| 216 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 217 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 218 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 220 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 221 | InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool HadError() { return hadError; } |
| 223 | |
| 224 | // @brief Retrieves the fully-structured initializer list used for |
| 225 | // semantic analysis and code generation. |
| 226 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 227 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 228 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 230 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 231 | const InitializedEntity &ParentEntity, |
| 232 | InitListExpr *ILE, |
| 233 | bool &RequiresSecondPass) { |
| 234 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 235 | unsigned NumInits = ILE->getNumInits(); |
| 236 | InitializedEntity MemberEntity |
| 237 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 238 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 239 | // FIXME: We probably don't need to handle references |
| 240 | // specially here, since value-initialization of references is |
| 241 | // handled in InitializationSequence. |
| 242 | if (Field->getType()->isReferenceType()) { |
| 243 | // C++ [dcl.init.aggr]p9: |
| 244 | // If an incomplete or empty initializer-list leaves a |
| 245 | // member of reference type uninitialized, the program is |
| 246 | // ill-formed. |
| 247 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 248 | << Field->getType() |
| 249 | << ILE->getSyntacticForm()->getSourceRange(); |
| 250 | SemaRef.Diag(Field->getLocation(), |
| 251 | diag::note_uninit_reference_member); |
| 252 | hadError = true; |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 257 | true); |
| 258 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 259 | if (!InitSeq) { |
| 260 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 261 | hadError = true; |
| 262 | return; |
| 263 | } |
| 264 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 265 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 266 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 267 | if (MemberInit.isInvalid()) { |
| 268 | hadError = true; |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (hadError) { |
| 273 | // Do nothing |
| 274 | } else if (Init < NumInits) { |
| 275 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
| 276 | } else if (InitSeq.getKind() |
| 277 | == InitializationSequence::ConstructorInitialization) { |
| 278 | // Value-initialization requires a constructor call, so |
| 279 | // extend the initializer list to include the constructor |
| 280 | // call and make a note that we'll need to take another pass |
| 281 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 282 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 283 | RequiresSecondPass = true; |
| 284 | } |
| 285 | } else if (InitListExpr *InnerILE |
| 286 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 287 | FillInValueInitializations(MemberEntity, InnerILE, |
| 288 | RequiresSecondPass); |
| 289 | } |
| 290 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 291 | /// Recursively replaces NULL values within the given initializer list |
| 292 | /// with expressions that perform value-initialization of the |
| 293 | /// appropriate type. |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 294 | void |
| 295 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 296 | InitListExpr *ILE, |
| 297 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 299 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 300 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 301 | if (ILE->getSyntacticForm()) |
| 302 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 304 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 305 | if (RType->getDecl()->isUnion() && |
| 306 | ILE->getInitializedFieldInUnion()) |
| 307 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 308 | Entity, ILE, RequiresSecondPass); |
| 309 | else { |
| 310 | unsigned Init = 0; |
| 311 | for (RecordDecl::field_iterator |
| 312 | Field = RType->getDecl()->field_begin(), |
| 313 | FieldEnd = RType->getDecl()->field_end(); |
| 314 | Field != FieldEnd; ++Field) { |
| 315 | if (Field->isUnnamedBitfield()) |
| 316 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 317 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 318 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 319 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 320 | |
| 321 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 322 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 323 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 325 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 326 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 327 | // Only look at the first initialization of a union. |
| 328 | if (RType->getDecl()->isUnion()) |
| 329 | break; |
| 330 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 335 | |
| 336 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 338 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 339 | unsigned NumInits = ILE->getNumInits(); |
| 340 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 341 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 342 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 343 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 344 | NumElements = CAType->getSize().getZExtValue(); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 345 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 346 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 347 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 348 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 349 | NumElements = VType->getNumElements(); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 350 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 351 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 353 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 356 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 357 | if (hadError) |
| 358 | return; |
| 359 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 360 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 361 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 362 | ElementEntity.setElementIndex(Init); |
| 363 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 364 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 365 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 366 | true); |
| 367 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 368 | if (!InitSeq) { |
| 369 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 370 | hadError = true; |
| 371 | return; |
| 372 | } |
| 373 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 374 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 375 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 376 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 377 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (hadError) { |
| 382 | // Do nothing |
| 383 | } else if (Init < NumInits) { |
| 384 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
| 385 | } else if (InitSeq.getKind() |
| 386 | == InitializationSequence::ConstructorInitialization) { |
| 387 | // Value-initialization requires a constructor call, so |
| 388 | // extend the initializer list to include the constructor |
| 389 | // call and make a note that we'll need to take another pass |
| 390 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 391 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 392 | RequiresSecondPass = true; |
| 393 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 394 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 395 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 396 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 400 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 401 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 402 | InitListExpr *IL, QualType &T) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 403 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 404 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 405 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 406 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 407 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 409 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 410 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 411 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 412 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 414 | if (!hadError) { |
| 415 | bool RequiresSecondPass = false; |
| 416 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 417 | if (RequiresSecondPass && !hadError) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 418 | FillInValueInitializations(Entity, FullyStructuredList, |
| 419 | RequiresSecondPass); |
| 420 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 424 | // FIXME: use a proper constant |
| 425 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 426 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 427 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 428 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 429 | } |
| 430 | return maxElements; |
| 431 | } |
| 432 | |
| 433 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 434 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 435 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 437 | Field = structDecl->field_begin(), |
| 438 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 439 | Field != FieldEnd; ++Field) { |
| 440 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 441 | ++InitializableMembers; |
| 442 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 443 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 444 | return std::min(InitializableMembers, 1); |
| 445 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 448 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 449 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 450 | QualType T, unsigned &Index, |
| 451 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 452 | unsigned &StructuredIndex, |
| 453 | bool TopLevelObject) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 454 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 456 | if (T->isArrayType()) |
| 457 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 458 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 459 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 460 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 461 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 462 | else |
| 463 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 464 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 465 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 466 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 467 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 469 | hadError = true; |
| 470 | return; |
| 471 | } |
| 472 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 473 | // Build a structured initializer list corresponding to this subobject. |
| 474 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 476 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 477 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 478 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 479 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 481 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 482 | unsigned StartIndex = Index; |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 483 | CheckListElementTypes(Entity, ParentIList, T, |
| 484 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | StructuredSubobjectInitList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 486 | StructuredSubobjectInitIndex, |
| 487 | TopLevelObject); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 488 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 489 | StructuredSubobjectInitList->setType(T); |
| 490 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 491 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 492 | // range corresponds with the end of the last initializer it used. |
| 493 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 495 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 496 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 497 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 498 | |
| 499 | // Warn about missing braces. |
| 500 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 47f164e | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 501 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 502 | diag::warn_missing_braces) |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 503 | << StructuredSubobjectInitList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 504 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
| 505 | "{") |
| 506 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
Tanya Lattner | 1dcd061 | 2010-03-07 04:47:12 +0000 | [diff] [blame] | 507 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 508 | "}"); |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 509 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 512 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 513 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 514 | unsigned &Index, |
| 515 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 516 | unsigned &StructuredIndex, |
| 517 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 518 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 519 | SyntacticToSemantic[IList] = StructuredList; |
| 520 | StructuredList->setSyntacticForm(IList); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 521 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
| 522 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 523 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 524 | IList->setType(ExprTy); |
| 525 | StructuredList->setType(ExprTy); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 526 | if (hadError) |
| 527 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 528 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 529 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 530 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 531 | if (StructuredIndex == 1 && |
| 532 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 533 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 534 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 535 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 536 | hadError = true; |
| 537 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 538 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 539 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 540 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 541 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 542 | // Don't complain for incomplete types, since we'll get an error |
| 543 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 544 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 546 | CurrentObjectType->isArrayType()? 0 : |
| 547 | CurrentObjectType->isVectorType()? 1 : |
| 548 | CurrentObjectType->isScalarType()? 2 : |
| 549 | CurrentObjectType->isUnionType()? 3 : |
| 550 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 551 | |
| 552 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 553 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 554 | DK = diag::err_excess_initializers; |
| 555 | hadError = true; |
| 556 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 557 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 558 | DK = diag::err_excess_initializers; |
| 559 | hadError = true; |
| 560 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 562 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 563 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 566 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 567 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 568 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 569 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 570 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 571 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 574 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 575 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 577 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 578 | unsigned &Index, |
| 579 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 580 | unsigned &StructuredIndex, |
| 581 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 582 | if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 583 | CheckScalarType(Entity, IList, DeclType, Index, |
| 584 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 585 | } else if (DeclType->isVectorType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 586 | CheckVectorType(Entity, IList, DeclType, Index, |
| 587 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 588 | } else if (DeclType->isAggregateType()) { |
| 589 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 590 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 591 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 592 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 593 | StructuredList, StructuredIndex, |
| 594 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 595 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 596 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 597 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 598 | false); |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 599 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 600 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 601 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 602 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 603 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 604 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 605 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 606 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 607 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 608 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 609 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 610 | } else if (DeclType->isRecordType()) { |
| 611 | // C++ [dcl.init]p14: |
| 612 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 613 | // is a brace-enclosed list, see 8.5.1. |
| 614 | // |
| 615 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 616 | // we have an initializer list and a destination type that is not |
| 617 | // an aggregate. |
| 618 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 619 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 620 | << DeclType << IList->getSourceRange(); |
| 621 | hadError = true; |
| 622 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 623 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 624 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 625 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 626 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 627 | << DeclType; |
| 628 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 629 | } else { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 630 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 631 | << DeclType; |
| 632 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 636 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 637 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 639 | unsigned &Index, |
| 640 | InitListExpr *StructuredList, |
| 641 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 642 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 643 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 644 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 645 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 647 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 648 | StructuredList, StructuredIndex, |
| 649 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 650 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 651 | newStructuredList, newStructuredIndex); |
| 652 | ++StructuredIndex; |
| 653 | ++Index; |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 654 | } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) { |
| 655 | CheckStringInit(Str, ElemType, SemaRef); |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 656 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 657 | ++Index; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 658 | } else if (ElemType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 659 | CheckScalarType(Entity, IList, ElemType, Index, |
| 660 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 661 | } else if (ElemType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 662 | CheckReferenceType(Entity, IList, ElemType, Index, |
| 663 | StructuredList, StructuredIndex); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 664 | } else { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 665 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 666 | // C++ [dcl.init.aggr]p12: |
| 667 | // All implicit type conversions (clause 4) are considered when |
| 668 | // initializing the aggregate member with an ini- tializer from |
| 669 | // an initializer-list. If the initializer can initialize a |
| 670 | // member, the member is initialized. [...] |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 671 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 672 | // FIXME: Better EqualLoc? |
| 673 | InitializationKind Kind = |
| 674 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 675 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 676 | |
| 677 | if (Seq) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 678 | ExprResult Result = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 679 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 680 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 681 | hadError = true; |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 682 | |
| 683 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 684 | Result.takeAs<Expr>()); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 685 | ++Index; |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Fall through for subaggregate initialization |
| 690 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | // C99 6.7.8p13: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 692 | // |
| 693 | // The initializer for a structure or union object that has |
| 694 | // automatic storage duration shall be either an initializer |
| 695 | // list as described below, or a single expression that has |
| 696 | // compatible structure or union type. In the latter case, the |
| 697 | // initial value of the object, including unnamed members, is |
| 698 | // that of the expression. |
Eli Friedman | 6b5374f | 2009-06-13 10:38:46 +0000 | [diff] [blame] | 699 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 700 | SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) { |
John McCall | 74e40b7 | 2010-12-04 09:03:57 +0000 | [diff] [blame^] | 701 | SemaRef.DefaultFunctionArrayLvalueConversion(expr); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 702 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 703 | ++Index; |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | // Fall through for subaggregate initialization |
| 708 | } |
| 709 | |
| 710 | // C++ [dcl.init.aggr]p12: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | // |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 712 | // [...] Otherwise, if the member is itself a non-empty |
| 713 | // subaggregate, brace elision is assumed and the initializer is |
| 714 | // considered for the initialization of the first member of |
| 715 | // the subaggregate. |
| 716 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 717 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 718 | StructuredIndex); |
| 719 | ++StructuredIndex; |
| 720 | } else { |
| 721 | // We cannot initialize this element, so let |
| 722 | // PerformCopyInitialization produce the appropriate diagnostic. |
Anders Carlsson | ca755fe | 2010-01-30 01:56:32 +0000 | [diff] [blame] | 723 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 724 | SemaRef.Owned(expr)); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 725 | hadError = true; |
| 726 | ++Index; |
| 727 | ++StructuredIndex; |
| 728 | } |
| 729 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 732 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 733 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 734 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 735 | InitListExpr *StructuredList, |
| 736 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 737 | if (Index >= IList->getNumInits()) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 738 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 739 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 740 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 741 | ++Index; |
| 742 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 743 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 744 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 745 | |
| 746 | Expr *expr = IList->getInit(Index); |
| 747 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 748 | SemaRef.Diag(SubIList->getLocStart(), |
| 749 | diag::warn_many_braces_around_scalar_init) |
| 750 | << SubIList->getSourceRange(); |
| 751 | |
| 752 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 753 | StructuredIndex); |
| 754 | return; |
| 755 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 756 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 757 | diag::err_designator_for_scalar_init) |
| 758 | << DeclType << expr->getSourceRange(); |
| 759 | hadError = true; |
| 760 | ++Index; |
| 761 | ++StructuredIndex; |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | ExprResult Result = |
| 766 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 767 | SemaRef.Owned(expr)); |
| 768 | |
| 769 | Expr *ResultExpr = 0; |
| 770 | |
| 771 | if (Result.isInvalid()) |
| 772 | hadError = true; // types weren't compatible. |
| 773 | else { |
| 774 | ResultExpr = Result.takeAs<Expr>(); |
| 775 | |
| 776 | if (ResultExpr != expr) { |
| 777 | // The type was promoted, update initializer list. |
| 778 | IList->setInit(Index, ResultExpr); |
| 779 | } |
| 780 | } |
| 781 | if (hadError) |
| 782 | ++StructuredIndex; |
| 783 | else |
| 784 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 785 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 788 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 789 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 790 | unsigned &Index, |
| 791 | InitListExpr *StructuredList, |
| 792 | unsigned &StructuredIndex) { |
| 793 | if (Index < IList->getNumInits()) { |
| 794 | Expr *expr = IList->getInit(Index); |
| 795 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 796 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 797 | << DeclType << IList->getSourceRange(); |
| 798 | hadError = true; |
| 799 | ++Index; |
| 800 | ++StructuredIndex; |
| 801 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 803 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 804 | ExprResult Result = |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 805 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 806 | SemaRef.Owned(expr)); |
| 807 | |
| 808 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 809 | hadError = true; |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 810 | |
| 811 | expr = Result.takeAs<Expr>(); |
| 812 | IList->setInit(Index, expr); |
| 813 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 814 | if (hadError) |
| 815 | ++StructuredIndex; |
| 816 | else |
| 817 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 818 | ++Index; |
| 819 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 820 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 821 | // general, it would be useful to pass location information down the stack, |
| 822 | // so that we know the location (or decl) of the "current object" being |
| 823 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 825 | diag::err_init_reference_member_uninitialized) |
| 826 | << DeclType |
| 827 | << IList->getSourceRange(); |
| 828 | hadError = true; |
| 829 | ++Index; |
| 830 | ++StructuredIndex; |
| 831 | return; |
| 832 | } |
| 833 | } |
| 834 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 835 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 836 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 837 | unsigned &Index, |
| 838 | InitListExpr *StructuredList, |
| 839 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 840 | if (Index >= IList->getNumInits()) |
| 841 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 843 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 844 | unsigned maxElements = VT->getNumElements(); |
| 845 | unsigned numEltsInit = 0; |
| 846 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 847 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 848 | if (!SemaRef.getLangOptions().OpenCL) { |
| 849 | // If the initializing element is a vector, try to copy-initialize |
| 850 | // instead of breaking it apart (which is doomed to failure anyway). |
| 851 | Expr *Init = IList->getInit(Index); |
| 852 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 853 | ExprResult Result = |
| 854 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
| 855 | SemaRef.Owned(Init)); |
| 856 | |
| 857 | Expr *ResultExpr = 0; |
| 858 | if (Result.isInvalid()) |
| 859 | hadError = true; // types weren't compatible. |
| 860 | else { |
| 861 | ResultExpr = Result.takeAs<Expr>(); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 862 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 863 | if (ResultExpr != Init) { |
| 864 | // The type was promoted, update initializer list. |
| 865 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 868 | if (hadError) |
| 869 | ++StructuredIndex; |
| 870 | else |
| 871 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 872 | ++Index; |
| 873 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 876 | InitializedEntity ElementEntity = |
| 877 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 878 | |
| 879 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 880 | // Don't attempt to go past the end of the init list |
| 881 | if (Index >= IList->getNumInits()) |
| 882 | break; |
| 883 | |
| 884 | ElementEntity.setElementIndex(Index); |
| 885 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 886 | StructuredList, StructuredIndex); |
| 887 | } |
| 888 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 889 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 890 | |
| 891 | InitializedEntity ElementEntity = |
| 892 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 893 | |
| 894 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 895 | for (unsigned i = 0; i < maxElements; ++i) { |
| 896 | // Don't attempt to go past the end of the init list |
| 897 | if (Index >= IList->getNumInits()) |
| 898 | break; |
| 899 | |
| 900 | ElementEntity.setElementIndex(Index); |
| 901 | |
| 902 | QualType IType = IList->getInit(Index)->getType(); |
| 903 | if (!IType->isVectorType()) { |
| 904 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 905 | StructuredList, StructuredIndex); |
| 906 | ++numEltsInit; |
| 907 | } else { |
| 908 | QualType VecType; |
| 909 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 910 | unsigned numIElts = IVT->getNumElements(); |
| 911 | |
| 912 | if (IType->isExtVectorType()) |
| 913 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 914 | else |
| 915 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 916 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 917 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 918 | StructuredList, StructuredIndex); |
| 919 | numEltsInit += numIElts; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | // OpenCL requires all elements to be initialized. |
| 924 | if (numEltsInit != maxElements) |
| 925 | if (SemaRef.getLangOptions().OpenCL) |
| 926 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 927 | diag::err_vector_incorrect_num_initializers) |
| 928 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 931 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 932 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 933 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 935 | unsigned &Index, |
| 936 | InitListExpr *StructuredList, |
| 937 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 938 | // Check for the special-case of initializing an array with a string. |
| 939 | if (Index < IList->getNumInits()) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 940 | if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType, |
| 941 | SemaRef.Context)) { |
| 942 | CheckStringInit(Str, DeclType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 943 | // We place the string literal directly into the resulting |
| 944 | // initializer list. This is the only place where the structure |
| 945 | // of the structured initializer list doesn't match exactly, |
| 946 | // because doing so would involve allocating one character |
| 947 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 948 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 949 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 950 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 954 | if (const VariableArrayType *VAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 955 | SemaRef.Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 956 | // Check for VLAs; in standard C it would be possible to check this |
| 957 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 958 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 959 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 960 | diag::err_variable_object_no_init) |
| 961 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 962 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 963 | ++Index; |
| 964 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 968 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 969 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 970 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 971 | bool maxElementsKnown = false; |
| 972 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 973 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 974 | maxElements = CAT->getSize(); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 975 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 976 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 977 | maxElementsKnown = true; |
| 978 | } |
| 979 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 980 | QualType elementType = SemaRef.Context.getAsArrayType(DeclType) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 981 | ->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 982 | while (Index < IList->getNumInits()) { |
| 983 | Expr *Init = IList->getInit(Index); |
| 984 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 985 | // If we're not the subobject that matches up with the '{' for |
| 986 | // the designator, we shouldn't be handling the |
| 987 | // designator. Return immediately. |
| 988 | if (!SubobjectIsDesignatorContext) |
| 989 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 991 | // Handle this designated initializer. elementIndex will be |
| 992 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 993 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 994 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 995 | StructuredList, StructuredIndex, true, |
| 996 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 997 | hadError = true; |
| 998 | continue; |
| 999 | } |
| 1000 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1001 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 1002 | maxElements.extend(elementIndex.getBitWidth()); |
| 1003 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 1004 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1005 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1007 | // If the array is of incomplete type, keep track of the number of |
| 1008 | // elements in the initializer. |
| 1009 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1010 | maxElements = elementIndex; |
| 1011 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1012 | continue; |
| 1013 | } |
| 1014 | |
| 1015 | // If we know the maximum number of elements, and we've already |
| 1016 | // hit it, stop consuming elements in the initializer list. |
| 1017 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1018 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1019 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1020 | InitializedEntity ElementEntity = |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1021 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1022 | Entity); |
| 1023 | // Check this element. |
| 1024 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1025 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1026 | ++elementIndex; |
| 1027 | |
| 1028 | // If the array is of incomplete type, keep track of the number of |
| 1029 | // elements in the initializer. |
| 1030 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1031 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1032 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1033 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1034 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1035 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1036 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1037 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1038 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1039 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1040 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1041 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1042 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1043 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1045 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1049 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1050 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1052 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1054 | unsigned &Index, |
| 1055 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1056 | unsigned &StructuredIndex, |
| 1057 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1058 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1060 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1061 | // confusion, we forgo checking the intializer for the entire record. |
| 1062 | if (structDecl->isInvalidDecl()) { |
| 1063 | hadError = true; |
| 1064 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1068 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1069 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1070 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1071 | Field != FieldEnd; ++Field) { |
| 1072 | if (Field->getDeclName()) { |
| 1073 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | return; |
| 1078 | } |
| 1079 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1080 | // If structDecl is a forward declaration, this loop won't do |
| 1081 | // anything except look at designated initializers; That's okay, |
| 1082 | // because an error should get printed out elsewhere. It might be |
| 1083 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1084 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1085 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1086 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1087 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1088 | while (Index < IList->getNumInits()) { |
| 1089 | Expr *Init = IList->getInit(Index); |
| 1090 | |
| 1091 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1092 | // If we're not the subobject that matches up with the '{' for |
| 1093 | // the designator, we shouldn't be handling the |
| 1094 | // designator. Return immediately. |
| 1095 | if (!SubobjectIsDesignatorContext) |
| 1096 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1098 | // Handle this designated initializer. Field will be updated to |
| 1099 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1100 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1101 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1102 | StructuredList, StructuredIndex, |
| 1103 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1104 | hadError = true; |
| 1105 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1106 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Disable check for missing fields when designators are used. |
| 1109 | // This matches gcc behaviour. |
| 1110 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1111 | continue; |
| 1112 | } |
| 1113 | |
| 1114 | if (Field == FieldEnd) { |
| 1115 | // We've run out of fields. We're done. |
| 1116 | break; |
| 1117 | } |
| 1118 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1119 | // We've already initialized a member of a union. We're done. |
| 1120 | if (InitializedSomething && DeclType->isUnionType()) |
| 1121 | break; |
| 1122 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1123 | // If we've hit the flexible array member at the end, we're done. |
| 1124 | if (Field->getType()->isIncompleteArrayType()) |
| 1125 | break; |
| 1126 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1127 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1128 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1129 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1130 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1131 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1132 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1133 | InitializedEntity MemberEntity = |
| 1134 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1135 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1136 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1137 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1138 | |
| 1139 | if (DeclType->isUnionType()) { |
| 1140 | // Initialize the first field within the union. |
| 1141 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1142 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1143 | |
| 1144 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1145 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1146 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1147 | // Emit warnings for missing struct field initializers. |
Douglas Gregor | 8e19890 | 2010-06-18 21:43:10 +0000 | [diff] [blame] | 1148 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1149 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1150 | // It is possible we have one or more unnamed bitfields remaining. |
| 1151 | // Find first (if any) named field and emit warning. |
| 1152 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1153 | it != end; ++it) { |
| 1154 | if (!it->isUnnamedBitfield()) { |
| 1155 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1156 | diag::warn_missing_field_initializers) << it->getName(); |
| 1157 | break; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1163 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | |
| 1166 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | if (!TopLevelObject && |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1168 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1169 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1171 | diag::err_flexible_array_init_nonempty) |
| 1172 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1173 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1174 | << *Field; |
| 1175 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1176 | ++Index; |
| 1177 | return; |
| 1178 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1180 | diag::ext_flexible_array_init) |
| 1181 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1182 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1183 | << *Field; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1186 | InitializedEntity MemberEntity = |
| 1187 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1188 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1189 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 1190 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1191 | StructuredList, StructuredIndex); |
| 1192 | else |
| 1193 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1194 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1195 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1196 | |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1197 | /// \brief Similar to Sema::BuildAnonymousStructUnionMemberPath() but builds a |
| 1198 | /// relative path and has strict checks. |
| 1199 | static void BuildRelativeAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 1200 | llvm::SmallVectorImpl<FieldDecl *> &Path, |
| 1201 | DeclContext *BaseDC) { |
| 1202 | Path.push_back(Field); |
| 1203 | for (DeclContext *Ctx = Field->getDeclContext(); |
| 1204 | !Ctx->Equals(BaseDC); |
| 1205 | Ctx = Ctx->getParent()) { |
| 1206 | ValueDecl *AnonObject = |
| 1207 | cast<RecordDecl>(Ctx)->getAnonymousStructOrUnionObject(); |
| 1208 | FieldDecl *AnonField = cast<FieldDecl>(AnonObject); |
| 1209 | Path.push_back(AnonField); |
| 1210 | } |
| 1211 | } |
| 1212 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1213 | /// \brief Expand a field designator that refers to a member of an |
| 1214 | /// anonymous struct or union into a series of field designators that |
| 1215 | /// refers to the field within the appropriate subobject. |
| 1216 | /// |
| 1217 | /// Field/FieldIndex will be updated to point to the (new) |
| 1218 | /// currently-designated field. |
| 1219 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | DesignatedInitExpr *DIE, |
| 1221 | unsigned DesigIdx, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1222 | FieldDecl *Field, |
| 1223 | RecordDecl::field_iterator &FieldIter, |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1224 | unsigned &FieldIndex, |
| 1225 | DeclContext *BaseDC) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1226 | typedef DesignatedInitExpr::Designator Designator; |
| 1227 | |
| 1228 | // Build the path from the current object to the member of the |
| 1229 | // anonymous struct/union (backwards). |
| 1230 | llvm::SmallVector<FieldDecl *, 4> Path; |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1231 | BuildRelativeAnonymousStructUnionMemberPath(Field, Path, BaseDC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1233 | // Build the replacement designators. |
| 1234 | llvm::SmallVector<Designator, 4> Replacements; |
| 1235 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 1236 | FI = Path.rbegin(), FIEnd = Path.rend(); |
| 1237 | FI != FIEnd; ++FI) { |
| 1238 | if (FI + 1 == FIEnd) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1240 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1241 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1242 | else |
| 1243 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1244 | SourceLocation())); |
| 1245 | Replacements.back().setField(*FI); |
| 1246 | } |
| 1247 | |
| 1248 | // Expand the current designator into the set of replacement |
| 1249 | // designators, so we have a full subobject path down to where the |
| 1250 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1251 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1252 | &Replacements[0] + Replacements.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1254 | // Update FieldIter/FieldIndex; |
| 1255 | RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1256 | FieldIter = Record->field_begin(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1257 | FieldIndex = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1258 | for (RecordDecl::field_iterator FEnd = Record->field_end(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1259 | FieldIter != FEnd; ++FieldIter) { |
| 1260 | if (FieldIter->isUnnamedBitfield()) |
| 1261 | continue; |
| 1262 | |
| 1263 | if (*FieldIter == Path.back()) |
| 1264 | return; |
| 1265 | |
| 1266 | ++FieldIndex; |
| 1267 | } |
| 1268 | |
| 1269 | assert(false && "Unable to find anonymous struct/union field"); |
| 1270 | } |
| 1271 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1272 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1273 | /// |
| 1274 | /// Determines whether the designated initializer @p DIE, which |
| 1275 | /// resides at the given @p Index within the initializer list @p |
| 1276 | /// IList, is well-formed for a current object of type @p DeclType |
| 1277 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1279 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1280 | /// |
| 1281 | /// @param IList The initializer list in which this designated |
| 1282 | /// initializer occurs. |
| 1283 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1284 | /// @param DIE The designated initializer expression. |
| 1285 | /// |
| 1286 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1287 | /// |
| 1288 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1289 | /// into which the designation in @p DIE should refer. |
| 1290 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1291 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1292 | /// a field, this will be set to the field declaration corresponding |
| 1293 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1294 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1295 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1296 | /// DIE is an array designator or GNU array-range designator, this |
| 1297 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1298 | /// |
| 1299 | /// @param Index Index into @p IList where the designated initializer |
| 1300 | /// @p DIE occurs. |
| 1301 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1302 | /// @param StructuredList The initializer list expression that |
| 1303 | /// describes all of the subobject initializers in the order they'll |
| 1304 | /// actually be initialized. |
| 1305 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1306 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1308 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1309 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1311 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1312 | QualType &CurrentObjectType, |
| 1313 | RecordDecl::field_iterator *NextField, |
| 1314 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1315 | unsigned &Index, |
| 1316 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1317 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1318 | bool FinishSubobjectInit, |
| 1319 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1320 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1321 | // Check the actual initialization for the designated object type. |
| 1322 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Temporarily remove the designator expression from the |
| 1325 | // initializer list that the child calls see, so that we don't try |
| 1326 | // to re-process the designator. |
| 1327 | unsigned OldIndex = Index; |
| 1328 | IList->setInit(OldIndex, DIE->getInit()); |
| 1329 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1330 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1331 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Restore the designated initializer expression in the syntactic |
| 1334 | // form of the initializer list. |
| 1335 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1336 | DIE->setInit(IList->getInit(OldIndex)); |
| 1337 | IList->setInit(OldIndex, DIE); |
| 1338 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1339 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1342 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1344 | "Need a non-designated initializer list to start from"); |
| 1345 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1346 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1347 | // Determine the structural initializer list that corresponds to the |
| 1348 | // current subobject. |
| 1349 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1351 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1352 | SourceRange(D->getStartLocation(), |
| 1353 | DIE->getSourceRange().getEnd())); |
| 1354 | assert(StructuredList && "Expected a structured initializer list"); |
| 1355 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1356 | if (D->isFieldDesignator()) { |
| 1357 | // C99 6.7.8p7: |
| 1358 | // |
| 1359 | // If a designator has the form |
| 1360 | // |
| 1361 | // . identifier |
| 1362 | // |
| 1363 | // then the current object (defined below) shall have |
| 1364 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1366 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1367 | if (!RT) { |
| 1368 | SourceLocation Loc = D->getDotLoc(); |
| 1369 | if (Loc.isInvalid()) |
| 1370 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1371 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1372 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1373 | ++Index; |
| 1374 | return true; |
| 1375 | } |
| 1376 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1377 | // Note: we perform a linear search of the fields here, despite |
| 1378 | // the fact that we have a faster lookup method, because we always |
| 1379 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1380 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1381 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1382 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1384 | Field = RT->getDecl()->field_begin(), |
| 1385 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1386 | for (; Field != FieldEnd; ++Field) { |
| 1387 | if (Field->isUnnamedBitfield()) |
| 1388 | continue; |
| 1389 | |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1390 | if (KnownField && KnownField == *Field) |
| 1391 | break; |
| 1392 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | |
| 1395 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1398 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1399 | // There was no normal field in the struct with the designated |
| 1400 | // name. Perform another lookup for this name, which may find |
| 1401 | // something that we can't designate (e.g., a member function), |
| 1402 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1404 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1405 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1406 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1407 | // Name lookup didn't find anything. Determine whether this |
| 1408 | // was a typo for another field name. |
| 1409 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
| 1410 | Sema::LookupMemberName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1411 | if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false, |
| 1412 | Sema::CTC_NoKeywords) && |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1413 | (ReplacementField = R.getAsSingle<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1414 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1415 | ->Equals(RT->getDecl())) { |
| 1416 | SemaRef.Diag(D->getFieldLoc(), |
| 1417 | diag::err_field_designator_unknown_suggest) |
| 1418 | << FieldName << CurrentObjectType << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1419 | << FixItHint::CreateReplacement(D->getFieldLoc(), |
| 1420 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1421 | SemaRef.Diag(ReplacementField->getLocation(), |
| 1422 | diag::note_previous_decl) |
| 1423 | << ReplacementField->getDeclName(); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1424 | } else { |
| 1425 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1426 | << FieldName << CurrentObjectType; |
| 1427 | ++Index; |
| 1428 | return true; |
| 1429 | } |
| 1430 | } else if (!KnownField) { |
| 1431 | // Determine whether we found a field at all. |
| 1432 | ReplacementField = dyn_cast<FieldDecl>(*Lookup.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1433 | |
| 1434 | // Check if ReplacementField is an anonymous field. |
| 1435 | if (!ReplacementField) |
| 1436 | if (IndirectFieldDecl* IField = dyn_cast<IndirectFieldDecl>(*Lookup.first)) |
| 1437 | ReplacementField = IField->getAnonField(); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1441 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1442 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1443 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1445 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1446 | ++Index; |
| 1447 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (!KnownField && |
| 1451 | cast<RecordDecl>((ReplacementField)->getDeclContext()) |
| 1452 | ->isAnonymousStructOrUnion()) { |
| 1453 | // Handle an field designator that refers to a member of an |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1454 | // anonymous struct or union. This is a C1X feature. |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1455 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, |
| 1456 | ReplacementField, |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1457 | Field, FieldIndex, RT->getDecl()); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1458 | D = DIE->getDesignator(DesigIdx); |
| 1459 | } else if (!KnownField) { |
| 1460 | // The replacement field comes from typo correction; find it |
| 1461 | // in the list of fields. |
| 1462 | FieldIndex = 0; |
| 1463 | Field = RT->getDecl()->field_begin(); |
| 1464 | for (; Field != FieldEnd; ++Field) { |
| 1465 | if (Field->isUnnamedBitfield()) |
| 1466 | continue; |
| 1467 | |
| 1468 | if (ReplacementField == *Field || |
| 1469 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1470 | break; |
| 1471 | |
| 1472 | ++FieldIndex; |
| 1473 | } |
| 1474 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1475 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1476 | |
| 1477 | // All of the fields of a union are located at the same place in |
| 1478 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1479 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1480 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1481 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1482 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1484 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1485 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1487 | // Make sure that our non-designated initializer list has space |
| 1488 | // for a subobject corresponding to this field. |
| 1489 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1490 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1492 | // This designator names a flexible array member. |
| 1493 | if (Field->getType()->isIncompleteArrayType()) { |
| 1494 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1495 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1496 | // We can't designate an object within the flexible array |
| 1497 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1499 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1501 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1503 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1504 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1505 | << *Field; |
| 1506 | Invalid = true; |
| 1507 | } |
| 1508 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1509 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1510 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1511 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1512 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1513 | diag::err_flexible_array_init_needs_braces) |
| 1514 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1515 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1516 | << *Field; |
| 1517 | Invalid = true; |
| 1518 | } |
| 1519 | |
| 1520 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1522 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1524 | diag::err_flexible_array_init_nonempty) |
| 1525 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1526 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1527 | << *Field; |
| 1528 | Invalid = true; |
| 1529 | } |
| 1530 | |
| 1531 | if (Invalid) { |
| 1532 | ++Index; |
| 1533 | return true; |
| 1534 | } |
| 1535 | |
| 1536 | // Initialize the array. |
| 1537 | bool prevHadError = hadError; |
| 1538 | unsigned newStructuredIndex = FieldIndex; |
| 1539 | unsigned OldIndex = Index; |
| 1540 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1541 | |
| 1542 | InitializedEntity MemberEntity = |
| 1543 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1544 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1545 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1547 | IList->setInit(OldIndex, DIE); |
| 1548 | if (hadError && !prevHadError) { |
| 1549 | ++Field; |
| 1550 | ++FieldIndex; |
| 1551 | if (NextField) |
| 1552 | *NextField = Field; |
| 1553 | StructuredIndex = FieldIndex; |
| 1554 | return true; |
| 1555 | } |
| 1556 | } else { |
| 1557 | // Recurse to check later designated subobjects. |
| 1558 | QualType FieldType = (*Field)->getType(); |
| 1559 | unsigned newStructuredIndex = FieldIndex; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1560 | |
| 1561 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1562 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1563 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1564 | FieldType, 0, 0, Index, |
| 1565 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1566 | true, false)) |
| 1567 | return true; |
| 1568 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Find the position of the next field to be initialized in this |
| 1571 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1572 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1573 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1574 | |
| 1575 | // If this the first designator, our caller will continue checking |
| 1576 | // the rest of this struct/class/union subobject. |
| 1577 | if (IsFirstDesignator) { |
| 1578 | if (NextField) |
| 1579 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1580 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1581 | return false; |
| 1582 | } |
| 1583 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1584 | if (!FinishSubobjectInit) |
| 1585 | return false; |
| 1586 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1587 | // We've already initialized something in the union; we're done. |
| 1588 | if (RT->getDecl()->isUnion()) |
| 1589 | return hadError; |
| 1590 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1591 | // Check the remaining fields within this class/struct/union subobject. |
| 1592 | bool prevHadError = hadError; |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1593 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1594 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1595 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1596 | return hadError && !prevHadError; |
| 1597 | } |
| 1598 | |
| 1599 | // C99 6.7.8p6: |
| 1600 | // |
| 1601 | // If a designator has the form |
| 1602 | // |
| 1603 | // [ constant-expression ] |
| 1604 | // |
| 1605 | // then the current object (defined below) shall have array |
| 1606 | // type and the expression shall be an integer constant |
| 1607 | // expression. If the array is of unknown size, any |
| 1608 | // nonnegative value is valid. |
| 1609 | // |
| 1610 | // Additionally, cope with the GNU extension that permits |
| 1611 | // designators of the form |
| 1612 | // |
| 1613 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1614 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1615 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1616 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1617 | << CurrentObjectType; |
| 1618 | ++Index; |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1623 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1624 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1625 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1626 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1627 | DesignatedEndIndex = DesignatedStartIndex; |
| 1628 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1629 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
| 1632 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1633 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1635 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1636 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1638 | if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1639 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1642 | if (isa<ConstantArrayType>(AT)) { |
| 1643 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1644 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1645 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1646 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1647 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1648 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1649 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1650 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1651 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1652 | << IndexExpr->getSourceRange(); |
| 1653 | ++Index; |
| 1654 | return true; |
| 1655 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1656 | } else { |
| 1657 | // Make sure the bit-widths and signedness match. |
| 1658 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1659 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1660 | else if (DesignatedStartIndex.getBitWidth() < |
| 1661 | DesignatedEndIndex.getBitWidth()) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1662 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1663 | DesignatedStartIndex.setIsUnsigned(true); |
| 1664 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1667 | // Make sure that our non-designated initializer list has space |
| 1668 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1669 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1671 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1672 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1673 | // Repeatedly perform subobject initializations in the range |
| 1674 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1676 | // Move to the next designator |
| 1677 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1678 | unsigned OldIndex = Index; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1679 | |
| 1680 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1681 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1682 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1683 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1684 | // Recurse to check later designated subobjects. |
| 1685 | QualType ElementType = AT->getElementType(); |
| 1686 | Index = OldIndex; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1687 | |
| 1688 | ElementEntity.setElementIndex(ElementIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1689 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1690 | ElementType, 0, 0, Index, |
| 1691 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1692 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1693 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1694 | return true; |
| 1695 | |
| 1696 | // Move to the next index in the array that we'll be initializing. |
| 1697 | ++DesignatedStartIndex; |
| 1698 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1699 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1700 | |
| 1701 | // If this the first designator, our caller will continue checking |
| 1702 | // the rest of this array subobject. |
| 1703 | if (IsFirstDesignator) { |
| 1704 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1705 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1706 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1707 | return false; |
| 1708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1710 | if (!FinishSubobjectInit) |
| 1711 | return false; |
| 1712 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1713 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1714 | bool prevHadError = hadError; |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1715 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1716 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1717 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1721 | // Get the structured initializer list for a subobject of type |
| 1722 | // @p CurrentObjectType. |
| 1723 | InitListExpr * |
| 1724 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1725 | QualType CurrentObjectType, |
| 1726 | InitListExpr *StructuredList, |
| 1727 | unsigned StructuredIndex, |
| 1728 | SourceRange InitRange) { |
| 1729 | Expr *ExistingInit = 0; |
| 1730 | if (!StructuredList) |
| 1731 | ExistingInit = SyntacticToSemantic[IList]; |
| 1732 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1733 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1735 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1736 | return Result; |
| 1737 | |
| 1738 | if (ExistingInit) { |
| 1739 | // We are creating an initializer list that initializes the |
| 1740 | // subobjects of the current object, but there was already an |
| 1741 | // initialization that completely initialized the current |
| 1742 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1744 | // struct X { int a, b; }; |
| 1745 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1747 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1748 | // designated initializer re-initializes the whole |
| 1749 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1751 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1752 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1754 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1755 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1756 | << ExistingInit->getSourceRange(); |
| 1757 | } |
| 1758 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1760 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1761 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1762 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1764 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1766 | // Pre-allocate storage for the structured initializer list. |
| 1767 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1768 | unsigned NumInits = 0; |
| 1769 | if (!StructuredList) |
| 1770 | NumInits = IList->getNumInits(); |
| 1771 | else if (Index < IList->getNumInits()) { |
| 1772 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1773 | NumInits = SubList->getNumInits(); |
| 1774 | } |
| 1775 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1777 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1778 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1779 | NumElements = CAType->getSize().getZExtValue(); |
| 1780 | // Simple heuristic so that we don't allocate a very large |
| 1781 | // initializer with many empty entries at the end. |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1782 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1783 | NumElements = 0; |
| 1784 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1785 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1786 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1787 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1788 | RecordDecl *RDecl = RType->getDecl(); |
| 1789 | if (RDecl->isUnion()) |
| 1790 | NumElements = 1; |
| 1791 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1793 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1796 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1797 | NumElements = IList->getNumInits(); |
| 1798 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1799 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1801 | // Link this new initializer list into the structured initializer |
| 1802 | // lists. |
| 1803 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1804 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1805 | else { |
| 1806 | Result->setSyntacticForm(IList); |
| 1807 | SyntacticToSemantic[IList] = Result; |
| 1808 | } |
| 1809 | |
| 1810 | return Result; |
| 1811 | } |
| 1812 | |
| 1813 | /// Update the initializer at index @p StructuredIndex within the |
| 1814 | /// structured initializer list to the value @p expr. |
| 1815 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1816 | unsigned &StructuredIndex, |
| 1817 | Expr *expr) { |
| 1818 | // No structured initializer list to update |
| 1819 | if (!StructuredList) |
| 1820 | return; |
| 1821 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1822 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1823 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1824 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1826 | diag::warn_initializer_overrides) |
| 1827 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1829 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1830 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1831 | << PrevInit->getSourceRange(); |
| 1832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1834 | ++StructuredIndex; |
| 1835 | } |
| 1836 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1837 | /// Check that the given Index expression is a valid array designator |
| 1838 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1839 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1840 | /// and produces a reasonable diagnostic if there is a |
| 1841 | /// failure. Returns true if there was an error, false otherwise. If |
| 1842 | /// everything went okay, Value will receive the value of the constant |
| 1843 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1845 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1846 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1847 | |
| 1848 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1849 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1850 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1852 | if (Value.isSigned() && Value.isNegative()) |
| 1853 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1854 | << Value.toString(10) << Index->getSourceRange(); |
| 1855 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1856 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1857 | return false; |
| 1858 | } |
| 1859 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1860 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1861 | SourceLocation Loc, |
| 1862 | bool GNUSyntax, |
| 1863 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1864 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1865 | |
| 1866 | bool Invalid = false; |
| 1867 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1868 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1869 | |
| 1870 | // Build designators and check array designator expressions. |
| 1871 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1872 | const Designator &D = Desig.getDesignator(Idx); |
| 1873 | switch (D.getKind()) { |
| 1874 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1876 | D.getFieldLoc())); |
| 1877 | break; |
| 1878 | |
| 1879 | case Designator::ArrayDesignator: { |
| 1880 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1881 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1882 | if (!Index->isTypeDependent() && |
| 1883 | !Index->isValueDependent() && |
| 1884 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1885 | Invalid = true; |
| 1886 | else { |
| 1887 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1889 | D.getRBracketLoc())); |
| 1890 | InitExpressions.push_back(Index); |
| 1891 | } |
| 1892 | break; |
| 1893 | } |
| 1894 | |
| 1895 | case Designator::ArrayRangeDesignator: { |
| 1896 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1897 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1898 | llvm::APSInt StartValue; |
| 1899 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1900 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1901 | StartIndex->isValueDependent(); |
| 1902 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1903 | EndIndex->isValueDependent(); |
| 1904 | if ((!StartDependent && |
| 1905 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1906 | (!EndDependent && |
| 1907 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1908 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1909 | else { |
| 1910 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1911 | if (StartDependent || EndDependent) { |
| 1912 | // Nothing to compute. |
| 1913 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1914 | EndValue.extend(StartValue.getBitWidth()); |
| 1915 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1916 | StartValue.extend(EndValue.getBitWidth()); |
| 1917 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1918 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1919 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1921 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1922 | Invalid = true; |
| 1923 | } else { |
| 1924 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1926 | D.getEllipsisLoc(), |
| 1927 | D.getRBracketLoc())); |
| 1928 | InitExpressions.push_back(StartIndex); |
| 1929 | InitExpressions.push_back(EndIndex); |
| 1930 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1931 | } |
| 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | if (Invalid || Init.isInvalid()) |
| 1938 | return ExprError(); |
| 1939 | |
| 1940 | // Clear out the expressions within the designation. |
| 1941 | Desig.ClearExprs(*this); |
| 1942 | |
| 1943 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1944 | = DesignatedInitExpr::Create(Context, |
| 1945 | Designators.data(), Designators.size(), |
| 1946 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1947 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1948 | return Owned(DIE); |
| 1949 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1951 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 1952 | InitListExpr *&InitList, QualType &DeclType) { |
| 1953 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1954 | if (!CheckInitList.HadError()) |
| 1955 | InitList = CheckInitList.getFullyStructuredList(); |
| 1956 | |
| 1957 | return CheckInitList.HadError(); |
| 1958 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1960 | //===----------------------------------------------------------------------===// |
| 1961 | // Initialization entity |
| 1962 | //===----------------------------------------------------------------------===// |
| 1963 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1964 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
| 1965 | const InitializedEntity &Parent) |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1966 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1967 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1968 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 1969 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1970 | Type = AT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1971 | } else { |
| 1972 | Kind = EK_VectorElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1973 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1974 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1978 | CXXBaseSpecifier *Base, |
| 1979 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1980 | { |
| 1981 | InitializedEntity Result; |
| 1982 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1983 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 1984 | if (IsInheritedVirtualBase) |
| 1985 | Result.Base |= 0x01; |
| 1986 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1987 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1988 | return Result; |
| 1989 | } |
| 1990 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1991 | DeclarationName InitializedEntity::getName() const { |
| 1992 | switch (getKind()) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1993 | case EK_Parameter: |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 1994 | if (!VariableOrMember) |
| 1995 | return DeclarationName(); |
| 1996 | // Fall through |
| 1997 | |
| 1998 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1999 | case EK_Member: |
| 2000 | return VariableOrMember->getDeclName(); |
| 2001 | |
| 2002 | case EK_Result: |
| 2003 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2004 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2005 | case EK_Temporary: |
| 2006 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2007 | case EK_ArrayElement: |
| 2008 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2009 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2010 | return DeclarationName(); |
| 2011 | } |
| 2012 | |
| 2013 | // Silence GCC warning |
| 2014 | return DeclarationName(); |
| 2015 | } |
| 2016 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2017 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2018 | switch (getKind()) { |
| 2019 | case EK_Variable: |
| 2020 | case EK_Parameter: |
| 2021 | case EK_Member: |
| 2022 | return VariableOrMember; |
| 2023 | |
| 2024 | case EK_Result: |
| 2025 | case EK_Exception: |
| 2026 | case EK_New: |
| 2027 | case EK_Temporary: |
| 2028 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2029 | case EK_ArrayElement: |
| 2030 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2031 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2032 | return 0; |
| 2033 | } |
| 2034 | |
| 2035 | // Silence GCC warning |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2039 | bool InitializedEntity::allowsNRVO() const { |
| 2040 | switch (getKind()) { |
| 2041 | case EK_Result: |
| 2042 | case EK_Exception: |
| 2043 | return LocAndNRVO.NRVO; |
| 2044 | |
| 2045 | case EK_Variable: |
| 2046 | case EK_Parameter: |
| 2047 | case EK_Member: |
| 2048 | case EK_New: |
| 2049 | case EK_Temporary: |
| 2050 | case EK_Base: |
| 2051 | case EK_ArrayElement: |
| 2052 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2053 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2054 | break; |
| 2055 | } |
| 2056 | |
| 2057 | return false; |
| 2058 | } |
| 2059 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2060 | //===----------------------------------------------------------------------===// |
| 2061 | // Initialization sequence |
| 2062 | //===----------------------------------------------------------------------===// |
| 2063 | |
| 2064 | void InitializationSequence::Step::Destroy() { |
| 2065 | switch (Kind) { |
| 2066 | case SK_ResolveAddressOfOverloadedFunction: |
| 2067 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2068 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2069 | case SK_CastDerivedToBaseLValue: |
| 2070 | case SK_BindReference: |
| 2071 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2072 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2073 | case SK_UserConversion: |
| 2074 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2075 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2076 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2077 | case SK_ListInitialization: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2078 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2079 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2080 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2081 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2082 | case SK_ObjCObjectConversion: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2083 | break; |
| 2084 | |
| 2085 | case SK_ConversionSequence: |
| 2086 | delete ICS; |
| 2087 | } |
| 2088 | } |
| 2089 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2090 | bool InitializationSequence::isDirectReferenceBinding() const { |
| 2091 | return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference; |
| 2092 | } |
| 2093 | |
| 2094 | bool InitializationSequence::isAmbiguous() const { |
| 2095 | if (getKind() != FailedSequence) |
| 2096 | return false; |
| 2097 | |
| 2098 | switch (getFailureKind()) { |
| 2099 | case FK_TooManyInitsForReference: |
| 2100 | case FK_ArrayNeedsInitList: |
| 2101 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2102 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2103 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2104 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2105 | case FK_RValueReferenceBindingToLValue: |
| 2106 | case FK_ReferenceInitDropsQualifiers: |
| 2107 | case FK_ReferenceInitFailed: |
| 2108 | case FK_ConversionFailed: |
| 2109 | case FK_TooManyInitsForScalar: |
| 2110 | case FK_ReferenceBindingToInitList: |
| 2111 | case FK_InitListBadDestinationType: |
| 2112 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2113 | case FK_Incomplete: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2114 | return false; |
| 2115 | |
| 2116 | case FK_ReferenceInitOverloadFailed: |
| 2117 | case FK_UserConversionOverloadFailed: |
| 2118 | case FK_ConstructorOverloadFailed: |
| 2119 | return FailedOverloadResult == OR_Ambiguous; |
| 2120 | } |
| 2121 | |
| 2122 | return false; |
| 2123 | } |
| 2124 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2125 | bool InitializationSequence::isConstructorInitialization() const { |
| 2126 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2127 | } |
| 2128 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2129 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2130 | FunctionDecl *Function, |
| 2131 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2132 | Step S; |
| 2133 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2134 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2135 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2136 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2137 | Steps.push_back(S); |
| 2138 | } |
| 2139 | |
| 2140 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2141 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2142 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2143 | switch (VK) { |
| 2144 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2145 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2146 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2147 | default: llvm_unreachable("No such category"); |
| 2148 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2149 | S.Type = BaseType; |
| 2150 | Steps.push_back(S); |
| 2151 | } |
| 2152 | |
| 2153 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
| 2154 | bool BindingTemporary) { |
| 2155 | Step S; |
| 2156 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2157 | S.Type = T; |
| 2158 | Steps.push_back(S); |
| 2159 | } |
| 2160 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2161 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2162 | Step S; |
| 2163 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2164 | S.Type = T; |
| 2165 | Steps.push_back(S); |
| 2166 | } |
| 2167 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2168 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2169 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2170 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2171 | Step S; |
| 2172 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2173 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2174 | S.Function.Function = Function; |
| 2175 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2176 | Steps.push_back(S); |
| 2177 | } |
| 2178 | |
| 2179 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2180 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2181 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2182 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2183 | switch (VK) { |
| 2184 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2185 | S.Kind = SK_QualificationConversionRValue; |
| 2186 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2187 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2188 | S.Kind = SK_QualificationConversionXValue; |
| 2189 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2190 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2191 | S.Kind = SK_QualificationConversionLValue; |
| 2192 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2193 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2194 | S.Type = Ty; |
| 2195 | Steps.push_back(S); |
| 2196 | } |
| 2197 | |
| 2198 | void InitializationSequence::AddConversionSequenceStep( |
| 2199 | const ImplicitConversionSequence &ICS, |
| 2200 | QualType T) { |
| 2201 | Step S; |
| 2202 | S.Kind = SK_ConversionSequence; |
| 2203 | S.Type = T; |
| 2204 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2205 | Steps.push_back(S); |
| 2206 | } |
| 2207 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2208 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2209 | Step S; |
| 2210 | S.Kind = SK_ListInitialization; |
| 2211 | S.Type = T; |
| 2212 | Steps.push_back(S); |
| 2213 | } |
| 2214 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2215 | void |
| 2216 | InitializationSequence::AddConstructorInitializationStep( |
| 2217 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2218 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2219 | QualType T) { |
| 2220 | Step S; |
| 2221 | S.Kind = SK_ConstructorInitialization; |
| 2222 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2223 | S.Function.Function = Constructor; |
| 2224 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2225 | Steps.push_back(S); |
| 2226 | } |
| 2227 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2228 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2229 | Step S; |
| 2230 | S.Kind = SK_ZeroInitialization; |
| 2231 | S.Type = T; |
| 2232 | Steps.push_back(S); |
| 2233 | } |
| 2234 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2235 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2236 | Step S; |
| 2237 | S.Kind = SK_CAssignment; |
| 2238 | S.Type = T; |
| 2239 | Steps.push_back(S); |
| 2240 | } |
| 2241 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2242 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2243 | Step S; |
| 2244 | S.Kind = SK_StringInit; |
| 2245 | S.Type = T; |
| 2246 | Steps.push_back(S); |
| 2247 | } |
| 2248 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2249 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2250 | Step S; |
| 2251 | S.Kind = SK_ObjCObjectConversion; |
| 2252 | S.Type = T; |
| 2253 | Steps.push_back(S); |
| 2254 | } |
| 2255 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2256 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
| 2257 | OverloadingResult Result) { |
| 2258 | SequenceKind = FailedSequence; |
| 2259 | this->Failure = Failure; |
| 2260 | this->FailedOverloadResult = Result; |
| 2261 | } |
| 2262 | |
| 2263 | //===----------------------------------------------------------------------===// |
| 2264 | // Attempt initialization |
| 2265 | //===----------------------------------------------------------------------===// |
| 2266 | |
| 2267 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2268 | static void TryListInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2269 | const InitializedEntity &Entity, |
| 2270 | const InitializationKind &Kind, |
| 2271 | InitListExpr *InitList, |
| 2272 | InitializationSequence &Sequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2273 | // FIXME: We only perform rudimentary checking of list |
| 2274 | // initializations at this point, then assume that any list |
| 2275 | // initialization of an array, aggregate, or scalar will be |
Sebastian Redl | 36c28db | 2010-06-30 16:41:54 +0000 | [diff] [blame] | 2276 | // well-formed. When we actually "perform" list initialization, we'll |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2277 | // do all of the necessary checking. C++0x initializer lists will |
| 2278 | // force us to perform more checking here. |
| 2279 | Sequence.setSequenceKind(InitializationSequence::ListInitialization); |
| 2280 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2281 | QualType DestType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2282 | |
| 2283 | // C++ [dcl.init]p13: |
| 2284 | // If T is a scalar type, then a declaration of the form |
| 2285 | // |
| 2286 | // T x = { a }; |
| 2287 | // |
| 2288 | // is equivalent to |
| 2289 | // |
| 2290 | // T x = a; |
| 2291 | if (DestType->isScalarType()) { |
| 2292 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2293 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2294 | return; |
| 2295 | } |
| 2296 | |
| 2297 | // Assume scalar initialization from a single value works. |
| 2298 | } else if (DestType->isAggregateType()) { |
| 2299 | // Assume aggregate initialization works. |
| 2300 | } else if (DestType->isVectorType()) { |
| 2301 | // Assume vector initialization works. |
| 2302 | } else if (DestType->isReferenceType()) { |
| 2303 | // FIXME: C++0x defines behavior for this. |
| 2304 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2305 | return; |
| 2306 | } else if (DestType->isRecordType()) { |
| 2307 | // FIXME: C++0x defines behavior for this |
| 2308 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2309 | } |
| 2310 | |
| 2311 | // Add a general "list initialization" step. |
| 2312 | Sequence.AddListInitializationStep(DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /// \brief Try a reference initialization that involves calling a conversion |
| 2316 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2317 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2318 | const InitializedEntity &Entity, |
| 2319 | const InitializationKind &Kind, |
| 2320 | Expr *Initializer, |
| 2321 | bool AllowRValues, |
| 2322 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2323 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2324 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2325 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2326 | QualType cv2T2 = Initializer->getType(); |
| 2327 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2328 | |
| 2329 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2330 | bool ObjCConversion; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2331 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2332 | T1, T2, DerivedToBase, |
| 2333 | ObjCConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2334 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2335 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2336 | (void)ObjCConversion; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2337 | |
| 2338 | // Build the candidate set directly in the initialization sequence |
| 2339 | // structure, so that it will persist if we fail. |
| 2340 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2341 | CandidateSet.clear(); |
| 2342 | |
| 2343 | // Determine whether we are allowed to call explicit constructors or |
| 2344 | // explicit conversion operators. |
| 2345 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
| 2346 | |
| 2347 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2348 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2349 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2350 | // The type we're converting to is a class type. Enumerate its constructors |
| 2351 | // to see if there is a suitable conversion. |
| 2352 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2354 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2355 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2356 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2357 | NamedDecl *D = *Con; |
| 2358 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2359 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2360 | // Find the constructor (which may be a template). |
| 2361 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2362 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2363 | if (ConstructorTmpl) |
| 2364 | Constructor = cast<CXXConstructorDecl>( |
| 2365 | ConstructorTmpl->getTemplatedDecl()); |
| 2366 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2367 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2368 | |
| 2369 | if (!Constructor->isInvalidDecl() && |
| 2370 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2371 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2372 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2373 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2374 | &Initializer, 1, CandidateSet, |
| 2375 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2376 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2377 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2378 | &Initializer, 1, CandidateSet, |
| 2379 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2383 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2384 | return OR_No_Viable_Function; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2385 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2386 | const RecordType *T2RecordType = 0; |
| 2387 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2388 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2389 | // The type we're converting from is a class type, enumerate its conversion |
| 2390 | // functions. |
| 2391 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2392 | |
| 2393 | // Determine the type we are converting to. If we are allowed to |
| 2394 | // convert to an rvalue, take the type that the destination type |
| 2395 | // refers to. |
| 2396 | QualType ToType = AllowRValues? cv1T1 : DestType; |
| 2397 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2398 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2400 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2401 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2402 | NamedDecl *D = *I; |
| 2403 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2404 | if (isa<UsingShadowDecl>(D)) |
| 2405 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2406 | |
| 2407 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2408 | CXXConversionDecl *Conv; |
| 2409 | if (ConvTemplate) |
| 2410 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2411 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2412 | Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2413 | |
| 2414 | // If the conversion function doesn't return a reference type, |
| 2415 | // it can't be considered for this conversion unless we're allowed to |
| 2416 | // consider rvalues. |
| 2417 | // FIXME: Do we need to make sure that we only consider conversion |
| 2418 | // candidates with reference-compatible results? That might be needed to |
| 2419 | // break recursion. |
| 2420 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2421 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2422 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2423 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2424 | ActingDC, Initializer, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2425 | ToType, CandidateSet); |
| 2426 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2427 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 692f85c | 2010-02-26 01:17:27 +0000 | [diff] [blame] | 2428 | Initializer, ToType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2432 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2433 | return OR_No_Viable_Function; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2434 | |
| 2435 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2436 | |
| 2437 | // Perform overload resolution. If it fails, return the failed result. |
| 2438 | OverloadCandidateSet::iterator Best; |
| 2439 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2440 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2441 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2442 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2443 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2444 | |
| 2445 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2446 | if (isa<CXXConversionDecl>(Function)) |
| 2447 | T2 = Function->getResultType(); |
| 2448 | else |
| 2449 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2450 | |
| 2451 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2452 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2453 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Determine whether we need to perform derived-to-base or |
| 2456 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2457 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2458 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2459 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2460 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2461 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2463 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2464 | bool NewObjCConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2465 | Sema::ReferenceCompareResult NewRefRelationship |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2466 | = S.CompareReferenceRelationship(DeclLoc, T1, |
| 2467 | T2.getNonLValueExprType(S.Context), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2468 | NewDerivedToBase, NewObjCConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2469 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2470 | // If the type we've converted to is not reference-related to the |
| 2471 | // type we're looking for, then there is another conversion step |
| 2472 | // we need to perform to produce a temporary of the right type |
| 2473 | // that we'll be binding to. |
| 2474 | ImplicitConversionSequence ICS; |
| 2475 | ICS.setStandard(); |
| 2476 | ICS.Standard = Best->FinalConversion; |
| 2477 | T2 = ICS.Standard.getToType(2); |
| 2478 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2479 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2480 | Sequence.AddDerivedToBaseCastStep( |
| 2481 | S.Context.getQualifiedType(T1, |
| 2482 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2483 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2484 | else if (NewObjCConversion) |
| 2485 | Sequence.AddObjCObjectConversionStep( |
| 2486 | S.Context.getQualifiedType(T1, |
| 2487 | T2.getNonReferenceType().getQualifiers())); |
| 2488 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2489 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2490 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2491 | |
| 2492 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2493 | return OR_Success; |
| 2494 | } |
| 2495 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2496 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2497 | static void TryReferenceInitialization(Sema &S, |
| 2498 | const InitializedEntity &Entity, |
| 2499 | const InitializationKind &Kind, |
| 2500 | Expr *Initializer, |
| 2501 | InitializationSequence &Sequence) { |
| 2502 | Sequence.setSequenceKind(InitializationSequence::ReferenceBinding); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2504 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2505 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2506 | Qualifiers T1Quals; |
| 2507 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2508 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2509 | Qualifiers T2Quals; |
| 2510 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2513 | // If the initializer is the address of an overloaded function, try |
| 2514 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2515 | // type of the resulting function. |
| 2516 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2517 | DeclAccessPair Found; |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2518 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2519 | T1, |
| 2520 | false, |
| 2521 | Found)) { |
| 2522 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2523 | cv2T2 = Fn->getType(); |
| 2524 | T2 = cv2T2.getUnqualifiedType(); |
| 2525 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2526 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2527 | return; |
| 2528 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2529 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2530 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2531 | // Compute some basic properties of the types and the initializer. |
| 2532 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2533 | bool isRValueRef = !isLValueRef; |
| 2534 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2535 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2536 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2537 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2538 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
| 2539 | ObjCConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2541 | // C++0x [dcl.init.ref]p5: |
| 2542 | // A reference to type "cv1 T1" is initialized by an expression of type |
| 2543 | // "cv2 T2" as follows: |
| 2544 | // |
| 2545 | // - If the reference is an lvalue reference and the initializer |
| 2546 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2547 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2548 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2549 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2550 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2551 | bool T1Function = T1->isFunctionType(); |
| 2552 | if (isLValueRef || T1Function) { |
| 2553 | if (InitCategory.isLValue() && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2554 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2555 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2556 | // reference-compatible with "cv2 T2," or |
| 2557 | // |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2558 | // 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] | 2559 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2560 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2561 | // to decide whether we're actually binding to a temporary created from |
| 2562 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2563 | if (DerivedToBase) |
| 2564 | Sequence.AddDerivedToBaseCastStep( |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2565 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2566 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2567 | else if (ObjCConversion) |
| 2568 | Sequence.AddObjCObjectConversionStep( |
| 2569 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2570 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2571 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2572 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2573 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2574 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2575 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2576 | return; |
| 2577 | } |
| 2578 | |
| 2579 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2580 | // reference-related to T2, and can be implicitly converted to an |
| 2581 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2582 | // with "cv3 T3" (this conversion is selected by enumerating the |
| 2583 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2584 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2585 | // If we have an rvalue ref to function type here, the rhs must be |
| 2586 | // an rvalue. |
| 2587 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2588 | (isLValueRef || InitCategory.isRValue())) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2589 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
| 2590 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2591 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2592 | Sequence); |
| 2593 | if (ConvOvlResult == OR_Success) |
| 2594 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2595 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2596 | Sequence.SetOverloadFailure( |
| 2597 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2598 | ConvOvlResult); |
| 2599 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2602 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2603 | // - Otherwise, the reference shall be an lvalue reference to a |
| 2604 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2605 | // shall be an rvalue reference and the initializer expression shall |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2606 | // be an rvalue or have a function type. |
| 2607 | // We handled the function type stuff above. |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 2608 | if (!((isLValueRef && T1Quals.hasConst() && !T1Quals.hasVolatile()) || |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2609 | (isRValueRef && InitCategory.isRValue()))) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2610 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2611 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2612 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2613 | Sequence.SetOverloadFailure( |
| 2614 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2615 | ConvOvlResult); |
| 2616 | else if (isLValueRef) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2617 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2618 | ? (RefRelationship == Sema::Ref_Related |
| 2619 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2620 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2621 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 2622 | else |
| 2623 | Sequence.SetFailed( |
| 2624 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2626 | return; |
| 2627 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2628 | |
| 2629 | // - [If T1 is not a function type], if T2 is a class type and |
| 2630 | if (!T1Function && T2->isRecordType()) { |
Sebastian Redl | 66d0acd | 2010-07-26 17:52:21 +0000 | [diff] [blame] | 2631 | bool isXValue = InitCategory.isXValue(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2632 | // - the initializer expression is an rvalue and "cv1 T1" is |
| 2633 | // reference-compatible with "cv2 T2", or |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2634 | if (InitCategory.isRValue() && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2635 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2636 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2637 | // compiler the freedom to perform a copy here or bind to the |
| 2638 | // object, while C++0x requires that we bind directly to the |
| 2639 | // object. Hence, we always bind to the object without making an |
| 2640 | // extra copy. However, in C++03 requires that we check for the |
| 2641 | // presence of a suitable copy constructor: |
| 2642 | // |
| 2643 | // The constructor that would be used to make the copy shall |
| 2644 | // be callable whether or not the copy is actually done. |
| 2645 | if (!S.getLangOptions().CPlusPlus0x) |
| 2646 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
| 2647 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2648 | if (DerivedToBase) |
| 2649 | Sequence.AddDerivedToBaseCastStep( |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2650 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2651 | isXValue ? VK_XValue : VK_RValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2652 | else if (ObjCConversion) |
| 2653 | Sequence.AddObjCObjectConversionStep( |
| 2654 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2655 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2656 | if (T1Quals != T2Quals) |
Sebastian Redl | 66d0acd | 2010-07-26 17:52:21 +0000 | [diff] [blame] | 2657 | Sequence.AddQualificationConversionStep(cv1T1, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2658 | isXValue ? VK_XValue : VK_RValue); |
Sebastian Redl | 66d0acd | 2010-07-26 17:52:21 +0000 | [diff] [blame] | 2659 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/!isXValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2660 | return; |
| 2661 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2663 | // - T1 is not reference-related to T2 and the initializer expression |
| 2664 | // can be implicitly converted to an rvalue of type "cv3 T3" (this |
| 2665 | // conversion is selected by enumerating the applicable conversion |
| 2666 | // functions (13.3.1.6) and choosing the best one through overload |
| 2667 | // resolution (13.3)), |
| 2668 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2669 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2670 | Kind, Initializer, |
| 2671 | /*AllowRValues=*/true, |
| 2672 | Sequence); |
| 2673 | if (ConvOvlResult) |
| 2674 | Sequence.SetOverloadFailure( |
| 2675 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2676 | ConvOvlResult); |
| 2677 | |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2682 | return; |
| 2683 | } |
| 2684 | |
| 2685 | // - If the initializer expression is an rvalue, with T2 an array type, |
| 2686 | // and "cv1 T1" is reference-compatible with "cv2 T2," the reference |
| 2687 | // is bound to the object represented by the rvalue (see 3.10). |
| 2688 | // FIXME: How can an array type be reference-compatible with anything? |
| 2689 | // Don't we mean the element types of T1 and T2? |
| 2690 | |
| 2691 | // - Otherwise, a temporary of type “cv1 T1” is created and initialized |
| 2692 | // from the initializer expression using the rules for a non-reference |
| 2693 | // copy initialization (8.5). The reference is then bound to the |
| 2694 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2696 | // Determine whether we are allowed to call explicit constructors or |
| 2697 | // explicit conversion operators. |
| 2698 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2699 | |
| 2700 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2701 | |
| 2702 | if (S.TryImplicitConversion(Sequence, TempEntity, Initializer, |
| 2703 | /*SuppressUserConversions*/ false, |
| 2704 | AllowExplicit, |
| 2705 | /*FIXME:InOverloadResolution=*/false)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2706 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2707 | // this into an overloading ambiguity diagnostic. However, we need |
| 2708 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2709 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2710 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2711 | Sequence.SetOverloadFailure( |
| 2712 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2713 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2714 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2715 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2716 | else |
| 2717 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2718 | return; |
| 2719 | } |
| 2720 | |
| 2721 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2722 | // same cv-qualification as, or greater cv-qualification |
| 2723 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2724 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2725 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2726 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2727 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2728 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2729 | return; |
| 2730 | } |
| 2731 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2732 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | /// \brief Attempt character array initialization from a string literal |
| 2737 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2738 | static void TryStringLiteralInitialization(Sema &S, |
| 2739 | const InitializedEntity &Entity, |
| 2740 | const InitializationKind &Kind, |
| 2741 | Expr *Initializer, |
| 2742 | InitializationSequence &Sequence) { |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2743 | Sequence.setSequenceKind(InitializationSequence::StringInit); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2744 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2747 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2748 | /// enumerates the constructors of the initialized entity and performs overload |
| 2749 | /// resolution to select the best. |
| 2750 | static void TryConstructorInitialization(Sema &S, |
| 2751 | const InitializedEntity &Entity, |
| 2752 | const InitializationKind &Kind, |
| 2753 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2754 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2755 | InitializationSequence &Sequence) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2756 | Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2757 | |
| 2758 | // Build the candidate set directly in the initialization sequence |
| 2759 | // structure, so that it will persist if we fail. |
| 2760 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2761 | CandidateSet.clear(); |
| 2762 | |
| 2763 | // Determine whether we are allowed to call explicit constructors or |
| 2764 | // explicit conversion operators. |
| 2765 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2766 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2767 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2768 | |
| 2769 | // The type we're constructing needs to be complete. |
| 2770 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2771 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2774 | |
| 2775 | // The type we're converting to is a class type. Enumerate its constructors |
| 2776 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2777 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2778 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2779 | CXXRecordDecl *DestRecordDecl |
| 2780 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2781 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2782 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2783 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2784 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2785 | NamedDecl *D = *Con; |
| 2786 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2787 | bool SuppressUserConversions = false; |
| 2788 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2789 | // Find the constructor (which may be a template). |
| 2790 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2791 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2792 | if (ConstructorTmpl) |
| 2793 | Constructor = cast<CXXConstructorDecl>( |
| 2794 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2795 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2796 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2797 | |
| 2798 | // If we're performing copy initialization using a copy constructor, we |
| 2799 | // suppress user-defined conversions on the arguments. |
| 2800 | // FIXME: Move constructors? |
| 2801 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2802 | Constructor->isCopyConstructor()) |
| 2803 | SuppressUserConversions = true; |
| 2804 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2805 | |
| 2806 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2807 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2808 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2809 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2810 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2811 | Args, NumArgs, CandidateSet, |
| 2812 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2813 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2814 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2815 | Args, NumArgs, CandidateSet, |
| 2816 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | SourceLocation DeclLoc = Kind.getLocation(); |
| 2821 | |
| 2822 | // Perform overload resolution. If it fails, return the failed result. |
| 2823 | OverloadCandidateSet::iterator Best; |
| 2824 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2825 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2826 | Sequence.SetOverloadFailure( |
| 2827 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 2828 | Result); |
| 2829 | return; |
| 2830 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2831 | |
| 2832 | // C++0x [dcl.init]p6: |
| 2833 | // If a program calls for the default initialization of an object |
| 2834 | // of a const-qualified type T, T shall be a class type with a |
| 2835 | // user-provided default constructor. |
| 2836 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2837 | Entity.getType().isConstQualified() && |
| 2838 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2839 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2840 | return; |
| 2841 | } |
| 2842 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2843 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2844 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2845 | Sequence.AddConstructorInitializationStep( |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2846 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2847 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2848 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2851 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
| 2852 | static void TryValueInitialization(Sema &S, |
| 2853 | const InitializedEntity &Entity, |
| 2854 | const InitializationKind &Kind, |
| 2855 | InitializationSequence &Sequence) { |
| 2856 | // C++ [dcl.init]p5: |
| 2857 | // |
| 2858 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2859 | QualType T = Entity.getType(); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2860 | |
| 2861 | // -- if T is an array type, then each element is value-initialized; |
| 2862 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2863 | T = AT->getElementType(); |
| 2864 | |
| 2865 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2866 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2867 | // -- if T is a class type (clause 9) with a user-declared |
| 2868 | // constructor (12.1), then the default constructor for T is |
| 2869 | // called (and the initialization is ill-formed if T has no |
| 2870 | // accessible default constructor); |
| 2871 | // |
| 2872 | // FIXME: we really want to refer to a single subobject of the array, |
| 2873 | // but Entity doesn't have a way to capture that (yet). |
| 2874 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 2875 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
| 2876 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2877 | // -- if T is a (possibly cv-qualified) non-union class type |
| 2878 | // without a user-provided constructor, then the object is |
| 2879 | // zero-initialized and, if T’s implicitly-declared default |
| 2880 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2881 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2882 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2883 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2884 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
| 2885 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2889 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2890 | Sequence.setSequenceKind(InitializationSequence::ZeroInitialization); |
| 2891 | } |
| 2892 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2893 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 2894 | static void TryDefaultInitialization(Sema &S, |
| 2895 | const InitializedEntity &Entity, |
| 2896 | const InitializationKind &Kind, |
| 2897 | InitializationSequence &Sequence) { |
| 2898 | assert(Kind.getKind() == InitializationKind::IK_Default); |
| 2899 | |
| 2900 | // C++ [dcl.init]p6: |
| 2901 | // To default-initialize an object of type T means: |
| 2902 | // - if T is an array type, each element is default-initialized; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2903 | QualType DestType = Entity.getType(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2904 | while (const ArrayType *Array = S.Context.getAsArrayType(DestType)) |
| 2905 | DestType = Array->getElementType(); |
| 2906 | |
| 2907 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 2908 | // constructor for T is called (and the initialization is ill-formed if |
| 2909 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2910 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 2911 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 2912 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | // - otherwise, no initialization is performed. |
| 2916 | Sequence.setSequenceKind(InitializationSequence::NoInitialization); |
| 2917 | |
| 2918 | // If a program calls for the default initialization of an object of |
| 2919 | // a const-qualified type T, T shall be a class type with a user-provided |
| 2920 | // default constructor. |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2921 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2922 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2923 | } |
| 2924 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2925 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 2926 | /// which enumerates all conversion functions and performs overload resolution |
| 2927 | /// to select the best. |
| 2928 | static void TryUserDefinedConversion(Sema &S, |
| 2929 | const InitializedEntity &Entity, |
| 2930 | const InitializationKind &Kind, |
| 2931 | Expr *Initializer, |
| 2932 | InitializationSequence &Sequence) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2933 | Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion); |
| 2934 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2935 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2936 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 2937 | QualType SourceType = Initializer->getType(); |
| 2938 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 2939 | "Must have a class type to perform a user-defined conversion"); |
| 2940 | |
| 2941 | // Build the candidate set directly in the initialization sequence |
| 2942 | // structure, so that it will persist if we fail. |
| 2943 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2944 | CandidateSet.clear(); |
| 2945 | |
| 2946 | // Determine whether we are allowed to call explicit constructors or |
| 2947 | // explicit conversion operators. |
| 2948 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
| 2949 | |
| 2950 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 2951 | // The type we're converting to is a class type. Enumerate its constructors |
| 2952 | // to see if there is a suitable conversion. |
| 2953 | CXXRecordDecl *DestRecordDecl |
| 2954 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2955 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2956 | // Try to complete the type we're converting to. |
| 2957 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2958 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2959 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2960 | Con != ConEnd; ++Con) { |
| 2961 | NamedDecl *D = *Con; |
| 2962 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2963 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2964 | // Find the constructor (which may be a template). |
| 2965 | CXXConstructorDecl *Constructor = 0; |
| 2966 | FunctionTemplateDecl *ConstructorTmpl |
| 2967 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2968 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2969 | Constructor = cast<CXXConstructorDecl>( |
| 2970 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2971 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2972 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2973 | |
| 2974 | if (!Constructor->isInvalidDecl() && |
| 2975 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2976 | if (ConstructorTmpl) |
| 2977 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2978 | /*ExplicitArgs*/ 0, |
| 2979 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2980 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2981 | else |
| 2982 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2983 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2984 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2988 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2989 | |
| 2990 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2991 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2992 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 2993 | // The type we're converting from is a class type, enumerate its conversion |
| 2994 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2995 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2996 | // We can only enumerate the conversion functions for a complete type; if |
| 2997 | // the type isn't complete, simply skip this step. |
| 2998 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 2999 | CXXRecordDecl *SourceRecordDecl |
| 3000 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3001 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3002 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3003 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3004 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3005 | E = Conversions->end(); |
| 3006 | I != E; ++I) { |
| 3007 | NamedDecl *D = *I; |
| 3008 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3009 | if (isa<UsingShadowDecl>(D)) |
| 3010 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3011 | |
| 3012 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3013 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3014 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3015 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3016 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3017 | Conv = cast<CXXConversionDecl>(D); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3018 | |
| 3019 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3020 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3021 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3022 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3023 | CandidateSet); |
| 3024 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3025 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3026 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3027 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3032 | // Perform overload resolution. If it fails, return the failed result. |
| 3033 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3034 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3035 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3036 | Sequence.SetOverloadFailure( |
| 3037 | InitializationSequence::FK_UserConversionOverloadFailed, |
| 3038 | Result); |
| 3039 | return; |
| 3040 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3041 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3042 | FunctionDecl *Function = Best->Function; |
| 3043 | |
| 3044 | if (isa<CXXConstructorDecl>(Function)) { |
| 3045 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3046 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3047 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3048 | return; |
| 3049 | } |
| 3050 | |
| 3051 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3052 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3053 | if (ConvType->getAs<RecordType>()) { |
| 3054 | // If we're converting to a class type, there may be an copy if |
| 3055 | // the resulting temporary object (possible to create an object of |
| 3056 | // a base class type). That copy is not a separate conversion, so |
| 3057 | // we just make a note of the actual destination type (possibly a |
| 3058 | // base class of the type returned by the conversion function) and |
| 3059 | // let the user-defined conversion step handle the conversion. |
| 3060 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3061 | return; |
| 3062 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3064 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
| 3065 | |
| 3066 | // If the conversion following the call to the conversion function |
| 3067 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3068 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3069 | Best->FinalConversion.Third) { |
| 3070 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3071 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3072 | ICS.Standard = Best->FinalConversion; |
| 3073 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3074 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3077 | InitializationSequence::InitializationSequence(Sema &S, |
| 3078 | const InitializedEntity &Entity, |
| 3079 | const InitializationKind &Kind, |
| 3080 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3081 | unsigned NumArgs) |
| 3082 | : FailedCandidateSet(Kind.getLocation()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3083 | ASTContext &Context = S.Context; |
| 3084 | |
| 3085 | // C++0x [dcl.init]p16: |
| 3086 | // The semantics of initializers are as follows. The destination type is |
| 3087 | // the type of the object or reference being initialized and the source |
| 3088 | // type is the type of the initializer expression. The source type is not |
| 3089 | // defined when the initializer is a braced-init-list or when it is a |
| 3090 | // parenthesized list of expressions. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3091 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3092 | |
| 3093 | if (DestType->isDependentType() || |
| 3094 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3095 | SequenceKind = DependentSequence; |
| 3096 | return; |
| 3097 | } |
| 3098 | |
| 3099 | QualType SourceType; |
| 3100 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3101 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3102 | Initializer = Args[0]; |
| 3103 | if (!isa<InitListExpr>(Initializer)) |
| 3104 | SourceType = Initializer->getType(); |
| 3105 | } |
| 3106 | |
| 3107 | // - If the initializer is a braced-init-list, the object is |
| 3108 | // list-initialized (8.5.4). |
| 3109 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 3110 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3111 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | // - If the destination type is a reference type, see 8.5.3. |
| 3115 | if (DestType->isReferenceType()) { |
| 3116 | // C++0x [dcl.init.ref]p1: |
| 3117 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3118 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3119 | // by an object that can be converted into a T. |
| 3120 | // (Therefore, multiple arguments are not permitted.) |
| 3121 | if (NumArgs != 1) |
| 3122 | SetFailed(FK_TooManyInitsForReference); |
| 3123 | else |
| 3124 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 3125 | return; |
| 3126 | } |
| 3127 | |
| 3128 | // - If the destination type is an array of characters, an array of |
| 3129 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3130 | // initializer is a string literal, see 8.5.2. |
| 3131 | if (Initializer && IsStringInit(Initializer, DestType, Context)) { |
| 3132 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 3133 | return; |
| 3134 | } |
| 3135 | |
| 3136 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3137 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3138 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3139 | TryValueInitialization(S, Entity, Kind, *this); |
| 3140 | return; |
| 3141 | } |
| 3142 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3143 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3144 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3145 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 3146 | return; |
| 3147 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3149 | // - Otherwise, if the destination type is an array, the program is |
| 3150 | // ill-formed. |
| 3151 | if (const ArrayType *AT = Context.getAsArrayType(DestType)) { |
| 3152 | if (AT->getElementType()->isAnyCharacterType()) |
| 3153 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 3154 | else |
| 3155 | SetFailed(FK_ArrayNeedsInitList); |
| 3156 | |
| 3157 | return; |
| 3158 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Handle initialization in C |
| 3161 | if (!S.getLangOptions().CPlusPlus) { |
| 3162 | setSequenceKind(CAssignment); |
| 3163 | AddCAssignmentStep(DestType); |
| 3164 | return; |
| 3165 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3166 | |
| 3167 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3168 | if (DestType->isRecordType()) { |
| 3169 | // - If the initialization is direct-initialization, or if it is |
| 3170 | // copy-initialization where the cv-unqualified version of the |
| 3171 | // source type is the same class as, or a derived class of, the |
| 3172 | // class of the destination, constructors are considered. [...] |
| 3173 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3174 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3175 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3176 | S.IsDerivedFrom(SourceType, DestType)))) |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3177 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3178 | Entity.getType(), *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3179 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
| 3180 | // user-defined conversion sequences that can convert from the source |
| 3181 | // type to the destination type or (when a conversion function is |
| 3182 | // used) to a derived class thereof are enumerated as described in |
| 3183 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3184 | // (13.3). |
| 3185 | else |
| 3186 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3187 | return; |
| 3188 | } |
| 3189 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3190 | if (NumArgs > 1) { |
| 3191 | SetFailed(FK_TooManyInitsForScalar); |
| 3192 | return; |
| 3193 | } |
| 3194 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
| 3195 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3196 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
| 3197 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3198 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3199 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3200 | return; |
| 3201 | } |
| 3202 | |
| 3203 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3204 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3205 | // conversions (Clause 4) will be used, if necessary, to convert the |
| 3206 | // initializer expression to the cv-unqualified version of the |
| 3207 | // destination type; no user-defined conversions are considered. |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3208 | if (S.TryImplicitConversion(*this, Entity, Initializer, |
| 3209 | /*SuppressUserConversions*/ true, |
| 3210 | /*AllowExplicitConversions*/ false, |
| 3211 | /*InOverloadResolution*/ false)) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3212 | { |
| 3213 | if (Initializer->getType() == Context.OverloadTy ) |
| 3214 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3215 | else |
| 3216 | SetFailed(InitializationSequence::FK_ConversionFailed); |
| 3217 | } |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3218 | else |
| 3219 | setSequenceKind(StandardConversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | InitializationSequence::~InitializationSequence() { |
| 3223 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 3224 | StepEnd = Steps.end(); |
| 3225 | Step != StepEnd; ++Step) |
| 3226 | Step->Destroy(); |
| 3227 | } |
| 3228 | |
| 3229 | //===----------------------------------------------------------------------===// |
| 3230 | // Perform initialization |
| 3231 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3232 | static Sema::AssignmentAction |
| 3233 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3234 | switch(Entity.getKind()) { |
| 3235 | case InitializedEntity::EK_Variable: |
| 3236 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3237 | case InitializedEntity::EK_Exception: |
| 3238 | case InitializedEntity::EK_Base: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3239 | return Sema::AA_Initializing; |
| 3240 | |
| 3241 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3242 | if (Entity.getDecl() && |
| 3243 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3244 | return Sema::AA_Sending; |
| 3245 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3246 | return Sema::AA_Passing; |
| 3247 | |
| 3248 | case InitializedEntity::EK_Result: |
| 3249 | return Sema::AA_Returning; |
| 3250 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3251 | case InitializedEntity::EK_Temporary: |
| 3252 | // FIXME: Can we tell apart casting vs. converting? |
| 3253 | return Sema::AA_Casting; |
| 3254 | |
| 3255 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3256 | case InitializedEntity::EK_ArrayElement: |
| 3257 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3258 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3259 | return Sema::AA_Initializing; |
| 3260 | } |
| 3261 | |
| 3262 | return Sema::AA_Converting; |
| 3263 | } |
| 3264 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3265 | /// \brief Whether we should binding a created object as a temporary when |
| 3266 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3267 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3268 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3269 | case InitializedEntity::EK_ArrayElement: |
| 3270 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3271 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3272 | case InitializedEntity::EK_New: |
| 3273 | case InitializedEntity::EK_Variable: |
| 3274 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3275 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3276 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3277 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3278 | return false; |
| 3279 | |
| 3280 | case InitializedEntity::EK_Parameter: |
| 3281 | case InitializedEntity::EK_Temporary: |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
| 3285 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3286 | } |
| 3287 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3288 | /// \brief Whether the given entity, when initialized with an object |
| 3289 | /// created for that initialization, requires destruction. |
| 3290 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3291 | switch (Entity.getKind()) { |
| 3292 | case InitializedEntity::EK_Member: |
| 3293 | case InitializedEntity::EK_Result: |
| 3294 | case InitializedEntity::EK_New: |
| 3295 | case InitializedEntity::EK_Base: |
| 3296 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3297 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3298 | return false; |
| 3299 | |
| 3300 | case InitializedEntity::EK_Variable: |
| 3301 | case InitializedEntity::EK_Parameter: |
| 3302 | case InitializedEntity::EK_Temporary: |
| 3303 | case InitializedEntity::EK_ArrayElement: |
| 3304 | case InitializedEntity::EK_Exception: |
| 3305 | return true; |
| 3306 | } |
| 3307 | |
| 3308 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3309 | } |
| 3310 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3311 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3312 | /// provided by the given initializer by calling the appropriate copy |
| 3313 | /// constructor. |
| 3314 | /// |
| 3315 | /// \param S The Sema object used for type-checking. |
| 3316 | /// |
| 3317 | /// \param T The type of the temporary object, which must either by |
| 3318 | /// the type of the initializer expression or a superclass thereof. |
| 3319 | /// |
| 3320 | /// \param Enter The entity being initialized. |
| 3321 | /// |
| 3322 | /// \param CurInit The initializer expression. |
| 3323 | /// |
| 3324 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3325 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3326 | /// an rvalue. |
| 3327 | /// |
| 3328 | /// \returns An expression that copies the initializer expression into |
| 3329 | /// a temporary object, or an error expression if a copy could not be |
| 3330 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3331 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3332 | QualType T, |
| 3333 | const InitializedEntity &Entity, |
| 3334 | ExprResult CurInit, |
| 3335 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3336 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3337 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3338 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3339 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3340 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3341 | if (!Class) |
| 3342 | return move(CurInit); |
| 3343 | |
| 3344 | // C++0x [class.copy]p34: |
| 3345 | // When certain criteria are met, an implementation is allowed to |
| 3346 | // omit the copy/move construction of a class object, even if the |
| 3347 | // copy/move constructor and/or destructor for the object have |
| 3348 | // side effects. [...] |
| 3349 | // - when a temporary class object that has not been bound to a |
| 3350 | // reference (12.2) would be copied/moved to a class object |
| 3351 | // with the same cv-unqualified type, the copy/move operation |
| 3352 | // can be omitted by constructing the temporary object |
| 3353 | // directly into the target of the omitted copy/move |
| 3354 | // |
| 3355 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3356 | // elision for return statements and throw expressions are handled as part |
| 3357 | // of constructor initialization, while copy elision for exception handlers |
| 3358 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3359 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3360 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3361 | switch (Entity.getKind()) { |
| 3362 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3363 | Loc = Entity.getReturnLoc(); |
| 3364 | break; |
| 3365 | |
| 3366 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3367 | Loc = Entity.getThrowLoc(); |
| 3368 | break; |
| 3369 | |
| 3370 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3371 | Loc = Entity.getDecl()->getLocation(); |
| 3372 | break; |
| 3373 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3374 | case InitializedEntity::EK_ArrayElement: |
| 3375 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3376 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3377 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3378 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3379 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3380 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3381 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3382 | Loc = CurInitExpr->getLocStart(); |
| 3383 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3384 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Make sure that the type we are copying is complete. |
| 3387 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3388 | return move(CurInit); |
| 3389 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3390 | // Perform overload resolution using the class's copy constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3391 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3392 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3393 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3394 | Con != ConEnd; ++Con) { |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3395 | // Only consider copy constructors and constructor templates. Per |
| 3396 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3397 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3398 | CXXConstructorDecl *Constructor = 0; |
| 3399 | |
| 3400 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
| 3401 | // Handle copy constructors, only. |
| 3402 | if (!Constructor || Constructor->isInvalidDecl() || |
| 3403 | !Constructor->isCopyConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3404 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3405 | continue; |
| 3406 | |
| 3407 | DeclAccessPair FoundDecl |
| 3408 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3409 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3410 | &CurInitExpr, 1, CandidateSet); |
| 3411 | continue; |
| 3412 | } |
| 3413 | |
| 3414 | // Handle constructor templates. |
| 3415 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3416 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3417 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3419 | Constructor = cast<CXXConstructorDecl>( |
| 3420 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3421 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3422 | continue; |
| 3423 | |
| 3424 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3425 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3426 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3427 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3428 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3429 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3430 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3431 | |
| 3432 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3433 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3434 | case OR_Success: |
| 3435 | break; |
| 3436 | |
| 3437 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3438 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3439 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3440 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3441 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3442 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3443 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3444 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3445 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3446 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3447 | |
| 3448 | case OR_Ambiguous: |
| 3449 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3450 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3451 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3452 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3453 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3454 | |
| 3455 | case OR_Deleted: |
| 3456 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3457 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3458 | << CurInitExpr->getSourceRange(); |
| 3459 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3460 | << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3461 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3464 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3465 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3466 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3467 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3468 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3469 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3470 | |
| 3471 | if (IsExtraneousCopy) { |
| 3472 | // If this is a totally extraneous copy for C++03 reference |
| 3473 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3474 | // expression. We don't generate an (elided) copy operation here |
| 3475 | // because doing so would require us to pass down a flag to avoid |
| 3476 | // infinite recursion, where each step adds another extraneous, |
| 3477 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3478 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3479 | // Instantiate the default arguments of any extra parameters in |
| 3480 | // the selected copy constructor, as if we were going to create a |
| 3481 | // proper call to the copy constructor. |
| 3482 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 3483 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 3484 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 3485 | S.PDiag(diag::err_call_incomplete_argument))) |
| 3486 | break; |
| 3487 | |
| 3488 | // Build the default argument expression; we don't actually care |
| 3489 | // if this succeeds or not, because this routine will complain |
| 3490 | // if there was a problem. |
| 3491 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 3492 | } |
| 3493 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3494 | return S.Owned(CurInitExpr); |
| 3495 | } |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3496 | |
| 3497 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3498 | // constructor call (we might have derived-to-base conversions, or |
| 3499 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3500 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3501 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3502 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3503 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3504 | // Actually perform the constructor call. |
| 3505 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3506 | move_arg(ConstructorArgs), |
| 3507 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3508 | CXXConstructExpr::CK_Complete, |
| 3509 | SourceRange()); |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3510 | |
| 3511 | // If we're supposed to bind temporaries, do so. |
| 3512 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 3513 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3514 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3515 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3517 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 3518 | const InitializedEntity &Entity) { |
| 3519 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 3520 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 3521 | return; |
| 3522 | |
| 3523 | if (Entity.getDecl()->getDeclName()) |
| 3524 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 3525 | << Entity.getDecl()->getDeclName(); |
| 3526 | else |
| 3527 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 3528 | } |
| 3529 | } |
| 3530 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3531 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3532 | InitializationSequence::Perform(Sema &S, |
| 3533 | const InitializedEntity &Entity, |
| 3534 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3535 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3536 | QualType *ResultType) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3537 | if (SequenceKind == FailedSequence) { |
| 3538 | unsigned NumArgs = Args.size(); |
| 3539 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3540 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | if (SequenceKind == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3544 | // If the declaration is a non-dependent, incomplete array type |
| 3545 | // that has an initializer, then its type will be completed once |
| 3546 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3547 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3548 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3549 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3550 | if (const IncompleteArrayType *ArrayT |
| 3551 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 3552 | // FIXME: We don't currently have the ability to accurately |
| 3553 | // compute the length of an initializer list without |
| 3554 | // performing full type-checking of the initializer list |
| 3555 | // (since we have to determine where braces are implicitly |
| 3556 | // introduced and such). So, we fall back to making the array |
| 3557 | // type a dependently-sized array type with no specified |
| 3558 | // bound. |
| 3559 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 3560 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3562 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3563 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 3564 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 3565 | TypeLoc TL = TInfo->getTypeLoc(); |
| 3566 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 3567 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 3568 | Brackets = ArrayLoc->getBracketsRange(); |
| 3569 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | *ResultType |
| 3573 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 3574 | /*NumElts=*/0, |
| 3575 | ArrayT->getSizeModifier(), |
| 3576 | ArrayT->getIndexTypeCVRQualifiers(), |
| 3577 | Brackets); |
| 3578 | } |
| 3579 | |
| 3580 | } |
| 3581 | } |
| 3582 | |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3583 | if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast()) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3584 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3585 | |
Douglas Gregor | 67fa05b | 2010-02-05 07:56:11 +0000 | [diff] [blame] | 3586 | if (Args.size() == 0) |
| 3587 | return S.Owned((Expr *)0); |
| 3588 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3589 | unsigned NumArgs = Args.size(); |
| 3590 | return S.Owned(new (S.Context) ParenListExpr(S.Context, |
| 3591 | SourceLocation(), |
| 3592 | (Expr **)Args.release(), |
| 3593 | NumArgs, |
| 3594 | SourceLocation())); |
| 3595 | } |
| 3596 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3597 | if (SequenceKind == NoInitialization) |
| 3598 | return S.Owned((Expr *)0); |
| 3599 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3600 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 3601 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3602 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 3603 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3604 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3605 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3606 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3608 | ExprResult CurInit = S.Owned((Expr *)0); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3609 | |
| 3610 | assert(!Steps.empty() && "Cannot have an empty initialization sequence"); |
| 3611 | |
| 3612 | // For initialization steps that start with a single initializer, |
| 3613 | // grab the only argument out the Args and place it into the "current" |
| 3614 | // initializer. |
| 3615 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3616 | case SK_ResolveAddressOfOverloadedFunction: |
| 3617 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3618 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3619 | case SK_CastDerivedToBaseLValue: |
| 3620 | case SK_BindReference: |
| 3621 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3622 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3623 | case SK_UserConversion: |
| 3624 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3625 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3626 | case SK_QualificationConversionRValue: |
| 3627 | case SK_ConversionSequence: |
| 3628 | case SK_ListInitialization: |
| 3629 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3630 | case SK_StringInit: |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3631 | case SK_ObjCObjectConversion: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3632 | assert(Args.size() == 1); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3633 | Expr *CurInitExpr = Args.get()[0]; |
| 3634 | if (!CurInitExpr) return ExprError(); |
| 3635 | |
| 3636 | // Read from a property when initializing something with it. |
| 3637 | if (CurInitExpr->getObjectKind() == OK_ObjCProperty) |
| 3638 | S.ConvertPropertyForRValue(CurInitExpr); |
| 3639 | |
| 3640 | CurInit = ExprResult(CurInitExpr); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3641 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3642 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3643 | |
| 3644 | case SK_ConstructorInitialization: |
| 3645 | case SK_ZeroInitialization: |
| 3646 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | // Walk through the computed steps for the initialization sequence, |
| 3650 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3651 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3652 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 3653 | Step != StepEnd; ++Step) { |
| 3654 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3655 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3656 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3657 | Expr *CurInitExpr = CurInit.get(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3658 | QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3659 | |
| 3660 | switch (Step->Kind) { |
| 3661 | case SK_ResolveAddressOfOverloadedFunction: |
| 3662 | // Overload resolution determined which function invoke; update the |
| 3663 | // initializer to reflect that choice. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3664 | S.CheckAddressOfMemberAccess(CurInitExpr, Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3665 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3666 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3667 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3668 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | break; |
| 3670 | |
| 3671 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3672 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3673 | case SK_CastDerivedToBaseLValue: { |
| 3674 | // We have a derived-to-base cast that produces either an rvalue or an |
| 3675 | // lvalue. Perform that cast. |
| 3676 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3677 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3678 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3679 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 3680 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 3681 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
| 3682 | CurInitExpr->getLocStart(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3683 | CurInitExpr->getSourceRange(), |
| 3684 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3685 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3687 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 3688 | QualType T = SourceType; |
| 3689 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 3690 | T = Pointer->getPointeeType(); |
| 3691 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
| 3692 | S.MarkVTableUsed(CurInitExpr->getLocStart(), |
| 3693 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 3694 | } |
| 3695 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3696 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3697 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3698 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3699 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3700 | VK_XValue : |
| 3701 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3702 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 3703 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3704 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3705 | CurInit.get(), |
| 3706 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3707 | break; |
| 3708 | } |
| 3709 | |
| 3710 | case SK_BindReference: |
| 3711 | if (FieldDecl *BitField = CurInitExpr->getBitField()) { |
| 3712 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 3713 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3714 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3715 | << BitField->getDeclName() |
| 3716 | << CurInitExpr->getSourceRange(); |
| 3717 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3718 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3719 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 3720 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3721 | if (CurInitExpr->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 3722 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3723 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 3724 | << Entity.getType().isVolatileQualified() |
| 3725 | << CurInitExpr->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3726 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3727 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3730 | // Reference binding does not have any corresponding ASTs. |
| 3731 | |
| 3732 | // Check exception specifications |
| 3733 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3734 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3735 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3736 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3737 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3738 | case SK_BindReferenceToTemporary: |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 3739 | // Reference binding does not have any corresponding ASTs. |
| 3740 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3741 | // Check exception specifications |
| 3742 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3743 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3744 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3745 | break; |
| 3746 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3747 | case SK_ExtraneousCopyToTemporary: |
| 3748 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
| 3749 | /*IsExtraneousCopy=*/true); |
| 3750 | break; |
| 3751 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3752 | case SK_UserConversion: { |
| 3753 | // We have a user-defined conversion that invokes either a constructor |
| 3754 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3755 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3756 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3757 | FunctionDecl *Fn = Step->Function.Function; |
| 3758 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3759 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3760 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3761 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3762 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3763 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3764 | SourceLocation Loc = CurInitExpr->getLocStart(); |
| 3765 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3767 | // Determine the arguments required to actually perform the constructor |
| 3768 | // call. |
| 3769 | if (S.CompleteConstructorCall(Constructor, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3770 | MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3771 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3772 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3773 | |
| 3774 | // Build the an expression that constructs a temporary. |
| 3775 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3776 | move_arg(ConstructorArgs), |
| 3777 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3778 | CXXConstructExpr::CK_Complete, |
| 3779 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3780 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3781 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3782 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3783 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3784 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3785 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3786 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3787 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3788 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 3789 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 3790 | S.IsDerivedFrom(SourceType, Class)) |
| 3791 | IsCopy = true; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3792 | |
| 3793 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3794 | } else { |
| 3795 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3796 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3797 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3798 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3799 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3800 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3802 | // FIXME: Should we move this initialization into a separate |
| 3803 | // derived-to-base conversion? I believe the answer is "no", because |
| 3804 | // we don't want to turn off access control here for c-style casts. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3805 | if (S.PerformObjectArgumentInitialization(CurInitExpr, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3806 | FoundFn, Conversion)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3807 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Do a little dance to make sure that CurInit has the proper |
| 3810 | // pointer. |
| 3811 | CurInit.release(); |
| 3812 | |
| 3813 | // Build the actual call to the conversion function. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3814 | CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, FoundFn, |
| 3815 | Conversion)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3816 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3817 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3818 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3819 | CastKind = CK_UserDefinedConversion; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3820 | |
| 3821 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3824 | bool RequiresCopy = !IsCopy && |
| 3825 | getKind() != InitializationSequence::ReferenceBinding; |
| 3826 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3827 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3828 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
| 3829 | CurInitExpr = static_cast<Expr *>(CurInit.get()); |
| 3830 | QualType T = CurInitExpr->getType(); |
| 3831 | if (const RecordType *Record = T->getAs<RecordType>()) { |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3832 | CXXDestructorDecl *Destructor |
| 3833 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3834 | S.CheckDestructorAccess(CurInitExpr->getLocStart(), Destructor, |
| 3835 | S.PDiag(diag::err_access_dtor_temp) << T); |
| 3836 | S.MarkDeclarationReferenced(CurInitExpr->getLocStart(), Destructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3837 | S.DiagnoseUseOfDecl(Destructor, CurInitExpr->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3838 | } |
| 3839 | } |
| 3840 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3841 | CurInitExpr = CurInit.takeAs<Expr>(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3842 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3843 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 3844 | CurInitExpr->getType(), |
| 3845 | CastKind, CurInitExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3846 | IsLvalue ? VK_LValue : VK_RValue)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3848 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3849 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 3850 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3852 | break; |
| 3853 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3855 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3856 | case SK_QualificationConversionXValue: |
| 3857 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3858 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3859 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3860 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3861 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3862 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3863 | VK_XValue : |
| 3864 | VK_RValue); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3865 | S.ImpCastExprToType(CurInitExpr, Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3866 | CurInit.release(); |
| 3867 | CurInit = S.Owned(CurInitExpr); |
| 3868 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3871 | case SK_ConversionSequence: { |
| 3872 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 3873 | |
| 3874 | if (S.PerformImplicitConversion(CurInitExpr, Step->Type, *Step->ICS, |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3875 | getAssignmentAction(Entity), |
| 3876 | IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3877 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3878 | |
| 3879 | CurInit.release(); |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3880 | CurInit = S.Owned(CurInitExpr); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3881 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3884 | case SK_ListInitialization: { |
| 3885 | InitListExpr *InitList = cast<InitListExpr>(CurInitExpr); |
| 3886 | QualType Ty = Step->Type; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3887 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3888 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3889 | |
| 3890 | CurInit.release(); |
| 3891 | CurInit = S.Owned(InitList); |
| 3892 | break; |
| 3893 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3894 | |
| 3895 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3896 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3897 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3898 | = cast<CXXConstructorDecl>(Step->Function.Function); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3900 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3901 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 3902 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 3903 | ? Kind.getEqualLoc() |
| 3904 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3905 | |
| 3906 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 3907 | // Force even a trivial, implicit default constructor to be |
| 3908 | // semantically checked. We do this explicitly because we don't build |
| 3909 | // the definition for completely trivial constructors. |
| 3910 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3911 | assert(ClassDecl && "No parent class for constructor."); |
| 3912 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 3913 | ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false)) |
| 3914 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 3915 | } |
| 3916 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3917 | // Determine the arguments required to actually perform the constructor |
| 3918 | // call. |
| 3919 | if (S.CompleteConstructorCall(Constructor, move(Args), |
| 3920 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3921 | return ExprError(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3922 | |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3924 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3925 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3926 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 3927 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 3928 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 3929 | unsigned NumExprs = ConstructorArgs.size(); |
| 3930 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 3931 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3932 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 3933 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 3934 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 3935 | if (!TSInfo) |
| 3936 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
| 3937 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3938 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 3939 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 3940 | TSInfo, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3941 | Exprs, |
| 3942 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3943 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 3944 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 3947 | CXXConstructExpr::CK_Complete; |
| 3948 | |
| 3949 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 3950 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 3951 | CXXConstructExpr::CK_VirtualBase : |
| 3952 | CXXConstructExpr::CK_NonVirtualBase; |
| 3953 | } |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3954 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3955 | // Only get the parenthesis range if it is a direct construction. |
| 3956 | SourceRange parenRange = |
| 3957 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 3958 | Kind.getParenRange() : SourceRange(); |
| 3959 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3960 | // If the entity allows NRVO, mark the construction as elidable |
| 3961 | // unconditionally. |
| 3962 | if (Entity.allowsNRVO()) |
| 3963 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 3964 | Constructor, /*Elidable=*/true, |
| 3965 | move_arg(ConstructorArgs), |
| 3966 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3967 | ConstructKind, |
| 3968 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3969 | else |
| 3970 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 3971 | Constructor, |
| 3972 | move_arg(ConstructorArgs), |
| 3973 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3974 | ConstructKind, |
| 3975 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3976 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3977 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3978 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3979 | |
| 3980 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3981 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3982 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3983 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3985 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3986 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3988 | break; |
| 3989 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3990 | |
| 3991 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3992 | step_iterator NextStep = Step; |
| 3993 | ++NextStep; |
| 3994 | if (NextStep != StepEnd && |
| 3995 | NextStep->Kind == SK_ConstructorInitialization) { |
| 3996 | // The need for zero-initialization is recorded directly into |
| 3997 | // the call to the object's constructor within the next step. |
| 3998 | ConstructorInitRequiresZeroInit = true; |
| 3999 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4000 | S.getLangOptions().CPlusPlus && |
| 4001 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4002 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4003 | if (!TSInfo) |
| 4004 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
| 4005 | Kind.getRange().getBegin()); |
| 4006 | |
| 4007 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4008 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4009 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4010 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4011 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4012 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4013 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4014 | break; |
| 4015 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4016 | |
| 4017 | case SK_CAssignment: { |
| 4018 | QualType SourceType = CurInitExpr->getType(); |
| 4019 | Sema::AssignConvertType ConvTy = |
| 4020 | S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4021 | |
| 4022 | // If this is a call, allow conversion to a transparent union. |
| 4023 | if (ConvTy != Sema::Compatible && |
| 4024 | Entity.getKind() == InitializedEntity::EK_Parameter && |
| 4025 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr) |
| 4026 | == Sema::Compatible) |
| 4027 | ConvTy = Sema::Compatible; |
| 4028 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4029 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4030 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4031 | Step->Type, SourceType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4032 | CurInitExpr, |
| 4033 | getAssignmentAction(Entity), |
| 4034 | &Complained)) { |
| 4035 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4036 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4037 | } else if (Complained) |
| 4038 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4039 | |
| 4040 | CurInit.release(); |
| 4041 | CurInit = S.Owned(CurInitExpr); |
| 4042 | break; |
| 4043 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4044 | |
| 4045 | case SK_StringInit: { |
| 4046 | QualType Ty = Step->Type; |
| 4047 | CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S); |
| 4048 | break; |
| 4049 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4050 | |
| 4051 | case SK_ObjCObjectConversion: |
| 4052 | S.ImpCastExprToType(CurInitExpr, Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4053 | CK_ObjCObjectLValueCast, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4054 | S.CastCategory(CurInitExpr)); |
| 4055 | CurInit.release(); |
| 4056 | CurInit = S.Owned(CurInitExpr); |
| 4057 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4058 | } |
| 4059 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4060 | |
| 4061 | // Diagnose non-fatal problems with the completed initialization. |
| 4062 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4063 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4064 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4065 | cast<FieldDecl>(Entity.getDecl()), |
| 4066 | CurInit.get()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4067 | |
| 4068 | return move(CurInit); |
| 4069 | } |
| 4070 | |
| 4071 | //===----------------------------------------------------------------------===// |
| 4072 | // Diagnose initialization failures |
| 4073 | //===----------------------------------------------------------------------===// |
| 4074 | bool InitializationSequence::Diagnose(Sema &S, |
| 4075 | const InitializedEntity &Entity, |
| 4076 | const InitializationKind &Kind, |
| 4077 | Expr **Args, unsigned NumArgs) { |
| 4078 | if (SequenceKind != FailedSequence) |
| 4079 | return false; |
| 4080 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4081 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4082 | switch (Failure) { |
| 4083 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4084 | // FIXME: Customize for the initialized entity? |
| 4085 | if (NumArgs == 0) |
| 4086 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4087 | << DestType.getNonReferenceType(); |
| 4088 | else // FIXME: diagnostic below could be better! |
| 4089 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4090 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | |
| 4093 | case FK_ArrayNeedsInitList: |
| 4094 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4095 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4096 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4097 | break; |
| 4098 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4099 | case FK_AddressOfOverloadFailed: { |
| 4100 | DeclAccessPair Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4101 | S.ResolveAddressOfOverloadedFunction(Args[0], |
| 4102 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4103 | true, |
| 4104 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4105 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4107 | |
| 4108 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4109 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4110 | switch (FailedOverloadResult) { |
| 4111 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4112 | if (Failure == FK_UserConversionOverloadFailed) |
| 4113 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4114 | << Args[0]->getType() << DestType |
| 4115 | << Args[0]->getSourceRange(); |
| 4116 | else |
| 4117 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4118 | << DestType << Args[0]->getType() |
| 4119 | << Args[0]->getSourceRange(); |
| 4120 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4121 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4122 | break; |
| 4123 | |
| 4124 | case OR_No_Viable_Function: |
| 4125 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4126 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4127 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4128 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4129 | break; |
| 4130 | |
| 4131 | case OR_Deleted: { |
| 4132 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4133 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4134 | << Args[0]->getSourceRange(); |
| 4135 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4136 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4137 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4138 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4139 | if (Ovl == OR_Deleted) { |
| 4140 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4141 | << Best->Function->isDeleted(); |
| 4142 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4143 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4144 | } |
| 4145 | break; |
| 4146 | } |
| 4147 | |
| 4148 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4149 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4150 | break; |
| 4151 | } |
| 4152 | break; |
| 4153 | |
| 4154 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4155 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4156 | S.Diag(Kind.getLocation(), |
| 4157 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4158 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4159 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4160 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4161 | << DestType.getNonReferenceType() |
| 4162 | << Args[0]->getType() |
| 4163 | << Args[0]->getSourceRange(); |
| 4164 | break; |
| 4165 | |
| 4166 | case FK_RValueReferenceBindingToLValue: |
| 4167 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
| 4168 | << Args[0]->getSourceRange(); |
| 4169 | break; |
| 4170 | |
| 4171 | case FK_ReferenceInitDropsQualifiers: |
| 4172 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4173 | << DestType.getNonReferenceType() |
| 4174 | << Args[0]->getType() |
| 4175 | << Args[0]->getSourceRange(); |
| 4176 | break; |
| 4177 | |
| 4178 | case FK_ReferenceInitFailed: |
| 4179 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4180 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4181 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4182 | << Args[0]->getType() |
| 4183 | << Args[0]->getSourceRange(); |
| 4184 | break; |
| 4185 | |
| 4186 | case FK_ConversionFailed: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4187 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4188 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4189 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4190 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4191 | << Args[0]->getType() |
| 4192 | << Args[0]->getSourceRange(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4193 | break; |
| 4194 | |
| 4195 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4196 | SourceRange R; |
| 4197 | |
| 4198 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4199 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4200 | InitList->getLocEnd()); |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4201 | else |
| 4202 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4204 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4205 | if (Kind.isCStyleOrFunctionalCast()) |
| 4206 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4207 | << R; |
| 4208 | else |
| 4209 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4210 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4211 | break; |
| 4212 | } |
| 4213 | |
| 4214 | case FK_ReferenceBindingToInitList: |
| 4215 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4216 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4217 | break; |
| 4218 | |
| 4219 | case FK_InitListBadDestinationType: |
| 4220 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4221 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4222 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4223 | |
| 4224 | case FK_ConstructorOverloadFailed: { |
| 4225 | SourceRange ArgsRange; |
| 4226 | if (NumArgs) |
| 4227 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
| 4228 | Args[NumArgs - 1]->getLocEnd()); |
| 4229 | |
| 4230 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4231 | // bad. |
| 4232 | switch (FailedOverloadResult) { |
| 4233 | case OR_Ambiguous: |
| 4234 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4235 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4236 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4237 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4238 | break; |
| 4239 | |
| 4240 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4241 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4242 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4243 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4244 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4245 | // This is implicit default initialization of a member or |
| 4246 | // base within a constructor. If no viable function was |
| 4247 | // found, notify the user that she needs to explicitly |
| 4248 | // initialize this base/member. |
| 4249 | CXXConstructorDecl *Constructor |
| 4250 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4251 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4252 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4253 | << Constructor->isImplicit() |
| 4254 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4255 | << /*base=*/0 |
| 4256 | << Entity.getType(); |
| 4257 | |
| 4258 | RecordDecl *BaseDecl |
| 4259 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4260 | ->getDecl(); |
| 4261 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4262 | << S.Context.getTagDeclType(BaseDecl); |
| 4263 | } else { |
| 4264 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4265 | << Constructor->isImplicit() |
| 4266 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4267 | << /*member=*/1 |
| 4268 | << Entity.getName(); |
| 4269 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4270 | |
| 4271 | if (const RecordType *Record |
| 4272 | = Entity.getType()->getAs<RecordType>()) |
| 4273 | S.Diag(Record->getDecl()->getLocation(), |
| 4274 | diag::note_previous_decl) |
| 4275 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4276 | } |
| 4277 | break; |
| 4278 | } |
| 4279 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4280 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4281 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4282 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4283 | break; |
| 4284 | |
| 4285 | case OR_Deleted: { |
| 4286 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4287 | << true << DestType << ArgsRange; |
| 4288 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4289 | OverloadingResult Ovl |
| 4290 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4291 | if (Ovl == OR_Deleted) { |
| 4292 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4293 | << Best->Function->isDeleted(); |
| 4294 | } else { |
| 4295 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4296 | } |
| 4297 | break; |
| 4298 | } |
| 4299 | |
| 4300 | case OR_Success: |
| 4301 | llvm_unreachable("Conversion did not fail!"); |
| 4302 | break; |
| 4303 | } |
| 4304 | break; |
| 4305 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4306 | |
| 4307 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4308 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4309 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4310 | // This is implicit default-initialization of a const member in |
| 4311 | // a constructor. Complain that it needs to be explicitly |
| 4312 | // initialized. |
| 4313 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4314 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4315 | << Constructor->isImplicit() |
| 4316 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4317 | << /*const=*/1 |
| 4318 | << Entity.getName(); |
| 4319 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4320 | << Entity.getName(); |
| 4321 | } else { |
| 4322 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4323 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4324 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4325 | break; |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4326 | |
| 4327 | case FK_Incomplete: |
| 4328 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 4329 | diag::err_init_incomplete_type); |
| 4330 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4333 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4334 | return true; |
| 4335 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4336 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4337 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 4338 | switch (SequenceKind) { |
| 4339 | case FailedSequence: { |
| 4340 | OS << "Failed sequence: "; |
| 4341 | switch (Failure) { |
| 4342 | case FK_TooManyInitsForReference: |
| 4343 | OS << "too many initializers for reference"; |
| 4344 | break; |
| 4345 | |
| 4346 | case FK_ArrayNeedsInitList: |
| 4347 | OS << "array requires initializer list"; |
| 4348 | break; |
| 4349 | |
| 4350 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4351 | OS << "array requires initializer list or string literal"; |
| 4352 | break; |
| 4353 | |
| 4354 | case FK_AddressOfOverloadFailed: |
| 4355 | OS << "address of overloaded function failed"; |
| 4356 | break; |
| 4357 | |
| 4358 | case FK_ReferenceInitOverloadFailed: |
| 4359 | OS << "overload resolution for reference initialization failed"; |
| 4360 | break; |
| 4361 | |
| 4362 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4363 | OS << "non-const lvalue reference bound to temporary"; |
| 4364 | break; |
| 4365 | |
| 4366 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4367 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4368 | break; |
| 4369 | |
| 4370 | case FK_RValueReferenceBindingToLValue: |
| 4371 | OS << "rvalue reference bound to an lvalue"; |
| 4372 | break; |
| 4373 | |
| 4374 | case FK_ReferenceInitDropsQualifiers: |
| 4375 | OS << "reference initialization drops qualifiers"; |
| 4376 | break; |
| 4377 | |
| 4378 | case FK_ReferenceInitFailed: |
| 4379 | OS << "reference initialization failed"; |
| 4380 | break; |
| 4381 | |
| 4382 | case FK_ConversionFailed: |
| 4383 | OS << "conversion failed"; |
| 4384 | break; |
| 4385 | |
| 4386 | case FK_TooManyInitsForScalar: |
| 4387 | OS << "too many initializers for scalar"; |
| 4388 | break; |
| 4389 | |
| 4390 | case FK_ReferenceBindingToInitList: |
| 4391 | OS << "referencing binding to initializer list"; |
| 4392 | break; |
| 4393 | |
| 4394 | case FK_InitListBadDestinationType: |
| 4395 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 4396 | break; |
| 4397 | |
| 4398 | case FK_UserConversionOverloadFailed: |
| 4399 | OS << "overloading failed for user-defined conversion"; |
| 4400 | break; |
| 4401 | |
| 4402 | case FK_ConstructorOverloadFailed: |
| 4403 | OS << "constructor overloading failed"; |
| 4404 | break; |
| 4405 | |
| 4406 | case FK_DefaultInitOfConst: |
| 4407 | OS << "default initialization of a const variable"; |
| 4408 | break; |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4409 | |
| 4410 | case FK_Incomplete: |
| 4411 | OS << "initialization of incomplete type"; |
| 4412 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4413 | } |
| 4414 | OS << '\n'; |
| 4415 | return; |
| 4416 | } |
| 4417 | |
| 4418 | case DependentSequence: |
| 4419 | OS << "Dependent sequence: "; |
| 4420 | return; |
| 4421 | |
| 4422 | case UserDefinedConversion: |
| 4423 | OS << "User-defined conversion sequence: "; |
| 4424 | break; |
| 4425 | |
| 4426 | case ConstructorInitialization: |
| 4427 | OS << "Constructor initialization sequence: "; |
| 4428 | break; |
| 4429 | |
| 4430 | case ReferenceBinding: |
| 4431 | OS << "Reference binding: "; |
| 4432 | break; |
| 4433 | |
| 4434 | case ListInitialization: |
| 4435 | OS << "List initialization: "; |
| 4436 | break; |
| 4437 | |
| 4438 | case ZeroInitialization: |
| 4439 | OS << "Zero initialization\n"; |
| 4440 | return; |
| 4441 | |
| 4442 | case NoInitialization: |
| 4443 | OS << "No initialization\n"; |
| 4444 | return; |
| 4445 | |
| 4446 | case StandardConversion: |
| 4447 | OS << "Standard conversion: "; |
| 4448 | break; |
| 4449 | |
| 4450 | case CAssignment: |
| 4451 | OS << "C assignment: "; |
| 4452 | break; |
| 4453 | |
| 4454 | case StringInit: |
| 4455 | OS << "String initialization: "; |
| 4456 | break; |
| 4457 | } |
| 4458 | |
| 4459 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 4460 | if (S != step_begin()) { |
| 4461 | OS << " -> "; |
| 4462 | } |
| 4463 | |
| 4464 | switch (S->Kind) { |
| 4465 | case SK_ResolveAddressOfOverloadedFunction: |
| 4466 | OS << "resolve address of overloaded function"; |
| 4467 | break; |
| 4468 | |
| 4469 | case SK_CastDerivedToBaseRValue: |
| 4470 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 4471 | break; |
| 4472 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4473 | case SK_CastDerivedToBaseXValue: |
| 4474 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 4475 | break; |
| 4476 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4477 | case SK_CastDerivedToBaseLValue: |
| 4478 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 4479 | break; |
| 4480 | |
| 4481 | case SK_BindReference: |
| 4482 | OS << "bind reference to lvalue"; |
| 4483 | break; |
| 4484 | |
| 4485 | case SK_BindReferenceToTemporary: |
| 4486 | OS << "bind reference to a temporary"; |
| 4487 | break; |
| 4488 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4489 | case SK_ExtraneousCopyToTemporary: |
| 4490 | OS << "extraneous C++03 copy to temporary"; |
| 4491 | break; |
| 4492 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4493 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4494 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4495 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4496 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4497 | case SK_QualificationConversionRValue: |
| 4498 | OS << "qualification conversion (rvalue)"; |
| 4499 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4500 | case SK_QualificationConversionXValue: |
| 4501 | OS << "qualification conversion (xvalue)"; |
| 4502 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4503 | case SK_QualificationConversionLValue: |
| 4504 | OS << "qualification conversion (lvalue)"; |
| 4505 | break; |
| 4506 | |
| 4507 | case SK_ConversionSequence: |
| 4508 | OS << "implicit conversion sequence ("; |
| 4509 | S->ICS->DebugPrint(); // FIXME: use OS |
| 4510 | OS << ")"; |
| 4511 | break; |
| 4512 | |
| 4513 | case SK_ListInitialization: |
| 4514 | OS << "list initialization"; |
| 4515 | break; |
| 4516 | |
| 4517 | case SK_ConstructorInitialization: |
| 4518 | OS << "constructor initialization"; |
| 4519 | break; |
| 4520 | |
| 4521 | case SK_ZeroInitialization: |
| 4522 | OS << "zero initialization"; |
| 4523 | break; |
| 4524 | |
| 4525 | case SK_CAssignment: |
| 4526 | OS << "C assignment"; |
| 4527 | break; |
| 4528 | |
| 4529 | case SK_StringInit: |
| 4530 | OS << "string initialization"; |
| 4531 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4532 | |
| 4533 | case SK_ObjCObjectConversion: |
| 4534 | OS << "Objective-C object conversion"; |
| 4535 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | void InitializationSequence::dump() const { |
| 4541 | dump(llvm::errs()); |
| 4542 | } |
| 4543 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4544 | //===----------------------------------------------------------------------===// |
| 4545 | // Initialization helper functions |
| 4546 | //===----------------------------------------------------------------------===// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4547 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4548 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 4549 | SourceLocation EqualLoc, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4550 | ExprResult Init) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4551 | if (Init.isInvalid()) |
| 4552 | return ExprError(); |
| 4553 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4554 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4555 | assert(InitE && "No initialization expression?"); |
| 4556 | |
| 4557 | if (EqualLoc.isInvalid()) |
| 4558 | EqualLoc = InitE->getLocStart(); |
| 4559 | |
| 4560 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 4561 | EqualLoc); |
| 4562 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 4563 | Init.release(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4564 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4565 | } |