clang API Documentation
00001 //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the C++ related Decl classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/AST/DeclCXX.h" 00015 #include "clang/AST/DeclTemplate.h" 00016 #include "clang/AST/ASTContext.h" 00017 #include "clang/AST/ASTMutationListener.h" 00018 #include "clang/AST/CXXInheritance.h" 00019 #include "clang/AST/Expr.h" 00020 #include "clang/AST/ExprCXX.h" 00021 #include "clang/AST/TypeLoc.h" 00022 #include "clang/Basic/IdentifierTable.h" 00023 #include "llvm/ADT/STLExtras.h" 00024 #include "llvm/ADT/SmallPtrSet.h" 00025 using namespace clang; 00026 00027 //===----------------------------------------------------------------------===// 00028 // Decl Allocation/Deallocation Method Implementations 00029 //===----------------------------------------------------------------------===// 00030 00031 void AccessSpecDecl::anchor() { } 00032 00033 AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 00034 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl)); 00035 return new (Mem) AccessSpecDecl(EmptyShell()); 00036 } 00037 00038 CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) 00039 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), 00040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false), 00041 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false), 00042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), 00043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true), 00044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false), 00045 HasMutableFields(false), HasOnlyCMembers(true), 00046 HasInClassInitializer(false), 00047 HasTrivialDefaultConstructor(true), 00048 HasConstexprNonCopyMoveConstructor(false), 00049 DefaultedDefaultConstructorIsConstexpr(true), 00050 DefaultedCopyConstructorIsConstexpr(true), 00051 DefaultedMoveConstructorIsConstexpr(true), 00052 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false), 00053 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true), 00054 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true), 00055 HasTrivialMoveAssignment(true), HasTrivialDestructor(true), 00056 HasIrrelevantDestructor(true), 00057 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false), 00058 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false), 00059 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false), 00060 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false), 00061 DeclaredDestructor(false), FailedImplicitMoveConstructor(false), 00062 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0), 00063 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) { 00064 } 00065 00066 CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 00067 SourceLocation StartLoc, SourceLocation IdLoc, 00068 IdentifierInfo *Id, CXXRecordDecl *PrevDecl) 00069 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl), 00070 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), 00071 TemplateOrInstantiation() { } 00072 00073 CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, 00074 DeclContext *DC, SourceLocation StartLoc, 00075 SourceLocation IdLoc, IdentifierInfo *Id, 00076 CXXRecordDecl* PrevDecl, 00077 bool DelayTypeCreation) { 00078 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc, 00079 Id, PrevDecl); 00080 00081 // FIXME: DelayTypeCreation seems like such a hack 00082 if (!DelayTypeCreation) 00083 C.getTypeDeclType(R, PrevDecl); 00084 return R; 00085 } 00086 00087 CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC, 00088 SourceLocation Loc, bool Dependent) { 00089 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc, 00090 0, 0); 00091 R->IsBeingDefined = true; 00092 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent); 00093 C.getTypeDeclType(R, /*PrevDecl=*/0); 00094 return R; 00095 } 00096 00097 CXXRecordDecl * 00098 CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { 00099 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl)); 00100 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 00101 SourceLocation(), 0, 0); 00102 } 00103 00104 void 00105 CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, 00106 unsigned NumBases) { 00107 ASTContext &C = getASTContext(); 00108 00109 if (!data().Bases.isOffset() && data().NumBases > 0) 00110 C.Deallocate(data().getBases()); 00111 00112 if (NumBases) { 00113 // C++ [dcl.init.aggr]p1: 00114 // An aggregate is [...] a class with [...] no base classes [...]. 00115 data().Aggregate = false; 00116 00117 // C++ [class]p4: 00118 // A POD-struct is an aggregate class... 00119 data().PlainOldData = false; 00120 } 00121 00122 // The set of seen virtual base types. 00123 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; 00124 00125 // The virtual bases of this class. 00126 SmallVector<const CXXBaseSpecifier *, 8> VBases; 00127 00128 data().Bases = new(C) CXXBaseSpecifier [NumBases]; 00129 data().NumBases = NumBases; 00130 for (unsigned i = 0; i < NumBases; ++i) { 00131 data().getBases()[i] = *Bases[i]; 00132 // Keep track of inherited vbases for this base class. 00133 const CXXBaseSpecifier *Base = Bases[i]; 00134 QualType BaseType = Base->getType(); 00135 // Skip dependent types; we can't do any checking on them now. 00136 if (BaseType->isDependentType()) 00137 continue; 00138 CXXRecordDecl *BaseClassDecl 00139 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 00140 00141 // A class with a non-empty base class is not empty. 00142 // FIXME: Standard ref? 00143 if (!BaseClassDecl->isEmpty()) { 00144 if (!data().Empty) { 00145 // C++0x [class]p7: 00146 // A standard-layout class is a class that: 00147 // [...] 00148 // -- either has no non-static data members in the most derived 00149 // class and at most one base class with non-static data members, 00150 // or has no base classes with non-static data members, and 00151 // If this is the second non-empty base, then neither of these two 00152 // clauses can be true. 00153 data().IsStandardLayout = false; 00154 } 00155 00156 data().Empty = false; 00157 data().HasNoNonEmptyBases = false; 00158 } 00159 00160 // C++ [class.virtual]p1: 00161 // A class that declares or inherits a virtual function is called a 00162 // polymorphic class. 00163 if (BaseClassDecl->isPolymorphic()) 00164 data().Polymorphic = true; 00165 00166 // C++0x [class]p7: 00167 // A standard-layout class is a class that: [...] 00168 // -- has no non-standard-layout base classes 00169 if (!BaseClassDecl->isStandardLayout()) 00170 data().IsStandardLayout = false; 00171 00172 // Record if this base is the first non-literal field or base. 00173 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType()) 00174 data().HasNonLiteralTypeFieldsOrBases = true; 00175 00176 // Now go through all virtual bases of this base and add them. 00177 for (CXXRecordDecl::base_class_iterator VBase = 00178 BaseClassDecl->vbases_begin(), 00179 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { 00180 // Add this base if it's not already in the list. 00181 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) 00182 VBases.push_back(VBase); 00183 } 00184 00185 if (Base->isVirtual()) { 00186 // Add this base if it's not already in the list. 00187 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) 00188 VBases.push_back(Base); 00189 00190 // C++0x [meta.unary.prop] is_empty: 00191 // T is a class type, but not a union type, with ... no virtual base 00192 // classes 00193 data().Empty = false; 00194 00195 // C++ [class.ctor]p5: 00196 // A default constructor is trivial [...] if: 00197 // -- its class has [...] no virtual bases 00198 data().HasTrivialDefaultConstructor = false; 00199 00200 // C++0x [class.copy]p13: 00201 // A copy/move constructor for class X is trivial if it is neither 00202 // user-provided nor deleted and if 00203 // -- class X has no virtual functions and no virtual base classes, and 00204 data().HasTrivialCopyConstructor = false; 00205 data().HasTrivialMoveConstructor = false; 00206 00207 // C++0x [class.copy]p27: 00208 // A copy/move assignment operator for class X is trivial if it is 00209 // neither user-provided nor deleted and if 00210 // -- class X has no virtual functions and no virtual base classes, and 00211 data().HasTrivialCopyAssignment = false; 00212 data().HasTrivialMoveAssignment = false; 00213 00214 // C++0x [class]p7: 00215 // A standard-layout class is a class that: [...] 00216 // -- has [...] no virtual base classes 00217 data().IsStandardLayout = false; 00218 00219 // C++11 [dcl.constexpr]p4: 00220 // In the definition of a constexpr constructor [...] 00221 // -- the class shall not have any virtual base classes 00222 data().DefaultedDefaultConstructorIsConstexpr = false; 00223 data().DefaultedCopyConstructorIsConstexpr = false; 00224 data().DefaultedMoveConstructorIsConstexpr = false; 00225 } else { 00226 // C++ [class.ctor]p5: 00227 // A default constructor is trivial [...] if: 00228 // -- all the direct base classes of its class have trivial default 00229 // constructors. 00230 if (!BaseClassDecl->hasTrivialDefaultConstructor()) 00231 data().HasTrivialDefaultConstructor = false; 00232 00233 // C++0x [class.copy]p13: 00234 // A copy/move constructor for class X is trivial if [...] 00235 // [...] 00236 // -- the constructor selected to copy/move each direct base class 00237 // subobject is trivial, and 00238 // FIXME: C++0x: We need to only consider the selected constructor 00239 // instead of all of them. 00240 if (!BaseClassDecl->hasTrivialCopyConstructor()) 00241 data().HasTrivialCopyConstructor = false; 00242 if (!BaseClassDecl->hasTrivialMoveConstructor()) 00243 data().HasTrivialMoveConstructor = false; 00244 00245 // C++0x [class.copy]p27: 00246 // A copy/move assignment operator for class X is trivial if [...] 00247 // [...] 00248 // -- the assignment operator selected to copy/move each direct base 00249 // class subobject is trivial, and 00250 // FIXME: C++0x: We need to only consider the selected operator instead 00251 // of all of them. 00252 if (!BaseClassDecl->hasTrivialCopyAssignment()) 00253 data().HasTrivialCopyAssignment = false; 00254 if (!BaseClassDecl->hasTrivialMoveAssignment()) 00255 data().HasTrivialMoveAssignment = false; 00256 00257 // C++11 [class.ctor]p6: 00258 // If that user-written default constructor would satisfy the 00259 // requirements of a constexpr constructor, the implicitly-defined 00260 // default constructor is constexpr. 00261 if (!BaseClassDecl->hasConstexprDefaultConstructor()) 00262 data().DefaultedDefaultConstructorIsConstexpr = false; 00263 00264 // C++11 [class.copy]p13: 00265 // If the implicitly-defined constructor would satisfy the requirements 00266 // of a constexpr constructor, the implicitly-defined constructor is 00267 // constexpr. 00268 // C++11 [dcl.constexpr]p4: 00269 // -- every constructor involved in initializing [...] base class 00270 // sub-objects shall be a constexpr constructor 00271 if (!BaseClassDecl->hasConstexprCopyConstructor()) 00272 data().DefaultedCopyConstructorIsConstexpr = false; 00273 if (BaseClassDecl->hasDeclaredMoveConstructor() || 00274 BaseClassDecl->needsImplicitMoveConstructor()) 00275 // FIXME: If the implicit move constructor generated for the base class 00276 // would be ill-formed, the implicit move constructor generated for the 00277 // derived class calls the base class' copy constructor. 00278 data().DefaultedMoveConstructorIsConstexpr &= 00279 BaseClassDecl->hasConstexprMoveConstructor(); 00280 else if (!BaseClassDecl->hasConstexprCopyConstructor()) 00281 data().DefaultedMoveConstructorIsConstexpr = false; 00282 } 00283 00284 // C++ [class.ctor]p3: 00285 // A destructor is trivial if all the direct base classes of its class 00286 // have trivial destructors. 00287 if (!BaseClassDecl->hasTrivialDestructor()) 00288 data().HasTrivialDestructor = false; 00289 00290 if (!BaseClassDecl->hasIrrelevantDestructor()) 00291 data().HasIrrelevantDestructor = false; 00292 00293 // A class has an Objective-C object member if... or any of its bases 00294 // has an Objective-C object member. 00295 if (BaseClassDecl->hasObjectMember()) 00296 setHasObjectMember(true); 00297 00298 // Keep track of the presence of mutable fields. 00299 if (BaseClassDecl->hasMutableFields()) 00300 data().HasMutableFields = true; 00301 } 00302 00303 if (VBases.empty()) 00304 return; 00305 00306 // Create base specifier for any direct or indirect virtual bases. 00307 data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; 00308 data().NumVBases = VBases.size(); 00309 for (int I = 0, E = VBases.size(); I != E; ++I) 00310 data().getVBases()[I] = *VBases[I]; 00311 } 00312 00313 /// Callback function for CXXRecordDecl::forallBases that acknowledges 00314 /// that it saw a base class. 00315 static bool SawBase(const CXXRecordDecl *, void *) { 00316 return true; 00317 } 00318 00319 bool CXXRecordDecl::hasAnyDependentBases() const { 00320 if (!isDependentContext()) 00321 return false; 00322 00323 return !forallBases(SawBase, 0); 00324 } 00325 00326 bool CXXRecordDecl::hasConstCopyConstructor() const { 00327 return getCopyConstructor(Qualifiers::Const) != 0; 00328 } 00329 00330 bool CXXRecordDecl::isTriviallyCopyable() const { 00331 // C++0x [class]p5: 00332 // A trivially copyable class is a class that: 00333 // -- has no non-trivial copy constructors, 00334 if (!hasTrivialCopyConstructor()) return false; 00335 // -- has no non-trivial move constructors, 00336 if (!hasTrivialMoveConstructor()) return false; 00337 // -- has no non-trivial copy assignment operators, 00338 if (!hasTrivialCopyAssignment()) return false; 00339 // -- has no non-trivial move assignment operators, and 00340 if (!hasTrivialMoveAssignment()) return false; 00341 // -- has a trivial destructor. 00342 if (!hasTrivialDestructor()) return false; 00343 00344 return true; 00345 } 00346 00347 /// \brief Perform a simplistic form of overload resolution that only considers 00348 /// cv-qualifiers on a single parameter, and return the best overload candidate 00349 /// (if there is one). 00350 static CXXMethodDecl * 00351 GetBestOverloadCandidateSimple( 00352 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) { 00353 if (Cands.empty()) 00354 return 0; 00355 if (Cands.size() == 1) 00356 return Cands[0].first; 00357 00358 unsigned Best = 0, N = Cands.size(); 00359 for (unsigned I = 1; I != N; ++I) 00360 if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) 00361 Best = I; 00362 00363 for (unsigned I = 1; I != N; ++I) 00364 if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) 00365 return 0; 00366 00367 return Cands[Best].first; 00368 } 00369 00370 CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{ 00371 ASTContext &Context = getASTContext(); 00372 QualType ClassType 00373 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); 00374 DeclarationName ConstructorName 00375 = Context.DeclarationNames.getCXXConstructorName( 00376 Context.getCanonicalType(ClassType)); 00377 unsigned FoundTQs; 00378 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; 00379 DeclContext::lookup_const_iterator Con, ConEnd; 00380 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); 00381 Con != ConEnd; ++Con) { 00382 // C++ [class.copy]p2: 00383 // A non-template constructor for class X is a copy constructor if [...] 00384 if (isa<FunctionTemplateDecl>(*Con)) 00385 continue; 00386 00387 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); 00388 if (Constructor->isCopyConstructor(FoundTQs)) { 00389 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || 00390 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) 00391 Found.push_back(std::make_pair( 00392 const_cast<CXXConstructorDecl *>(Constructor), 00393 Qualifiers::fromCVRMask(FoundTQs))); 00394 } 00395 } 00396 00397 return cast_or_null<CXXConstructorDecl>( 00398 GetBestOverloadCandidateSimple(Found)); 00399 } 00400 00401 CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const { 00402 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I) 00403 if (I->isMoveConstructor()) 00404 return &*I; 00405 00406 return 0; 00407 } 00408 00409 CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const { 00410 ASTContext &Context = getASTContext(); 00411 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); 00412 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); 00413 00414 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; 00415 DeclContext::lookup_const_iterator Op, OpEnd; 00416 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) { 00417 // C++ [class.copy]p9: 00418 // A user-declared copy assignment operator is a non-static non-template 00419 // member function of class X with exactly one parameter of type X, X&, 00420 // const X&, volatile X& or const volatile X&. 00421 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); 00422 if (!Method || Method->isStatic() || Method->getPrimaryTemplate()) 00423 continue; 00424 00425 const FunctionProtoType *FnType 00426 = Method->getType()->getAs<FunctionProtoType>(); 00427 assert(FnType && "Overloaded operator has no prototype."); 00428 // Don't assert on this; an invalid decl might have been left in the AST. 00429 if (FnType->getNumArgs() != 1 || FnType->isVariadic()) 00430 continue; 00431 00432 QualType ArgType = FnType->getArgType(0); 00433 Qualifiers Quals; 00434 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { 00435 ArgType = Ref->getPointeeType(); 00436 // If we have a const argument and we have a reference to a non-const, 00437 // this function does not match. 00438 if (ArgIsConst && !ArgType.isConstQualified()) 00439 continue; 00440 00441 Quals = ArgType.getQualifiers(); 00442 } else { 00443 // By-value copy-assignment operators are treated like const X& 00444 // copy-assignment operators. 00445 Quals = Qualifiers::fromCVRMask(Qualifiers::Const); 00446 } 00447 00448 if (!Context.hasSameUnqualifiedType(ArgType, Class)) 00449 continue; 00450 00451 // Save this copy-assignment operator. It might be "the one". 00452 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals)); 00453 } 00454 00455 // Use a simplistic form of overload resolution to find the candidate. 00456 return GetBestOverloadCandidateSimple(Found); 00457 } 00458 00459 CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const { 00460 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I) 00461 if (I->isMoveAssignmentOperator()) 00462 return &*I; 00463 00464 return 0; 00465 } 00466 00467 void CXXRecordDecl::markedVirtualFunctionPure() { 00468 // C++ [class.abstract]p2: 00469 // A class is abstract if it has at least one pure virtual function. 00470 data().Abstract = true; 00471 } 00472 00473 void CXXRecordDecl::addedMember(Decl *D) { 00474 if (!D->isImplicit() && 00475 !isa<FieldDecl>(D) && 00476 !isa<IndirectFieldDecl>(D) && 00477 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class)) 00478 data().HasOnlyCMembers = false; 00479 00480 // Ignore friends and invalid declarations. 00481 if (D->getFriendObjectKind() || D->isInvalidDecl()) 00482 return; 00483 00484 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); 00485 if (FunTmpl) 00486 D = FunTmpl->getTemplatedDecl(); 00487 00488 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 00489 if (Method->isVirtual()) { 00490 // C++ [dcl.init.aggr]p1: 00491 // An aggregate is an array or a class with [...] no virtual functions. 00492 data().Aggregate = false; 00493 00494 // C++ [class]p4: 00495 // A POD-struct is an aggregate class... 00496 data().PlainOldData = false; 00497 00498 // Virtual functions make the class non-empty. 00499 // FIXME: Standard ref? 00500 data().Empty = false; 00501 00502 // C++ [class.virtual]p1: 00503 // A class that declares or inherits a virtual function is called a 00504 // polymorphic class. 00505 data().Polymorphic = true; 00506 00507 // C++0x [class.ctor]p5 00508 // A default constructor is trivial [...] if: 00509 // -- its class has no virtual functions [...] 00510 data().HasTrivialDefaultConstructor = false; 00511 00512 // C++0x [class.copy]p13: 00513 // A copy/move constructor for class X is trivial if [...] 00514 // -- class X has no virtual functions [...] 00515 data().HasTrivialCopyConstructor = false; 00516 data().HasTrivialMoveConstructor = false; 00517 00518 // C++0x [class.copy]p27: 00519 // A copy/move assignment operator for class X is trivial if [...] 00520 // -- class X has no virtual functions [...] 00521 data().HasTrivialCopyAssignment = false; 00522 data().HasTrivialMoveAssignment = false; 00523 00524 // C++0x [class]p7: 00525 // A standard-layout class is a class that: [...] 00526 // -- has no virtual functions 00527 data().IsStandardLayout = false; 00528 } 00529 } 00530 00531 if (D->isImplicit()) { 00532 // Notify that an implicit member was added after the definition 00533 // was completed. 00534 if (!isBeingDefined()) 00535 if (ASTMutationListener *L = getASTMutationListener()) 00536 L->AddedCXXImplicitMember(data().Definition, D); 00537 00538 // If this is a special member function, note that it was added and then 00539 // return early. 00540 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 00541 if (Constructor->isDefaultConstructor()) { 00542 data().DeclaredDefaultConstructor = true; 00543 if (Constructor->isConstexpr()) { 00544 data().HasConstexprDefaultConstructor = true; 00545 data().HasConstexprNonCopyMoveConstructor = true; 00546 } 00547 } else if (Constructor->isCopyConstructor()) { 00548 data().DeclaredCopyConstructor = true; 00549 if (Constructor->isConstexpr()) 00550 data().HasConstexprCopyConstructor = true; 00551 } else if (Constructor->isMoveConstructor()) { 00552 data().DeclaredMoveConstructor = true; 00553 if (Constructor->isConstexpr()) 00554 data().HasConstexprMoveConstructor = true; 00555 } else 00556 goto NotASpecialMember; 00557 return; 00558 } else if (isa<CXXDestructorDecl>(D)) { 00559 data().DeclaredDestructor = true; 00560 return; 00561 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 00562 if (Method->isCopyAssignmentOperator()) 00563 data().DeclaredCopyAssignment = true; 00564 else if (Method->isMoveAssignmentOperator()) 00565 data().DeclaredMoveAssignment = true; 00566 else 00567 goto NotASpecialMember; 00568 return; 00569 } 00570 00571 NotASpecialMember:; 00572 // Any other implicit declarations are handled like normal declarations. 00573 } 00574 00575 // Handle (user-declared) constructors. 00576 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 00577 // Note that we have a user-declared constructor. 00578 data().UserDeclaredConstructor = true; 00579 00580 // Technically, "user-provided" is only defined for special member 00581 // functions, but the intent of the standard is clearly that it should apply 00582 // to all functions. 00583 bool UserProvided = Constructor->isUserProvided(); 00584 00585 if (Constructor->isDefaultConstructor()) { 00586 data().DeclaredDefaultConstructor = true; 00587 if (UserProvided) { 00588 // C++0x [class.ctor]p5: 00589 // A default constructor is trivial if it is not user-provided [...] 00590 data().HasTrivialDefaultConstructor = false; 00591 data().UserProvidedDefaultConstructor = true; 00592 } 00593 if (Constructor->isConstexpr()) { 00594 data().HasConstexprDefaultConstructor = true; 00595 data().HasConstexprNonCopyMoveConstructor = true; 00596 } 00597 } 00598 00599 // Note when we have a user-declared copy or move constructor, which will 00600 // suppress the implicit declaration of those constructors. 00601 if (!FunTmpl) { 00602 if (Constructor->isCopyConstructor()) { 00603 data().UserDeclaredCopyConstructor = true; 00604 data().DeclaredCopyConstructor = true; 00605 00606 // C++0x [class.copy]p13: 00607 // A copy/move constructor for class X is trivial if it is not 00608 // user-provided [...] 00609 if (UserProvided) 00610 data().HasTrivialCopyConstructor = false; 00611 00612 if (Constructor->isConstexpr()) 00613 data().HasConstexprCopyConstructor = true; 00614 } else if (Constructor->isMoveConstructor()) { 00615 data().UserDeclaredMoveConstructor = true; 00616 data().DeclaredMoveConstructor = true; 00617 00618 // C++0x [class.copy]p13: 00619 // A copy/move constructor for class X is trivial if it is not 00620 // user-provided [...] 00621 if (UserProvided) 00622 data().HasTrivialMoveConstructor = false; 00623 00624 if (Constructor->isConstexpr()) 00625 data().HasConstexprMoveConstructor = true; 00626 } 00627 } 00628 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) { 00629 // Record if we see any constexpr constructors which are neither copy 00630 // nor move constructors. 00631 data().HasConstexprNonCopyMoveConstructor = true; 00632 } 00633 00634 // C++ [dcl.init.aggr]p1: 00635 // An aggregate is an array or a class with no user-declared 00636 // constructors [...]. 00637 // C++0x [dcl.init.aggr]p1: 00638 // An aggregate is an array or a class with no user-provided 00639 // constructors [...]. 00640 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided) 00641 data().Aggregate = false; 00642 00643 // C++ [class]p4: 00644 // A POD-struct is an aggregate class [...] 00645 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the 00646 // type is technically an aggregate in C++0x since it wouldn't be in 03. 00647 data().PlainOldData = false; 00648 00649 return; 00650 } 00651 00652 // Handle (user-declared) destructors. 00653 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) { 00654 data().DeclaredDestructor = true; 00655 data().UserDeclaredDestructor = true; 00656 data().HasIrrelevantDestructor = false; 00657 00658 // C++ [class]p4: 00659 // A POD-struct is an aggregate class that has [...] no user-defined 00660 // destructor. 00661 // This bit is the C++03 POD bit, not the 0x one. 00662 data().PlainOldData = false; 00663 00664 // C++11 [class.dtor]p5: 00665 // A destructor is trivial if it is not user-provided and if 00666 // -- the destructor is not virtual. 00667 if (DD->isUserProvided() || DD->isVirtual()) { 00668 data().HasTrivialDestructor = false; 00669 // C++11 [dcl.constexpr]p1: 00670 // The constexpr specifier shall be applied only to [...] the 00671 // declaration of a static data member of a literal type. 00672 // C++11 [basic.types]p10: 00673 // A type is a literal type if it is [...] a class type that [...] has 00674 // a trivial destructor. 00675 data().DefaultedDefaultConstructorIsConstexpr = false; 00676 data().DefaultedCopyConstructorIsConstexpr = false; 00677 data().DefaultedMoveConstructorIsConstexpr = false; 00678 } 00679 00680 return; 00681 } 00682 00683 // Handle (user-declared) member functions. 00684 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 00685 if (Method->isCopyAssignmentOperator()) { 00686 // C++ [class]p4: 00687 // A POD-struct is an aggregate class that [...] has no user-defined 00688 // copy assignment operator [...]. 00689 // This is the C++03 bit only. 00690 data().PlainOldData = false; 00691 00692 // This is a copy assignment operator. 00693 00694 // Suppress the implicit declaration of a copy constructor. 00695 data().UserDeclaredCopyAssignment = true; 00696 data().DeclaredCopyAssignment = true; 00697 00698 // C++0x [class.copy]p27: 00699 // A copy/move assignment operator for class X is trivial if it is 00700 // neither user-provided nor deleted [...] 00701 if (Method->isUserProvided()) 00702 data().HasTrivialCopyAssignment = false; 00703 00704 return; 00705 } 00706 00707 if (Method->isMoveAssignmentOperator()) { 00708 // This is an extension in C++03 mode, but we'll keep consistency by 00709 // taking a move assignment operator to induce non-POD-ness 00710 data().PlainOldData = false; 00711 00712 // This is a move assignment operator. 00713 data().UserDeclaredMoveAssignment = true; 00714 data().DeclaredMoveAssignment = true; 00715 00716 // C++0x [class.copy]p27: 00717 // A copy/move assignment operator for class X is trivial if it is 00718 // neither user-provided nor deleted [...] 00719 if (Method->isUserProvided()) 00720 data().HasTrivialMoveAssignment = false; 00721 } 00722 00723 // Keep the list of conversion functions up-to-date. 00724 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 00725 // We don't record specializations. 00726 if (Conversion->getPrimaryTemplate()) 00727 return; 00728 00729 // FIXME: We intentionally don't use the decl's access here because it 00730 // hasn't been set yet. That's really just a misdesign in Sema. 00731 00732 if (FunTmpl) { 00733 if (FunTmpl->getPreviousDecl()) 00734 data().Conversions.replace(FunTmpl->getPreviousDecl(), 00735 FunTmpl); 00736 else 00737 data().Conversions.addDecl(FunTmpl); 00738 } else { 00739 if (Conversion->getPreviousDecl()) 00740 data().Conversions.replace(Conversion->getPreviousDecl(), 00741 Conversion); 00742 else 00743 data().Conversions.addDecl(Conversion); 00744 } 00745 } 00746 00747 return; 00748 } 00749 00750 // Handle non-static data members. 00751 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 00752 // C++ [class.bit]p2: 00753 // A declaration for a bit-field that omits the identifier declares an 00754 // unnamed bit-field. Unnamed bit-fields are not members and cannot be 00755 // initialized. 00756 if (Field->isUnnamedBitfield()) 00757 return; 00758 00759 // C++ [dcl.init.aggr]p1: 00760 // An aggregate is an array or a class (clause 9) with [...] no 00761 // private or protected non-static data members (clause 11). 00762 // 00763 // A POD must be an aggregate. 00764 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { 00765 data().Aggregate = false; 00766 data().PlainOldData = false; 00767 } 00768 00769 // C++0x [class]p7: 00770 // A standard-layout class is a class that: 00771 // [...] 00772 // -- has the same access control for all non-static data members, 00773 switch (D->getAccess()) { 00774 case AS_private: data().HasPrivateFields = true; break; 00775 case AS_protected: data().HasProtectedFields = true; break; 00776 case AS_public: data().HasPublicFields = true; break; 00777 case AS_none: llvm_unreachable("Invalid access specifier"); 00778 }; 00779 if ((data().HasPrivateFields + data().HasProtectedFields + 00780 data().HasPublicFields) > 1) 00781 data().IsStandardLayout = false; 00782 00783 // Keep track of the presence of mutable fields. 00784 if (Field->isMutable()) 00785 data().HasMutableFields = true; 00786 00787 // C++0x [class]p9: 00788 // A POD struct is a class that is both a trivial class and a 00789 // standard-layout class, and has no non-static data members of type 00790 // non-POD struct, non-POD union (or array of such types). 00791 // 00792 // Automatic Reference Counting: the presence of a member of Objective-C pointer type 00793 // that does not explicitly have no lifetime makes the class a non-POD. 00794 // However, we delay setting PlainOldData to false in this case so that 00795 // Sema has a chance to diagnostic causes where the same class will be 00796 // non-POD with Automatic Reference Counting but a POD without Instant Objects. 00797 // In this case, the class will become a non-POD class when we complete 00798 // the definition. 00799 ASTContext &Context = getASTContext(); 00800 QualType T = Context.getBaseElementType(Field->getType()); 00801 if (T->isObjCRetainableType() || T.isObjCGCStrong()) { 00802 if (!Context.getLangOpts().ObjCAutoRefCount || 00803 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) 00804 setHasObjectMember(true); 00805 } else if (!T.isPODType(Context)) 00806 data().PlainOldData = false; 00807 00808 if (T->isReferenceType()) { 00809 data().HasTrivialDefaultConstructor = false; 00810 00811 // C++0x [class]p7: 00812 // A standard-layout class is a class that: 00813 // -- has no non-static data members of type [...] reference, 00814 data().IsStandardLayout = false; 00815 } 00816 00817 // Record if this field is the first non-literal or volatile field or base. 00818 if (!T->isLiteralType() || T.isVolatileQualified()) 00819 data().HasNonLiteralTypeFieldsOrBases = true; 00820 00821 if (Field->hasInClassInitializer()) { 00822 data().HasInClassInitializer = true; 00823 00824 // C++11 [class]p5: 00825 // A default constructor is trivial if [...] no non-static data member 00826 // of its class has a brace-or-equal-initializer. 00827 data().HasTrivialDefaultConstructor = false; 00828 00829 // C++11 [dcl.init.aggr]p1: 00830 // An aggregate is a [...] class with [...] no 00831 // brace-or-equal-initializers for non-static data members. 00832 data().Aggregate = false; 00833 00834 // C++11 [class]p10: 00835 // A POD struct is [...] a trivial class. 00836 data().PlainOldData = false; 00837 } 00838 00839 if (const RecordType *RecordTy = T->getAs<RecordType>()) { 00840 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); 00841 if (FieldRec->getDefinition()) { 00842 // C++0x [class.ctor]p5: 00843 // A default constructor is trivial [...] if: 00844 // -- for all the non-static data members of its class that are of 00845 // class type (or array thereof), each such class has a trivial 00846 // default constructor. 00847 if (!FieldRec->hasTrivialDefaultConstructor()) 00848 data().HasTrivialDefaultConstructor = false; 00849 00850 // C++0x [class.copy]p13: 00851 // A copy/move constructor for class X is trivial if [...] 00852 // [...] 00853 // -- for each non-static data member of X that is of class type (or 00854 // an array thereof), the constructor selected to copy/move that 00855 // member is trivial; 00856 // FIXME: C++0x: We don't correctly model 'selected' constructors. 00857 if (!FieldRec->hasTrivialCopyConstructor()) 00858 data().HasTrivialCopyConstructor = false; 00859 if (!FieldRec->hasTrivialMoveConstructor()) 00860 data().HasTrivialMoveConstructor = false; 00861 00862 // C++0x [class.copy]p27: 00863 // A copy/move assignment operator for class X is trivial if [...] 00864 // [...] 00865 // -- for each non-static data member of X that is of class type (or 00866 // an array thereof), the assignment operator selected to 00867 // copy/move that member is trivial; 00868 // FIXME: C++0x: We don't correctly model 'selected' operators. 00869 if (!FieldRec->hasTrivialCopyAssignment()) 00870 data().HasTrivialCopyAssignment = false; 00871 if (!FieldRec->hasTrivialMoveAssignment()) 00872 data().HasTrivialMoveAssignment = false; 00873 00874 if (!FieldRec->hasTrivialDestructor()) 00875 data().HasTrivialDestructor = false; 00876 if (!FieldRec->hasIrrelevantDestructor()) 00877 data().HasIrrelevantDestructor = false; 00878 if (FieldRec->hasObjectMember()) 00879 setHasObjectMember(true); 00880 00881 // C++0x [class]p7: 00882 // A standard-layout class is a class that: 00883 // -- has no non-static data members of type non-standard-layout 00884 // class (or array of such types) [...] 00885 if (!FieldRec->isStandardLayout()) 00886 data().IsStandardLayout = false; 00887 00888 // C++0x [class]p7: 00889 // A standard-layout class is a class that: 00890 // [...] 00891 // -- has no base classes of the same type as the first non-static 00892 // data member. 00893 // We don't want to expend bits in the state of the record decl 00894 // tracking whether this is the first non-static data member so we 00895 // cheat a bit and use some of the existing state: the empty bit. 00896 // Virtual bases and virtual methods make a class non-empty, but they 00897 // also make it non-standard-layout so we needn't check here. 00898 // A non-empty base class may leave the class standard-layout, but not 00899 // if we have arrived here, and have at least on non-static data 00900 // member. If IsStandardLayout remains true, then the first non-static 00901 // data member must come through here with Empty still true, and Empty 00902 // will subsequently be set to false below. 00903 if (data().IsStandardLayout && data().Empty) { 00904 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(), 00905 BE = bases_end(); 00906 BI != BE; ++BI) { 00907 if (Context.hasSameUnqualifiedType(BI->getType(), T)) { 00908 data().IsStandardLayout = false; 00909 break; 00910 } 00911 } 00912 } 00913 00914 // Keep track of the presence of mutable fields. 00915 if (FieldRec->hasMutableFields()) 00916 data().HasMutableFields = true; 00917 00918 // C++11 [class.copy]p13: 00919 // If the implicitly-defined constructor would satisfy the 00920 // requirements of a constexpr constructor, the implicitly-defined 00921 // constructor is constexpr. 00922 // C++11 [dcl.constexpr]p4: 00923 // -- every constructor involved in initializing non-static data 00924 // members [...] shall be a constexpr constructor 00925 if (!Field->hasInClassInitializer() && 00926 !FieldRec->hasConstexprDefaultConstructor() && !isUnion()) 00927 // The standard requires any in-class initializer to be a constant 00928 // expression. We consider this to be a defect. 00929 data().DefaultedDefaultConstructorIsConstexpr = false; 00930 00931 if (!FieldRec->hasConstexprCopyConstructor()) 00932 data().DefaultedCopyConstructorIsConstexpr = false; 00933 00934 if (FieldRec->hasDeclaredMoveConstructor() || 00935 FieldRec->needsImplicitMoveConstructor()) 00936 // FIXME: If the implicit move constructor generated for the member's 00937 // class would be ill-formed, the implicit move constructor generated 00938 // for this class calls the member's copy constructor. 00939 data().DefaultedMoveConstructorIsConstexpr &= 00940 FieldRec->hasConstexprMoveConstructor(); 00941 else if (!FieldRec->hasConstexprCopyConstructor()) 00942 data().DefaultedMoveConstructorIsConstexpr = false; 00943 } 00944 } else { 00945 // Base element type of field is a non-class type. 00946 if (!T->isLiteralType()) { 00947 data().DefaultedDefaultConstructorIsConstexpr = false; 00948 data().DefaultedCopyConstructorIsConstexpr = false; 00949 data().DefaultedMoveConstructorIsConstexpr = false; 00950 } else if (!Field->hasInClassInitializer() && !isUnion()) 00951 data().DefaultedDefaultConstructorIsConstexpr = false; 00952 } 00953 00954 // C++0x [class]p7: 00955 // A standard-layout class is a class that: 00956 // [...] 00957 // -- either has no non-static data members in the most derived 00958 // class and at most one base class with non-static data members, 00959 // or has no base classes with non-static data members, and 00960 // At this point we know that we have a non-static data member, so the last 00961 // clause holds. 00962 if (!data().HasNoNonEmptyBases) 00963 data().IsStandardLayout = false; 00964 00965 // If this is not a zero-length bit-field, then the class is not empty. 00966 if (data().Empty) { 00967 if (!Field->isBitField() || 00968 (!Field->getBitWidth()->isTypeDependent() && 00969 !Field->getBitWidth()->isValueDependent() && 00970 Field->getBitWidthValue(Context) != 0)) 00971 data().Empty = false; 00972 } 00973 } 00974 00975 // Handle using declarations of conversion functions. 00976 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) 00977 if (Shadow->getDeclName().getNameKind() 00978 == DeclarationName::CXXConversionFunctionName) 00979 data().Conversions.addDecl(Shadow, Shadow->getAccess()); 00980 } 00981 00982 bool CXXRecordDecl::isCLike() const { 00983 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull()) 00984 return false; 00985 if (!hasDefinition()) 00986 return true; 00987 00988 return isPOD() && data().HasOnlyCMembers; 00989 } 00990 00991 void CXXRecordDecl::getCaptureFields( 00992 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures, 00993 FieldDecl *&ThisCapture) const { 00994 Captures.clear(); 00995 ThisCapture = 0; 00996 00997 LambdaDefinitionData &Lambda = getLambdaData(); 00998 RecordDecl::field_iterator Field = field_begin(); 00999 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures; 01000 C != CEnd; ++C, ++Field) { 01001 if (C->capturesThis()) { 01002 ThisCapture = &*Field; 01003 continue; 01004 } 01005 01006 Captures[C->getCapturedVar()] = &*Field; 01007 } 01008 } 01009 01010 01011 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { 01012 QualType T; 01013 if (isa<UsingShadowDecl>(Conv)) 01014 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); 01015 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) 01016 T = ConvTemp->getTemplatedDecl()->getResultType(); 01017 else 01018 T = cast<CXXConversionDecl>(Conv)->getConversionType(); 01019 return Context.getCanonicalType(T); 01020 } 01021 01022 /// Collect the visible conversions of a base class. 01023 /// 01024 /// \param Base a base class of the class we're considering 01025 /// \param InVirtual whether this base class is a virtual base (or a base 01026 /// of a virtual base) 01027 /// \param Access the access along the inheritance path to this base 01028 /// \param ParentHiddenTypes the conversions provided by the inheritors 01029 /// of this base 01030 /// \param Output the set to which to add conversions from non-virtual bases 01031 /// \param VOutput the set to which to add conversions from virtual bases 01032 /// \param HiddenVBaseCs the set of conversions which were hidden in a 01033 /// virtual base along some inheritance path 01034 static void CollectVisibleConversions(ASTContext &Context, 01035 CXXRecordDecl *Record, 01036 bool InVirtual, 01037 AccessSpecifier Access, 01038 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, 01039 UnresolvedSetImpl &Output, 01040 UnresolvedSetImpl &VOutput, 01041 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { 01042 // The set of types which have conversions in this class or its 01043 // subclasses. As an optimization, we don't copy the derived set 01044 // unless it might change. 01045 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; 01046 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; 01047 01048 // Collect the direct conversions and figure out which conversions 01049 // will be hidden in the subclasses. 01050 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 01051 if (!Cs.empty()) { 01052 HiddenTypesBuffer = ParentHiddenTypes; 01053 HiddenTypes = &HiddenTypesBuffer; 01054 01055 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { 01056 CanQualType ConvType(GetConversionType(Context, I.getDecl())); 01057 bool Hidden = ParentHiddenTypes.count(ConvType); 01058 if (!Hidden) 01059 HiddenTypesBuffer.insert(ConvType); 01060 01061 // If this conversion is hidden and we're in a virtual base, 01062 // remember that it's hidden along some inheritance path. 01063 if (Hidden && InVirtual) 01064 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); 01065 01066 // If this conversion isn't hidden, add it to the appropriate output. 01067 else if (!Hidden) { 01068 AccessSpecifier IAccess 01069 = CXXRecordDecl::MergeAccess(Access, I.getAccess()); 01070 01071 if (InVirtual) 01072 VOutput.addDecl(I.getDecl(), IAccess); 01073 else 01074 Output.addDecl(I.getDecl(), IAccess); 01075 } 01076 } 01077 } 01078 01079 // Collect information recursively from any base classes. 01080 for (CXXRecordDecl::base_class_iterator 01081 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 01082 const RecordType *RT = I->getType()->getAs<RecordType>(); 01083 if (!RT) continue; 01084 01085 AccessSpecifier BaseAccess 01086 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); 01087 bool BaseInVirtual = InVirtual || I->isVirtual(); 01088 01089 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); 01090 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, 01091 *HiddenTypes, Output, VOutput, HiddenVBaseCs); 01092 } 01093 } 01094 01095 /// Collect the visible conversions of a class. 01096 /// 01097 /// This would be extremely straightforward if it weren't for virtual 01098 /// bases. It might be worth special-casing that, really. 01099 static void CollectVisibleConversions(ASTContext &Context, 01100 CXXRecordDecl *Record, 01101 UnresolvedSetImpl &Output) { 01102 // The collection of all conversions in virtual bases that we've 01103 // found. These will be added to the output as long as they don't 01104 // appear in the hidden-conversions set. 01105 UnresolvedSet<8> VBaseCs; 01106 01107 // The set of conversions in virtual bases that we've determined to 01108 // be hidden. 01109 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; 01110 01111 // The set of types hidden by classes derived from this one. 01112 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; 01113 01114 // Go ahead and collect the direct conversions and add them to the 01115 // hidden-types set. 01116 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 01117 Output.append(Cs.begin(), Cs.end()); 01118 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) 01119 HiddenTypes.insert(GetConversionType(Context, I.getDecl())); 01120 01121 // Recursively collect conversions from base classes. 01122 for (CXXRecordDecl::base_class_iterator 01123 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 01124 const RecordType *RT = I->getType()->getAs<RecordType>(); 01125 if (!RT) continue; 01126 01127 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), 01128 I->isVirtual(), I->getAccessSpecifier(), 01129 HiddenTypes, Output, VBaseCs, HiddenVBaseCs); 01130 } 01131 01132 // Add any unhidden conversions provided by virtual bases. 01133 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); 01134 I != E; ++I) { 01135 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) 01136 Output.addDecl(I.getDecl(), I.getAccess()); 01137 } 01138 } 01139 01140 /// getVisibleConversionFunctions - get all conversion functions visible 01141 /// in current class; including conversion function templates. 01142 const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { 01143 // If root class, all conversions are visible. 01144 if (bases_begin() == bases_end()) 01145 return &data().Conversions; 01146 // If visible conversion list is already evaluated, return it. 01147 if (data().ComputedVisibleConversions) 01148 return &data().VisibleConversions; 01149 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); 01150 data().ComputedVisibleConversions = true; 01151 return &data().VisibleConversions; 01152 } 01153 01154 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { 01155 // This operation is O(N) but extremely rare. Sema only uses it to 01156 // remove UsingShadowDecls in a class that were followed by a direct 01157 // declaration, e.g.: 01158 // class A : B { 01159 // using B::operator int; 01160 // operator int(); 01161 // }; 01162 // This is uncommon by itself and even more uncommon in conjunction 01163 // with sufficiently large numbers of directly-declared conversions 01164 // that asymptotic behavior matters. 01165 01166 UnresolvedSetImpl &Convs = *getConversionFunctions(); 01167 for (unsigned I = 0, E = Convs.size(); I != E; ++I) { 01168 if (Convs[I].getDecl() == ConvDecl) { 01169 Convs.erase(I); 01170 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() 01171 && "conversion was found multiple times in unresolved set"); 01172 return; 01173 } 01174 } 01175 01176 llvm_unreachable("conversion not found in set!"); 01177 } 01178 01179 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { 01180 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 01181 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); 01182 01183 return 0; 01184 } 01185 01186 MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { 01187 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); 01188 } 01189 01190 void 01191 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, 01192 TemplateSpecializationKind TSK) { 01193 assert(TemplateOrInstantiation.isNull() && 01194 "Previous template or instantiation?"); 01195 assert(!isa<ClassTemplateSpecializationDecl>(this)); 01196 TemplateOrInstantiation 01197 = new (getASTContext()) MemberSpecializationInfo(RD, TSK); 01198 } 01199 01200 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ 01201 if (const ClassTemplateSpecializationDecl *Spec 01202 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 01203 return Spec->getSpecializationKind(); 01204 01205 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 01206 return MSInfo->getTemplateSpecializationKind(); 01207 01208 return TSK_Undeclared; 01209 } 01210 01211 void 01212 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 01213 if (ClassTemplateSpecializationDecl *Spec 01214 = dyn_cast<ClassTemplateSpecializationDecl>(this)) { 01215 Spec->setSpecializationKind(TSK); 01216 return; 01217 } 01218 01219 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 01220 MSInfo->setTemplateSpecializationKind(TSK); 01221 return; 01222 } 01223 01224 llvm_unreachable("Not a class template or member class specialization"); 01225 } 01226 01227 CXXDestructorDecl *CXXRecordDecl::getDestructor() const { 01228 ASTContext &Context = getASTContext(); 01229 QualType ClassType = Context.getTypeDeclType(this); 01230 01231 DeclarationName Name 01232 = Context.DeclarationNames.getCXXDestructorName( 01233 Context.getCanonicalType(ClassType)); 01234 01235 DeclContext::lookup_const_iterator I, E; 01236 llvm::tie(I, E) = lookup(Name); 01237 if (I == E) 01238 return 0; 01239 01240 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); 01241 return Dtor; 01242 } 01243 01244 void CXXRecordDecl::completeDefinition() { 01245 completeDefinition(0); 01246 } 01247 01248 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { 01249 RecordDecl::completeDefinition(); 01250 01251 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) { 01252 // Objective-C Automatic Reference Counting: 01253 // If a class has a non-static data member of Objective-C pointer 01254 // type (or array thereof), it is a non-POD type and its 01255 // default constructor (if any), copy constructor, copy assignment 01256 // operator, and destructor are non-trivial. 01257 struct DefinitionData &Data = data(); 01258 Data.PlainOldData = false; 01259 Data.HasTrivialDefaultConstructor = false; 01260 Data.HasTrivialCopyConstructor = false; 01261 Data.HasTrivialCopyAssignment = false; 01262 Data.HasTrivialDestructor = false; 01263 Data.HasIrrelevantDestructor = false; 01264 } 01265 01266 // If the class may be abstract (but hasn't been marked as such), check for 01267 // any pure final overriders. 01268 if (mayBeAbstract()) { 01269 CXXFinalOverriderMap MyFinalOverriders; 01270 if (!FinalOverriders) { 01271 getFinalOverriders(MyFinalOverriders); 01272 FinalOverriders = &MyFinalOverriders; 01273 } 01274 01275 bool Done = false; 01276 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), 01277 MEnd = FinalOverriders->end(); 01278 M != MEnd && !Done; ++M) { 01279 for (OverridingMethods::iterator SO = M->second.begin(), 01280 SOEnd = M->second.end(); 01281 SO != SOEnd && !Done; ++SO) { 01282 assert(SO->second.size() > 0 && 01283 "All virtual functions have overridding virtual functions"); 01284 01285 // C++ [class.abstract]p4: 01286 // A class is abstract if it contains or inherits at least one 01287 // pure virtual function for which the final overrider is pure 01288 // virtual. 01289 if (SO->second.front().Method->isPure()) { 01290 data().Abstract = true; 01291 Done = true; 01292 break; 01293 } 01294 } 01295 } 01296 } 01297 01298 // Set access bits correctly on the directly-declared conversions. 01299 for (UnresolvedSetIterator I = data().Conversions.begin(), 01300 E = data().Conversions.end(); 01301 I != E; ++I) 01302 data().Conversions.setAccess(I, (*I)->getAccess()); 01303 } 01304 01305 bool CXXRecordDecl::mayBeAbstract() const { 01306 if (data().Abstract || isInvalidDecl() || !data().Polymorphic || 01307 isDependentContext()) 01308 return false; 01309 01310 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), 01311 BEnd = bases_end(); 01312 B != BEnd; ++B) { 01313 CXXRecordDecl *BaseDecl 01314 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); 01315 if (BaseDecl->isAbstract()) 01316 return true; 01317 } 01318 01319 return false; 01320 } 01321 01322 void CXXMethodDecl::anchor() { } 01323 01324 CXXMethodDecl * 01325 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 01326 SourceLocation StartLoc, 01327 const DeclarationNameInfo &NameInfo, 01328 QualType T, TypeSourceInfo *TInfo, 01329 bool isStatic, StorageClass SCAsWritten, bool isInline, 01330 bool isConstexpr, SourceLocation EndLocation) { 01331 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, 01332 isStatic, SCAsWritten, isInline, isConstexpr, 01333 EndLocation); 01334 } 01335 01336 CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01337 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl)); 01338 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(), 01339 DeclarationNameInfo(), QualType(), 01340 0, false, SC_None, false, false, 01341 SourceLocation()); 01342 } 01343 01344 bool CXXMethodDecl::isUsualDeallocationFunction() const { 01345 if (getOverloadedOperator() != OO_Delete && 01346 getOverloadedOperator() != OO_Array_Delete) 01347 return false; 01348 01349 // C++ [basic.stc.dynamic.deallocation]p2: 01350 // A template instance is never a usual deallocation function, 01351 // regardless of its signature. 01352 if (getPrimaryTemplate()) 01353 return false; 01354 01355 // C++ [basic.stc.dynamic.deallocation]p2: 01356 // If a class T has a member deallocation function named operator delete 01357 // with exactly one parameter, then that function is a usual (non-placement) 01358 // deallocation function. [...] 01359 if (getNumParams() == 1) 01360 return true; 01361 01362 // C++ [basic.stc.dynamic.deallocation]p2: 01363 // [...] If class T does not declare such an operator delete but does 01364 // declare a member deallocation function named operator delete with 01365 // exactly two parameters, the second of which has type std::size_t (18.1), 01366 // then this function is a usual deallocation function. 01367 ASTContext &Context = getASTContext(); 01368 if (getNumParams() != 2 || 01369 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), 01370 Context.getSizeType())) 01371 return false; 01372 01373 // This function is a usual deallocation function if there are no 01374 // single-parameter deallocation functions of the same kind. 01375 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); 01376 R.first != R.second; ++R.first) { 01377 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) 01378 if (FD->getNumParams() == 1) 01379 return false; 01380 } 01381 01382 return true; 01383 } 01384 01385 bool CXXMethodDecl::isCopyAssignmentOperator() const { 01386 // C++0x [class.copy]p17: 01387 // A user-declared copy assignment operator X::operator= is a non-static 01388 // non-template member function of class X with exactly one parameter of 01389 // type X, X&, const X&, volatile X& or const volatile X&. 01390 if (/*operator=*/getOverloadedOperator() != OO_Equal || 01391 /*non-static*/ isStatic() || 01392 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate()) 01393 return false; 01394 01395 QualType ParamType = getParamDecl(0)->getType(); 01396 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) 01397 ParamType = Ref->getPointeeType(); 01398 01399 ASTContext &Context = getASTContext(); 01400 QualType ClassType 01401 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 01402 return Context.hasSameUnqualifiedType(ClassType, ParamType); 01403 } 01404 01405 bool CXXMethodDecl::isMoveAssignmentOperator() const { 01406 // C++0x [class.copy]p19: 01407 // A user-declared move assignment operator X::operator= is a non-static 01408 // non-template member function of class X with exactly one parameter of type 01409 // X&&, const X&&, volatile X&&, or const volatile X&&. 01410 if (getOverloadedOperator() != OO_Equal || isStatic() || 01411 getPrimaryTemplate() || getDescribedFunctionTemplate()) 01412 return false; 01413 01414 QualType ParamType = getParamDecl(0)->getType(); 01415 if (!isa<RValueReferenceType>(ParamType)) 01416 return false; 01417 ParamType = ParamType->getPointeeType(); 01418 01419 ASTContext &Context = getASTContext(); 01420 QualType ClassType 01421 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 01422 return Context.hasSameUnqualifiedType(ClassType, ParamType); 01423 } 01424 01425 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 01426 assert(MD->isCanonicalDecl() && "Method is not canonical!"); 01427 assert(!MD->getParent()->isDependentContext() && 01428 "Can't add an overridden method to a class template!"); 01429 assert(MD->isVirtual() && "Method is not virtual!"); 01430 01431 getASTContext().addOverriddenMethod(this, MD); 01432 } 01433 01434 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 01435 if (isa<CXXConstructorDecl>(this)) return 0; 01436 return getASTContext().overridden_methods_begin(this); 01437 } 01438 01439 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 01440 if (isa<CXXConstructorDecl>(this)) return 0; 01441 return getASTContext().overridden_methods_end(this); 01442 } 01443 01444 unsigned CXXMethodDecl::size_overridden_methods() const { 01445 if (isa<CXXConstructorDecl>(this)) return 0; 01446 return getASTContext().overridden_methods_size(this); 01447 } 01448 01449 QualType CXXMethodDecl::getThisType(ASTContext &C) const { 01450 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 01451 // If the member function is declared const, the type of this is const X*, 01452 // if the member function is declared volatile, the type of this is 01453 // volatile X*, and if the member function is declared const volatile, 01454 // the type of this is const volatile X*. 01455 01456 assert(isInstance() && "No 'this' for static methods!"); 01457 01458 QualType ClassTy = C.getTypeDeclType(getParent()); 01459 ClassTy = C.getQualifiedType(ClassTy, 01460 Qualifiers::fromCVRMask(getTypeQualifiers())); 01461 return C.getPointerType(ClassTy); 01462 } 01463 01464 bool CXXMethodDecl::hasInlineBody() const { 01465 // If this function is a template instantiation, look at the template from 01466 // which it was instantiated. 01467 const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); 01468 if (!CheckFn) 01469 CheckFn = this; 01470 01471 const FunctionDecl *fn; 01472 return CheckFn->hasBody(fn) && !fn->isOutOfLine(); 01473 } 01474 01475 bool CXXMethodDecl::isLambdaStaticInvoker() const { 01476 return getParent()->isLambda() && 01477 getIdentifier() && getIdentifier()->getName() == "__invoke"; 01478 } 01479 01480 01481 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 01482 TypeSourceInfo *TInfo, bool IsVirtual, 01483 SourceLocation L, Expr *Init, 01484 SourceLocation R, 01485 SourceLocation EllipsisLoc) 01486 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), 01487 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual), 01488 IsWritten(false), SourceOrderOrNumArrayIndices(0) 01489 { 01490 } 01491 01492 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 01493 FieldDecl *Member, 01494 SourceLocation MemberLoc, 01495 SourceLocation L, Expr *Init, 01496 SourceLocation R) 01497 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 01498 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), 01499 IsWritten(false), SourceOrderOrNumArrayIndices(0) 01500 { 01501 } 01502 01503 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 01504 IndirectFieldDecl *Member, 01505 SourceLocation MemberLoc, 01506 SourceLocation L, Expr *Init, 01507 SourceLocation R) 01508 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 01509 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), 01510 IsWritten(false), SourceOrderOrNumArrayIndices(0) 01511 { 01512 } 01513 01514 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 01515 TypeSourceInfo *TInfo, 01516 SourceLocation L, Expr *Init, 01517 SourceLocation R) 01518 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init), 01519 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false), 01520 IsWritten(false), SourceOrderOrNumArrayIndices(0) 01521 { 01522 } 01523 01524 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 01525 FieldDecl *Member, 01526 SourceLocation MemberLoc, 01527 SourceLocation L, Expr *Init, 01528 SourceLocation R, 01529 VarDecl **Indices, 01530 unsigned NumIndices) 01531 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 01532 LParenLoc(L), RParenLoc(R), IsVirtual(false), 01533 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) 01534 { 01535 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); 01536 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); 01537 } 01538 01539 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, 01540 FieldDecl *Member, 01541 SourceLocation MemberLoc, 01542 SourceLocation L, Expr *Init, 01543 SourceLocation R, 01544 VarDecl **Indices, 01545 unsigned NumIndices) { 01546 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + 01547 sizeof(VarDecl *) * NumIndices, 01548 llvm::alignOf<CXXCtorInitializer>()); 01549 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, 01550 Indices, NumIndices); 01551 } 01552 01553 TypeLoc CXXCtorInitializer::getBaseClassLoc() const { 01554 if (isBaseInitializer()) 01555 return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); 01556 else 01557 return TypeLoc(); 01558 } 01559 01560 const Type *CXXCtorInitializer::getBaseClass() const { 01561 if (isBaseInitializer()) 01562 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); 01563 else 01564 return 0; 01565 } 01566 01567 SourceLocation CXXCtorInitializer::getSourceLocation() const { 01568 if (isAnyMemberInitializer()) 01569 return getMemberLocation(); 01570 01571 if (isInClassMemberInitializer()) 01572 return getAnyMember()->getLocation(); 01573 01574 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>()) 01575 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin(); 01576 01577 return SourceLocation(); 01578 } 01579 01580 SourceRange CXXCtorInitializer::getSourceRange() const { 01581 if (isInClassMemberInitializer()) { 01582 FieldDecl *D = getAnyMember(); 01583 if (Expr *I = D->getInClassInitializer()) 01584 return I->getSourceRange(); 01585 return SourceRange(); 01586 } 01587 01588 return SourceRange(getSourceLocation(), getRParenLoc()); 01589 } 01590 01591 void CXXConstructorDecl::anchor() { } 01592 01593 CXXConstructorDecl * 01594 CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01595 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl)); 01596 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(), 01597 QualType(), 0, false, false, false,false); 01598 } 01599 01600 CXXConstructorDecl * 01601 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 01602 SourceLocation StartLoc, 01603 const DeclarationNameInfo &NameInfo, 01604 QualType T, TypeSourceInfo *TInfo, 01605 bool isExplicit, bool isInline, 01606 bool isImplicitlyDeclared, bool isConstexpr) { 01607 assert(NameInfo.getName().getNameKind() 01608 == DeclarationName::CXXConstructorName && 01609 "Name must refer to a constructor"); 01610 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, 01611 isExplicit, isInline, isImplicitlyDeclared, 01612 isConstexpr); 01613 } 01614 01615 CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const { 01616 assert(isDelegatingConstructor() && "Not a delegating constructor!"); 01617 Expr *E = (*init_begin())->getInit()->IgnoreImplicit(); 01618 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E)) 01619 return Construct->getConstructor(); 01620 01621 return 0; 01622 } 01623 01624 bool CXXConstructorDecl::isDefaultConstructor() const { 01625 // C++ [class.ctor]p5: 01626 // A default constructor for a class X is a constructor of class 01627 // X that can be called without an argument. 01628 return (getNumParams() == 0) || 01629 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); 01630 } 01631 01632 bool 01633 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { 01634 return isCopyOrMoveConstructor(TypeQuals) && 01635 getParamDecl(0)->getType()->isLValueReferenceType(); 01636 } 01637 01638 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { 01639 return isCopyOrMoveConstructor(TypeQuals) && 01640 getParamDecl(0)->getType()->isRValueReferenceType(); 01641 } 01642 01643 /// \brief Determine whether this is a copy or move constructor. 01644 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { 01645 // C++ [class.copy]p2: 01646 // A non-template constructor for class X is a copy constructor 01647 // if its first parameter is of type X&, const X&, volatile X& or 01648 // const volatile X&, and either there are no other parameters 01649 // or else all other parameters have default arguments (8.3.6). 01650 // C++0x [class.copy]p3: 01651 // A non-template constructor for class X is a move constructor if its 01652 // first parameter is of type X&&, const X&&, volatile X&&, or 01653 // const volatile X&&, and either there are no other parameters or else 01654 // all other parameters have default arguments. 01655 if ((getNumParams() < 1) || 01656 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 01657 (getPrimaryTemplate() != 0) || 01658 (getDescribedFunctionTemplate() != 0)) 01659 return false; 01660 01661 const ParmVarDecl *Param = getParamDecl(0); 01662 01663 // Do we have a reference type? 01664 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); 01665 if (!ParamRefType) 01666 return false; 01667 01668 // Is it a reference to our class type? 01669 ASTContext &Context = getASTContext(); 01670 01671 CanQualType PointeeType 01672 = Context.getCanonicalType(ParamRefType->getPointeeType()); 01673 CanQualType ClassTy 01674 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 01675 if (PointeeType.getUnqualifiedType() != ClassTy) 01676 return false; 01677 01678 // FIXME: other qualifiers? 01679 01680 // We have a copy or move constructor. 01681 TypeQuals = PointeeType.getCVRQualifiers(); 01682 return true; 01683 } 01684 01685 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { 01686 // C++ [class.conv.ctor]p1: 01687 // A constructor declared without the function-specifier explicit 01688 // that can be called with a single parameter specifies a 01689 // conversion from the type of its first parameter to the type of 01690 // its class. Such a constructor is called a converting 01691 // constructor. 01692 if (isExplicit() && !AllowExplicit) 01693 return false; 01694 01695 return (getNumParams() == 0 && 01696 getType()->getAs<FunctionProtoType>()->isVariadic()) || 01697 (getNumParams() == 1) || 01698 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); 01699 } 01700 01701 bool CXXConstructorDecl::isSpecializationCopyingObject() const { 01702 if ((getNumParams() < 1) || 01703 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 01704 (getPrimaryTemplate() == 0) || 01705 (getDescribedFunctionTemplate() != 0)) 01706 return false; 01707 01708 const ParmVarDecl *Param = getParamDecl(0); 01709 01710 ASTContext &Context = getASTContext(); 01711 CanQualType ParamType = Context.getCanonicalType(Param->getType()); 01712 01713 // Is it the same as our our class type? 01714 CanQualType ClassTy 01715 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 01716 if (ParamType.getUnqualifiedType() != ClassTy) 01717 return false; 01718 01719 return true; 01720 } 01721 01722 const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { 01723 // Hack: we store the inherited constructor in the overridden method table 01724 method_iterator It = getASTContext().overridden_methods_begin(this); 01725 if (It == getASTContext().overridden_methods_end(this)) 01726 return 0; 01727 01728 return cast<CXXConstructorDecl>(*It); 01729 } 01730 01731 void 01732 CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ 01733 // Hack: we store the inherited constructor in the overridden method table 01734 assert(getASTContext().overridden_methods_size(this) == 0 && 01735 "Base ctor already set."); 01736 getASTContext().addOverriddenMethod(this, BaseCtor); 01737 } 01738 01739 void CXXDestructorDecl::anchor() { } 01740 01741 CXXDestructorDecl * 01742 CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01743 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl)); 01744 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), 01745 QualType(), 0, false, false); 01746 } 01747 01748 CXXDestructorDecl * 01749 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 01750 SourceLocation StartLoc, 01751 const DeclarationNameInfo &NameInfo, 01752 QualType T, TypeSourceInfo *TInfo, 01753 bool isInline, bool isImplicitlyDeclared) { 01754 assert(NameInfo.getName().getNameKind() 01755 == DeclarationName::CXXDestructorName && 01756 "Name must refer to a destructor"); 01757 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, 01758 isImplicitlyDeclared); 01759 } 01760 01761 void CXXConversionDecl::anchor() { } 01762 01763 CXXConversionDecl * 01764 CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01765 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl)); 01766 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), 01767 QualType(), 0, false, false, false, 01768 SourceLocation()); 01769 } 01770 01771 CXXConversionDecl * 01772 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 01773 SourceLocation StartLoc, 01774 const DeclarationNameInfo &NameInfo, 01775 QualType T, TypeSourceInfo *TInfo, 01776 bool isInline, bool isExplicit, 01777 bool isConstexpr, SourceLocation EndLocation) { 01778 assert(NameInfo.getName().getNameKind() 01779 == DeclarationName::CXXConversionFunctionName && 01780 "Name must refer to a conversion function"); 01781 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, 01782 isInline, isExplicit, isConstexpr, 01783 EndLocation); 01784 } 01785 01786 bool CXXConversionDecl::isLambdaToBlockPointerConversion() const { 01787 return isImplicit() && getParent()->isLambda() && 01788 getConversionType()->isBlockPointerType(); 01789 } 01790 01791 void LinkageSpecDecl::anchor() { } 01792 01793 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 01794 DeclContext *DC, 01795 SourceLocation ExternLoc, 01796 SourceLocation LangLoc, 01797 LanguageIDs Lang, 01798 SourceLocation RBraceLoc) { 01799 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc); 01800 } 01801 01802 LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01803 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl)); 01804 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(), 01805 lang_c, SourceLocation()); 01806 } 01807 01808 void UsingDirectiveDecl::anchor() { } 01809 01810 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 01811 SourceLocation L, 01812 SourceLocation NamespaceLoc, 01813 NestedNameSpecifierLoc QualifierLoc, 01814 SourceLocation IdentLoc, 01815 NamedDecl *Used, 01816 DeclContext *CommonAncestor) { 01817 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) 01818 Used = NS->getOriginalNamespace(); 01819 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, 01820 IdentLoc, Used, CommonAncestor); 01821 } 01822 01823 UsingDirectiveDecl * 01824 UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01825 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl)); 01826 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(), 01827 NestedNameSpecifierLoc(), 01828 SourceLocation(), 0, 0); 01829 } 01830 01831 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { 01832 if (NamespaceAliasDecl *NA = 01833 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) 01834 return NA->getNamespace(); 01835 return cast_or_null<NamespaceDecl>(NominatedNamespace); 01836 } 01837 01838 void NamespaceDecl::anchor() { } 01839 01840 NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline, 01841 SourceLocation StartLoc, 01842 SourceLocation IdLoc, IdentifierInfo *Id, 01843 NamespaceDecl *PrevDecl) 01844 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace), 01845 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline) 01846 { 01847 setPreviousDeclaration(PrevDecl); 01848 01849 if (PrevDecl) 01850 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace()); 01851 } 01852 01853 NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, 01854 bool Inline, SourceLocation StartLoc, 01855 SourceLocation IdLoc, IdentifierInfo *Id, 01856 NamespaceDecl *PrevDecl) { 01857 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl); 01858 } 01859 01860 NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01861 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl)); 01862 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(), 01863 0, 0); 01864 } 01865 01866 void NamespaceAliasDecl::anchor() { } 01867 01868 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 01869 SourceLocation UsingLoc, 01870 SourceLocation AliasLoc, 01871 IdentifierInfo *Alias, 01872 NestedNameSpecifierLoc QualifierLoc, 01873 SourceLocation IdentLoc, 01874 NamedDecl *Namespace) { 01875 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) 01876 Namespace = NS->getOriginalNamespace(); 01877 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, 01878 QualifierLoc, IdentLoc, Namespace); 01879 } 01880 01881 NamespaceAliasDecl * 01882 NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl)); 01884 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0, 01885 NestedNameSpecifierLoc(), 01886 SourceLocation(), 0); 01887 } 01888 01889 void UsingShadowDecl::anchor() { } 01890 01891 UsingShadowDecl * 01892 UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01893 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl)); 01894 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0); 01895 } 01896 01897 UsingDecl *UsingShadowDecl::getUsingDecl() const { 01898 const UsingShadowDecl *Shadow = this; 01899 while (const UsingShadowDecl *NextShadow = 01900 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) 01901 Shadow = NextShadow; 01902 return cast<UsingDecl>(Shadow->UsingOrNextShadow); 01903 } 01904 01905 void UsingDecl::anchor() { } 01906 01907 void UsingDecl::addShadowDecl(UsingShadowDecl *S) { 01908 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && 01909 "declaration already in set"); 01910 assert(S->getUsingDecl() == this); 01911 01912 if (FirstUsingShadow.getPointer()) 01913 S->UsingOrNextShadow = FirstUsingShadow.getPointer(); 01914 FirstUsingShadow.setPointer(S); 01915 } 01916 01917 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { 01918 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && 01919 "declaration not in set"); 01920 assert(S->getUsingDecl() == this); 01921 01922 // Remove S from the shadow decl chain. This is O(n) but hopefully rare. 01923 01924 if (FirstUsingShadow.getPointer() == S) { 01925 FirstUsingShadow.setPointer( 01926 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow)); 01927 S->UsingOrNextShadow = this; 01928 return; 01929 } 01930 01931 UsingShadowDecl *Prev = FirstUsingShadow.getPointer(); 01932 while (Prev->UsingOrNextShadow != S) 01933 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); 01934 Prev->UsingOrNextShadow = S->UsingOrNextShadow; 01935 S->UsingOrNextShadow = this; 01936 } 01937 01938 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, 01939 NestedNameSpecifierLoc QualifierLoc, 01940 const DeclarationNameInfo &NameInfo, 01941 bool IsTypeNameArg) { 01942 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg); 01943 } 01944 01945 UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl)); 01947 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(), 01948 DeclarationNameInfo(), false); 01949 } 01950 01951 void UnresolvedUsingValueDecl::anchor() { } 01952 01953 UnresolvedUsingValueDecl * 01954 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, 01955 SourceLocation UsingLoc, 01956 NestedNameSpecifierLoc QualifierLoc, 01957 const DeclarationNameInfo &NameInfo) { 01958 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, 01959 QualifierLoc, NameInfo); 01960 } 01961 01962 UnresolvedUsingValueDecl * 01963 UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01964 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl)); 01965 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(), 01966 NestedNameSpecifierLoc(), 01967 DeclarationNameInfo()); 01968 } 01969 01970 void UnresolvedUsingTypenameDecl::anchor() { } 01971 01972 UnresolvedUsingTypenameDecl * 01973 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, 01974 SourceLocation UsingLoc, 01975 SourceLocation TypenameLoc, 01976 NestedNameSpecifierLoc QualifierLoc, 01977 SourceLocation TargetNameLoc, 01978 DeclarationName TargetName) { 01979 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, 01980 QualifierLoc, TargetNameLoc, 01981 TargetName.getAsIdentifierInfo()); 01982 } 01983 01984 UnresolvedUsingTypenameDecl * 01985 UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 01986 void *Mem = AllocateDeserializedDecl(C, ID, 01987 sizeof(UnresolvedUsingTypenameDecl)); 01988 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(), 01989 SourceLocation(), 01990 NestedNameSpecifierLoc(), 01991 SourceLocation(), 01992 0); 01993 } 01994 01995 void StaticAssertDecl::anchor() { } 01996 01997 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 01998 SourceLocation StaticAssertLoc, 01999 Expr *AssertExpr, 02000 StringLiteral *Message, 02001 SourceLocation RParenLoc) { 02002 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, 02003 RParenLoc); 02004 } 02005 02006 StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C, 02007 unsigned ID) { 02008 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl)); 02009 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation()); 02010 } 02011 02012 static const char *getAccessName(AccessSpecifier AS) { 02013 switch (AS) { 02014 case AS_none: 02015 llvm_unreachable("Invalid access specifier!"); 02016 case AS_public: 02017 return "public"; 02018 case AS_private: 02019 return "private"; 02020 case AS_protected: 02021 return "protected"; 02022 } 02023 llvm_unreachable("Invalid access specifier!"); 02024 } 02025 02026 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 02027 AccessSpecifier AS) { 02028 return DB << getAccessName(AS); 02029 } 02030 02031 const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB, 02032 AccessSpecifier AS) { 02033 return DB << getAccessName(AS); 02034 }