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