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