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