DeclCXX.cpp 80 KB

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