Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
| 13 | // |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 14 | // This file also implements Sema::CheckInitializerTypes. |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 18 | #include "SemaInit.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "Sema.h" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 20 | #include "clang/Parse/Designator.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 25 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 26 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 27 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Sema Initialization Checking |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 32 | static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) { |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 33 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
| 34 | if (!AT) return 0; |
| 35 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 36 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 37 | return 0; |
| 38 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 39 | // See if this is a string literal or @encode. |
| 40 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 42 | // Handle @encode, which is a narrow string. |
| 43 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 44 | return Init; |
| 45 | |
| 46 | // Otherwise we can only handle string literals. |
| 47 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 48 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 49 | |
| 50 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 51 | // char array can be initialized with a narrow string. |
| 52 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 53 | if (!SL->isWide()) |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 55 | |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 57 | // correction from DR343): "An array with element type compatible with a |
| 58 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 59 | // string literal, optionally enclosed in braces." |
| 60 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 61 | ElemTy.getUnqualifiedType())) |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 62 | return Init; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | static bool CheckSingleInitializer(Expr *&Init, QualType DeclType, |
Chris Lattner | 95e8d65 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 68 | bool DirectInit, Sema &S) { |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 69 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 70 | // it can promote the expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | QualType InitType = Init->getType(); |
| 72 | |
Chris Lattner | 95e8d65 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 73 | if (S.getLangOptions().CPlusPlus) { |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 74 | // FIXME: I dislike this error message. A lot. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 75 | if (S.PerformImplicitConversion(Init, DeclType, |
| 76 | "initializing", DirectInit)) { |
| 77 | ImplicitConversionSequence ICS; |
| 78 | OverloadCandidateSet CandidateSet; |
| 79 | if (S.IsUserDefinedConversion(Init, DeclType, ICS.UserDefined, |
| 80 | CandidateSet, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 81 | true, false, false) != OR_Ambiguous) |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 82 | return S.Diag(Init->getSourceRange().getBegin(), |
| 83 | diag::err_typecheck_convert_incompatible) |
| 84 | << DeclType << Init->getType() << "initializing" |
| 85 | << Init->getSourceRange(); |
| 86 | S.Diag(Init->getSourceRange().getBegin(), |
| 87 | diag::err_typecheck_convert_ambiguous) |
| 88 | << DeclType << Init->getType() << Init->getSourceRange(); |
| 89 | S.PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 90 | return true; |
| 91 | } |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 95e8d65 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 95 | Sema::AssignConvertType ConvTy = |
| 96 | S.CheckSingleAssignmentConstraints(DeclType, Init); |
| 97 | return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 98 | InitType, Init, "initializing"); |
| 99 | } |
| 100 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 101 | static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) { |
| 102 | // Get the length of the string as parsed. |
| 103 | uint64_t StrLength = |
| 104 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 105 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 107 | const ArrayType *AT = S.Context.getAsArrayType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 108 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 110 | // being initialized to a string literal. |
| 111 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 112 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 113 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 114 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 115 | ConstVal, |
| 116 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 117 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 120 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 122 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 123 | // the size may be smaller or larger than the string we are initializing. |
| 124 | // FIXME: Avoid truncation for 64-bit length strings. |
| 125 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 126 | S.Diag(Str->getSourceRange().getBegin(), |
| 127 | diag::warn_initializer_string_for_char_array_too_long) |
| 128 | << Str->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 130 | // Set the type to the actual size that we are initializing. If we have |
| 131 | // something like: |
| 132 | // char x[1] = "foo"; |
| 133 | // then this will set the string literal's type to char[1]. |
| 134 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType, |
| 138 | SourceLocation InitLoc, |
Anders Carlsson | 0f5f2c6 | 2009-05-30 20:41:30 +0000 | [diff] [blame] | 139 | DeclarationName InitEntity, bool DirectInit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | if (DeclType->isDependentType() || |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 141 | Init->isTypeDependent() || Init->isValueDependent()) { |
| 142 | // We have either a dependent type or a type- or value-dependent |
| 143 | // initializer, so we don't perform any additional checking at |
| 144 | // this point. |
| 145 | |
| 146 | // If the declaration is a non-dependent, incomplete array type |
| 147 | // that has an initializer, then its type will be completed once |
Douglas Gregor | 73460a3 | 2009-11-19 23:25:22 +0000 | [diff] [blame] | 148 | // the initializer is instantiated. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 149 | if (!DeclType->isDependentType()) { |
| 150 | if (const IncompleteArrayType *ArrayT |
| 151 | = Context.getAsIncompleteArrayType(DeclType)) { |
Douglas Gregor | 73460a3 | 2009-11-19 23:25:22 +0000 | [diff] [blame] | 152 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 153 | if (!ILE->isTypeDependent()) { |
| 154 | // Compute the constant array type from the length of the |
| 155 | // initializer list. |
| 156 | // FIXME: This will be wrong if there are designated |
| 157 | // initializations. Good thing they don't exist in C++! |
| 158 | llvm::APInt NumElements(Context.getTypeSize(Context.getSizeType()), |
| 159 | ILE->getNumInits()); |
| 160 | llvm::APInt Zero(Context.getTypeSize(Context.getSizeType()), 0); |
| 161 | if (NumElements == Zero) { |
| 162 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 163 | // but is supported by GNU. |
| 164 | Diag(ILE->getLocStart(), diag::ext_typecheck_zero_array_size); |
| 165 | } |
| 166 | |
| 167 | DeclType = Context.getConstantArrayType(ArrayT->getElementType(), |
| 168 | NumElements, |
| 169 | ArrayT->getSizeModifier(), |
| 170 | ArrayT->getIndexTypeCVRQualifiers()); |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Make the array type-dependent by making it dependently-sized. |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 176 | DeclType = Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 177 | /*NumElts=*/0, |
| 178 | ArrayT->getSizeModifier(), |
| 179 | ArrayT->getIndexTypeCVRQualifiers(), |
| 180 | SourceRange()); |
| 181 | } |
| 182 | } |
| 183 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 184 | return false; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 187 | // C++ [dcl.init.ref]p1: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 188 | // A variable declared to be a T& or T&&, that is "reference to type T" |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 189 | // (8.3.2), shall be initialized by an object, or function, of |
| 190 | // type T or by an object that can be converted into a T. |
| 191 | if (DeclType->isReferenceType()) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 192 | return CheckReferenceInit(Init, DeclType, InitLoc, |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 193 | /*SuppressUserConversions=*/false, |
| 194 | /*AllowExplicit=*/DirectInit, |
| 195 | /*ForceRValue=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 197 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 198 | // of unknown size ("[]") or an object type that is not a variable array type. |
| 199 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
| 200 | return Diag(InitLoc, diag::err_variable_object_no_init) |
| 201 | << VAT->getSizeExpr()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 204 | if (!InitList) { |
| 205 | // FIXME: Handle wide strings |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 206 | if (Expr *Str = IsStringInit(Init, DeclType, Context)) { |
| 207 | CheckStringInit(Str, DeclType, *this); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 211 | // C++ [dcl.init]p14: |
| 212 | // -- If the destination type is a (possibly cv-qualified) class |
| 213 | // type: |
| 214 | if (getLangOptions().CPlusPlus && DeclType->isRecordType()) { |
| 215 | QualType DeclTypeC = Context.getCanonicalType(DeclType); |
| 216 | QualType InitTypeC = Context.getCanonicalType(Init->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 218 | // -- If the initialization is direct-initialization, or if it is |
| 219 | // copy-initialization where the cv-unqualified version of the |
| 220 | // source type is the same class as, or a derived class of, the |
| 221 | // class of the destination, constructors are considered. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 222 | if ((DeclTypeC.getLocalUnqualifiedType() |
| 223 | == InitTypeC.getLocalUnqualifiedType()) || |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 224 | IsDerivedFrom(InitTypeC, DeclTypeC)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | const CXXRecordDecl *RD = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 226 | cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Anders Carlsson | bffed8a | 2009-05-27 16:38:58 +0000 | [diff] [blame] | 228 | // No need to make a CXXConstructExpr if both the ctor and dtor are |
| 229 | // trivial. |
| 230 | if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor()) |
| 231 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 233 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 234 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 235 | // FIXME: Poor location information |
| 236 | InitializationKind InitKind |
| 237 | = InitializationKind::CreateCopy(Init->getLocStart(), |
| 238 | SourceLocation()); |
| 239 | if (DirectInit) |
| 240 | InitKind = InitializationKind::CreateDirect(Init->getLocStart(), |
| 241 | SourceLocation(), |
| 242 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | CXXConstructorDecl *Constructor |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 244 | = PerformInitializationByConstructor(DeclType, |
| 245 | MultiExprArg(*this, |
| 246 | (void **)&Init, 1), |
| 247 | InitLoc, Init->getSourceRange(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 248 | InitEntity, InitKind, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 249 | ConstructorArgs); |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 250 | if (!Constructor) |
| 251 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
| 253 | OwningExprResult InitResult = |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 254 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | DeclType, Constructor, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 256 | move_arg(ConstructorArgs)); |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 257 | if (InitResult.isInvalid()) |
| 258 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 260 | Init = InitResult.takeAs<Expr>(); |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 261 | return false; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 264 | // -- Otherwise (i.e., for the remaining copy-initialization |
| 265 | // cases), user-defined conversion sequences that can |
| 266 | // convert from the source type to the destination type or |
| 267 | // (when a conversion function is used) to a derived class |
| 268 | // thereof are enumerated as described in 13.3.1.4, and the |
| 269 | // best one is chosen through overload resolution |
| 270 | // (13.3). If the conversion cannot be done or is |
| 271 | // ambiguous, the initialization is ill-formed. The |
| 272 | // function selected is called with the initializer |
| 273 | // expression as its argument; if the function is a |
| 274 | // constructor, the call initializes a temporary of the |
| 275 | // destination type. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 276 | // FIXME: We're pretending to do copy elision here; return to this when we |
| 277 | // have ASTs for such things. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 278 | if (!PerformImplicitConversion(Init, DeclType, "initializing")) |
| 279 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 281 | if (InitEntity) |
| 282 | return Diag(InitLoc, diag::err_cannot_initialize_decl) |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 283 | << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 284 | << Init->getType() << Init->getSourceRange(); |
| 285 | return Diag(InitLoc, diag::err_cannot_initialize_decl_noname) |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 286 | << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 287 | << Init->getType() << Init->getSourceRange(); |
| 288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 290 | // C99 6.7.8p16. |
| 291 | if (DeclType->isArrayType()) |
| 292 | return Diag(Init->getLocStart(), diag::err_array_init_list_required) |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 293 | << Init->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 95e8d65 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 295 | return CheckSingleInitializer(Init, DeclType, DirectInit, *this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 298 | bool hadError = CheckInitList(InitList, DeclType); |
| 299 | Init = InitList; |
| 300 | return hadError; |
| 301 | } |
| 302 | |
| 303 | //===----------------------------------------------------------------------===// |
| 304 | // Semantic checking for initializer lists. |
| 305 | //===----------------------------------------------------------------------===// |
| 306 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 307 | /// @brief Semantic checking for initializer lists. |
| 308 | /// |
| 309 | /// The InitListChecker class contains a set of routines that each |
| 310 | /// handle the initialization of a certain kind of entity, e.g., |
| 311 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 312 | /// InitListChecker itself performs a recursive walk of the subobject |
| 313 | /// structure of the type to be initialized, while stepping through |
| 314 | /// the initializer list one element at a time. The IList and Index |
| 315 | /// parameters to each of the Check* routines contain the active |
| 316 | /// (syntactic) initializer list and the index into that initializer |
| 317 | /// list that represents the current initializer. Each routine is |
| 318 | /// responsible for moving that Index forward as it consumes elements. |
| 319 | /// |
| 320 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 321 | /// arguments, which contains the current the "structured" (semantic) |
| 322 | /// initializer list and the index into that initializer list where we |
| 323 | /// are copying initializers as we map them over to the semantic |
| 324 | /// list. Once we have completed our recursive walk of the subobject |
| 325 | /// structure, we will have constructed a full semantic initializer |
| 326 | /// list. |
| 327 | /// |
| 328 | /// C99 designators cause changes in the initializer list traversal, |
| 329 | /// because they make the initialization "jump" into a specific |
| 330 | /// subobject and then continue the initialization from that |
| 331 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 332 | /// designated subobject and manages backing out the recursion to |
| 333 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 334 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 335 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 336 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 337 | bool hadError; |
| 338 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 339 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
| 341 | void CheckImplicitInitList(InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 342 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 343 | unsigned &StructuredIndex, |
| 344 | bool TopLevelObject = false); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 345 | void CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 346 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 347 | unsigned &StructuredIndex, |
| 348 | bool TopLevelObject = false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | void CheckListElementTypes(InitListExpr *IList, QualType &DeclType, |
| 350 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 351 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 352 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 353 | unsigned &StructuredIndex, |
| 354 | bool TopLevelObject = false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | void CheckSubElementType(InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 356 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 357 | InitListExpr *StructuredList, |
| 358 | unsigned &StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | void CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 360 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 361 | InitListExpr *StructuredList, |
| 362 | unsigned &StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | void CheckReferenceType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 364 | unsigned &Index, |
| 365 | InitListExpr *StructuredList, |
| 366 | unsigned &StructuredIndex); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 367 | void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 368 | InitListExpr *StructuredList, |
| 369 | unsigned &StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType, |
| 371 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 372 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 373 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 374 | unsigned &StructuredIndex, |
| 375 | bool TopLevelObject = false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | void CheckArrayType(InitListExpr *IList, QualType &DeclType, |
| 377 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 378 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 379 | InitListExpr *StructuredList, |
| 380 | unsigned &StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 382 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 384 | RecordDecl::field_iterator *NextField, |
| 385 | llvm::APSInt *NextElementIndex, |
| 386 | unsigned &Index, |
| 387 | InitListExpr *StructuredList, |
| 388 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 389 | bool FinishSubobjectInit, |
| 390 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 391 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 392 | QualType CurrentObjectType, |
| 393 | InitListExpr *StructuredList, |
| 394 | unsigned StructuredIndex, |
| 395 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 396 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 397 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 398 | Expr *expr); |
| 399 | int numArrayElements(QualType DeclType); |
| 400 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 401 | |
| 402 | void FillInValueInitializations(InitListExpr *ILE); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 403 | public: |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 404 | InitListChecker(Sema &S, InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 405 | bool HadError() { return hadError; } |
| 406 | |
| 407 | // @brief Retrieves the fully-structured initializer list used for |
| 408 | // semantic analysis and code generation. |
| 409 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 410 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 411 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 413 | /// Recursively replaces NULL values within the given initializer list |
| 414 | /// with expressions that perform value-initialization of the |
| 415 | /// appropriate type. |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 416 | void InitListChecker::FillInValueInitializations(InitListExpr *ILE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 418 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 419 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 420 | if (ILE->getSyntacticForm()) |
| 421 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 423 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 424 | unsigned Init = 0, NumInits = ILE->getNumInits(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 426 | Field = RType->getDecl()->field_begin(), |
| 427 | FieldEnd = RType->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 428 | Field != FieldEnd; ++Field) { |
| 429 | if (Field->isUnnamedBitfield()) |
| 430 | continue; |
| 431 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 432 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 433 | if (Field->getType()->isReferenceType()) { |
| 434 | // C++ [dcl.init.aggr]p9: |
| 435 | // If an incomplete or empty initializer-list leaves a |
| 436 | // member of reference type uninitialized, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | // ill-formed. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 438 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 439 | << Field->getType() |
| 440 | << ILE->getSyntacticForm()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | SemaRef.Diag(Field->getLocation(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 442 | diag::note_uninit_reference_member); |
| 443 | hadError = true; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 444 | return; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 445 | } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) { |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 446 | hadError = true; |
| 447 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 448 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 449 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 450 | // FIXME: If value-initialization involves calling a constructor, should |
| 451 | // we make that call explicit in the representation (even when it means |
| 452 | // extending the initializer list)? |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 453 | if (Init < NumInits && !hadError) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | ILE->setInit(Init, |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 455 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 457 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 458 | FillInValueInitializations(InnerILE); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 459 | ++Init; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 460 | |
| 461 | // Only look at the first initialization of a union. |
| 462 | if (RType->getDecl()->isUnion()) |
| 463 | break; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | |
| 469 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 471 | unsigned NumInits = ILE->getNumInits(); |
| 472 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 473 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 474 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 475 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 476 | NumElements = CAType->getSize().getZExtValue(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 477 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 478 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 479 | NumElements = VType->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 481 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 483 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
| 484 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 485 | if (SemaRef.CheckValueInitialization(ElementType, Loc)) { |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 486 | hadError = true; |
| 487 | return; |
| 488 | } |
| 489 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 490 | // FIXME: If value-initialization involves calling a constructor, should |
| 491 | // we make that call explicit in the representation (even when it means |
| 492 | // extending the initializer list)? |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 493 | if (Init < NumInits && !hadError) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | ILE->setInit(Init, |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 495 | new (SemaRef.Context) ImplicitValueInitExpr(ElementType)); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 496 | } else if (InitListExpr *InnerILE |
| 497 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 498 | FillInValueInitializations(InnerILE); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 503 | InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T) |
| 504 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 505 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 506 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 507 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 508 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 510 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 511 | CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex, |
| 512 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 514 | if (!hadError) |
| 515 | FillInValueInitializations(FullyStructuredList); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 519 | // FIXME: use a proper constant |
| 520 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 521 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 522 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 523 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 524 | } |
| 525 | return maxElements; |
| 526 | } |
| 527 | |
| 528 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 529 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 530 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 532 | Field = structDecl->field_begin(), |
| 533 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 534 | Field != FieldEnd; ++Field) { |
| 535 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 536 | ++InitializableMembers; |
| 537 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 538 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 539 | return std::min(InitializableMembers, 1); |
| 540 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 544 | QualType T, unsigned &Index, |
| 545 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 546 | unsigned &StructuredIndex, |
| 547 | bool TopLevelObject) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 548 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 550 | if (T->isArrayType()) |
| 551 | maxElements = numArrayElements(T); |
| 552 | else if (T->isStructureType() || T->isUnionType()) |
| 553 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 554 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 555 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 556 | else |
| 557 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 558 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 559 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 560 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 561 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 562 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 563 | hadError = true; |
| 564 | return; |
| 565 | } |
| 566 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 567 | // Build a structured initializer list corresponding to this subobject. |
| 568 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 570 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 571 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 572 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 573 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 575 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 576 | unsigned StartIndex = Index; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 577 | CheckListElementTypes(ParentIList, T, false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | StructuredSubobjectInitList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 579 | StructuredSubobjectInitIndex, |
| 580 | TopLevelObject); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 581 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 582 | StructuredSubobjectInitList->setType(T); |
| 583 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 584 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 585 | // range corresponds with the end of the last initializer it used. |
| 586 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 588 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 589 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 590 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Steve Naroff | a647caa | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 593 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 594 | unsigned &Index, |
| 595 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 596 | unsigned &StructuredIndex, |
| 597 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 598 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 599 | SyntacticToSemantic[IList] = StructuredList; |
| 600 | StructuredList->setSyntacticForm(IList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | CheckListElementTypes(IList, T, true, Index, StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 602 | StructuredIndex, TopLevelObject); |
Steve Naroff | a647caa | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 603 | IList->setType(T); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 604 | StructuredList->setType(T); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 605 | if (hadError) |
| 606 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 607 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 608 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 609 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 610 | if (StructuredIndex == 1 && |
| 611 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 612 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 613 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 614 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 615 | hadError = true; |
| 616 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 617 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 618 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 619 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 620 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 621 | // Don't complain for incomplete types, since we'll get an error |
| 622 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 623 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 625 | CurrentObjectType->isArrayType()? 0 : |
| 626 | CurrentObjectType->isVectorType()? 1 : |
| 627 | CurrentObjectType->isScalarType()? 2 : |
| 628 | CurrentObjectType->isUnionType()? 3 : |
| 629 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 630 | |
| 631 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 632 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 633 | DK = diag::err_excess_initializers; |
| 634 | hadError = true; |
| 635 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 636 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 637 | DK = diag::err_excess_initializers; |
| 638 | hadError = true; |
| 639 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 641 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 642 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 645 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 646 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 647 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 648 | << IList->getSourceRange() |
Chris Lattner | 29d9c1a | 2009-12-06 17:36:05 +0000 | [diff] [blame] | 649 | << CodeModificationHint::CreateRemoval(IList->getLocStart()) |
| 650 | << CodeModificationHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 653 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 655 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 656 | unsigned &Index, |
| 657 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 658 | unsigned &StructuredIndex, |
| 659 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 660 | if (DeclType->isScalarType()) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 661 | CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 662 | } else if (DeclType->isVectorType()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 663 | CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 664 | } else if (DeclType->isAggregateType()) { |
| 665 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 666 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | CheckStructUnionTypes(IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 668 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 669 | StructuredList, StructuredIndex, |
| 670 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 671 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 672 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 673 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 674 | false); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index, |
| 676 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 677 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 678 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 679 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 680 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 681 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 682 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 683 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 684 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 685 | } else if (DeclType->isRecordType()) { |
| 686 | // C++ [dcl.init]p14: |
| 687 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 688 | // is a brace-enclosed list, see 8.5.1. |
| 689 | // |
| 690 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 691 | // we have an initializer list and a destination type that is not |
| 692 | // an aggregate. |
| 693 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 694 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 695 | << DeclType << IList->getSourceRange(); |
| 696 | hadError = true; |
| 697 | } else if (DeclType->isReferenceType()) { |
| 698 | CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 699 | } else { |
| 700 | // In C, all types are either scalars or aggregates, but |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | // additional handling is needed here for C++ (and possibly others?). |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 702 | assert(0 && "Unsupported initializer type"); |
| 703 | } |
| 704 | } |
| 705 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 706 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 708 | unsigned &Index, |
| 709 | InitListExpr *StructuredList, |
| 710 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 711 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 712 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 713 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 714 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 716 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 717 | StructuredList, StructuredIndex, |
| 718 | SubInitList->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | CheckExplicitInitList(SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 720 | newStructuredList, newStructuredIndex); |
| 721 | ++StructuredIndex; |
| 722 | ++Index; |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 723 | } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) { |
| 724 | CheckStringInit(Str, ElemType, SemaRef); |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 725 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 726 | ++Index; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 727 | } else if (ElemType->isScalarType()) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 728 | CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 729 | } else if (ElemType->isReferenceType()) { |
| 730 | CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 731 | } else { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 732 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 733 | // C++ [dcl.init.aggr]p12: |
| 734 | // All implicit type conversions (clause 4) are considered when |
| 735 | // initializing the aggregate member with an ini- tializer from |
| 736 | // an initializer-list. If the initializer can initialize a |
| 737 | // member, the member is initialized. [...] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | ImplicitConversionSequence ICS |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 739 | = SemaRef.TryCopyInitialization(expr, ElemType, |
| 740 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 741 | /*ForceRValue=*/false, |
| 742 | /*InOverloadResolution=*/false); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 744 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 746 | "initializing")) |
| 747 | hadError = true; |
| 748 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 749 | ++Index; |
| 750 | return; |
| 751 | } |
| 752 | |
| 753 | // Fall through for subaggregate initialization |
| 754 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | // C99 6.7.8p13: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 756 | // |
| 757 | // The initializer for a structure or union object that has |
| 758 | // automatic storage duration shall be either an initializer |
| 759 | // list as described below, or a single expression that has |
| 760 | // compatible structure or union type. In the latter case, the |
| 761 | // initial value of the object, including unnamed members, is |
| 762 | // that of the expression. |
Eli Friedman | 6b5374f | 2009-06-13 10:38:46 +0000 | [diff] [blame] | 763 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 764 | SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 765 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 766 | ++Index; |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | // Fall through for subaggregate initialization |
| 771 | } |
| 772 | |
| 773 | // C++ [dcl.init.aggr]p12: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | // |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 775 | // [...] Otherwise, if the member is itself a non-empty |
| 776 | // subaggregate, brace elision is assumed and the initializer is |
| 777 | // considered for the initialization of the first member of |
| 778 | // the subaggregate. |
| 779 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | CheckImplicitInitList(IList, ElemType, Index, StructuredList, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 781 | StructuredIndex); |
| 782 | ++StructuredIndex; |
| 783 | } else { |
| 784 | // We cannot initialize this element, so let |
| 785 | // PerformCopyInitialization produce the appropriate diagnostic. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 786 | SemaRef.PerformCopyInitialization(expr, ElemType, "initializing"); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 787 | hadError = true; |
| 788 | ++Index; |
| 789 | ++StructuredIndex; |
| 790 | } |
| 791 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 794 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 795 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 796 | InitListExpr *StructuredList, |
| 797 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 798 | if (Index < IList->getNumInits()) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 799 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 800 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 801 | SemaRef.Diag(IList->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 802 | diag::err_many_braces_around_scalar_init) |
| 803 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 804 | hadError = true; |
| 805 | ++Index; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 806 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 807 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 808 | } else if (isa<DesignatedInitExpr>(expr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 810 | diag::err_designator_for_scalar_init) |
| 811 | << DeclType << expr->getSourceRange(); |
| 812 | hadError = true; |
| 813 | ++Index; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 814 | ++StructuredIndex; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 815 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 816 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 817 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 818 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 819 | if (CheckSingleInitializer(expr, DeclType, false, SemaRef)) |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 820 | hadError = true; // types weren't compatible. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 821 | else if (savExpr != expr) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 822 | // The type was promoted, update initializer list. |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 823 | IList->setInit(Index, expr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 824 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 825 | if (hadError) |
| 826 | ++StructuredIndex; |
| 827 | else |
| 828 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 829 | ++Index; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 830 | } else { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 831 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 832 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 833 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 834 | ++Index; |
| 835 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 836 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 837 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 840 | void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 841 | unsigned &Index, |
| 842 | InitListExpr *StructuredList, |
| 843 | unsigned &StructuredIndex) { |
| 844 | if (Index < IList->getNumInits()) { |
| 845 | Expr *expr = IList->getInit(Index); |
| 846 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 847 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 848 | << DeclType << IList->getSourceRange(); |
| 849 | hadError = true; |
| 850 | ++Index; |
| 851 | ++StructuredIndex; |
| 852 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 854 | |
| 855 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 856 | if (SemaRef.CheckReferenceInit(expr, DeclType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 857 | /*FIXME:*/expr->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 858 | /*SuppressUserConversions=*/false, |
| 859 | /*AllowExplicit=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | /*ForceRValue=*/false)) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 861 | hadError = true; |
| 862 | else if (savExpr != expr) { |
| 863 | // The type was promoted, update initializer list. |
| 864 | IList->setInit(Index, expr); |
| 865 | } |
| 866 | if (hadError) |
| 867 | ++StructuredIndex; |
| 868 | else |
| 869 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 870 | ++Index; |
| 871 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 872 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 873 | // general, it would be useful to pass location information down the stack, |
| 874 | // so that we know the location (or decl) of the "current object" being |
| 875 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 877 | diag::err_init_reference_member_uninitialized) |
| 878 | << DeclType |
| 879 | << IList->getSourceRange(); |
| 880 | hadError = true; |
| 881 | ++Index; |
| 882 | ++StructuredIndex; |
| 883 | return; |
| 884 | } |
| 885 | } |
| 886 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 888 | unsigned &Index, |
| 889 | InitListExpr *StructuredList, |
| 890 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 891 | if (Index < IList->getNumInits()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 892 | const VectorType *VT = DeclType->getAs<VectorType>(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 893 | unsigned maxElements = VT->getNumElements(); |
| 894 | unsigned numEltsInit = 0; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 895 | QualType elementType = VT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 897 | if (!SemaRef.getLangOptions().OpenCL) { |
| 898 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 899 | // Don't attempt to go past the end of the init list |
| 900 | if (Index >= IList->getNumInits()) |
| 901 | break; |
| 902 | CheckSubElementType(IList, elementType, Index, |
| 903 | StructuredList, StructuredIndex); |
| 904 | } |
| 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()) { |
| 913 | CheckSubElementType(IList, elementType, Index, |
| 914 | StructuredList, StructuredIndex); |
| 915 | ++numEltsInit; |
| 916 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 917 | const VectorType *IVT = IType->getAs<VectorType>(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 918 | unsigned numIElts = IVT->getNumElements(); |
| 919 | QualType VecType = SemaRef.Context.getExtVectorType(elementType, |
| 920 | numIElts); |
| 921 | CheckSubElementType(IList, VecType, Index, |
| 922 | StructuredList, StructuredIndex); |
| 923 | numEltsInit += numIElts; |
| 924 | } |
| 925 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Nate Begeman | 2ef13e5 | 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 | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 938 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 940 | unsigned &Index, |
| 941 | InitListExpr *StructuredList, |
| 942 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 943 | // Check for the special-case of initializing an array with a string. |
| 944 | if (Index < IList->getNumInits()) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 945 | if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType, |
| 946 | SemaRef.Context)) { |
| 947 | CheckStringInit(Str, DeclType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 948 | // We place the string literal directly into the resulting |
| 949 | // initializer list. This is the only place where the structure |
| 950 | // of the structured initializer list doesn't match exactly, |
| 951 | // because doing so would involve allocating one character |
| 952 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 953 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 954 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 955 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 959 | if (const VariableArrayType *VAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 960 | SemaRef.Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 961 | // Check for VLAs; in standard C it would be possible to check this |
| 962 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 963 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 964 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 965 | diag::err_variable_object_no_init) |
| 966 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 967 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 968 | ++Index; |
| 969 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 973 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 974 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 975 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 976 | bool maxElementsKnown = false; |
| 977 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 978 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 979 | maxElements = CAT->getSize(); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 980 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 981 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 982 | maxElementsKnown = true; |
| 983 | } |
| 984 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 985 | QualType elementType = SemaRef.Context.getAsArrayType(DeclType) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 986 | ->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 987 | while (Index < IList->getNumInits()) { |
| 988 | Expr *Init = IList->getInit(Index); |
| 989 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 990 | // If we're not the subobject that matches up with the '{' for |
| 991 | // the designator, we shouldn't be handling the |
| 992 | // designator. Return immediately. |
| 993 | if (!SubobjectIsDesignatorContext) |
| 994 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 996 | // Handle this designated initializer. elementIndex will be |
| 997 | // updated to be the next array element we'll initialize. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 999 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1000 | StructuredList, StructuredIndex, true, |
| 1001 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1002 | hadError = true; |
| 1003 | continue; |
| 1004 | } |
| 1005 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1006 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 1007 | maxElements.extend(elementIndex.getBitWidth()); |
| 1008 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 1009 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1010 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1011 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1012 | // If the array is of incomplete type, keep track of the number of |
| 1013 | // elements in the initializer. |
| 1014 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1015 | maxElements = elementIndex; |
| 1016 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | // If we know the maximum number of elements, and we've already |
| 1021 | // hit it, stop consuming elements in the initializer list. |
| 1022 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1023 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Check this element. |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1026 | CheckSubElementType(IList, elementType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1027 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1028 | ++elementIndex; |
| 1029 | |
| 1030 | // If the array is of incomplete type, keep track of the number of |
| 1031 | // elements in the initializer. |
| 1032 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1033 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1034 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1035 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1036 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1037 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1038 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1039 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1040 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1041 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1042 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1043 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1044 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1045 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1047 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 1052 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1053 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1055 | unsigned &Index, |
| 1056 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1057 | unsigned &StructuredIndex, |
| 1058 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1059 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1061 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1062 | // confusion, we forgo checking the intializer for the entire record. |
| 1063 | if (structDecl->isInvalidDecl()) { |
| 1064 | hadError = true; |
| 1065 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1069 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1070 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1071 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1072 | Field != FieldEnd; ++Field) { |
| 1073 | if (Field->getDeclName()) { |
| 1074 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | return; |
| 1079 | } |
| 1080 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1081 | // If structDecl is a forward declaration, this loop won't do |
| 1082 | // anything except look at designated initializers; That's okay, |
| 1083 | // because an error should get printed out elsewhere. It might be |
| 1084 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1085 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1086 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1087 | bool InitializedSomething = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1088 | while (Index < IList->getNumInits()) { |
| 1089 | Expr *Init = IList->getInit(Index); |
| 1090 | |
| 1091 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1092 | // If we're not the subobject that matches up with the '{' for |
| 1093 | // the designator, we shouldn't be handling the |
| 1094 | // designator. Return immediately. |
| 1095 | if (!SubobjectIsDesignatorContext) |
| 1096 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1098 | // Handle this designated initializer. Field will be updated to |
| 1099 | // the next field that we'll be initializing. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1101 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1102 | StructuredList, StructuredIndex, |
| 1103 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1104 | hadError = true; |
| 1105 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1106 | InitializedSomething = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1107 | continue; |
| 1108 | } |
| 1109 | |
| 1110 | if (Field == FieldEnd) { |
| 1111 | // We've run out of fields. We're done. |
| 1112 | break; |
| 1113 | } |
| 1114 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1115 | // We've already initialized a member of a union. We're done. |
| 1116 | if (InitializedSomething && DeclType->isUnionType()) |
| 1117 | break; |
| 1118 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1119 | // If we've hit the flexible array member at the end, we're done. |
| 1120 | if (Field->getType()->isIncompleteArrayType()) |
| 1121 | break; |
| 1122 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1123 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1124 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1125 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1126 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1127 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1129 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1130 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1131 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1132 | |
| 1133 | if (DeclType->isUnionType()) { |
| 1134 | // Initialize the first field within the union. |
| 1135 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1136 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1137 | |
| 1138 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1139 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1142 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1143 | return; |
| 1144 | |
| 1145 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | if (!TopLevelObject && |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1147 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1148 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1150 | diag::err_flexible_array_init_nonempty) |
| 1151 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1152 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1153 | << *Field; |
| 1154 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1155 | ++Index; |
| 1156 | return; |
| 1157 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1159 | diag::ext_flexible_array_init) |
| 1160 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1161 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1162 | << *Field; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1165 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 1166 | CheckSubElementType(IList, Field->getType(), Index, StructuredList, |
| 1167 | StructuredIndex); |
| 1168 | else |
| 1169 | CheckImplicitInitList(IList, Field->getType(), Index, StructuredList, |
| 1170 | StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1171 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1172 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1173 | /// \brief Expand a field designator that refers to a member of an |
| 1174 | /// anonymous struct or union into a series of field designators that |
| 1175 | /// refers to the field within the appropriate subobject. |
| 1176 | /// |
| 1177 | /// Field/FieldIndex will be updated to point to the (new) |
| 1178 | /// currently-designated field. |
| 1179 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | DesignatedInitExpr *DIE, |
| 1181 | unsigned DesigIdx, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1182 | FieldDecl *Field, |
| 1183 | RecordDecl::field_iterator &FieldIter, |
| 1184 | unsigned &FieldIndex) { |
| 1185 | typedef DesignatedInitExpr::Designator Designator; |
| 1186 | |
| 1187 | // Build the path from the current object to the member of the |
| 1188 | // anonymous struct/union (backwards). |
| 1189 | llvm::SmallVector<FieldDecl *, 4> Path; |
| 1190 | SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1192 | // Build the replacement designators. |
| 1193 | llvm::SmallVector<Designator, 4> Replacements; |
| 1194 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 1195 | FI = Path.rbegin(), FIEnd = Path.rend(); |
| 1196 | FI != FIEnd; ++FI) { |
| 1197 | if (FI + 1 == FIEnd) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1199 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1200 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1201 | else |
| 1202 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1203 | SourceLocation())); |
| 1204 | Replacements.back().setField(*FI); |
| 1205 | } |
| 1206 | |
| 1207 | // Expand the current designator into the set of replacement |
| 1208 | // designators, so we have a full subobject path down to where the |
| 1209 | // member of the anonymous struct/union is actually stored. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | DIE->ExpandDesignator(DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1211 | &Replacements[0] + Replacements.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1213 | // Update FieldIter/FieldIndex; |
| 1214 | RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1215 | FieldIter = Record->field_begin(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1216 | FieldIndex = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1217 | for (RecordDecl::field_iterator FEnd = Record->field_end(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1218 | FieldIter != FEnd; ++FieldIter) { |
| 1219 | if (FieldIter->isUnnamedBitfield()) |
| 1220 | continue; |
| 1221 | |
| 1222 | if (*FieldIter == Path.back()) |
| 1223 | return; |
| 1224 | |
| 1225 | ++FieldIndex; |
| 1226 | } |
| 1227 | |
| 1228 | assert(false && "Unable to find anonymous struct/union field"); |
| 1229 | } |
| 1230 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1231 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1232 | /// |
| 1233 | /// Determines whether the designated initializer @p DIE, which |
| 1234 | /// resides at the given @p Index within the initializer list @p |
| 1235 | /// IList, is well-formed for a current object of type @p DeclType |
| 1236 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1238 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1239 | /// |
| 1240 | /// @param IList The initializer list in which this designated |
| 1241 | /// initializer occurs. |
| 1242 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1243 | /// @param DIE The designated initializer expression. |
| 1244 | /// |
| 1245 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1246 | /// |
| 1247 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1248 | /// into which the designation in @p DIE should refer. |
| 1249 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1250 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1251 | /// a field, this will be set to the field declaration corresponding |
| 1252 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1253 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1254 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1255 | /// DIE is an array designator or GNU array-range designator, this |
| 1256 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1257 | /// |
| 1258 | /// @param Index Index into @p IList where the designated initializer |
| 1259 | /// @p DIE occurs. |
| 1260 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1261 | /// @param StructuredList The initializer list expression that |
| 1262 | /// describes all of the subobject initializers in the order they'll |
| 1263 | /// actually be initialized. |
| 1264 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1265 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | bool |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1267 | InitListChecker::CheckDesignatedInitializer(InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1269 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1270 | QualType &CurrentObjectType, |
| 1271 | RecordDecl::field_iterator *NextField, |
| 1272 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1273 | unsigned &Index, |
| 1274 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1275 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1276 | bool FinishSubobjectInit, |
| 1277 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1278 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1279 | // Check the actual initialization for the designated object type. |
| 1280 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Temporarily remove the designator expression from the |
| 1283 | // initializer list that the child calls see, so that we don't try |
| 1284 | // to re-process the designator. |
| 1285 | unsigned OldIndex = Index; |
| 1286 | IList->setInit(OldIndex, DIE->getInit()); |
| 1287 | |
| 1288 | CheckSubElementType(IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1289 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Restore the designated initializer expression in the syntactic |
| 1292 | // form of the initializer list. |
| 1293 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1294 | DIE->setInit(IList->getInit(OldIndex)); |
| 1295 | IList->setInit(OldIndex, DIE); |
| 1296 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1297 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1300 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1302 | "Need a non-designated initializer list to start from"); |
| 1303 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1304 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1305 | // Determine the structural initializer list that corresponds to the |
| 1306 | // current subobject. |
| 1307 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1309 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1310 | SourceRange(D->getStartLocation(), |
| 1311 | DIE->getSourceRange().getEnd())); |
| 1312 | assert(StructuredList && "Expected a structured initializer list"); |
| 1313 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1314 | if (D->isFieldDesignator()) { |
| 1315 | // C99 6.7.8p7: |
| 1316 | // |
| 1317 | // If a designator has the form |
| 1318 | // |
| 1319 | // . identifier |
| 1320 | // |
| 1321 | // then the current object (defined below) shall have |
| 1322 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1324 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1325 | if (!RT) { |
| 1326 | SourceLocation Loc = D->getDotLoc(); |
| 1327 | if (Loc.isInvalid()) |
| 1328 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1329 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1330 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1331 | ++Index; |
| 1332 | return true; |
| 1333 | } |
| 1334 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1335 | // Note: we perform a linear search of the fields here, despite |
| 1336 | // the fact that we have a faster lookup method, because we always |
| 1337 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1338 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1339 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1340 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1342 | Field = RT->getDecl()->field_begin(), |
| 1343 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1344 | for (; Field != FieldEnd; ++Field) { |
| 1345 | if (Field->isUnnamedBitfield()) |
| 1346 | continue; |
| 1347 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1348 | if (KnownField == *Field || Field->getIdentifier() == FieldName) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1349 | break; |
| 1350 | |
| 1351 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1354 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1355 | // There was no normal field in the struct with the designated |
| 1356 | // name. Perform another lookup for this name, which may find |
| 1357 | // something that we can't designate (e.g., a member function), |
| 1358 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1360 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1361 | if (Lookup.first == Lookup.second) { |
| 1362 | // Name lookup didn't find anything. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1363 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1364 | << FieldName << CurrentObjectType; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1365 | ++Index; |
| 1366 | return true; |
| 1367 | } else if (!KnownField && isa<FieldDecl>(*Lookup.first) && |
| 1368 | cast<RecordDecl>((*Lookup.first)->getDeclContext()) |
| 1369 | ->isAnonymousStructOrUnion()) { |
| 1370 | // Handle an field designator that refers to a member of an |
| 1371 | // anonymous struct or union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1373 | cast<FieldDecl>(*Lookup.first), |
| 1374 | Field, FieldIndex); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1375 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1376 | } else { |
| 1377 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1378 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1379 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1381 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1382 | ++Index; |
| 1383 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } else if (!KnownField && |
| 1386 | cast<RecordDecl>((*Field)->getDeclContext()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1387 | ->isAnonymousStructOrUnion()) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1388 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field, |
| 1389 | Field, FieldIndex); |
| 1390 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1391 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1392 | |
| 1393 | // All of the fields of a union are located at the same place in |
| 1394 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1395 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1396 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1397 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1398 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1400 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1401 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1403 | // Make sure that our non-designated initializer list has space |
| 1404 | // for a subobject corresponding to this field. |
| 1405 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1406 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1408 | // This designator names a flexible array member. |
| 1409 | if (Field->getType()->isIncompleteArrayType()) { |
| 1410 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1411 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1412 | // We can't designate an object within the flexible array |
| 1413 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1415 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1417 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1419 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1420 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1421 | << *Field; |
| 1422 | Invalid = true; |
| 1423 | } |
| 1424 | |
| 1425 | if (!hadError && !isa<InitListExpr>(DIE->getInit())) { |
| 1426 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1427 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1428 | diag::err_flexible_array_init_needs_braces) |
| 1429 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1430 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1431 | << *Field; |
| 1432 | Invalid = true; |
| 1433 | } |
| 1434 | |
| 1435 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1437 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1439 | diag::err_flexible_array_init_nonempty) |
| 1440 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1441 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1442 | << *Field; |
| 1443 | Invalid = true; |
| 1444 | } |
| 1445 | |
| 1446 | if (Invalid) { |
| 1447 | ++Index; |
| 1448 | return true; |
| 1449 | } |
| 1450 | |
| 1451 | // Initialize the array. |
| 1452 | bool prevHadError = hadError; |
| 1453 | unsigned newStructuredIndex = FieldIndex; |
| 1454 | unsigned OldIndex = Index; |
| 1455 | IList->setInit(Index, DIE->getInit()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1457 | StructuredList, newStructuredIndex); |
| 1458 | IList->setInit(OldIndex, DIE); |
| 1459 | if (hadError && !prevHadError) { |
| 1460 | ++Field; |
| 1461 | ++FieldIndex; |
| 1462 | if (NextField) |
| 1463 | *NextField = Field; |
| 1464 | StructuredIndex = FieldIndex; |
| 1465 | return true; |
| 1466 | } |
| 1467 | } else { |
| 1468 | // Recurse to check later designated subobjects. |
| 1469 | QualType FieldType = (*Field)->getType(); |
| 1470 | unsigned newStructuredIndex = FieldIndex; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1471 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0, |
| 1472 | Index, StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1473 | true, false)) |
| 1474 | return true; |
| 1475 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Find the position of the next field to be initialized in this |
| 1478 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1479 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1480 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1481 | |
| 1482 | // If this the first designator, our caller will continue checking |
| 1483 | // the rest of this struct/class/union subobject. |
| 1484 | if (IsFirstDesignator) { |
| 1485 | if (NextField) |
| 1486 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1487 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1488 | return false; |
| 1489 | } |
| 1490 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1491 | if (!FinishSubobjectInit) |
| 1492 | return false; |
| 1493 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1494 | // We've already initialized something in the union; we're done. |
| 1495 | if (RT->getDecl()->isUnion()) |
| 1496 | return hadError; |
| 1497 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1498 | // Check the remaining fields within this class/struct/union subobject. |
| 1499 | bool prevHadError = hadError; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1500 | CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index, |
| 1501 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1502 | return hadError && !prevHadError; |
| 1503 | } |
| 1504 | |
| 1505 | // C99 6.7.8p6: |
| 1506 | // |
| 1507 | // If a designator has the form |
| 1508 | // |
| 1509 | // [ constant-expression ] |
| 1510 | // |
| 1511 | // then the current object (defined below) shall have array |
| 1512 | // type and the expression shall be an integer constant |
| 1513 | // expression. If the array is of unknown size, any |
| 1514 | // nonnegative value is valid. |
| 1515 | // |
| 1516 | // Additionally, cope with the GNU extension that permits |
| 1517 | // designators of the form |
| 1518 | // |
| 1519 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1520 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1521 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1522 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1523 | << CurrentObjectType; |
| 1524 | ++Index; |
| 1525 | return true; |
| 1526 | } |
| 1527 | |
| 1528 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1529 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1530 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1531 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1532 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1533 | DesignatedEndIndex = DesignatedStartIndex; |
| 1534 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1535 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
| 1538 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1539 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1541 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1542 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1544 | if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1545 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1548 | if (isa<ConstantArrayType>(AT)) { |
| 1549 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1550 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1551 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1552 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1553 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1554 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1555 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1556 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1557 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1558 | << IndexExpr->getSourceRange(); |
| 1559 | ++Index; |
| 1560 | return true; |
| 1561 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | // Make sure the bit-widths and signedness match. |
| 1564 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1565 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1566 | else if (DesignatedStartIndex.getBitWidth() < |
| 1567 | DesignatedEndIndex.getBitWidth()) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1568 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1569 | DesignatedStartIndex.setIsUnsigned(true); |
| 1570 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1573 | // Make sure that our non-designated initializer list has space |
| 1574 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1575 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1577 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1579 | // Repeatedly perform subobject initializations in the range |
| 1580 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1582 | // Move to the next designator |
| 1583 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1584 | unsigned OldIndex = Index; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1585 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1586 | // Recurse to check later designated subobjects. |
| 1587 | QualType ElementType = AT->getElementType(); |
| 1588 | Index = OldIndex; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1589 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0, |
| 1590 | Index, StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1591 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1592 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | |
| 1595 | // Move to the next index in the array that we'll be initializing. |
| 1596 | ++DesignatedStartIndex; |
| 1597 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1598 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1599 | |
| 1600 | // If this the first designator, our caller will continue checking |
| 1601 | // the rest of this array subobject. |
| 1602 | if (IsFirstDesignator) { |
| 1603 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1604 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1605 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1606 | return false; |
| 1607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1609 | if (!FinishSubobjectInit) |
| 1610 | return false; |
| 1611 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1612 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1613 | bool prevHadError = hadError; |
Douglas Gregor | fdf5569 | 2009-02-09 19:45:19 +0000 | [diff] [blame] | 1614 | CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1615 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1619 | // Get the structured initializer list for a subobject of type |
| 1620 | // @p CurrentObjectType. |
| 1621 | InitListExpr * |
| 1622 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1623 | QualType CurrentObjectType, |
| 1624 | InitListExpr *StructuredList, |
| 1625 | unsigned StructuredIndex, |
| 1626 | SourceRange InitRange) { |
| 1627 | Expr *ExistingInit = 0; |
| 1628 | if (!StructuredList) |
| 1629 | ExistingInit = SyntacticToSemantic[IList]; |
| 1630 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1631 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1633 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1634 | return Result; |
| 1635 | |
| 1636 | if (ExistingInit) { |
| 1637 | // We are creating an initializer list that initializes the |
| 1638 | // subobjects of the current object, but there was already an |
| 1639 | // initialization that completely initialized the current |
| 1640 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1642 | // struct X { int a, b; }; |
| 1643 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1645 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1646 | // designated initializer re-initializes the whole |
| 1647 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1649 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1650 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1652 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1653 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1654 | << ExistingInit->getSourceRange(); |
| 1655 | } |
| 1656 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | InitListExpr *Result |
| 1658 | = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1659 | InitRange.getEnd()); |
| 1660 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1661 | Result->setType(CurrentObjectType); |
| 1662 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1663 | // Pre-allocate storage for the structured initializer list. |
| 1664 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1665 | unsigned NumInits = 0; |
| 1666 | if (!StructuredList) |
| 1667 | NumInits = IList->getNumInits(); |
| 1668 | else if (Index < IList->getNumInits()) { |
| 1669 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1670 | NumInits = SubList->getNumInits(); |
| 1671 | } |
| 1672 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1674 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1675 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1676 | NumElements = CAType->getSize().getZExtValue(); |
| 1677 | // Simple heuristic so that we don't allocate a very large |
| 1678 | // initializer with many empty entries at the end. |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1679 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1680 | NumElements = 0; |
| 1681 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1682 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1683 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1684 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1685 | RecordDecl *RDecl = RType->getDecl(); |
| 1686 | if (RDecl->isUnion()) |
| 1687 | NumElements = 1; |
| 1688 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1690 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1693 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1694 | NumElements = IList->getNumInits(); |
| 1695 | |
| 1696 | Result->reserveInits(NumElements); |
| 1697 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1698 | // Link this new initializer list into the structured initializer |
| 1699 | // lists. |
| 1700 | if (StructuredList) |
| 1701 | StructuredList->updateInit(StructuredIndex, Result); |
| 1702 | else { |
| 1703 | Result->setSyntacticForm(IList); |
| 1704 | SyntacticToSemantic[IList] = Result; |
| 1705 | } |
| 1706 | |
| 1707 | return Result; |
| 1708 | } |
| 1709 | |
| 1710 | /// Update the initializer at index @p StructuredIndex within the |
| 1711 | /// structured initializer list to the value @p expr. |
| 1712 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1713 | unsigned &StructuredIndex, |
| 1714 | Expr *expr) { |
| 1715 | // No structured initializer list to update |
| 1716 | if (!StructuredList) |
| 1717 | return; |
| 1718 | |
| 1719 | if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) { |
| 1720 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1722 | diag::warn_initializer_overrides) |
| 1723 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1725 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1726 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1727 | << PrevInit->getSourceRange(); |
| 1728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1730 | ++StructuredIndex; |
| 1731 | } |
| 1732 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1733 | /// Check that the given Index expression is a valid array designator |
| 1734 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1735 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1736 | /// and produces a reasonable diagnostic if there is a |
| 1737 | /// failure. Returns true if there was an error, false otherwise. If |
| 1738 | /// everything went okay, Value will receive the value of the constant |
| 1739 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1741 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1742 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1743 | |
| 1744 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1745 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1746 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1748 | if (Value.isSigned() && Value.isNegative()) |
| 1749 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1750 | << Value.toString(10) << Index->getSourceRange(); |
| 1751 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1752 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | } |
| 1755 | |
| 1756 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 1757 | SourceLocation Loc, |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 1758 | bool GNUSyntax, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1759 | OwningExprResult Init) { |
| 1760 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1761 | |
| 1762 | bool Invalid = false; |
| 1763 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1764 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1765 | |
| 1766 | // Build designators and check array designator expressions. |
| 1767 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1768 | const Designator &D = Desig.getDesignator(Idx); |
| 1769 | switch (D.getKind()) { |
| 1770 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1772 | D.getFieldLoc())); |
| 1773 | break; |
| 1774 | |
| 1775 | case Designator::ArrayDesignator: { |
| 1776 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1777 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1778 | if (!Index->isTypeDependent() && |
| 1779 | !Index->isValueDependent() && |
| 1780 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1781 | Invalid = true; |
| 1782 | else { |
| 1783 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1785 | D.getRBracketLoc())); |
| 1786 | InitExpressions.push_back(Index); |
| 1787 | } |
| 1788 | break; |
| 1789 | } |
| 1790 | |
| 1791 | case Designator::ArrayRangeDesignator: { |
| 1792 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1793 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1794 | llvm::APSInt StartValue; |
| 1795 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1796 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1797 | StartIndex->isValueDependent(); |
| 1798 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1799 | EndIndex->isValueDependent(); |
| 1800 | if ((!StartDependent && |
| 1801 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1802 | (!EndDependent && |
| 1803 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1804 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1805 | else { |
| 1806 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1807 | if (StartDependent || EndDependent) { |
| 1808 | // Nothing to compute. |
| 1809 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1810 | EndValue.extend(StartValue.getBitWidth()); |
| 1811 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1812 | StartValue.extend(EndValue.getBitWidth()); |
| 1813 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1814 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1815 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1817 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1818 | Invalid = true; |
| 1819 | } else { |
| 1820 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1822 | D.getEllipsisLoc(), |
| 1823 | D.getRBracketLoc())); |
| 1824 | InitExpressions.push_back(StartIndex); |
| 1825 | InitExpressions.push_back(EndIndex); |
| 1826 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1827 | } |
| 1828 | break; |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | if (Invalid || Init.isInvalid()) |
| 1834 | return ExprError(); |
| 1835 | |
| 1836 | // Clear out the expressions within the designation. |
| 1837 | Desig.ClearExprs(*this); |
| 1838 | |
| 1839 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1840 | = DesignatedInitExpr::Create(Context, |
| 1841 | Designators.data(), Designators.size(), |
| 1842 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1843 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1844 | return Owned(DIE); |
| 1845 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1846 | |
| 1847 | bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1848 | InitListChecker CheckInitList(*this, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1849 | if (!CheckInitList.HadError()) |
| 1850 | InitList = CheckInitList.getFullyStructuredList(); |
| 1851 | |
| 1852 | return CheckInitList.HadError(); |
| 1853 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1854 | |
| 1855 | /// \brief Diagnose any semantic errors with value-initialization of |
| 1856 | /// the given type. |
| 1857 | /// |
| 1858 | /// Value-initialization effectively zero-initializes any types |
| 1859 | /// without user-declared constructors, and calls the default |
| 1860 | /// constructor for a for any type that has a user-declared |
| 1861 | /// constructor (C++ [dcl.init]p5). Value-initialization can fail when |
| 1862 | /// a type with a user-declared constructor does not have an |
| 1863 | /// accessible, non-deleted default constructor. In C, everything can |
| 1864 | /// be value-initialized, which corresponds to C's notion of |
| 1865 | /// initializing objects with static storage duration when no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | /// initializer is provided for that object. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1867 | /// |
| 1868 | /// \returns true if there was an error, false otherwise. |
| 1869 | bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) { |
| 1870 | // C++ [dcl.init]p5: |
| 1871 | // |
| 1872 | // To value-initialize an object of type T means: |
| 1873 | |
| 1874 | // -- if T is an array type, then each element is value-initialized; |
| 1875 | if (const ArrayType *AT = Context.getAsArrayType(Type)) |
| 1876 | return CheckValueInitialization(AT->getElementType(), Loc); |
| 1877 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1878 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1879 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1880 | // -- if T is a class type (clause 9) with a user-declared |
| 1881 | // constructor (12.1), then the default constructor for T is |
| 1882 | // called (and the initialization is ill-formed if T has no |
| 1883 | // accessible default constructor); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1884 | if (ClassDecl->hasUserDeclaredConstructor()) { |
| 1885 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1886 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1887 | // FIXME: Poor location information |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1888 | CXXConstructorDecl *Constructor |
| 1889 | = PerformInitializationByConstructor(Type, |
| 1890 | MultiExprArg(*this, 0, 0), |
| 1891 | Loc, SourceRange(Loc), |
| 1892 | DeclarationName(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1893 | InitializationKind::CreateValue(Loc, Loc, Loc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1894 | ConstructorArgs); |
| 1895 | if (!Constructor) |
| 1896 | return true; |
| 1897 | |
| 1898 | OwningExprResult Init |
| 1899 | = BuildCXXConstructExpr(Loc, Type, Constructor, |
| 1900 | move_arg(ConstructorArgs)); |
| 1901 | if (Init.isInvalid()) |
| 1902 | return true; |
| 1903 | |
| 1904 | // FIXME: Actually perform the value-initialization! |
| 1905 | return false; |
| 1906 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | if (Type->isReferenceType()) { |
| 1911 | // C++ [dcl.init]p5: |
| 1912 | // [...] A program that calls for default-initialization or |
| 1913 | // value-initialization of an entity of reference type is |
| 1914 | // ill-formed. [...] |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1915 | // FIXME: Once we have code that goes through this path, add an actual |
| 1916 | // diagnostic :) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | return false; |
| 1920 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1921 | |
| 1922 | //===----------------------------------------------------------------------===// |
| 1923 | // Initialization entity |
| 1924 | //===----------------------------------------------------------------------===// |
| 1925 | |
| 1926 | void InitializedEntity::InitDeclLoc() { |
| 1927 | assert((Kind == EK_Variable || Kind == EK_Parameter || Kind == EK_Member) && |
| 1928 | "InitDeclLoc cannot be used with non-declaration entities."); |
| 1929 | |
| 1930 | if (TypeSourceInfo *DI = VariableOrMember->getTypeSourceInfo()) { |
| 1931 | TL = DI->getTypeLoc(); |
| 1932 | return; |
| 1933 | } |
| 1934 | |
| 1935 | // FIXME: Once we've gone through the effort to create the fake |
| 1936 | // TypeSourceInfo, should we cache it in the declaration? |
| 1937 | // (If not, we "leak" it). |
| 1938 | TypeSourceInfo *DI = VariableOrMember->getASTContext() |
| 1939 | .CreateTypeSourceInfo(VariableOrMember->getType()); |
| 1940 | DI->getTypeLoc().initialize(VariableOrMember->getLocation()); |
| 1941 | TL = DI->getTypeLoc(); |
| 1942 | } |
| 1943 | |
| 1944 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
| 1945 | CXXBaseSpecifier *Base) |
| 1946 | { |
| 1947 | InitializedEntity Result; |
| 1948 | Result.Kind = EK_Base; |
| 1949 | Result.Base = Base; |
| 1950 | // FIXME: CXXBaseSpecifier should store a TypeLoc. |
| 1951 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Base->getType()); |
| 1952 | DI->getTypeLoc().initialize(Base->getSourceRange().getBegin()); |
| 1953 | Result.TL = DI->getTypeLoc(); |
| 1954 | return Result; |
| 1955 | } |
| 1956 | |
| 1957 | //===----------------------------------------------------------------------===// |
| 1958 | // Initialization sequence |
| 1959 | //===----------------------------------------------------------------------===// |
| 1960 | |
| 1961 | void InitializationSequence::Step::Destroy() { |
| 1962 | switch (Kind) { |
| 1963 | case SK_ResolveAddressOfOverloadedFunction: |
| 1964 | case SK_CastDerivedToBaseRValue: |
| 1965 | case SK_CastDerivedToBaseLValue: |
| 1966 | case SK_BindReference: |
| 1967 | case SK_BindReferenceToTemporary: |
| 1968 | case SK_UserConversion: |
| 1969 | case SK_QualificationConversionRValue: |
| 1970 | case SK_QualificationConversionLValue: |
| 1971 | break; |
| 1972 | |
| 1973 | case SK_ConversionSequence: |
| 1974 | delete ICS; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | void InitializationSequence::AddAddressOverloadResolutionStep( |
| 1979 | FunctionDecl *Function) { |
| 1980 | Step S; |
| 1981 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 1982 | S.Type = Function->getType(); |
| 1983 | S.Function = Function; |
| 1984 | Steps.push_back(S); |
| 1985 | } |
| 1986 | |
| 1987 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
| 1988 | bool IsLValue) { |
| 1989 | Step S; |
| 1990 | S.Kind = IsLValue? SK_CastDerivedToBaseLValue : SK_CastDerivedToBaseRValue; |
| 1991 | S.Type = BaseType; |
| 1992 | Steps.push_back(S); |
| 1993 | } |
| 1994 | |
| 1995 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
| 1996 | bool BindingTemporary) { |
| 1997 | Step S; |
| 1998 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 1999 | S.Type = T; |
| 2000 | Steps.push_back(S); |
| 2001 | } |
| 2002 | |
| 2003 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function) { |
| 2004 | Step S; |
| 2005 | S.Kind = SK_UserConversion; |
| 2006 | S.Type = Function->getResultType().getNonReferenceType(); |
| 2007 | S.Function = Function; |
| 2008 | Steps.push_back(S); |
| 2009 | } |
| 2010 | |
| 2011 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
| 2012 | bool IsLValue) { |
| 2013 | Step S; |
| 2014 | S.Kind = IsLValue? SK_QualificationConversionLValue |
| 2015 | : SK_QualificationConversionRValue; |
| 2016 | S.Type = Ty; |
| 2017 | Steps.push_back(S); |
| 2018 | } |
| 2019 | |
| 2020 | void InitializationSequence::AddConversionSequenceStep( |
| 2021 | const ImplicitConversionSequence &ICS, |
| 2022 | QualType T) { |
| 2023 | Step S; |
| 2024 | S.Kind = SK_ConversionSequence; |
| 2025 | S.Type = T; |
| 2026 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2027 | Steps.push_back(S); |
| 2028 | } |
| 2029 | |
| 2030 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
| 2031 | OverloadingResult Result) { |
| 2032 | SequenceKind = FailedSequence; |
| 2033 | this->Failure = Failure; |
| 2034 | this->FailedOverloadResult = Result; |
| 2035 | } |
| 2036 | |
| 2037 | //===----------------------------------------------------------------------===// |
| 2038 | // Attempt initialization |
| 2039 | //===----------------------------------------------------------------------===// |
| 2040 | |
| 2041 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2042 | static bool TryListInitialization(Sema &S, |
| 2043 | const InitializedEntity &Entity, |
| 2044 | const InitializationKind &Kind, |
| 2045 | InitListExpr *InitList, |
| 2046 | InitializationSequence &Sequence) { |
| 2047 | // FIXME: For now, it is safe to assume that list initialization always |
| 2048 | // works. When we actually perform list initialization, we'll do all of the |
| 2049 | // necessary checking. |
| 2050 | // C++0x initializer lists will force us to perform more checking here. |
| 2051 | return true; |
| 2052 | } |
| 2053 | |
| 2054 | /// \brief Try a reference initialization that involves calling a conversion |
| 2055 | /// function. |
| 2056 | /// |
| 2057 | /// FIXME: look intos DRs 656, 896 |
| 2058 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2059 | const InitializedEntity &Entity, |
| 2060 | const InitializationKind &Kind, |
| 2061 | Expr *Initializer, |
| 2062 | bool AllowRValues, |
| 2063 | InitializationSequence &Sequence) { |
| 2064 | QualType DestType = Entity.getType().getType(); |
| 2065 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2066 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2067 | QualType cv2T2 = Initializer->getType(); |
| 2068 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2069 | |
| 2070 | bool DerivedToBase; |
| 2071 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
| 2072 | T1, T2, DerivedToBase) && |
| 2073 | "Must have incompatible references when binding via conversion"); |
| 2074 | |
| 2075 | // Build the candidate set directly in the initialization sequence |
| 2076 | // structure, so that it will persist if we fail. |
| 2077 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2078 | CandidateSet.clear(); |
| 2079 | |
| 2080 | // Determine whether we are allowed to call explicit constructors or |
| 2081 | // explicit conversion operators. |
| 2082 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
| 2083 | |
| 2084 | const RecordType *T1RecordType = 0; |
| 2085 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>())) { |
| 2086 | // The type we're converting to is a class type. Enumerate its constructors |
| 2087 | // to see if there is a suitable conversion. |
| 2088 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
| 2089 | |
| 2090 | DeclarationName ConstructorName |
| 2091 | = S.Context.DeclarationNames.getCXXConstructorName( |
| 2092 | S.Context.getCanonicalType(T1).getUnqualifiedType()); |
| 2093 | DeclContext::lookup_iterator Con, ConEnd; |
| 2094 | for (llvm::tie(Con, ConEnd) = T1RecordDecl->lookup(ConstructorName); |
| 2095 | Con != ConEnd; ++Con) { |
| 2096 | // Find the constructor (which may be a template). |
| 2097 | CXXConstructorDecl *Constructor = 0; |
| 2098 | FunctionTemplateDecl *ConstructorTmpl |
| 2099 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 2100 | if (ConstructorTmpl) |
| 2101 | Constructor = cast<CXXConstructorDecl>( |
| 2102 | ConstructorTmpl->getTemplatedDecl()); |
| 2103 | else |
| 2104 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2105 | |
| 2106 | if (!Constructor->isInvalidDecl() && |
| 2107 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2108 | if (ConstructorTmpl) |
| 2109 | S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0, |
| 2110 | &Initializer, 1, CandidateSet); |
| 2111 | else |
| 2112 | S.AddOverloadCandidate(Constructor, &Initializer, 1, CandidateSet); |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | if (const RecordType *T2RecordType = T2->getAs<RecordType>()) { |
| 2118 | // The type we're converting from is a class type, enumerate its conversion |
| 2119 | // functions. |
| 2120 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2121 | |
| 2122 | // Determine the type we are converting to. If we are allowed to |
| 2123 | // convert to an rvalue, take the type that the destination type |
| 2124 | // refers to. |
| 2125 | QualType ToType = AllowRValues? cv1T1 : DestType; |
| 2126 | |
| 2127 | const UnresolvedSet *Conversions |
| 2128 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2129 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 2130 | E = Conversions->end(); |
| 2131 | I != E; ++I) { |
| 2132 | NamedDecl *D = *I; |
| 2133 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2134 | if (isa<UsingShadowDecl>(D)) |
| 2135 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2136 | |
| 2137 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2138 | CXXConversionDecl *Conv; |
| 2139 | if (ConvTemplate) |
| 2140 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2141 | else |
| 2142 | Conv = cast<CXXConversionDecl>(*I); |
| 2143 | |
| 2144 | // If the conversion function doesn't return a reference type, |
| 2145 | // it can't be considered for this conversion unless we're allowed to |
| 2146 | // consider rvalues. |
| 2147 | // FIXME: Do we need to make sure that we only consider conversion |
| 2148 | // candidates with reference-compatible results? That might be needed to |
| 2149 | // break recursion. |
| 2150 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2151 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2152 | if (ConvTemplate) |
| 2153 | S.AddTemplateConversionCandidate(ConvTemplate, ActingDC, Initializer, |
| 2154 | ToType, CandidateSet); |
| 2155 | else |
| 2156 | S.AddConversionCandidate(Conv, ActingDC, Initializer, cv1T1, |
| 2157 | CandidateSet); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2163 | |
| 2164 | // Perform overload resolution. If it fails, return the failed result. |
| 2165 | OverloadCandidateSet::iterator Best; |
| 2166 | if (OverloadingResult Result |
| 2167 | = S.BestViableFunction(CandidateSet, DeclLoc, Best)) |
| 2168 | return Result; |
| 2169 | |
| 2170 | // Add the user-defined conversion step. |
| 2171 | FunctionDecl *Function = Best->Function; |
| 2172 | Sequence.AddUserConversionStep(Function); |
| 2173 | |
| 2174 | // Determine whether we need to perform derived-to-base or |
| 2175 | // cv-qualification adjustments. |
| 2176 | if (isa<CXXConversionDecl>(Function)) |
| 2177 | T2 = Function->getResultType(); |
| 2178 | else |
| 2179 | T2 = cv1T1; |
| 2180 | |
| 2181 | bool NewDerivedToBase = false; |
| 2182 | Sema::ReferenceCompareResult NewRefRelationship |
| 2183 | = S.CompareReferenceRelationship(DeclLoc, T1, T2.getNonReferenceType(), |
| 2184 | NewDerivedToBase); |
| 2185 | assert(NewRefRelationship != Sema::Ref_Incompatible && |
| 2186 | "Overload resolution picked a bad conversion function"); |
| 2187 | (void)NewRefRelationship; |
| 2188 | if (NewDerivedToBase) |
| 2189 | Sequence.AddDerivedToBaseCastStep( |
| 2190 | S.Context.getQualifiedType(T1, |
| 2191 | T2.getNonReferenceType().getQualifiers()), |
| 2192 | /*isLValue=*/true); |
| 2193 | |
| 2194 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
| 2195 | Sequence.AddQualificationConversionStep(cv1T1, T2->isReferenceType()); |
| 2196 | |
| 2197 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2198 | return OR_Success; |
| 2199 | } |
| 2200 | |
| 2201 | /// \brief Attempt reference initialization (C++0x [dcl.init.list]) |
| 2202 | static void TryReferenceInitialization(Sema &S, |
| 2203 | const InitializedEntity &Entity, |
| 2204 | const InitializationKind &Kind, |
| 2205 | Expr *Initializer, |
| 2206 | InitializationSequence &Sequence) { |
| 2207 | Sequence.setSequenceKind(InitializationSequence::ReferenceBinding); |
| 2208 | |
| 2209 | QualType DestType = Entity.getType().getType(); |
| 2210 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2211 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2212 | QualType cv2T2 = Initializer->getType(); |
| 2213 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2214 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2215 | |
| 2216 | // If the initializer is the address of an overloaded function, try |
| 2217 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2218 | // type of the resulting function. |
| 2219 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2220 | FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2221 | T1, |
| 2222 | false); |
| 2223 | if (!Fn) { |
| 2224 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2225 | return; |
| 2226 | } |
| 2227 | |
| 2228 | Sequence.AddAddressOverloadResolutionStep(Fn); |
| 2229 | cv2T2 = Fn->getType(); |
| 2230 | T2 = cv2T2.getUnqualifiedType(); |
| 2231 | } |
| 2232 | |
| 2233 | // FIXME: Rvalue references |
| 2234 | bool ForceRValue = false; |
| 2235 | |
| 2236 | // Compute some basic properties of the types and the initializer. |
| 2237 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2238 | bool isRValueRef = !isLValueRef; |
| 2239 | bool DerivedToBase = false; |
| 2240 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 2241 | Initializer->isLvalue(S.Context); |
| 2242 | Sema::ReferenceCompareResult RefRelationship |
| 2243 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase); |
| 2244 | |
| 2245 | // C++0x [dcl.init.ref]p5: |
| 2246 | // A reference to type "cv1 T1" is initialized by an expression of type |
| 2247 | // "cv2 T2" as follows: |
| 2248 | // |
| 2249 | // - If the reference is an lvalue reference and the initializer |
| 2250 | // expression |
| 2251 | OverloadingResult ConvOvlResult = OR_Success; |
| 2252 | if (isLValueRef) { |
| 2253 | if (InitLvalue == Expr::LV_Valid && |
| 2254 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2255 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2256 | // reference-compatible with "cv2 T2," or |
| 2257 | // |
| 2258 | // Per C++ [over.best.ics]p2, we ignore whether the lvalue is a |
| 2259 | // bit-field when we're determining whether the reference initialization |
| 2260 | // can occur. This property will be checked by PerformInitialization. |
| 2261 | if (DerivedToBase) |
| 2262 | Sequence.AddDerivedToBaseCastStep( |
| 2263 | S.Context.getQualifiedType(T1, cv2T2.getQualifiers()), |
| 2264 | /*isLValue=*/true); |
| 2265 | if (cv1T1.getQualifiers() != cv2T2.getQualifiers()) |
| 2266 | Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/true); |
| 2267 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/false); |
| 2268 | return; |
| 2269 | } |
| 2270 | |
| 2271 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2272 | // reference-related to T2, and can be implicitly converted to an |
| 2273 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2274 | // with "cv3 T3" (this conversion is selected by enumerating the |
| 2275 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2276 | // one through overload resolution (13.3)), |
| 2277 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType()) { |
| 2278 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
| 2279 | Initializer, |
| 2280 | /*AllowRValues=*/false, |
| 2281 | Sequence); |
| 2282 | if (ConvOvlResult == OR_Success) |
| 2283 | return; |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | // - Otherwise, the reference shall be an lvalue reference to a |
| 2288 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2289 | // shall be an rvalue reference and the initializer expression shall |
| 2290 | // be an rvalue. |
| 2291 | if (!((isLValueRef && cv1T1.getCVRQualifiers() == Qualifiers::Const) || |
| 2292 | (isRValueRef && InitLvalue != Expr::LV_Valid))) { |
| 2293 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2294 | Sequence.SetOverloadFailure( |
| 2295 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2296 | ConvOvlResult); |
| 2297 | else if (isLValueRef) |
| 2298 | Sequence.SetFailed(InitLvalue == Expr::LV_Valid |
| 2299 | ? (RefRelationship == Sema::Ref_Related |
| 2300 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2301 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2302 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 2303 | else |
| 2304 | Sequence.SetFailed( |
| 2305 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2306 | |
| 2307 | return; |
| 2308 | } |
| 2309 | |
| 2310 | // - If T1 and T2 are class types and |
| 2311 | if (T1->isRecordType() && T2->isRecordType()) { |
| 2312 | // - the initializer expression is an rvalue and "cv1 T1" is |
| 2313 | // reference-compatible with "cv2 T2", or |
| 2314 | if (InitLvalue != Expr::LV_Valid && |
| 2315 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2316 | if (DerivedToBase) |
| 2317 | Sequence.AddDerivedToBaseCastStep( |
| 2318 | S.Context.getQualifiedType(T1, cv2T2.getQualifiers()), |
| 2319 | /*isLValue=*/false); |
| 2320 | if (cv1T1.getQualifiers() != cv2T2.getQualifiers()) |
| 2321 | Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/false); |
| 2322 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | // - T1 is not reference-related to T2 and the initializer expression |
| 2327 | // can be implicitly converted to an rvalue of type "cv3 T3" (this |
| 2328 | // conversion is selected by enumerating the applicable conversion |
| 2329 | // functions (13.3.1.6) and choosing the best one through overload |
| 2330 | // resolution (13.3)), |
| 2331 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2332 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2333 | Kind, Initializer, |
| 2334 | /*AllowRValues=*/true, |
| 2335 | Sequence); |
| 2336 | if (ConvOvlResult) |
| 2337 | Sequence.SetOverloadFailure( |
| 2338 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2339 | ConvOvlResult); |
| 2340 | |
| 2341 | return; |
| 2342 | } |
| 2343 | |
| 2344 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2345 | return; |
| 2346 | } |
| 2347 | |
| 2348 | // - If the initializer expression is an rvalue, with T2 an array type, |
| 2349 | // and "cv1 T1" is reference-compatible with "cv2 T2," the reference |
| 2350 | // is bound to the object represented by the rvalue (see 3.10). |
| 2351 | // FIXME: How can an array type be reference-compatible with anything? |
| 2352 | // Don't we mean the element types of T1 and T2? |
| 2353 | |
| 2354 | // - Otherwise, a temporary of type “cv1 T1” is created and initialized |
| 2355 | // from the initializer expression using the rules for a non-reference |
| 2356 | // copy initialization (8.5). The reference is then bound to the |
| 2357 | // temporary. [...] |
| 2358 | // Determine whether we are allowed to call explicit constructors or |
| 2359 | // explicit conversion operators. |
| 2360 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
| 2361 | ImplicitConversionSequence ICS |
| 2362 | = S.TryImplicitConversion(Initializer, cv1T1, |
| 2363 | /*SuppressUserConversions=*/false, AllowExplicit, |
| 2364 | /*ForceRValue=*/false, |
| 2365 | /*FIXME:InOverloadResolution=*/false, |
| 2366 | /*UserCast=*/Kind.isExplicitCast()); |
| 2367 | |
| 2368 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2369 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2370 | // this into an overloading ambiguity diagnostic. However, we need |
| 2371 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2372 | // other kind of set. |
| 2373 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
| 2374 | return; |
| 2375 | } |
| 2376 | |
| 2377 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2378 | // same cv-qualification as, or greater cv-qualification |
| 2379 | // than, cv2; otherwise, the program is ill-formed. |
| 2380 | if (RefRelationship == Sema::Ref_Related && |
| 2381 | !cv1T1.isAtLeastAsQualifiedAs(cv2T2)) { |
| 2382 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2383 | return; |
| 2384 | } |
| 2385 | |
| 2386 | // Perform the actual conversion. |
| 2387 | Sequence.AddConversionSequenceStep(ICS, cv1T1); |
| 2388 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2389 | return; |
| 2390 | } |
| 2391 | |
| 2392 | /// \brief Attempt character array initialization from a string literal |
| 2393 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2394 | static void TryStringLiteralInitialization(Sema &S, |
| 2395 | const InitializedEntity &Entity, |
| 2396 | const InitializationKind &Kind, |
| 2397 | Expr *Initializer, |
| 2398 | InitializationSequence &Sequence) { |
| 2399 | // FIXME: Implement! |
| 2400 | } |
| 2401 | |
| 2402 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
| 2403 | static void TryValueInitialization(Sema &S, |
| 2404 | const InitializedEntity &Entity, |
| 2405 | const InitializationKind &Kind, |
| 2406 | InitializationSequence &Sequence) { |
| 2407 | // FIXME: Implement! |
| 2408 | } |
| 2409 | |
| 2410 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2411 | /// enumerates the constructors of the initialized entity and performs overload |
| 2412 | /// resolution to select the best. |
| 2413 | static void TryConstructorInitialization(Sema &S, |
| 2414 | const InitializedEntity &Entity, |
| 2415 | const InitializationKind &Kind, |
| 2416 | Expr **Args, unsigned NumArgs, |
| 2417 | InitializationSequence &Sequence) { |
| 2418 | // FIXME: Implement! |
| 2419 | } |
| 2420 | |
| 2421 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 2422 | /// which enumerates all conversion functions and performs overload resolution |
| 2423 | /// to select the best. |
| 2424 | static void TryUserDefinedConversion(Sema &S, |
| 2425 | const InitializedEntity &Entity, |
| 2426 | const InitializationKind &Kind, |
| 2427 | Expr *Initializer, |
| 2428 | InitializationSequence &Sequence) { |
| 2429 | // FIXME: Implement! |
| 2430 | } |
| 2431 | |
| 2432 | /// \brief Attempt an implicit conversion (C++ [conv]) converting from one |
| 2433 | /// non-class type to another. |
| 2434 | static void TryImplicitConversion(Sema &S, |
| 2435 | const InitializedEntity &Entity, |
| 2436 | const InitializationKind &Kind, |
| 2437 | Expr *Initializer, |
| 2438 | InitializationSequence &Sequence) { |
| 2439 | ImplicitConversionSequence ICS |
| 2440 | = S.TryImplicitConversion(Initializer, Entity.getType().getType(), |
| 2441 | /*SuppressUserConversions=*/true, |
| 2442 | /*AllowExplicit=*/false, |
| 2443 | /*ForceRValue=*/false, |
| 2444 | /*FIXME:InOverloadResolution=*/false, |
| 2445 | /*UserCast=*/Kind.isExplicitCast()); |
| 2446 | |
| 2447 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2448 | Sequence.SetFailed(InitializationSequence::FK_ConversionFailed); |
| 2449 | return; |
| 2450 | } |
| 2451 | |
| 2452 | Sequence.AddConversionSequenceStep(ICS, Entity.getType().getType()); |
| 2453 | } |
| 2454 | |
| 2455 | InitializationSequence::InitializationSequence(Sema &S, |
| 2456 | const InitializedEntity &Entity, |
| 2457 | const InitializationKind &Kind, |
| 2458 | Expr **Args, |
| 2459 | unsigned NumArgs) { |
| 2460 | ASTContext &Context = S.Context; |
| 2461 | |
| 2462 | // C++0x [dcl.init]p16: |
| 2463 | // The semantics of initializers are as follows. The destination type is |
| 2464 | // the type of the object or reference being initialized and the source |
| 2465 | // type is the type of the initializer expression. The source type is not |
| 2466 | // defined when the initializer is a braced-init-list or when it is a |
| 2467 | // parenthesized list of expressions. |
| 2468 | QualType DestType = Entity.getType().getType(); |
| 2469 | |
| 2470 | if (DestType->isDependentType() || |
| 2471 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 2472 | SequenceKind = DependentSequence; |
| 2473 | return; |
| 2474 | } |
| 2475 | |
| 2476 | QualType SourceType; |
| 2477 | Expr *Initializer = 0; |
| 2478 | if (Kind.getKind() == InitializationKind::IK_Copy) { |
| 2479 | Initializer = Args[0]; |
| 2480 | if (!isa<InitListExpr>(Initializer)) |
| 2481 | SourceType = Initializer->getType(); |
| 2482 | } |
| 2483 | |
| 2484 | // - If the initializer is a braced-init-list, the object is |
| 2485 | // list-initialized (8.5.4). |
| 2486 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 2487 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 2488 | } |
| 2489 | |
| 2490 | // - If the destination type is a reference type, see 8.5.3. |
| 2491 | if (DestType->isReferenceType()) { |
| 2492 | // C++0x [dcl.init.ref]p1: |
| 2493 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 2494 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 2495 | // by an object that can be converted into a T. |
| 2496 | // (Therefore, multiple arguments are not permitted.) |
| 2497 | if (NumArgs != 1) |
| 2498 | SetFailed(FK_TooManyInitsForReference); |
| 2499 | else |
| 2500 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 2501 | return; |
| 2502 | } |
| 2503 | |
| 2504 | // - If the destination type is an array of characters, an array of |
| 2505 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 2506 | // initializer is a string literal, see 8.5.2. |
| 2507 | if (Initializer && IsStringInit(Initializer, DestType, Context)) { |
| 2508 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 2509 | return; |
| 2510 | } |
| 2511 | |
| 2512 | // - If the initializer is (), the object is value-initialized. |
| 2513 | if (Kind.getKind() == InitializationKind::IK_Value) { |
| 2514 | TryValueInitialization(S, Entity, Kind, *this); |
| 2515 | return; |
| 2516 | } |
| 2517 | |
| 2518 | // - Otherwise, if the destination type is an array, the program is |
| 2519 | // ill-formed. |
| 2520 | if (const ArrayType *AT = Context.getAsArrayType(DestType)) { |
| 2521 | if (AT->getElementType()->isAnyCharacterType()) |
| 2522 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 2523 | else |
| 2524 | SetFailed(FK_ArrayNeedsInitList); |
| 2525 | |
| 2526 | return; |
| 2527 | } |
| 2528 | |
| 2529 | // - If the destination type is a (possibly cv-qualified) class type: |
| 2530 | if (DestType->isRecordType()) { |
| 2531 | // - If the initialization is direct-initialization, or if it is |
| 2532 | // copy-initialization where the cv-unqualified version of the |
| 2533 | // source type is the same class as, or a derived class of, the |
| 2534 | // class of the destination, constructors are considered. [...] |
| 2535 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 2536 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 2537 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 2538 | S.IsDerivedFrom(SourceType, DestType)))) |
| 2539 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, *this); |
| 2540 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
| 2541 | // user-defined conversion sequences that can convert from the source |
| 2542 | // type to the destination type or (when a conversion function is |
| 2543 | // used) to a derived class thereof are enumerated as described in |
| 2544 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 2545 | // (13.3). |
| 2546 | else |
| 2547 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
| 2552 | // type, conversion functions are considered. |
| 2553 | if (SourceType->isRecordType()) { |
| 2554 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
| 2558 | // - Otherwise, the initial value of the object being initialized is the |
| 2559 | // (possibly converted) value of the ini- tializer expression. Standard |
| 2560 | // conversions (Clause 4) will be used, if necessary, to convert the |
| 2561 | // initializer expression to the cv-unqualified version of the |
| 2562 | // destination type; no user-defined conversions are considered. |
| 2563 | TryImplicitConversion(S, Entity, Kind, Initializer, *this); |
| 2564 | } |
| 2565 | |
| 2566 | InitializationSequence::~InitializationSequence() { |
| 2567 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 2568 | StepEnd = Steps.end(); |
| 2569 | Step != StepEnd; ++Step) |
| 2570 | Step->Destroy(); |
| 2571 | } |
| 2572 | |
| 2573 | //===----------------------------------------------------------------------===// |
| 2574 | // Perform initialization |
| 2575 | //===----------------------------------------------------------------------===// |
| 2576 | |
| 2577 | Action::OwningExprResult |
| 2578 | InitializationSequence::Perform(Sema &S, |
| 2579 | const InitializedEntity &Entity, |
| 2580 | const InitializationKind &Kind, |
| 2581 | Action::MultiExprArg Args) { |
| 2582 | if (SequenceKind == FailedSequence) { |
| 2583 | unsigned NumArgs = Args.size(); |
| 2584 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
| 2585 | return S.ExprError(); |
| 2586 | } |
| 2587 | |
| 2588 | if (SequenceKind == DependentSequence) { |
| 2589 | if (Kind.getKind() == InitializationKind::IK_Copy) |
| 2590 | return Sema::OwningExprResult(S, Args.release()[0]); |
| 2591 | |
| 2592 | unsigned NumArgs = Args.size(); |
| 2593 | return S.Owned(new (S.Context) ParenListExpr(S.Context, |
| 2594 | SourceLocation(), |
| 2595 | (Expr **)Args.release(), |
| 2596 | NumArgs, |
| 2597 | SourceLocation())); |
| 2598 | } |
| 2599 | |
| 2600 | QualType DestType = Entity.getType().getType().getNonReferenceType(); |
| 2601 | |
| 2602 | Sema::OwningExprResult CurInit(S); |
| 2603 | // For copy initialization and any other initialization forms that |
| 2604 | // only have a single initializer, we start with the (only) |
| 2605 | // initializer we have. |
| 2606 | // FIXME: DPG is not happy about this. There's confusion regarding whether |
| 2607 | // we're supposed to start the conversion from the solitary initializer or |
| 2608 | // from the set of arguments. |
| 2609 | if (Kind.getKind() == InitializationKind::IK_Copy || |
| 2610 | SequenceKind == ReferenceBinding) { |
| 2611 | assert(Args.size() == 1); |
| 2612 | CurInit = Sema::OwningExprResult(S, Args.release()[0]); |
| 2613 | if (CurInit.isInvalid()) |
| 2614 | return S.ExprError(); |
| 2615 | } |
| 2616 | |
| 2617 | // Walk through the computed steps for the initialization sequence, |
| 2618 | // performing the specified conversions along the way. |
| 2619 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 2620 | Step != StepEnd; ++Step) { |
| 2621 | if (CurInit.isInvalid()) |
| 2622 | return S.ExprError(); |
| 2623 | |
| 2624 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
| 2625 | QualType SourceType = CurInitExpr->getType(); |
| 2626 | |
| 2627 | switch (Step->Kind) { |
| 2628 | case SK_ResolveAddressOfOverloadedFunction: |
| 2629 | // Overload resolution determined which function invoke; update the |
| 2630 | // initializer to reflect that choice. |
| 2631 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), Step->Function); |
| 2632 | break; |
| 2633 | |
| 2634 | case SK_CastDerivedToBaseRValue: |
| 2635 | case SK_CastDerivedToBaseLValue: { |
| 2636 | // We have a derived-to-base cast that produces either an rvalue or an |
| 2637 | // lvalue. Perform that cast. |
| 2638 | |
| 2639 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 2640 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 2641 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
| 2642 | CurInitExpr->getLocStart(), |
| 2643 | CurInitExpr->getSourceRange(), |
| 2644 | IgnoreBaseAccess)) |
| 2645 | return S.ExprError(); |
| 2646 | |
| 2647 | CurInit = S.Owned(new (S.Context) ImplicitCastExpr(Step->Type, |
| 2648 | CastExpr::CK_DerivedToBase, |
| 2649 | (Expr*)CurInit.release(), |
| 2650 | Step->Kind == SK_CastDerivedToBaseLValue)); |
| 2651 | break; |
| 2652 | } |
| 2653 | |
| 2654 | case SK_BindReference: |
| 2655 | if (FieldDecl *BitField = CurInitExpr->getBitField()) { |
| 2656 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 2657 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 2658 | << Entity.getType().getType().isVolatileQualified() |
| 2659 | << BitField->getDeclName() |
| 2660 | << CurInitExpr->getSourceRange(); |
| 2661 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 2662 | return S.ExprError(); |
| 2663 | } |
| 2664 | |
| 2665 | // Reference binding does not have any corresponding ASTs. |
| 2666 | |
| 2667 | // Check exception specifications |
| 2668 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
| 2669 | return S.ExprError(); |
| 2670 | break; |
| 2671 | |
| 2672 | case SK_BindReferenceToTemporary: |
| 2673 | // Check exception specifications |
| 2674 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
| 2675 | return S.ExprError(); |
| 2676 | |
| 2677 | // FIXME: At present, we have no AST to describe when we need to make a |
| 2678 | // temporary to bind a reference to. We should. |
| 2679 | break; |
| 2680 | |
| 2681 | case SK_UserConversion: { |
| 2682 | // We have a user-defined conversion that invokes either a constructor |
| 2683 | // or a conversion function. |
| 2684 | CastExpr::CastKind CastKind = CastExpr::CK_Unknown; |
| 2685 | if (CXXConstructorDecl *Constructor |
| 2686 | = dyn_cast<CXXConstructorDecl>(Step->Function)) { |
| 2687 | // Build a call to the selected constructor. |
| 2688 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S); |
| 2689 | SourceLocation Loc = CurInitExpr->getLocStart(); |
| 2690 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
| 2691 | |
| 2692 | // Determine the arguments required to actually perform the constructor |
| 2693 | // call. |
| 2694 | if (S.CompleteConstructorCall(Constructor, |
| 2695 | Sema::MultiExprArg(S, |
| 2696 | (void **)&CurInitExpr, |
| 2697 | 1), |
| 2698 | Loc, ConstructorArgs)) |
| 2699 | return S.ExprError(); |
| 2700 | |
| 2701 | // Build the an expression that constructs a temporary. |
| 2702 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
| 2703 | move_arg(ConstructorArgs)); |
| 2704 | if (CurInit.isInvalid()) |
| 2705 | return S.ExprError(); |
| 2706 | |
| 2707 | CastKind = CastExpr::CK_ConstructorConversion; |
| 2708 | } else { |
| 2709 | // Build a call to the conversion function. |
| 2710 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Step->Function); |
| 2711 | |
| 2712 | // FIXME: Should we move this initialization into a separate |
| 2713 | // derived-to-base conversion? I believe the answer is "no", because |
| 2714 | // we don't want to turn off access control here for c-style casts. |
| 2715 | if (S.PerformObjectArgumentInitialization(CurInitExpr, Conversion)) |
| 2716 | return S.ExprError(); |
| 2717 | |
| 2718 | // Do a little dance to make sure that CurInit has the proper |
| 2719 | // pointer. |
| 2720 | CurInit.release(); |
| 2721 | |
| 2722 | // Build the actual call to the conversion function. |
| 2723 | CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, Conversion)); |
| 2724 | if (CurInit.isInvalid() || !CurInit.get()) |
| 2725 | return S.ExprError(); |
| 2726 | |
| 2727 | CastKind = CastExpr::CK_UserDefinedConversion; |
| 2728 | } |
| 2729 | |
| 2730 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 2731 | CurInitExpr = CurInit.takeAs<Expr>(); |
| 2732 | CurInit = S.Owned(new (S.Context) ImplicitCastExpr(CurInitExpr->getType(), |
| 2733 | CastKind, |
| 2734 | CurInitExpr, |
| 2735 | false)); |
| 2736 | break; |
| 2737 | } |
| 2738 | |
| 2739 | case SK_QualificationConversionLValue: |
| 2740 | case SK_QualificationConversionRValue: |
| 2741 | // Perform a qualification conversion; these can never go wrong. |
| 2742 | S.ImpCastExprToType(CurInitExpr, Step->Type, |
| 2743 | CastExpr::CK_NoOp, |
| 2744 | Step->Kind == SK_QualificationConversionLValue); |
| 2745 | CurInit.release(); |
| 2746 | CurInit = S.Owned(CurInitExpr); |
| 2747 | break; |
| 2748 | |
| 2749 | case SK_ConversionSequence: |
| 2750 | if (S.PerformImplicitConversion(CurInitExpr, Step->Type, "converting", |
| 2751 | false, false, *Step->ICS)) |
| 2752 | return S.ExprError(); |
| 2753 | |
| 2754 | CurInit.release(); |
| 2755 | CurInit = S.Owned(CurInitExpr); |
| 2756 | break; |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | return move(CurInit); |
| 2761 | } |
| 2762 | |
| 2763 | //===----------------------------------------------------------------------===// |
| 2764 | // Diagnose initialization failures |
| 2765 | //===----------------------------------------------------------------------===// |
| 2766 | bool InitializationSequence::Diagnose(Sema &S, |
| 2767 | const InitializedEntity &Entity, |
| 2768 | const InitializationKind &Kind, |
| 2769 | Expr **Args, unsigned NumArgs) { |
| 2770 | if (SequenceKind != FailedSequence) |
| 2771 | return false; |
| 2772 | |
| 2773 | QualType DestType = Entity.getType().getType(); |
| 2774 | switch (Failure) { |
| 2775 | case FK_TooManyInitsForReference: |
| 2776 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 2777 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
| 2778 | break; |
| 2779 | |
| 2780 | case FK_ArrayNeedsInitList: |
| 2781 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2782 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 2783 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 2784 | break; |
| 2785 | |
| 2786 | case FK_AddressOfOverloadFailed: |
| 2787 | S.ResolveAddressOfOverloadedFunction(Args[0], |
| 2788 | DestType.getNonReferenceType(), |
| 2789 | true); |
| 2790 | break; |
| 2791 | |
| 2792 | case FK_ReferenceInitOverloadFailed: |
| 2793 | switch (FailedOverloadResult) { |
| 2794 | case OR_Ambiguous: |
| 2795 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 2796 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 2797 | << Args[0]->getSourceRange(); |
| 2798 | S.PrintOverloadCandidates(FailedCandidateSet, true); |
| 2799 | break; |
| 2800 | |
| 2801 | case OR_No_Viable_Function: |
| 2802 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 2803 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 2804 | << Args[0]->getSourceRange(); |
| 2805 | S.PrintOverloadCandidates(FailedCandidateSet, false); |
| 2806 | break; |
| 2807 | |
| 2808 | case OR_Deleted: { |
| 2809 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 2810 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 2811 | << Args[0]->getSourceRange(); |
| 2812 | OverloadCandidateSet::iterator Best; |
| 2813 | OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet, |
| 2814 | Kind.getLocation(), |
| 2815 | Best); |
| 2816 | if (Ovl == OR_Deleted) { |
| 2817 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 2818 | << Best->Function->isDeleted(); |
| 2819 | } else { |
| 2820 | llvm::llvm_unreachable("Inconsistent overload resolution?"); |
| 2821 | } |
| 2822 | break; |
| 2823 | } |
| 2824 | |
| 2825 | case OR_Success: |
| 2826 | llvm::llvm_unreachable("Conversion did not fail!"); |
| 2827 | break; |
| 2828 | } |
| 2829 | break; |
| 2830 | |
| 2831 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2832 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2833 | S.Diag(Kind.getLocation(), |
| 2834 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 2835 | ? diag::err_lvalue_reference_bind_to_temporary |
| 2836 | : diag::err_lvalue_reference_bind_to_unrelated) |
| 2837 | << DestType.getNonReferenceType() |
| 2838 | << Args[0]->getType() |
| 2839 | << Args[0]->getSourceRange(); |
| 2840 | break; |
| 2841 | |
| 2842 | case FK_RValueReferenceBindingToLValue: |
| 2843 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
| 2844 | << Args[0]->getSourceRange(); |
| 2845 | break; |
| 2846 | |
| 2847 | case FK_ReferenceInitDropsQualifiers: |
| 2848 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 2849 | << DestType.getNonReferenceType() |
| 2850 | << Args[0]->getType() |
| 2851 | << Args[0]->getSourceRange(); |
| 2852 | break; |
| 2853 | |
| 2854 | case FK_ReferenceInitFailed: |
| 2855 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 2856 | << DestType.getNonReferenceType() |
| 2857 | << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid) |
| 2858 | << Args[0]->getType() |
| 2859 | << Args[0]->getSourceRange(); |
| 2860 | break; |
| 2861 | |
| 2862 | case FK_ConversionFailed: |
| 2863 | S.Diag(Kind.getLocation(), diag::err_cannot_initialize_decl_noname) |
| 2864 | << DestType |
| 2865 | << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid) |
| 2866 | << Args[0]->getType() |
| 2867 | << Args[0]->getSourceRange(); |
| 2868 | break; |
| 2869 | } |
| 2870 | |
| 2871 | return true; |
| 2872 | } |