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