DeclCXX.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
  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. //
  10. // This file implements the C++ related Decl classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/DeclCXX.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ASTLambda.h"
  16. #include "clang/AST/ASTMutationListener.h"
  17. #include "clang/AST/CXXInheritance.h"
  18. #include "clang/AST/DeclTemplate.h"
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/ExprCXX.h"
  21. #include "clang/AST/TypeLoc.h"
  22. #include "clang/Basic/IdentifierTable.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. using namespace clang;
  26. // //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //===----------------------------------------------------------------------===//
  29. // Decl Allocation/Deallocation Method Implementations
  30. //===----------------------------------------------------------------------===//
  31. void AccessSpecDecl::anchor() { }
  32. AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  33. return new (C, ID) AccessSpecDecl(EmptyShell());
  34. }
  35. void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
  36. ExternalASTSource *Source = C.getExternalSource();
  37. assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
  38. assert(Source && "getFromExternalSource with no external source");
  39. for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
  40. I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
  41. reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
  42. Impl.Decls.setLazy(false);
  43. }
  44. CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
  45. : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
  46. Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
  47. Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
  48. HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
  49. HasMutableFields(false), HasVariantMembers(false), HasOnlyCMembers(true),
  50. HasInClassInitializer(false), HasUninitializedReferenceMember(false),
  51. NeedOverloadResolutionForMoveConstructor(false),
  52. NeedOverloadResolutionForMoveAssignment(false),
  53. NeedOverloadResolutionForDestructor(false),
  54. DefaultedMoveConstructorIsDeleted(false),
  55. DefaultedMoveAssignmentIsDeleted(false),
  56. DefaultedDestructorIsDeleted(false),
  57. HasTrivialSpecialMembers(SMF_All),
  58. DeclaredNonTrivialSpecialMembers(0),
  59. HasIrrelevantDestructor(true),
  60. HasConstexprNonCopyMoveConstructor(false),
  61. DefaultedDefaultConstructorIsConstexpr(true),
  62. HasConstexprDefaultConstructor(false),
  63. HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
  64. UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
  65. ImplicitCopyConstructorHasConstParam(true),
  66. ImplicitCopyAssignmentHasConstParam(true),
  67. HasDeclaredCopyConstructorWithConstParam(false),
  68. HasDeclaredCopyAssignmentWithConstParam(false),
  69. IsLambda(false), IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0),
  70. Bases(), VBases(),
  71. Definition(D), FirstFriend() {
  72. }
  73. CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
  74. return Bases.get(Definition->getASTContext().getExternalSource());
  75. }
  76. CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
  77. return VBases.get(Definition->getASTContext().getExternalSource());
  78. }
  79. CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
  80. DeclContext *DC, SourceLocation StartLoc,
  81. SourceLocation IdLoc, IdentifierInfo *Id,
  82. CXXRecordDecl *PrevDecl)
  83. : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
  84. DefinitionData(PrevDecl ? PrevDecl->DefinitionData
  85. : DefinitionDataPtr(this)),
  86. TemplateOrInstantiation() {}
  87. CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
  88. DeclContext *DC, SourceLocation StartLoc,
  89. SourceLocation IdLoc, IdentifierInfo *Id,
  90. CXXRecordDecl* PrevDecl,
  91. bool DelayTypeCreation) {
  92. CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
  93. IdLoc, Id, PrevDecl);
  94. R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
  95. // FIXME: DelayTypeCreation seems like such a hack
  96. if (!DelayTypeCreation)
  97. C.getTypeDeclType(R, PrevDecl);
  98. return R;
  99. }
  100. CXXRecordDecl *
  101. CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
  102. TypeSourceInfo *Info, SourceLocation Loc,
  103. bool Dependent, bool IsGeneric,
  104. LambdaCaptureDefault CaptureDefault) {
  105. CXXRecordDecl *R =
  106. new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
  107. nullptr, nullptr);
  108. R->IsBeingDefined = true;
  109. R->DefinitionData =
  110. new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
  111. CaptureDefault);
  112. R->MayHaveOutOfDateDef = false;
  113. R->setImplicit(true);
  114. C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
  115. return R;
  116. }
  117. CXXRecordDecl *
  118. CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
  119. CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
  120. CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
  121. nullptr, nullptr);
  122. R->MayHaveOutOfDateDef = false;
  123. return R;
  124. }
  125. void
  126. CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
  127. unsigned NumBases) {
  128. ASTContext &C = getASTContext();
  129. if (!data().Bases.isOffset() && data().NumBases > 0)
  130. C.Deallocate(data().getBases());
  131. if (NumBases) {
  132. // C++ [dcl.init.aggr]p1:
  133. // An aggregate is [...] a class with [...] no base classes [...].
  134. data().Aggregate = false;
  135. // C++ [class]p4:
  136. // A POD-struct is an aggregate class...
  137. data().PlainOldData = false;
  138. }
  139. // The set of seen virtual base types.
  140. llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
  141. // The virtual bases of this class.
  142. SmallVector<const CXXBaseSpecifier *, 8> VBases;
  143. data().Bases = new(C) CXXBaseSpecifier [NumBases];
  144. data().NumBases = NumBases;
  145. for (unsigned i = 0; i < NumBases; ++i) {
  146. data().getBases()[i] = *Bases[i];
  147. // Keep track of inherited vbases for this base class.
  148. const CXXBaseSpecifier *Base = Bases[i];
  149. QualType BaseType = Base->getType();
  150. // Skip dependent types; we can't do any checking on them now.
  151. if (BaseType->isDependentType())
  152. continue;
  153. CXXRecordDecl *BaseClassDecl
  154. = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
  155. // A class with a non-empty base class is not empty.
  156. // FIXME: Standard ref?
  157. if (!BaseClassDecl->isEmpty()) {
  158. if (!data().Empty) {
  159. // C++0x [class]p7:
  160. // A standard-layout class is a class that:
  161. // [...]
  162. // -- either has no non-static data members in the most derived
  163. // class and at most one base class with non-static data members,
  164. // or has no base classes with non-static data members, and
  165. // If this is the second non-empty base, then neither of these two
  166. // clauses can be true.
  167. data().IsStandardLayout = false;
  168. }
  169. data().Empty = false;
  170. data().HasNoNonEmptyBases = false;
  171. }
  172. // C++ [class.virtual]p1:
  173. // A class that declares or inherits a virtual function is called a
  174. // polymorphic class.
  175. if (BaseClassDecl->isPolymorphic())
  176. data().Polymorphic = true;
  177. // C++0x [class]p7:
  178. // A standard-layout class is a class that: [...]
  179. // -- has no non-standard-layout base classes
  180. if (!BaseClassDecl->isStandardLayout())
  181. data().IsStandardLayout = false;
  182. // Record if this base is the first non-literal field or base.
  183. if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
  184. data().HasNonLiteralTypeFieldsOrBases = true;
  185. // Now go through all virtual bases of this base and add them.
  186. for (const auto &VBase : BaseClassDecl->vbases()) {
  187. // Add this base if it's not already in the list.
  188. if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
  189. VBases.push_back(&VBase);
  190. // C++11 [class.copy]p8:
  191. // The implicitly-declared copy constructor for a class X will have
  192. // the form 'X::X(const X&)' if each [...] virtual base class B of X
  193. // has a copy constructor whose first parameter is of type
  194. // 'const B&' or 'const volatile B&' [...]
  195. if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
  196. if (!VBaseDecl->hasCopyConstructorWithConstParam())
  197. data().ImplicitCopyConstructorHasConstParam = false;
  198. }
  199. }
  200. if (Base->isVirtual()) {
  201. // Add this base if it's not already in the list.
  202. if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
  203. VBases.push_back(Base);
  204. // C++0x [meta.unary.prop] is_empty:
  205. // T is a class type, but not a union type, with ... no virtual base
  206. // classes
  207. data().Empty = false;
  208. // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
  209. // A [default constructor, copy/move constructor, or copy/move assignment
  210. // operator for a class X] is trivial [...] if:
  211. // -- class X has [...] no virtual base classes
  212. data().HasTrivialSpecialMembers &= SMF_Destructor;
  213. // C++0x [class]p7:
  214. // A standard-layout class is a class that: [...]
  215. // -- has [...] no virtual base classes
  216. data().IsStandardLayout = false;
  217. // C++11 [dcl.constexpr]p4:
  218. // In the definition of a constexpr constructor [...]
  219. // -- the class shall not have any virtual base classes
  220. data().DefaultedDefaultConstructorIsConstexpr = false;
  221. } else {
  222. // C++ [class.ctor]p5:
  223. // A default constructor is trivial [...] if:
  224. // -- all the direct base classes of its class have trivial default
  225. // constructors.
  226. if (!BaseClassDecl->hasTrivialDefaultConstructor())
  227. data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
  228. // C++0x [class.copy]p13:
  229. // A copy/move constructor for class X is trivial if [...]
  230. // [...]
  231. // -- the constructor selected to copy/move each direct base class
  232. // subobject is trivial, and
  233. if (!BaseClassDecl->hasTrivialCopyConstructor())
  234. data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
  235. // If the base class doesn't have a simple move constructor, we'll eagerly
  236. // declare it and perform overload resolution to determine which function
  237. // it actually calls. If it does have a simple move constructor, this
  238. // check is correct.
  239. if (!BaseClassDecl->hasTrivialMoveConstructor())
  240. data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
  241. // C++0x [class.copy]p27:
  242. // A copy/move assignment operator for class X is trivial if [...]
  243. // [...]
  244. // -- the assignment operator selected to copy/move each direct base
  245. // class subobject is trivial, and
  246. if (!BaseClassDecl->hasTrivialCopyAssignment())
  247. data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
  248. // If the base class doesn't have a simple move assignment, we'll eagerly
  249. // declare it and perform overload resolution to determine which function
  250. // it actually calls. If it does have a simple move assignment, this
  251. // check is correct.
  252. if (!BaseClassDecl->hasTrivialMoveAssignment())
  253. data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
  254. // C++11 [class.ctor]p6:
  255. // If that user-written default constructor would satisfy the
  256. // requirements of a constexpr constructor, the implicitly-defined
  257. // default constructor is constexpr.
  258. if (!BaseClassDecl->hasConstexprDefaultConstructor())
  259. data().DefaultedDefaultConstructorIsConstexpr = false;
  260. }
  261. // C++ [class.ctor]p3:
  262. // A destructor is trivial if all the direct base classes of its class
  263. // have trivial destructors.
  264. if (!BaseClassDecl->hasTrivialDestructor())
  265. data().HasTrivialSpecialMembers &= ~SMF_Destructor;
  266. if (!BaseClassDecl->hasIrrelevantDestructor())
  267. data().HasIrrelevantDestructor = false;
  268. // C++11 [class.copy]p18:
  269. // The implicitly-declared copy assignment oeprator for a class X will
  270. // have the form 'X& X::operator=(const X&)' if each direct base class B
  271. // of X has a copy assignment operator whose parameter is of type 'const
  272. // B&', 'const volatile B&', or 'B' [...]
  273. if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
  274. data().ImplicitCopyAssignmentHasConstParam = false;
  275. // C++11 [class.copy]p8:
  276. // The implicitly-declared copy constructor for a class X will have
  277. // the form 'X::X(const X&)' if each direct [...] base class B of X
  278. // has a copy constructor whose first parameter is of type
  279. // 'const B&' or 'const volatile B&' [...]
  280. if (!BaseClassDecl->hasCopyConstructorWithConstParam())
  281. data().ImplicitCopyConstructorHasConstParam = false;
  282. // A class has an Objective-C object member if... or any of its bases
  283. // has an Objective-C object member.
  284. if (BaseClassDecl->hasObjectMember())
  285. setHasObjectMember(true);
  286. if (BaseClassDecl->hasVolatileMember())
  287. setHasVolatileMember(true);
  288. // Keep track of the presence of mutable fields.
  289. if (BaseClassDecl->hasMutableFields())
  290. data().HasMutableFields = true;
  291. if (BaseClassDecl->hasUninitializedReferenceMember())
  292. data().HasUninitializedReferenceMember = true;
  293. addedClassSubobject(BaseClassDecl);
  294. }
  295. if (VBases.empty()) {
  296. data().IsParsingBaseSpecifiers = false;
  297. return;
  298. }
  299. // Create base specifier for any direct or indirect virtual bases.
  300. data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
  301. data().NumVBases = VBases.size();
  302. for (int I = 0, E = VBases.size(); I != E; ++I) {
  303. QualType Type = VBases[I]->getType();
  304. if (!Type->isDependentType())
  305. addedClassSubobject(Type->getAsCXXRecordDecl());
  306. data().getVBases()[I] = *VBases[I];
  307. }
  308. data().IsParsingBaseSpecifiers = false;
  309. }
  310. void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
  311. // C++11 [class.copy]p11:
  312. // A defaulted copy/move constructor for a class X is defined as
  313. // deleted if X has:
  314. // -- a direct or virtual base class B that cannot be copied/moved [...]
  315. // -- a non-static data member of class type M (or array thereof)
  316. // that cannot be copied or moved [...]
  317. if (!Subobj->hasSimpleMoveConstructor())
  318. data().NeedOverloadResolutionForMoveConstructor = true;
  319. // C++11 [class.copy]p23:
  320. // A defaulted copy/move assignment operator for a class X is defined as
  321. // deleted if X has:
  322. // -- a direct or virtual base class B that cannot be copied/moved [...]
  323. // -- a non-static data member of class type M (or array thereof)
  324. // that cannot be copied or moved [...]
  325. if (!Subobj->hasSimpleMoveAssignment())
  326. data().NeedOverloadResolutionForMoveAssignment = true;
  327. // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
  328. // A defaulted [ctor or dtor] for a class X is defined as
  329. // deleted if X has:
  330. // -- any direct or virtual base class [...] has a type with a destructor
  331. // that is deleted or inaccessible from the defaulted [ctor or dtor].
  332. // -- any non-static data member has a type with a destructor
  333. // that is deleted or inaccessible from the defaulted [ctor or dtor].
  334. if (!Subobj->hasSimpleDestructor()) {
  335. data().NeedOverloadResolutionForMoveConstructor = true;
  336. data().NeedOverloadResolutionForDestructor = true;
  337. }
  338. }
  339. /// Callback function for CXXRecordDecl::forallBases that acknowledges
  340. /// that it saw a base class.
  341. static bool SawBase(const CXXRecordDecl *, void *) {
  342. return true;
  343. }
  344. bool CXXRecordDecl::hasAnyDependentBases() const {
  345. if (!isDependentContext())
  346. return false;
  347. return !forallBases(SawBase, nullptr);
  348. }
  349. bool CXXRecordDecl::isTriviallyCopyable() const {
  350. // C++0x [class]p5:
  351. // A trivially copyable class is a class that:
  352. // -- has no non-trivial copy constructors,
  353. if (hasNonTrivialCopyConstructor()) return false;
  354. // -- has no non-trivial move constructors,
  355. if (hasNonTrivialMoveConstructor()) return false;
  356. // -- has no non-trivial copy assignment operators,
  357. if (hasNonTrivialCopyAssignment()) return false;
  358. // -- has no non-trivial move assignment operators, and
  359. if (hasNonTrivialMoveAssignment()) return false;
  360. // -- has a trivial destructor.
  361. if (!hasTrivialDestructor()) return false;
  362. return true;
  363. }
  364. void CXXRecordDecl::markedVirtualFunctionPure() {
  365. // C++ [class.abstract]p2:
  366. // A class is abstract if it has at least one pure virtual function.
  367. data().Abstract = true;
  368. }
  369. void CXXRecordDecl::addedMember(Decl *D) {
  370. if (!D->isImplicit() &&
  371. !isa<FieldDecl>(D) &&
  372. !isa<IndirectFieldDecl>(D) &&
  373. (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
  374. cast<TagDecl>(D)->getTagKind() == TTK_Interface))
  375. data().HasOnlyCMembers = false;
  376. // Ignore friends and invalid declarations.
  377. if (D->getFriendObjectKind() || D->isInvalidDecl())
  378. return;
  379. FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
  380. if (FunTmpl)
  381. D = FunTmpl->getTemplatedDecl();
  382. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
  383. if (Method->isVirtual()) {
  384. // C++ [dcl.init.aggr]p1:
  385. // An aggregate is an array or a class with [...] no virtual functions.
  386. data().Aggregate = false;
  387. // C++ [class]p4:
  388. // A POD-struct is an aggregate class...
  389. data().PlainOldData = false;
  390. // Virtual functions make the class non-empty.
  391. // FIXME: Standard ref?
  392. data().Empty = false;
  393. // C++ [class.virtual]p1:
  394. // A class that declares or inherits a virtual function is called a
  395. // polymorphic class.
  396. data().Polymorphic = true;
  397. // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
  398. // A [default constructor, copy/move constructor, or copy/move
  399. // assignment operator for a class X] is trivial [...] if:
  400. // -- class X has no virtual functions [...]
  401. data().HasTrivialSpecialMembers &= SMF_Destructor;
  402. // C++0x [class]p7:
  403. // A standard-layout class is a class that: [...]
  404. // -- has no virtual functions
  405. data().IsStandardLayout = false;
  406. }
  407. }
  408. // Notify the listener if an implicit member was added after the definition
  409. // was completed.
  410. if (!isBeingDefined() && D->isImplicit())
  411. if (ASTMutationListener *L = getASTMutationListener())
  412. L->AddedCXXImplicitMember(data().Definition, D);
  413. // The kind of special member this declaration is, if any.
  414. unsigned SMKind = 0;
  415. // Handle constructors.
  416. if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
  417. if (!Constructor->isImplicit()) {
  418. // Note that we have a user-declared constructor.
  419. data().UserDeclaredConstructor = true;
  420. // C++ [class]p4:
  421. // A POD-struct is an aggregate class [...]
  422. // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
  423. // type is technically an aggregate in C++0x since it wouldn't be in 03.
  424. data().PlainOldData = false;
  425. }
  426. // Technically, "user-provided" is only defined for special member
  427. // functions, but the intent of the standard is clearly that it should apply
  428. // to all functions.
  429. bool UserProvided = Constructor->isUserProvided();
  430. if (Constructor->isDefaultConstructor()) {
  431. SMKind |= SMF_DefaultConstructor;
  432. if (UserProvided)
  433. data().UserProvidedDefaultConstructor = true;
  434. if (Constructor->isConstexpr())
  435. data().HasConstexprDefaultConstructor = true;
  436. }
  437. if (!FunTmpl) {
  438. unsigned Quals;
  439. if (Constructor->isCopyConstructor(Quals)) {
  440. SMKind |= SMF_CopyConstructor;
  441. if (Quals & Qualifiers::Const)
  442. data().HasDeclaredCopyConstructorWithConstParam = true;
  443. } else if (Constructor->isMoveConstructor())
  444. SMKind |= SMF_MoveConstructor;
  445. }
  446. // Record if we see any constexpr constructors which are neither copy
  447. // nor move constructors.
  448. if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
  449. data().HasConstexprNonCopyMoveConstructor = true;
  450. // C++ [dcl.init.aggr]p1:
  451. // An aggregate is an array or a class with no user-declared
  452. // constructors [...].
  453. // C++11 [dcl.init.aggr]p1:
  454. // An aggregate is an array or a class with no user-provided
  455. // constructors [...].
  456. if (getASTContext().getLangOpts().CPlusPlus11
  457. ? UserProvided : !Constructor->isImplicit())
  458. data().Aggregate = false;
  459. }
  460. // Handle destructors.
  461. if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
  462. SMKind |= SMF_Destructor;
  463. if (DD->isUserProvided())
  464. data().HasIrrelevantDestructor = false;
  465. // If the destructor is explicitly defaulted and not trivial or not public
  466. // or if the destructor is deleted, we clear HasIrrelevantDestructor in
  467. // finishedDefaultedOrDeletedMember.
  468. // C++11 [class.dtor]p5:
  469. // A destructor is trivial if [...] the destructor is not virtual.
  470. if (DD->isVirtual())
  471. data().HasTrivialSpecialMembers &= ~SMF_Destructor;
  472. }
  473. // Handle member functions.
  474. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
  475. if (Method->isCopyAssignmentOperator()) {
  476. SMKind |= SMF_CopyAssignment;
  477. const ReferenceType *ParamTy =
  478. Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
  479. if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
  480. data().HasDeclaredCopyAssignmentWithConstParam = true;
  481. }
  482. if (Method->isMoveAssignmentOperator())
  483. SMKind |= SMF_MoveAssignment;
  484. // Keep the list of conversion functions up-to-date.
  485. if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
  486. // FIXME: We use the 'unsafe' accessor for the access specifier here,
  487. // because Sema may not have set it yet. That's really just a misdesign
  488. // in Sema. However, LLDB *will* have set the access specifier correctly,
  489. // and adds declarations after the class is technically completed,
  490. // so completeDefinition()'s overriding of the access specifiers doesn't
  491. // work.
  492. AccessSpecifier AS = Conversion->getAccessUnsafe();
  493. if (Conversion->getPrimaryTemplate()) {
  494. // We don't record specializations.
  495. } else {
  496. ASTContext &Ctx = getASTContext();
  497. ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
  498. NamedDecl *Primary =
  499. FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
  500. if (Primary->getPreviousDecl())
  501. Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
  502. Primary, AS);
  503. else
  504. Conversions.addDecl(Ctx, Primary, AS);
  505. }
  506. }
  507. if (SMKind) {
  508. // If this is the first declaration of a special member, we no longer have
  509. // an implicit trivial special member.
  510. data().HasTrivialSpecialMembers &=
  511. data().DeclaredSpecialMembers | ~SMKind;
  512. if (!Method->isImplicit() && !Method->isUserProvided()) {
  513. // This method is user-declared but not user-provided. We can't work out
  514. // whether it's trivial yet (not until we get to the end of the class).
  515. // We'll handle this method in finishedDefaultedOrDeletedMember.
  516. } else if (Method->isTrivial())
  517. data().HasTrivialSpecialMembers |= SMKind;
  518. else
  519. data().DeclaredNonTrivialSpecialMembers |= SMKind;
  520. // Note when we have declared a declared special member, and suppress the
  521. // implicit declaration of this special member.
  522. data().DeclaredSpecialMembers |= SMKind;
  523. if (!Method->isImplicit()) {
  524. data().UserDeclaredSpecialMembers |= SMKind;
  525. // C++03 [class]p4:
  526. // A POD-struct is an aggregate class that has [...] no user-defined
  527. // copy assignment operator and no user-defined destructor.
  528. //
  529. // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
  530. // aggregates could not have any constructors, clear it even for an
  531. // explicitly defaulted or deleted constructor.
  532. // type is technically an aggregate in C++0x since it wouldn't be in 03.
  533. //
  534. // Also, a user-declared move assignment operator makes a class non-POD.
  535. // This is an extension in C++03.
  536. data().PlainOldData = false;
  537. }
  538. }
  539. return;
  540. }
  541. // Handle non-static data members.
  542. if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
  543. // C++ [class.bit]p2:
  544. // A declaration for a bit-field that omits the identifier declares an
  545. // unnamed bit-field. Unnamed bit-fields are not members and cannot be
  546. // initialized.
  547. if (Field->isUnnamedBitfield())
  548. return;
  549. // C++ [dcl.init.aggr]p1:
  550. // An aggregate is an array or a class (clause 9) with [...] no
  551. // private or protected non-static data members (clause 11).
  552. //
  553. // A POD must be an aggregate.
  554. if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
  555. data().Aggregate = false;
  556. data().PlainOldData = false;
  557. }
  558. // C++0x [class]p7:
  559. // A standard-layout class is a class that:
  560. // [...]
  561. // -- has the same access control for all non-static data members,
  562. switch (D->getAccess()) {
  563. case AS_private: data().HasPrivateFields = true; break;
  564. case AS_protected: data().HasProtectedFields = true; break;
  565. case AS_public: data().HasPublicFields = true; break;
  566. case AS_none: llvm_unreachable("Invalid access specifier");
  567. };
  568. if ((data().HasPrivateFields + data().HasProtectedFields +
  569. data().HasPublicFields) > 1)
  570. data().IsStandardLayout = false;
  571. // Keep track of the presence of mutable fields.
  572. if (Field->isMutable())
  573. data().HasMutableFields = true;
  574. // C++11 [class.union]p8, DR1460:
  575. // If X is a union, a non-static data member of X that is not an anonymous
  576. // union is a variant member of X.
  577. if (isUnion() && !Field->isAnonymousStructOrUnion())
  578. data().HasVariantMembers = true;
  579. // C++0x [class]p9:
  580. // A POD struct is a class that is both a trivial class and a
  581. // standard-layout class, and has no non-static data members of type
  582. // non-POD struct, non-POD union (or array of such types).
  583. //
  584. // Automatic Reference Counting: the presence of a member of Objective-C pointer type
  585. // that does not explicitly have no lifetime makes the class a non-POD.
  586. ASTContext &Context = getASTContext();
  587. QualType T = Context.getBaseElementType(Field->getType());
  588. if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
  589. if (!Context.getLangOpts().ObjCAutoRefCount) {
  590. setHasObjectMember(true);
  591. } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
  592. // Objective-C Automatic Reference Counting:
  593. // If a class has a non-static data member of Objective-C pointer
  594. // type (or array thereof), it is a non-POD type and its
  595. // default constructor (if any), copy constructor, move constructor,
  596. // copy assignment operator, move assignment operator, and destructor are
  597. // non-trivial.
  598. setHasObjectMember(true);
  599. struct DefinitionData &Data = data();
  600. Data.PlainOldData = false;
  601. Data.HasTrivialSpecialMembers = 0;
  602. Data.HasIrrelevantDestructor = false;
  603. }
  604. } else if (!T.isCXX98PODType(Context))
  605. data().PlainOldData = false;
  606. if (T->isReferenceType()) {
  607. if (!Field->hasInClassInitializer())
  608. data().HasUninitializedReferenceMember = true;
  609. // C++0x [class]p7:
  610. // A standard-layout class is a class that:
  611. // -- has no non-static data members of type [...] reference,
  612. data().IsStandardLayout = false;
  613. }
  614. // Record if this field is the first non-literal or volatile field or base.
  615. if (!T->isLiteralType(Context) || T.isVolatileQualified())
  616. data().HasNonLiteralTypeFieldsOrBases = true;
  617. if (Field->hasInClassInitializer() ||
  618. (Field->isAnonymousStructOrUnion() &&
  619. Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
  620. data().HasInClassInitializer = true;
  621. // C++11 [class]p5:
  622. // A default constructor is trivial if [...] no non-static data member
  623. // of its class has a brace-or-equal-initializer.
  624. data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
  625. // C++11 [dcl.init.aggr]p1:
  626. // An aggregate is a [...] class with [...] no
  627. // brace-or-equal-initializers for non-static data members.
  628. //
  629. // This rule was removed in C++1y.
  630. if (!getASTContext().getLangOpts().CPlusPlus14)
  631. data().Aggregate = false;
  632. // C++11 [class]p10:
  633. // A POD struct is [...] a trivial class.
  634. data().PlainOldData = false;
  635. }
  636. // C++11 [class.copy]p23:
  637. // A defaulted copy/move assignment operator for a class X is defined
  638. // as deleted if X has:
  639. // -- a non-static data member of reference type
  640. if (T->isReferenceType())
  641. data().DefaultedMoveAssignmentIsDeleted = true;
  642. if (const RecordType *RecordTy = T->getAs<RecordType>()) {
  643. CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
  644. if (FieldRec->getDefinition()) {
  645. addedClassSubobject(FieldRec);
  646. // We may need to perform overload resolution to determine whether a
  647. // field can be moved if it's const or volatile qualified.
  648. if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
  649. data().NeedOverloadResolutionForMoveConstructor = true;
  650. data().NeedOverloadResolutionForMoveAssignment = true;
  651. }
  652. // C++11 [class.ctor]p5, C++11 [class.copy]p11:
  653. // A defaulted [special member] for a class X is defined as
  654. // deleted if:
  655. // -- X is a union-like class that has a variant member with a
  656. // non-trivial [corresponding special member]
  657. if (isUnion()) {
  658. if (FieldRec->hasNonTrivialMoveConstructor())
  659. data().DefaultedMoveConstructorIsDeleted = true;
  660. if (FieldRec->hasNonTrivialMoveAssignment())
  661. data().DefaultedMoveAssignmentIsDeleted = true;
  662. if (FieldRec->hasNonTrivialDestructor())
  663. data().DefaultedDestructorIsDeleted = true;
  664. }
  665. // C++0x [class.ctor]p5:
  666. // A default constructor is trivial [...] if:
  667. // -- for all the non-static data members of its class that are of
  668. // class type (or array thereof), each such class has a trivial
  669. // default constructor.
  670. if (!FieldRec->hasTrivialDefaultConstructor())
  671. data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
  672. // C++0x [class.copy]p13:
  673. // A copy/move constructor for class X is trivial if [...]
  674. // [...]
  675. // -- for each non-static data member of X that is of class type (or
  676. // an array thereof), the constructor selected to copy/move that
  677. // member is trivial;
  678. if (!FieldRec->hasTrivialCopyConstructor())
  679. data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
  680. // If the field doesn't have a simple move constructor, we'll eagerly
  681. // declare the move constructor for this class and we'll decide whether
  682. // it's trivial then.
  683. if (!FieldRec->hasTrivialMoveConstructor())
  684. data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
  685. // C++0x [class.copy]p27:
  686. // A copy/move assignment operator for class X is trivial if [...]
  687. // [...]
  688. // -- for each non-static data member of X that is of class type (or
  689. // an array thereof), the assignment operator selected to
  690. // copy/move that member is trivial;
  691. if (!FieldRec->hasTrivialCopyAssignment())
  692. data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
  693. // If the field doesn't have a simple move assignment, we'll eagerly
  694. // declare the move assignment for this class and we'll decide whether
  695. // it's trivial then.
  696. if (!FieldRec->hasTrivialMoveAssignment())
  697. data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
  698. if (!FieldRec->hasTrivialDestructor())
  699. data().HasTrivialSpecialMembers &= ~SMF_Destructor;
  700. if (!FieldRec->hasIrrelevantDestructor())
  701. data().HasIrrelevantDestructor = false;
  702. if (FieldRec->hasObjectMember())
  703. setHasObjectMember(true);
  704. if (FieldRec->hasVolatileMember())
  705. setHasVolatileMember(true);
  706. // C++0x [class]p7:
  707. // A standard-layout class is a class that:
  708. // -- has no non-static data members of type non-standard-layout
  709. // class (or array of such types) [...]
  710. if (!FieldRec->isStandardLayout())
  711. data().IsStandardLayout = false;
  712. // C++0x [class]p7:
  713. // A standard-layout class is a class that:
  714. // [...]
  715. // -- has no base classes of the same type as the first non-static
  716. // data member.
  717. // We don't want to expend bits in the state of the record decl
  718. // tracking whether this is the first non-static data member so we
  719. // cheat a bit and use some of the existing state: the empty bit.
  720. // Virtual bases and virtual methods make a class non-empty, but they
  721. // also make it non-standard-layout so we needn't check here.
  722. // A non-empty base class may leave the class standard-layout, but not
  723. // if we have arrived here, and have at least one non-static data
  724. // member. If IsStandardLayout remains true, then the first non-static
  725. // data member must come through here with Empty still true, and Empty
  726. // will subsequently be set to false below.
  727. if (data().IsStandardLayout && data().Empty) {
  728. for (const auto &BI : bases()) {
  729. if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
  730. data().IsStandardLayout = false;
  731. break;
  732. }
  733. }
  734. }
  735. // Keep track of the presence of mutable fields.
  736. if (FieldRec->hasMutableFields())
  737. data().HasMutableFields = true;
  738. // C++11 [class.copy]p13:
  739. // If the implicitly-defined constructor would satisfy the
  740. // requirements of a constexpr constructor, the implicitly-defined
  741. // constructor is constexpr.
  742. // C++11 [dcl.constexpr]p4:
  743. // -- every constructor involved in initializing non-static data
  744. // members [...] shall be a constexpr constructor
  745. if (!Field->hasInClassInitializer() &&
  746. !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
  747. // The standard requires any in-class initializer to be a constant
  748. // expression. We consider this to be a defect.
  749. data().DefaultedDefaultConstructorIsConstexpr = false;
  750. // C++11 [class.copy]p8:
  751. // The implicitly-declared copy constructor for a class X will have
  752. // the form 'X::X(const X&)' if [...] for all the non-static data
  753. // members of X that are of a class type M (or array thereof), each
  754. // such class type has a copy constructor whose first parameter is
  755. // of type 'const M&' or 'const volatile M&'.
  756. if (!FieldRec->hasCopyConstructorWithConstParam())
  757. data().ImplicitCopyConstructorHasConstParam = false;
  758. // C++11 [class.copy]p18:
  759. // The implicitly-declared copy assignment oeprator for a class X will
  760. // have the form 'X& X::operator=(const X&)' if [...] for all the
  761. // non-static data members of X that are of a class type M (or array
  762. // thereof), each such class type has a copy assignment operator whose
  763. // parameter is of type 'const M&', 'const volatile M&' or 'M'.
  764. if (!FieldRec->hasCopyAssignmentWithConstParam())
  765. data().ImplicitCopyAssignmentHasConstParam = false;
  766. if (FieldRec->hasUninitializedReferenceMember() &&
  767. !Field->hasInClassInitializer())
  768. data().HasUninitializedReferenceMember = true;
  769. // C++11 [class.union]p8, DR1460:
  770. // a non-static data member of an anonymous union that is a member of
  771. // X is also a variant member of X.
  772. if (FieldRec->hasVariantMembers() &&
  773. Field->isAnonymousStructOrUnion())
  774. data().HasVariantMembers = true;
  775. }
  776. } else {
  777. // Base element type of field is a non-class type.
  778. if (!T->isLiteralType(Context) ||
  779. (!Field->hasInClassInitializer() && !isUnion()))
  780. data().DefaultedDefaultConstructorIsConstexpr = false;
  781. // C++11 [class.copy]p23:
  782. // A defaulted copy/move assignment operator for a class X is defined
  783. // as deleted if X has:
  784. // -- a non-static data member of const non-class type (or array
  785. // thereof)
  786. if (T.isConstQualified())
  787. data().DefaultedMoveAssignmentIsDeleted = true;
  788. }
  789. // C++0x [class]p7:
  790. // A standard-layout class is a class that:
  791. // [...]
  792. // -- either has no non-static data members in the most derived
  793. // class and at most one base class with non-static data members,
  794. // or has no base classes with non-static data members, and
  795. // At this point we know that we have a non-static data member, so the last
  796. // clause holds.
  797. if (!data().HasNoNonEmptyBases)
  798. data().IsStandardLayout = false;
  799. // If this is not a zero-length bit-field, then the class is not empty.
  800. if (data().Empty) {
  801. if (!Field->isBitField() ||
  802. (!Field->getBitWidth()->isTypeDependent() &&
  803. !Field->getBitWidth()->isValueDependent() &&
  804. Field->getBitWidthValue(Context) != 0))
  805. data().Empty = false;
  806. }
  807. }
  808. // Handle using declarations of conversion functions.
  809. if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
  810. if (Shadow->getDeclName().getNameKind()
  811. == DeclarationName::CXXConversionFunctionName) {
  812. ASTContext &Ctx = getASTContext();
  813. data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
  814. }
  815. }
  816. }
  817. void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
  818. assert(!D->isImplicit() && !D->isUserProvided());
  819. // The kind of special member this declaration is, if any.
  820. unsigned SMKind = 0;
  821. if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
  822. if (Constructor->isDefaultConstructor()) {
  823. SMKind |= SMF_DefaultConstructor;
  824. if (Constructor->isConstexpr())
  825. data().HasConstexprDefaultConstructor = true;
  826. }
  827. if (Constructor->isCopyConstructor())
  828. SMKind |= SMF_CopyConstructor;
  829. else if (Constructor->isMoveConstructor())
  830. SMKind |= SMF_MoveConstructor;
  831. else if (Constructor->isConstexpr())
  832. // We may now know that the constructor is constexpr.
  833. data().HasConstexprNonCopyMoveConstructor = true;
  834. } else if (isa<CXXDestructorDecl>(D)) {
  835. SMKind |= SMF_Destructor;
  836. if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
  837. data().HasIrrelevantDestructor = false;
  838. } else if (D->isCopyAssignmentOperator())
  839. SMKind |= SMF_CopyAssignment;
  840. else if (D->isMoveAssignmentOperator())
  841. SMKind |= SMF_MoveAssignment;
  842. // Update which trivial / non-trivial special members we have.
  843. // addedMember will have skipped this step for this member.
  844. if (D->isTrivial())
  845. data().HasTrivialSpecialMembers |= SMKind;
  846. else
  847. data().DeclaredNonTrivialSpecialMembers |= SMKind;
  848. }
  849. bool CXXRecordDecl::isCLike() const {
  850. if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
  851. !TemplateOrInstantiation.isNull())
  852. return false;
  853. if (!hasDefinition())
  854. return true;
  855. return isPOD() && data().HasOnlyCMembers;
  856. }
  857. bool CXXRecordDecl::isGenericLambda() const {
  858. if (!isLambda()) return false;
  859. return getLambdaData().IsGenericLambda;
  860. }
  861. CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
  862. if (!isLambda()) return nullptr;
  863. DeclarationName Name =
  864. getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
  865. DeclContext::lookup_result Calls = lookup(Name);
  866. assert(!Calls.empty() && "Missing lambda call operator!");
  867. assert(Calls.size() == 1 && "More than one lambda call operator!");
  868. NamedDecl *CallOp = Calls.front();
  869. if (FunctionTemplateDecl *CallOpTmpl =
  870. dyn_cast<FunctionTemplateDecl>(CallOp))
  871. return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
  872. return cast<CXXMethodDecl>(CallOp);
  873. }
  874. CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
  875. if (!isLambda()) return nullptr;
  876. DeclarationName Name =
  877. &getASTContext().Idents.get(getLambdaStaticInvokerName());
  878. DeclContext::lookup_result Invoker = lookup(Name);
  879. if (Invoker.empty()) return nullptr;
  880. assert(Invoker.size() == 1 && "More than one static invoker operator!");
  881. NamedDecl *InvokerFun = Invoker.front();
  882. if (FunctionTemplateDecl *InvokerTemplate =
  883. dyn_cast<FunctionTemplateDecl>(InvokerFun))
  884. return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
  885. return cast<CXXMethodDecl>(InvokerFun);
  886. }
  887. void CXXRecordDecl::getCaptureFields(
  888. llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
  889. FieldDecl *&ThisCapture) const {
  890. Captures.clear();
  891. ThisCapture = nullptr;
  892. LambdaDefinitionData &Lambda = getLambdaData();
  893. RecordDecl::field_iterator Field = field_begin();
  894. for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
  895. C != CEnd; ++C, ++Field) {
  896. if (C->capturesThis())
  897. ThisCapture = *Field;
  898. else if (C->capturesVariable())
  899. Captures[C->getCapturedVar()] = *Field;
  900. }
  901. assert(Field == field_end());
  902. }
  903. TemplateParameterList *
  904. CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
  905. if (!isLambda()) return nullptr;
  906. CXXMethodDecl *CallOp = getLambdaCallOperator();
  907. if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
  908. return Tmpl->getTemplateParameters();
  909. return nullptr;
  910. }
  911. static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
  912. QualType T =
  913. cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
  914. ->getConversionType();
  915. return Context.getCanonicalType(T);
  916. }
  917. /// Collect the visible conversions of a base class.
  918. ///
  919. /// \param Record a base class of the class we're considering
  920. /// \param InVirtual whether this base class is a virtual base (or a base
  921. /// of a virtual base)
  922. /// \param Access the access along the inheritance path to this base
  923. /// \param ParentHiddenTypes the conversions provided by the inheritors
  924. /// of this base
  925. /// \param Output the set to which to add conversions from non-virtual bases
  926. /// \param VOutput the set to which to add conversions from virtual bases
  927. /// \param HiddenVBaseCs the set of conversions which were hidden in a
  928. /// virtual base along some inheritance path
  929. static void CollectVisibleConversions(ASTContext &Context,
  930. CXXRecordDecl *Record,
  931. bool InVirtual,
  932. AccessSpecifier Access,
  933. const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
  934. ASTUnresolvedSet &Output,
  935. UnresolvedSetImpl &VOutput,
  936. llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
  937. // The set of types which have conversions in this class or its
  938. // subclasses. As an optimization, we don't copy the derived set
  939. // unless it might change.
  940. const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
  941. llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
  942. // Collect the direct conversions and figure out which conversions
  943. // will be hidden in the subclasses.
  944. CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
  945. CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
  946. if (ConvI != ConvE) {
  947. HiddenTypesBuffer = ParentHiddenTypes;
  948. HiddenTypes = &HiddenTypesBuffer;
  949. for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
  950. CanQualType ConvType(GetConversionType(Context, I.getDecl()));
  951. bool Hidden = ParentHiddenTypes.count(ConvType);
  952. if (!Hidden)
  953. HiddenTypesBuffer.insert(ConvType);
  954. // If this conversion is hidden and we're in a virtual base,
  955. // remember that it's hidden along some inheritance path.
  956. if (Hidden && InVirtual)
  957. HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
  958. // If this conversion isn't hidden, add it to the appropriate output.
  959. else if (!Hidden) {
  960. AccessSpecifier IAccess
  961. = CXXRecordDecl::MergeAccess(Access, I.getAccess());
  962. if (InVirtual)
  963. VOutput.addDecl(I.getDecl(), IAccess);
  964. else
  965. Output.addDecl(Context, I.getDecl(), IAccess);
  966. }
  967. }
  968. }
  969. // Collect information recursively from any base classes.
  970. for (const auto &I : Record->bases()) {
  971. const RecordType *RT = I.getType()->getAs<RecordType>();
  972. if (!RT) continue;
  973. AccessSpecifier BaseAccess
  974. = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
  975. bool BaseInVirtual = InVirtual || I.isVirtual();
  976. CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
  977. CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
  978. *HiddenTypes, Output, VOutput, HiddenVBaseCs);
  979. }
  980. }
  981. /// Collect the visible conversions of a class.
  982. ///
  983. /// This would be extremely straightforward if it weren't for virtual
  984. /// bases. It might be worth special-casing that, really.
  985. static void CollectVisibleConversions(ASTContext &Context,
  986. CXXRecordDecl *Record,
  987. ASTUnresolvedSet &Output) {
  988. // The collection of all conversions in virtual bases that we've
  989. // found. These will be added to the output as long as they don't
  990. // appear in the hidden-conversions set.
  991. UnresolvedSet<8> VBaseCs;
  992. // The set of conversions in virtual bases that we've determined to
  993. // be hidden.
  994. llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
  995. // The set of types hidden by classes derived from this one.
  996. llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
  997. // Go ahead and collect the direct conversions and add them to the
  998. // hidden-types set.
  999. CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
  1000. CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
  1001. Output.append(Context, ConvI, ConvE);
  1002. for (; ConvI != ConvE; ++ConvI)
  1003. HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
  1004. // Recursively collect conversions from base classes.
  1005. for (const auto &I : Record->bases()) {
  1006. const RecordType *RT = I.getType()->getAs<RecordType>();
  1007. if (!RT) continue;
  1008. CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
  1009. I.isVirtual(), I.getAccessSpecifier(),
  1010. HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
  1011. }
  1012. // Add any unhidden conversions provided by virtual bases.
  1013. for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
  1014. I != E; ++I) {
  1015. if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
  1016. Output.addDecl(Context, I.getDecl(), I.getAccess());
  1017. }
  1018. }
  1019. /// getVisibleConversionFunctions - get all conversion functions visible
  1020. /// in current class; including conversion function templates.
  1021. llvm::iterator_range<CXXRecordDecl::conversion_iterator>
  1022. CXXRecordDecl::getVisibleConversionFunctions() {
  1023. ASTContext &Ctx = getASTContext();
  1024. ASTUnresolvedSet *Set;
  1025. if (bases_begin() == bases_end()) {
  1026. // If root class, all conversions are visible.
  1027. Set = &data().Conversions.get(Ctx);
  1028. } else {
  1029. Set = &data().VisibleConversions.get(Ctx);
  1030. // If visible conversion list is not evaluated, evaluate it.
  1031. if (!data().ComputedVisibleConversions) {
  1032. CollectVisibleConversions(Ctx, this, *Set);
  1033. data().ComputedVisibleConversions = true;
  1034. }
  1035. }
  1036. return llvm::make_range(Set->begin(), Set->end());
  1037. }
  1038. void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
  1039. // This operation is O(N) but extremely rare. Sema only uses it to
  1040. // remove UsingShadowDecls in a class that were followed by a direct
  1041. // declaration, e.g.:
  1042. // class A : B {
  1043. // using B::operator int;
  1044. // operator int();
  1045. // };
  1046. // This is uncommon by itself and even more uncommon in conjunction
  1047. // with sufficiently large numbers of directly-declared conversions
  1048. // that asymptotic behavior matters.
  1049. ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
  1050. for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
  1051. if (Convs[I].getDecl() == ConvDecl) {
  1052. Convs.erase(I);
  1053. assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
  1054. && "conversion was found multiple times in unresolved set");
  1055. return;
  1056. }
  1057. }
  1058. llvm_unreachable("conversion not found in set!");
  1059. }
  1060. CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
  1061. if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
  1062. return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
  1063. return nullptr;
  1064. }
  1065. void
  1066. CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
  1067. TemplateSpecializationKind TSK) {
  1068. assert(TemplateOrInstantiation.isNull() &&
  1069. "Previous template or instantiation?");
  1070. assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
  1071. TemplateOrInstantiation
  1072. = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
  1073. }
  1074. TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
  1075. if (const ClassTemplateSpecializationDecl *Spec
  1076. = dyn_cast<ClassTemplateSpecializationDecl>(this))
  1077. return Spec->getSpecializationKind();
  1078. if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
  1079. return MSInfo->getTemplateSpecializationKind();
  1080. return TSK_Undeclared;
  1081. }
  1082. void
  1083. CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
  1084. if (ClassTemplateSpecializationDecl *Spec
  1085. = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
  1086. Spec->setSpecializationKind(TSK);
  1087. return;
  1088. }
  1089. if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
  1090. MSInfo->setTemplateSpecializationKind(TSK);
  1091. return;
  1092. }
  1093. llvm_unreachable("Not a class template or member class specialization");
  1094. }
  1095. const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
  1096. // If it's a class template specialization, find the template or partial
  1097. // specialization from which it was instantiated.
  1098. if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
  1099. auto From = TD->getInstantiatedFrom();
  1100. if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
  1101. while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
  1102. if (NewCTD->isMemberSpecialization())
  1103. break;
  1104. CTD = NewCTD;
  1105. }
  1106. return CTD->getTemplatedDecl()->getDefinition();
  1107. }
  1108. if (auto *CTPSD =
  1109. From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
  1110. while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
  1111. if (NewCTPSD->isMemberSpecialization())
  1112. break;
  1113. CTPSD = NewCTPSD;
  1114. }
  1115. return CTPSD->getDefinition();
  1116. }
  1117. }
  1118. if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
  1119. if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
  1120. const CXXRecordDecl *RD = this;
  1121. while (auto *NewRD = RD->getInstantiatedFromMemberClass())
  1122. RD = NewRD;
  1123. return RD->getDefinition();
  1124. }
  1125. }
  1126. assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
  1127. "couldn't find pattern for class template instantiation");
  1128. return nullptr;
  1129. }
  1130. CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
  1131. ASTContext &Context = getASTContext();
  1132. QualType ClassType = Context.getTypeDeclType(this);
  1133. DeclarationName Name
  1134. = Context.DeclarationNames.getCXXDestructorName(
  1135. Context.getCanonicalType(ClassType));
  1136. DeclContext::lookup_result R = lookup(Name);
  1137. if (R.empty())
  1138. return nullptr;
  1139. CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
  1140. return Dtor;
  1141. }
  1142. bool CXXRecordDecl::isAnyDestructorNoReturn() const {
  1143. // Destructor is noreturn.
  1144. if (const CXXDestructorDecl *Destructor = getDestructor())
  1145. if (Destructor->isNoReturn())
  1146. return true;
  1147. // Check base classes destructor for noreturn.
  1148. for (const auto &Base : bases())
  1149. if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
  1150. return true;
  1151. // Check fields for noreturn.
  1152. for (const auto *Field : fields())
  1153. if (const CXXRecordDecl *RD =
  1154. Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
  1155. if (RD->isAnyDestructorNoReturn())
  1156. return true;
  1157. // All destructors are not noreturn.
  1158. return false;
  1159. }
  1160. void CXXRecordDecl::completeDefinition() {
  1161. completeDefinition(nullptr);
  1162. }
  1163. void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
  1164. RecordDecl::completeDefinition();
  1165. // If the class may be abstract (but hasn't been marked as such), check for
  1166. // any pure final overriders.
  1167. if (mayBeAbstract()) {
  1168. CXXFinalOverriderMap MyFinalOverriders;
  1169. if (!FinalOverriders) {
  1170. getFinalOverriders(MyFinalOverriders);
  1171. FinalOverriders = &MyFinalOverriders;
  1172. }
  1173. bool Done = false;
  1174. for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
  1175. MEnd = FinalOverriders->end();
  1176. M != MEnd && !Done; ++M) {
  1177. for (OverridingMethods::iterator SO = M->second.begin(),
  1178. SOEnd = M->second.end();
  1179. SO != SOEnd && !Done; ++SO) {
  1180. assert(SO->second.size() > 0 &&
  1181. "All virtual functions have overridding virtual functions");
  1182. // C++ [class.abstract]p4:
  1183. // A class is abstract if it contains or inherits at least one
  1184. // pure virtual function for which the final overrider is pure
  1185. // virtual.
  1186. if (SO->second.front().Method->isPure()) {
  1187. data().Abstract = true;
  1188. Done = true;
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. // Set access bits correctly on the directly-declared conversions.
  1195. for (conversion_iterator I = conversion_begin(), E = conversion_end();
  1196. I != E; ++I)
  1197. I.setAccess((*I)->getAccess());
  1198. }
  1199. bool CXXRecordDecl::mayBeAbstract() const {
  1200. if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
  1201. isDependentContext())
  1202. return false;
  1203. for (const auto &B : bases()) {
  1204. CXXRecordDecl *BaseDecl
  1205. = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
  1206. if (BaseDecl->isAbstract())
  1207. return true;
  1208. }
  1209. return false;
  1210. }
  1211. void CXXMethodDecl::anchor() { }
  1212. bool CXXMethodDecl::isStatic() const {
  1213. const CXXMethodDecl *MD = getCanonicalDecl();
  1214. if (MD->getStorageClass() == SC_Static)
  1215. return true;
  1216. OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
  1217. return isStaticOverloadedOperator(OOK);
  1218. }
  1219. static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
  1220. const CXXMethodDecl *BaseMD) {
  1221. for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
  1222. E = DerivedMD->end_overridden_methods(); I != E; ++I) {
  1223. const CXXMethodDecl *MD = *I;
  1224. if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
  1225. return true;
  1226. if (recursivelyOverrides(MD, BaseMD))
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. CXXMethodDecl *
  1232. CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
  1233. bool MayBeBase) {
  1234. if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
  1235. return this;
  1236. // Lookup doesn't work for destructors, so handle them separately.
  1237. if (isa<CXXDestructorDecl>(this)) {
  1238. CXXMethodDecl *MD = RD->getDestructor();
  1239. if (MD) {
  1240. if (recursivelyOverrides(MD, this))
  1241. return MD;
  1242. if (MayBeBase && recursivelyOverrides(this, MD))
  1243. return MD;
  1244. }
  1245. return nullptr;
  1246. }
  1247. for (auto *ND : RD->lookup(getDeclName())) {
  1248. CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
  1249. if (!MD)
  1250. continue;
  1251. if (recursivelyOverrides(MD, this))
  1252. return MD;
  1253. if (MayBeBase && recursivelyOverrides(this, MD))
  1254. return MD;
  1255. }
  1256. for (const auto &I : RD->bases()) {
  1257. const RecordType *RT = I.getType()->getAs<RecordType>();
  1258. if (!RT)
  1259. continue;
  1260. const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
  1261. CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
  1262. if (T)
  1263. return T;
  1264. }
  1265. return nullptr;
  1266. }
  1267. CXXMethodDecl *
  1268. CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
  1269. SourceLocation StartLoc,
  1270. const DeclarationNameInfo &NameInfo,
  1271. QualType T, TypeSourceInfo *TInfo,
  1272. StorageClass SC, bool isInline,
  1273. bool isConstexpr, SourceLocation EndLocation) {
  1274. return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
  1275. T, TInfo, SC, isInline, isConstexpr,
  1276. EndLocation);
  1277. }
  1278. CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1279. return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
  1280. DeclarationNameInfo(), QualType(), nullptr,
  1281. SC_None, false, false, SourceLocation());
  1282. }
  1283. bool CXXMethodDecl::isUsualDeallocationFunction() const {
  1284. if (getOverloadedOperator() != OO_Delete &&
  1285. getOverloadedOperator() != OO_Array_Delete)
  1286. return false;
  1287. // C++ [basic.stc.dynamic.deallocation]p2:
  1288. // A template instance is never a usual deallocation function,
  1289. // regardless of its signature.
  1290. if (getPrimaryTemplate())
  1291. return false;
  1292. // C++ [basic.stc.dynamic.deallocation]p2:
  1293. // If a class T has a member deallocation function named operator delete
  1294. // with exactly one parameter, then that function is a usual (non-placement)
  1295. // deallocation function. [...]
  1296. if (getNumParams() == 1)
  1297. return true;
  1298. // C++ [basic.stc.dynamic.deallocation]p2:
  1299. // [...] If class T does not declare such an operator delete but does
  1300. // declare a member deallocation function named operator delete with
  1301. // exactly two parameters, the second of which has type std::size_t (18.1),
  1302. // then this function is a usual deallocation function.
  1303. ASTContext &Context = getASTContext();
  1304. if (getNumParams() != 2 ||
  1305. !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
  1306. Context.getSizeType()))
  1307. return false;
  1308. // This function is a usual deallocation function if there are no
  1309. // single-parameter deallocation functions of the same kind.
  1310. DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
  1311. for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
  1312. I != E; ++I) {
  1313. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
  1314. if (FD->getNumParams() == 1)
  1315. return false;
  1316. }
  1317. return true;
  1318. }
  1319. bool CXXMethodDecl::isCopyAssignmentOperator() const {
  1320. // C++0x [class.copy]p17:
  1321. // A user-declared copy assignment operator X::operator= is a non-static
  1322. // non-template member function of class X with exactly one parameter of
  1323. // type X, X&, const X&, volatile X& or const volatile X&.
  1324. if (/*operator=*/getOverloadedOperator() != OO_Equal ||
  1325. /*non-static*/ isStatic() ||
  1326. /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
  1327. getNumParams() != 1)
  1328. return false;
  1329. QualType ParamType = getParamDecl(0)->getType();
  1330. if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
  1331. ParamType = Ref->getPointeeType();
  1332. ASTContext &Context = getASTContext();
  1333. QualType ClassType
  1334. = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
  1335. return Context.hasSameUnqualifiedType(ClassType, ParamType);
  1336. }
  1337. bool CXXMethodDecl::isMoveAssignmentOperator() const {
  1338. // C++0x [class.copy]p19:
  1339. // A user-declared move assignment operator X::operator= is a non-static
  1340. // non-template member function of class X with exactly one parameter of type
  1341. // X&&, const X&&, volatile X&&, or const volatile X&&.
  1342. if (getOverloadedOperator() != OO_Equal || isStatic() ||
  1343. getPrimaryTemplate() || getDescribedFunctionTemplate() ||
  1344. getNumParams() != 1)
  1345. return false;
  1346. QualType ParamType = getParamDecl(0)->getType();
  1347. if (!isa<RValueReferenceType>(ParamType))
  1348. return false;
  1349. ParamType = ParamType->getPointeeType();
  1350. ASTContext &Context = getASTContext();
  1351. QualType ClassType
  1352. = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
  1353. return Context.hasSameUnqualifiedType(ClassType, ParamType);
  1354. }
  1355. void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
  1356. assert(MD->isCanonicalDecl() && "Method is not canonical!");
  1357. assert(!MD->getParent()->isDependentContext() &&
  1358. "Can't add an overridden method to a class template!");
  1359. assert(MD->isVirtual() && "Method is not virtual!");
  1360. getASTContext().addOverriddenMethod(this, MD);
  1361. }
  1362. CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
  1363. if (isa<CXXConstructorDecl>(this)) return nullptr;
  1364. return getASTContext().overridden_methods_begin(this);
  1365. }
  1366. CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
  1367. if (isa<CXXConstructorDecl>(this)) return nullptr;
  1368. return getASTContext().overridden_methods_end(this);
  1369. }
  1370. unsigned CXXMethodDecl::size_overridden_methods() const {
  1371. if (isa<CXXConstructorDecl>(this)) return 0;
  1372. return getASTContext().overridden_methods_size(this);
  1373. }
  1374. QualType CXXMethodDecl::getThisType(ASTContext &C) const {
  1375. // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
  1376. // If the member function is declared const, the type of this is const X*,
  1377. // if the member function is declared volatile, the type of this is
  1378. // volatile X*, and if the member function is declared const volatile,
  1379. // the type of this is const volatile X*.
  1380. assert(isInstance() && "No 'this' for static methods!");
  1381. QualType ClassTy = C.getTypeDeclType(getParent());
  1382. ClassTy = C.getQualifiedType(ClassTy,
  1383. Qualifiers::fromCVRMask(getTypeQualifiers()));
  1384. return C.getPointerType(ClassTy);
  1385. }
  1386. bool CXXMethodDecl::hasInlineBody() const {
  1387. // If this function is a template instantiation, look at the template from
  1388. // which it was instantiated.
  1389. const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
  1390. if (!CheckFn)
  1391. CheckFn = this;
  1392. const FunctionDecl *fn;
  1393. return CheckFn->hasBody(fn) && !fn->isOutOfLine();
  1394. }
  1395. bool CXXMethodDecl::isLambdaStaticInvoker() const {
  1396. const CXXRecordDecl *P = getParent();
  1397. if (P->isLambda()) {
  1398. if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
  1399. if (StaticInvoker == this) return true;
  1400. if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
  1401. return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
  1402. }
  1403. }
  1404. return false;
  1405. }
  1406. CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
  1407. TypeSourceInfo *TInfo, bool IsVirtual,
  1408. SourceLocation L, Expr *Init,
  1409. SourceLocation R,
  1410. SourceLocation EllipsisLoc)
  1411. : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
  1412. LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
  1413. IsWritten(false), SourceOrderOrNumArrayIndices(0)
  1414. {
  1415. }
  1416. CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
  1417. FieldDecl *Member,
  1418. SourceLocation MemberLoc,
  1419. SourceLocation L, Expr *Init,
  1420. SourceLocation R)
  1421. : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
  1422. LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
  1423. IsWritten(false), SourceOrderOrNumArrayIndices(0)
  1424. {
  1425. }
  1426. CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
  1427. IndirectFieldDecl *Member,
  1428. SourceLocation MemberLoc,
  1429. SourceLocation L, Expr *Init,
  1430. SourceLocation R)
  1431. : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
  1432. LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
  1433. IsWritten(false), SourceOrderOrNumArrayIndices(0)
  1434. {
  1435. }
  1436. CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
  1437. TypeSourceInfo *TInfo,
  1438. SourceLocation L, Expr *Init,
  1439. SourceLocation R)
  1440. : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
  1441. LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
  1442. IsWritten(false), SourceOrderOrNumArrayIndices(0)
  1443. {
  1444. }
  1445. CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
  1446. FieldDecl *Member,
  1447. SourceLocation MemberLoc,
  1448. SourceLocation L, Expr *Init,
  1449. SourceLocation R,
  1450. VarDecl **Indices,
  1451. unsigned NumIndices)
  1452. : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
  1453. LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
  1454. IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
  1455. {
  1456. VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
  1457. memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
  1458. }
  1459. CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
  1460. FieldDecl *Member,
  1461. SourceLocation MemberLoc,
  1462. SourceLocation L, Expr *Init,
  1463. SourceLocation R,
  1464. VarDecl **Indices,
  1465. unsigned NumIndices) {
  1466. void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
  1467. sizeof(VarDecl *) * NumIndices,
  1468. llvm::alignOf<CXXCtorInitializer>());
  1469. return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
  1470. Indices, NumIndices);
  1471. }
  1472. TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
  1473. if (isBaseInitializer())
  1474. return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
  1475. else
  1476. return TypeLoc();
  1477. }
  1478. const Type *CXXCtorInitializer::getBaseClass() const {
  1479. if (isBaseInitializer())
  1480. return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
  1481. else
  1482. return nullptr;
  1483. }
  1484. SourceLocation CXXCtorInitializer::getSourceLocation() const {
  1485. if (isInClassMemberInitializer())
  1486. return getAnyMember()->getLocation();
  1487. if (isAnyMemberInitializer())
  1488. return getMemberLocation();
  1489. if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
  1490. return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
  1491. return SourceLocation();
  1492. }
  1493. SourceRange CXXCtorInitializer::getSourceRange() const {
  1494. if (isInClassMemberInitializer()) {
  1495. FieldDecl *D = getAnyMember();
  1496. if (Expr *I = D->getInClassInitializer())
  1497. return I->getSourceRange();
  1498. return SourceRange();
  1499. }
  1500. return SourceRange(getSourceLocation(), getRParenLoc());
  1501. }
  1502. void CXXConstructorDecl::anchor() { }
  1503. CXXConstructorDecl *
  1504. CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1505. return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
  1506. DeclarationNameInfo(), QualType(),
  1507. nullptr, false, false, false, false);
  1508. }
  1509. CXXConstructorDecl *
  1510. CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
  1511. SourceLocation StartLoc,
  1512. const DeclarationNameInfo &NameInfo,
  1513. QualType T, TypeSourceInfo *TInfo,
  1514. bool isExplicit, bool isInline,
  1515. bool isImplicitlyDeclared, bool isConstexpr) {
  1516. assert(NameInfo.getName().getNameKind()
  1517. == DeclarationName::CXXConstructorName &&
  1518. "Name must refer to a constructor");
  1519. return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
  1520. isExplicit, isInline,
  1521. isImplicitlyDeclared, isConstexpr);
  1522. }
  1523. CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
  1524. return CtorInitializers.get(getASTContext().getExternalSource());
  1525. }
  1526. CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
  1527. assert(isDelegatingConstructor() && "Not a delegating constructor!");
  1528. Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
  1529. if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
  1530. return Construct->getConstructor();
  1531. return nullptr;
  1532. }
  1533. bool CXXConstructorDecl::isDefaultConstructor() const {
  1534. // C++ [class.ctor]p5:
  1535. // A default constructor for a class X is a constructor of class
  1536. // X that can be called without an argument.
  1537. return (getNumParams() == 0) ||
  1538. (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
  1539. }
  1540. bool
  1541. CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
  1542. return isCopyOrMoveConstructor(TypeQuals) &&
  1543. getParamDecl(0)->getType()->isLValueReferenceType();
  1544. }
  1545. bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
  1546. return isCopyOrMoveConstructor(TypeQuals) &&
  1547. getParamDecl(0)->getType()->isRValueReferenceType();
  1548. }
  1549. /// \brief Determine whether this is a copy or move constructor.
  1550. bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
  1551. // C++ [class.copy]p2:
  1552. // A non-template constructor for class X is a copy constructor
  1553. // if its first parameter is of type X&, const X&, volatile X& or
  1554. // const volatile X&, and either there are no other parameters
  1555. // or else all other parameters have default arguments (8.3.6).
  1556. // C++0x [class.copy]p3:
  1557. // A non-template constructor for class X is a move constructor if its
  1558. // first parameter is of type X&&, const X&&, volatile X&&, or
  1559. // const volatile X&&, and either there are no other parameters or else
  1560. // all other parameters have default arguments.
  1561. if ((getNumParams() < 1) ||
  1562. (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
  1563. (getPrimaryTemplate() != nullptr) ||
  1564. (getDescribedFunctionTemplate() != nullptr))
  1565. return false;
  1566. const ParmVarDecl *Param = getParamDecl(0);
  1567. // Do we have a reference type?
  1568. const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
  1569. if (!ParamRefType)
  1570. return false;
  1571. // Is it a reference to our class type?
  1572. ASTContext &Context = getASTContext();
  1573. CanQualType PointeeType
  1574. = Context.getCanonicalType(ParamRefType->getPointeeType());
  1575. CanQualType ClassTy
  1576. = Context.getCanonicalType(Context.getTagDeclType(getParent()));
  1577. if (PointeeType.getUnqualifiedType() != ClassTy)
  1578. return false;
  1579. // FIXME: other qualifiers?
  1580. // We have a copy or move constructor.
  1581. TypeQuals = PointeeType.getCVRQualifiers();
  1582. return true;
  1583. }
  1584. bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
  1585. // C++ [class.conv.ctor]p1:
  1586. // A constructor declared without the function-specifier explicit
  1587. // that can be called with a single parameter specifies a
  1588. // conversion from the type of its first parameter to the type of
  1589. // its class. Such a constructor is called a converting
  1590. // constructor.
  1591. if (isExplicit() && !AllowExplicit)
  1592. return false;
  1593. return (getNumParams() == 0 &&
  1594. getType()->getAs<FunctionProtoType>()->isVariadic()) ||
  1595. (getNumParams() == 1) ||
  1596. (getNumParams() > 1 &&
  1597. (getParamDecl(1)->hasDefaultArg() ||
  1598. getParamDecl(1)->isParameterPack()));
  1599. }
  1600. bool CXXConstructorDecl::isSpecializationCopyingObject() const {
  1601. if ((getNumParams() < 1) ||
  1602. (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
  1603. (getDescribedFunctionTemplate() != nullptr))
  1604. return false;
  1605. const ParmVarDecl *Param = getParamDecl(0);
  1606. ASTContext &Context = getASTContext();
  1607. CanQualType ParamType = Context.getCanonicalType(Param->getType());
  1608. // Is it the same as our our class type?
  1609. CanQualType ClassTy
  1610. = Context.getCanonicalType(Context.getTagDeclType(getParent()));
  1611. if (ParamType.getUnqualifiedType() != ClassTy)
  1612. return false;
  1613. return true;
  1614. }
  1615. const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
  1616. // Hack: we store the inherited constructor in the overridden method table
  1617. method_iterator It = getASTContext().overridden_methods_begin(this);
  1618. if (It == getASTContext().overridden_methods_end(this))
  1619. return nullptr;
  1620. return cast<CXXConstructorDecl>(*It);
  1621. }
  1622. void
  1623. CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
  1624. // Hack: we store the inherited constructor in the overridden method table
  1625. assert(getASTContext().overridden_methods_size(this) == 0 &&
  1626. "Base ctor already set.");
  1627. getASTContext().addOverriddenMethod(this, BaseCtor);
  1628. }
  1629. void CXXDestructorDecl::anchor() { }
  1630. CXXDestructorDecl *
  1631. CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1632. return new (C, ID)
  1633. CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
  1634. QualType(), nullptr, false, false);
  1635. }
  1636. CXXDestructorDecl *
  1637. CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
  1638. SourceLocation StartLoc,
  1639. const DeclarationNameInfo &NameInfo,
  1640. QualType T, TypeSourceInfo *TInfo,
  1641. bool isInline, bool isImplicitlyDeclared) {
  1642. assert(NameInfo.getName().getNameKind()
  1643. == DeclarationName::CXXDestructorName &&
  1644. "Name must refer to a destructor");
  1645. return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
  1646. isInline, isImplicitlyDeclared);
  1647. }
  1648. void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
  1649. auto *First = cast<CXXDestructorDecl>(getFirstDecl());
  1650. if (OD && !First->OperatorDelete) {
  1651. First->OperatorDelete = OD;
  1652. if (auto *L = getASTMutationListener())
  1653. L->ResolvedOperatorDelete(First, OD);
  1654. }
  1655. }
  1656. void CXXConversionDecl::anchor() { }
  1657. CXXConversionDecl *
  1658. CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1659. return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
  1660. DeclarationNameInfo(), QualType(),
  1661. nullptr, false, false, false,
  1662. SourceLocation());
  1663. }
  1664. CXXConversionDecl *
  1665. CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
  1666. SourceLocation StartLoc,
  1667. const DeclarationNameInfo &NameInfo,
  1668. QualType T, TypeSourceInfo *TInfo,
  1669. bool isInline, bool isExplicit,
  1670. bool isConstexpr, SourceLocation EndLocation) {
  1671. assert(NameInfo.getName().getNameKind()
  1672. == DeclarationName::CXXConversionFunctionName &&
  1673. "Name must refer to a conversion function");
  1674. return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
  1675. isInline, isExplicit, isConstexpr,
  1676. EndLocation);
  1677. }
  1678. bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
  1679. return isImplicit() && getParent()->isLambda() &&
  1680. getConversionType()->isBlockPointerType();
  1681. }
  1682. void LinkageSpecDecl::anchor() { }
  1683. LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
  1684. DeclContext *DC,
  1685. SourceLocation ExternLoc,
  1686. SourceLocation LangLoc,
  1687. LanguageIDs Lang,
  1688. bool HasBraces) {
  1689. return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
  1690. }
  1691. LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
  1692. unsigned ID) {
  1693. return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
  1694. SourceLocation(), lang_c, false);
  1695. }
  1696. void UsingDirectiveDecl::anchor() { }
  1697. UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
  1698. SourceLocation L,
  1699. SourceLocation NamespaceLoc,
  1700. NestedNameSpecifierLoc QualifierLoc,
  1701. SourceLocation IdentLoc,
  1702. NamedDecl *Used,
  1703. DeclContext *CommonAncestor) {
  1704. if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
  1705. Used = NS->getOriginalNamespace();
  1706. return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
  1707. IdentLoc, Used, CommonAncestor);
  1708. }
  1709. UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
  1710. unsigned ID) {
  1711. return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
  1712. SourceLocation(),
  1713. NestedNameSpecifierLoc(),
  1714. SourceLocation(), nullptr, nullptr);
  1715. }
  1716. NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
  1717. if (NamespaceAliasDecl *NA =
  1718. dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
  1719. return NA->getNamespace();
  1720. return cast_or_null<NamespaceDecl>(NominatedNamespace);
  1721. }
  1722. NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
  1723. SourceLocation StartLoc, SourceLocation IdLoc,
  1724. IdentifierInfo *Id, NamespaceDecl *PrevDecl)
  1725. : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
  1726. redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
  1727. AnonOrFirstNamespaceAndInline(nullptr, Inline) {
  1728. setPreviousDecl(PrevDecl);
  1729. if (PrevDecl)
  1730. AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
  1731. }
  1732. NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
  1733. bool Inline, SourceLocation StartLoc,
  1734. SourceLocation IdLoc, IdentifierInfo *Id,
  1735. NamespaceDecl *PrevDecl) {
  1736. return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
  1737. PrevDecl);
  1738. }
  1739. NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1740. return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
  1741. SourceLocation(), nullptr, nullptr);
  1742. }
  1743. NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
  1744. return getNextRedeclaration();
  1745. }
  1746. NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
  1747. return getPreviousDecl();
  1748. }
  1749. NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
  1750. return getMostRecentDecl();
  1751. }
  1752. void NamespaceAliasDecl::anchor() { }
  1753. NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
  1754. return getNextRedeclaration();
  1755. }
  1756. NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
  1757. return getPreviousDecl();
  1758. }
  1759. NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
  1760. return getMostRecentDecl();
  1761. }
  1762. NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
  1763. SourceLocation UsingLoc,
  1764. SourceLocation AliasLoc,
  1765. IdentifierInfo *Alias,
  1766. NestedNameSpecifierLoc QualifierLoc,
  1767. SourceLocation IdentLoc,
  1768. NamedDecl *Namespace) {
  1769. // FIXME: Preserve the aliased namespace as written.
  1770. if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
  1771. Namespace = NS->getOriginalNamespace();
  1772. return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
  1773. QualifierLoc, IdentLoc, Namespace);
  1774. }
  1775. NamespaceAliasDecl *
  1776. NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1777. return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
  1778. SourceLocation(), nullptr,
  1779. NestedNameSpecifierLoc(),
  1780. SourceLocation(), nullptr);
  1781. }
  1782. void UsingShadowDecl::anchor() { }
  1783. UsingShadowDecl *
  1784. UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1785. return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
  1786. nullptr, nullptr);
  1787. }
  1788. UsingDecl *UsingShadowDecl::getUsingDecl() const {
  1789. const UsingShadowDecl *Shadow = this;
  1790. while (const UsingShadowDecl *NextShadow =
  1791. dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
  1792. Shadow = NextShadow;
  1793. return cast<UsingDecl>(Shadow->UsingOrNextShadow);
  1794. }
  1795. void UsingDecl::anchor() { }
  1796. void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
  1797. assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
  1798. "declaration already in set");
  1799. assert(S->getUsingDecl() == this);
  1800. if (FirstUsingShadow.getPointer())
  1801. S->UsingOrNextShadow = FirstUsingShadow.getPointer();
  1802. FirstUsingShadow.setPointer(S);
  1803. }
  1804. void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
  1805. assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
  1806. "declaration not in set");
  1807. assert(S->getUsingDecl() == this);
  1808. // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
  1809. if (FirstUsingShadow.getPointer() == S) {
  1810. FirstUsingShadow.setPointer(
  1811. dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
  1812. S->UsingOrNextShadow = this;
  1813. return;
  1814. }
  1815. UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
  1816. while (Prev->UsingOrNextShadow != S)
  1817. Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
  1818. Prev->UsingOrNextShadow = S->UsingOrNextShadow;
  1819. S->UsingOrNextShadow = this;
  1820. }
  1821. UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
  1822. NestedNameSpecifierLoc QualifierLoc,
  1823. const DeclarationNameInfo &NameInfo,
  1824. bool HasTypename) {
  1825. return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
  1826. }
  1827. UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1828. return new (C, ID) UsingDecl(nullptr, SourceLocation(),
  1829. NestedNameSpecifierLoc(), DeclarationNameInfo(),
  1830. false);
  1831. }
  1832. SourceRange UsingDecl::getSourceRange() const {
  1833. SourceLocation Begin = isAccessDeclaration()
  1834. ? getQualifierLoc().getBeginLoc() : UsingLocation;
  1835. return SourceRange(Begin, getNameInfo().getEndLoc());
  1836. }
  1837. void UnresolvedUsingValueDecl::anchor() { }
  1838. UnresolvedUsingValueDecl *
  1839. UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
  1840. SourceLocation UsingLoc,
  1841. NestedNameSpecifierLoc QualifierLoc,
  1842. const DeclarationNameInfo &NameInfo) {
  1843. return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
  1844. QualifierLoc, NameInfo);
  1845. }
  1846. UnresolvedUsingValueDecl *
  1847. UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1848. return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
  1849. SourceLocation(),
  1850. NestedNameSpecifierLoc(),
  1851. DeclarationNameInfo());
  1852. }
  1853. SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
  1854. SourceLocation Begin = isAccessDeclaration()
  1855. ? getQualifierLoc().getBeginLoc() : UsingLocation;
  1856. return SourceRange(Begin, getNameInfo().getEndLoc());
  1857. }
  1858. void UnresolvedUsingTypenameDecl::anchor() { }
  1859. UnresolvedUsingTypenameDecl *
  1860. UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
  1861. SourceLocation UsingLoc,
  1862. SourceLocation TypenameLoc,
  1863. NestedNameSpecifierLoc QualifierLoc,
  1864. SourceLocation TargetNameLoc,
  1865. DeclarationName TargetName) {
  1866. return new (C, DC) UnresolvedUsingTypenameDecl(
  1867. DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
  1868. TargetName.getAsIdentifierInfo());
  1869. }
  1870. UnresolvedUsingTypenameDecl *
  1871. UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1872. return new (C, ID) UnresolvedUsingTypenameDecl(
  1873. nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
  1874. SourceLocation(), nullptr);
  1875. }
  1876. void StaticAssertDecl::anchor() { }
  1877. StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
  1878. SourceLocation StaticAssertLoc,
  1879. Expr *AssertExpr,
  1880. StringLiteral *Message,
  1881. SourceLocation RParenLoc,
  1882. bool Failed) {
  1883. return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
  1884. RParenLoc, Failed);
  1885. }
  1886. StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
  1887. unsigned ID) {
  1888. return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
  1889. nullptr, SourceLocation(), false);
  1890. }
  1891. MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
  1892. SourceLocation L, DeclarationName N,
  1893. QualType T, TypeSourceInfo *TInfo,
  1894. SourceLocation StartL,
  1895. IdentifierInfo *Getter,
  1896. IdentifierInfo *Setter) {
  1897. return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
  1898. }
  1899. MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
  1900. unsigned ID) {
  1901. return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
  1902. DeclarationName(), QualType(), nullptr,
  1903. SourceLocation(), nullptr, nullptr);
  1904. }
  1905. static const char *getAccessName(AccessSpecifier AS) {
  1906. switch (AS) {
  1907. case AS_none:
  1908. llvm_unreachable("Invalid access specifier!");
  1909. case AS_public:
  1910. return "public";
  1911. case AS_private:
  1912. return "private";
  1913. case AS_protected:
  1914. return "protected";
  1915. }
  1916. llvm_unreachable("Invalid access specifier!");
  1917. }
  1918. const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
  1919. AccessSpecifier AS) {
  1920. return DB << getAccessName(AS);
  1921. }
  1922. const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
  1923. AccessSpecifier AS) {
  1924. return DB << getAccessName(AS);
  1925. }