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