DeclObjC.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. //===- DeclObjC.cpp - ObjC Declaration AST Node Implementation ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the Objective-C related Decl classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/DeclObjC.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ASTMutationListener.h"
  15. #include "clang/AST/Attr.h"
  16. #include "clang/AST/Decl.h"
  17. #include "clang/AST/DeclBase.h"
  18. #include "clang/AST/Stmt.h"
  19. #include "clang/AST/Type.h"
  20. #include "clang/AST/TypeLoc.h"
  21. #include "clang/Basic/IdentifierTable.h"
  22. #include "clang/Basic/LLVM.h"
  23. #include "clang/Basic/LangOptions.h"
  24. #include "clang/Basic/SourceLocation.h"
  25. #include "llvm/ADT/None.h"
  26. #include "llvm/ADT/SmallString.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include <algorithm>
  32. #include <cassert>
  33. #include <cstdint>
  34. #include <cstring>
  35. #include <queue>
  36. #include <utility>
  37. using namespace clang;
  38. //===----------------------------------------------------------------------===//
  39. // ObjCListBase
  40. //===----------------------------------------------------------------------===//
  41. void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
  42. List = nullptr;
  43. if (Elts == 0) return; // Setting to an empty list is a noop.
  44. List = new (Ctx) void*[Elts];
  45. NumElts = Elts;
  46. memcpy(List, InList, sizeof(void*)*Elts);
  47. }
  48. void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
  49. const SourceLocation *Locs, ASTContext &Ctx) {
  50. if (Elts == 0)
  51. return;
  52. Locations = new (Ctx) SourceLocation[Elts];
  53. memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
  54. set(InList, Elts, Ctx);
  55. }
  56. //===----------------------------------------------------------------------===//
  57. // ObjCInterfaceDecl
  58. //===----------------------------------------------------------------------===//
  59. ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
  60. IdentifierInfo *Id, SourceLocation nameLoc,
  61. SourceLocation atStartLoc)
  62. : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
  63. setAtStartLoc(atStartLoc);
  64. }
  65. void ObjCContainerDecl::anchor() {}
  66. /// getIvarDecl - This method looks up an ivar in this ContextDecl.
  67. ///
  68. ObjCIvarDecl *
  69. ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
  70. lookup_result R = lookup(Id);
  71. for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
  72. Ivar != IvarEnd; ++Ivar) {
  73. if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
  74. return ivar;
  75. }
  76. return nullptr;
  77. }
  78. // Get the local instance/class method declared in this interface.
  79. ObjCMethodDecl *
  80. ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
  81. bool AllowHidden) const {
  82. // If this context is a hidden protocol definition, don't find any
  83. // methods there.
  84. if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
  85. if (const ObjCProtocolDecl *Def = Proto->getDefinition())
  86. if (!Def->isUnconditionallyVisible() && !AllowHidden)
  87. return nullptr;
  88. }
  89. // Since instance & class methods can have the same name, the loop below
  90. // ensures we get the correct method.
  91. //
  92. // @interface Whatever
  93. // - (int) class_method;
  94. // + (float) class_method;
  95. // @end
  96. lookup_result R = lookup(Sel);
  97. for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
  98. Meth != MethEnd; ++Meth) {
  99. auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
  100. if (MD && MD->isInstanceMethod() == isInstance)
  101. return MD;
  102. }
  103. return nullptr;
  104. }
  105. /// This routine returns 'true' if a user declared setter method was
  106. /// found in the class, its protocols, its super classes or categories.
  107. /// It also returns 'true' if one of its categories has declared a 'readwrite'
  108. /// property. This is because, user must provide a setter method for the
  109. /// category's 'readwrite' property.
  110. bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
  111. const ObjCPropertyDecl *Property) const {
  112. Selector Sel = Property->getSetterName();
  113. lookup_result R = lookup(Sel);
  114. for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
  115. Meth != MethEnd; ++Meth) {
  116. auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
  117. if (MD && MD->isInstanceMethod() && !MD->isImplicit())
  118. return true;
  119. }
  120. if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
  121. // Also look into categories, including class extensions, looking
  122. // for a user declared instance method.
  123. for (const auto *Cat : ID->visible_categories()) {
  124. if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
  125. if (!MD->isImplicit())
  126. return true;
  127. if (Cat->IsClassExtension())
  128. continue;
  129. // Also search through the categories looking for a 'readwrite'
  130. // declaration of this property. If one found, presumably a setter will
  131. // be provided (properties declared in categories will not get
  132. // auto-synthesized).
  133. for (const auto *P : Cat->properties())
  134. if (P->getIdentifier() == Property->getIdentifier()) {
  135. if (P->getPropertyAttributes() &
  136. ObjCPropertyAttribute::kind_readwrite)
  137. return true;
  138. break;
  139. }
  140. }
  141. // Also look into protocols, for a user declared instance method.
  142. for (const auto *Proto : ID->all_referenced_protocols())
  143. if (Proto->HasUserDeclaredSetterMethod(Property))
  144. return true;
  145. // And in its super class.
  146. ObjCInterfaceDecl *OSC = ID->getSuperClass();
  147. while (OSC) {
  148. if (OSC->HasUserDeclaredSetterMethod(Property))
  149. return true;
  150. OSC = OSC->getSuperClass();
  151. }
  152. }
  153. if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
  154. for (const auto *PI : PD->protocols())
  155. if (PI->HasUserDeclaredSetterMethod(Property))
  156. return true;
  157. return false;
  158. }
  159. ObjCPropertyDecl *
  160. ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
  161. const IdentifierInfo *propertyID,
  162. ObjCPropertyQueryKind queryKind) {
  163. // If this context is a hidden protocol definition, don't find any
  164. // property.
  165. if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
  166. if (const ObjCProtocolDecl *Def = Proto->getDefinition())
  167. if (!Def->isUnconditionallyVisible())
  168. return nullptr;
  169. }
  170. // If context is class, then lookup property in its visible extensions.
  171. // This comes before property is looked up in primary class.
  172. if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
  173. for (const auto *Ext : IDecl->visible_extensions())
  174. if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
  175. propertyID,
  176. queryKind))
  177. return PD;
  178. }
  179. DeclContext::lookup_result R = DC->lookup(propertyID);
  180. ObjCPropertyDecl *classProp = nullptr;
  181. for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
  182. ++I)
  183. if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
  184. // If queryKind is unknown, we return the instance property if one
  185. // exists; otherwise we return the class property.
  186. if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
  187. !PD->isClassProperty()) ||
  188. (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
  189. PD->isClassProperty()) ||
  190. (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
  191. !PD->isClassProperty()))
  192. return PD;
  193. if (PD->isClassProperty())
  194. classProp = PD;
  195. }
  196. if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
  197. // We can't find the instance property, return the class property.
  198. return classProp;
  199. return nullptr;
  200. }
  201. IdentifierInfo *
  202. ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
  203. SmallString<128> ivarName;
  204. {
  205. llvm::raw_svector_ostream os(ivarName);
  206. os << '_' << getIdentifier()->getName();
  207. }
  208. return &Ctx.Idents.get(ivarName.str());
  209. }
  210. ObjCPropertyDecl *ObjCContainerDecl::getProperty(const IdentifierInfo *Id,
  211. bool IsInstance) const {
  212. for (auto *LookupResult : lookup(Id)) {
  213. if (auto *Prop = dyn_cast<ObjCPropertyDecl>(LookupResult)) {
  214. if (Prop->isInstanceProperty() == IsInstance) {
  215. return Prop;
  216. }
  217. }
  218. }
  219. return nullptr;
  220. }
  221. /// FindPropertyDeclaration - Finds declaration of the property given its name
  222. /// in 'PropertyId' and returns it. It returns 0, if not found.
  223. ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
  224. const IdentifierInfo *PropertyId,
  225. ObjCPropertyQueryKind QueryKind) const {
  226. // Don't find properties within hidden protocol definitions.
  227. if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
  228. if (const ObjCProtocolDecl *Def = Proto->getDefinition())
  229. if (!Def->isUnconditionallyVisible())
  230. return nullptr;
  231. }
  232. // Search the extensions of a class first; they override what's in
  233. // the class itself.
  234. if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
  235. for (const auto *Ext : ClassDecl->visible_extensions()) {
  236. if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
  237. return P;
  238. }
  239. }
  240. if (ObjCPropertyDecl *PD =
  241. ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
  242. QueryKind))
  243. return PD;
  244. switch (getKind()) {
  245. default:
  246. break;
  247. case Decl::ObjCProtocol: {
  248. const auto *PID = cast<ObjCProtocolDecl>(this);
  249. for (const auto *I : PID->protocols())
  250. if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
  251. QueryKind))
  252. return P;
  253. break;
  254. }
  255. case Decl::ObjCInterface: {
  256. const auto *OID = cast<ObjCInterfaceDecl>(this);
  257. // Look through categories (but not extensions; they were handled above).
  258. for (const auto *Cat : OID->visible_categories()) {
  259. if (!Cat->IsClassExtension())
  260. if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
  261. PropertyId, QueryKind))
  262. return P;
  263. }
  264. // Look through protocols.
  265. for (const auto *I : OID->all_referenced_protocols())
  266. if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
  267. QueryKind))
  268. return P;
  269. // Finally, check the super class.
  270. if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
  271. return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
  272. break;
  273. }
  274. case Decl::ObjCCategory: {
  275. const auto *OCD = cast<ObjCCategoryDecl>(this);
  276. // Look through protocols.
  277. if (!OCD->IsClassExtension())
  278. for (const auto *I : OCD->protocols())
  279. if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
  280. QueryKind))
  281. return P;
  282. break;
  283. }
  284. }
  285. return nullptr;
  286. }
  287. void ObjCInterfaceDecl::anchor() {}
  288. ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
  289. // If this particular declaration has a type parameter list, return it.
  290. if (ObjCTypeParamList *written = getTypeParamListAsWritten())
  291. return written;
  292. // If there is a definition, return its type parameter list.
  293. if (const ObjCInterfaceDecl *def = getDefinition())
  294. return def->getTypeParamListAsWritten();
  295. // Otherwise, look at previous declarations to determine whether any
  296. // of them has a type parameter list, skipping over those
  297. // declarations that do not.
  298. for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
  299. decl = decl->getPreviousDecl()) {
  300. if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
  301. return written;
  302. }
  303. return nullptr;
  304. }
  305. void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
  306. TypeParamList = TPL;
  307. if (!TPL)
  308. return;
  309. // Set the declaration context of each of the type parameters.
  310. for (auto *typeParam : *TypeParamList)
  311. typeParam->setDeclContext(this);
  312. }
  313. ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
  314. // FIXME: Should make sure no callers ever do this.
  315. if (!hasDefinition())
  316. return nullptr;
  317. if (data().ExternallyCompleted)
  318. LoadExternalDefinition();
  319. if (const ObjCObjectType *superType = getSuperClassType()) {
  320. if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
  321. if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
  322. return superDef;
  323. return superDecl;
  324. }
  325. }
  326. return nullptr;
  327. }
  328. SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
  329. if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
  330. return superTInfo->getTypeLoc().getBeginLoc();
  331. return SourceLocation();
  332. }
  333. /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
  334. /// with name 'PropertyId' in the primary class; including those in protocols
  335. /// (direct or indirect) used by the primary class.
  336. ObjCPropertyDecl *
  337. ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
  338. IdentifierInfo *PropertyId,
  339. ObjCPropertyQueryKind QueryKind) const {
  340. // FIXME: Should make sure no callers ever do this.
  341. if (!hasDefinition())
  342. return nullptr;
  343. if (data().ExternallyCompleted)
  344. LoadExternalDefinition();
  345. if (ObjCPropertyDecl *PD =
  346. ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
  347. QueryKind))
  348. return PD;
  349. // Look through protocols.
  350. for (const auto *I : all_referenced_protocols())
  351. if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
  352. QueryKind))
  353. return P;
  354. return nullptr;
  355. }
  356. void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
  357. PropertyDeclOrder &PO) const {
  358. for (auto *Prop : properties()) {
  359. PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
  360. PO.push_back(Prop);
  361. }
  362. for (const auto *Ext : known_extensions()) {
  363. const ObjCCategoryDecl *ClassExt = Ext;
  364. for (auto *Prop : ClassExt->properties()) {
  365. PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
  366. PO.push_back(Prop);
  367. }
  368. }
  369. for (const auto *PI : all_referenced_protocols())
  370. PI->collectPropertiesToImplement(PM, PO);
  371. // Note, the properties declared only in class extensions are still copied
  372. // into the main @interface's property list, and therefore we don't
  373. // explicitly, have to search class extension properties.
  374. }
  375. bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
  376. const ObjCInterfaceDecl *Class = this;
  377. while (Class) {
  378. if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
  379. return true;
  380. Class = Class->getSuperClass();
  381. }
  382. return false;
  383. }
  384. const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
  385. const ObjCInterfaceDecl *Class = this;
  386. while (Class) {
  387. if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
  388. return Class;
  389. Class = Class->getSuperClass();
  390. }
  391. return nullptr;
  392. }
  393. void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
  394. ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
  395. ASTContext &C) {
  396. if (data().ExternallyCompleted)
  397. LoadExternalDefinition();
  398. if (data().AllReferencedProtocols.empty() &&
  399. data().ReferencedProtocols.empty()) {
  400. data().AllReferencedProtocols.set(ExtList, ExtNum, C);
  401. return;
  402. }
  403. // Check for duplicate protocol in class's protocol list.
  404. // This is O(n*m). But it is extremely rare and number of protocols in
  405. // class or its extension are very few.
  406. SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
  407. for (unsigned i = 0; i < ExtNum; i++) {
  408. bool protocolExists = false;
  409. ObjCProtocolDecl *ProtoInExtension = ExtList[i];
  410. for (auto *Proto : all_referenced_protocols()) {
  411. if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
  412. protocolExists = true;
  413. break;
  414. }
  415. }
  416. // Do we want to warn on a protocol in extension class which
  417. // already exist in the class? Probably not.
  418. if (!protocolExists)
  419. ProtocolRefs.push_back(ProtoInExtension);
  420. }
  421. if (ProtocolRefs.empty())
  422. return;
  423. // Merge ProtocolRefs into class's protocol list;
  424. ProtocolRefs.append(all_referenced_protocol_begin(),
  425. all_referenced_protocol_end());
  426. data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
  427. }
  428. const ObjCInterfaceDecl *
  429. ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
  430. const ObjCInterfaceDecl *IFace = this;
  431. while (IFace) {
  432. if (IFace->hasDesignatedInitializers())
  433. return IFace;
  434. if (!IFace->inheritsDesignatedInitializers())
  435. break;
  436. IFace = IFace->getSuperClass();
  437. }
  438. return nullptr;
  439. }
  440. static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
  441. for (const auto *MD : D->instance_methods()) {
  442. if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
  443. return true;
  444. }
  445. for (const auto *Ext : D->visible_extensions()) {
  446. for (const auto *MD : Ext->instance_methods()) {
  447. if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
  448. return true;
  449. }
  450. }
  451. if (const auto *ImplD = D->getImplementation()) {
  452. for (const auto *MD : ImplD->instance_methods()) {
  453. if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
  460. switch (data().InheritedDesignatedInitializers) {
  461. case DefinitionData::IDI_Inherited:
  462. return true;
  463. case DefinitionData::IDI_NotInherited:
  464. return false;
  465. case DefinitionData::IDI_Unknown:
  466. // If the class introduced initializers we conservatively assume that we
  467. // don't know if any of them is a designated initializer to avoid possible
  468. // misleading warnings.
  469. if (isIntroducingInitializers(this)) {
  470. data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
  471. } else {
  472. if (auto SuperD = getSuperClass()) {
  473. data().InheritedDesignatedInitializers =
  474. SuperD->declaresOrInheritsDesignatedInitializers() ?
  475. DefinitionData::IDI_Inherited :
  476. DefinitionData::IDI_NotInherited;
  477. } else {
  478. data().InheritedDesignatedInitializers =
  479. DefinitionData::IDI_NotInherited;
  480. }
  481. }
  482. assert(data().InheritedDesignatedInitializers
  483. != DefinitionData::IDI_Unknown);
  484. return data().InheritedDesignatedInitializers ==
  485. DefinitionData::IDI_Inherited;
  486. }
  487. llvm_unreachable("unexpected InheritedDesignatedInitializers value");
  488. }
  489. void ObjCInterfaceDecl::getDesignatedInitializers(
  490. llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
  491. // Check for a complete definition and recover if not so.
  492. if (!isThisDeclarationADefinition())
  493. return;
  494. if (data().ExternallyCompleted)
  495. LoadExternalDefinition();
  496. const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
  497. if (!IFace)
  498. return;
  499. for (const auto *MD : IFace->instance_methods())
  500. if (MD->isThisDeclarationADesignatedInitializer())
  501. Methods.push_back(MD);
  502. for (const auto *Ext : IFace->visible_extensions()) {
  503. for (const auto *MD : Ext->instance_methods())
  504. if (MD->isThisDeclarationADesignatedInitializer())
  505. Methods.push_back(MD);
  506. }
  507. }
  508. bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
  509. const ObjCMethodDecl **InitMethod) const {
  510. bool HasCompleteDef = isThisDeclarationADefinition();
  511. // During deserialization the data record for the ObjCInterfaceDecl could
  512. // be made invariant by reusing the canonical decl. Take this into account
  513. // when checking for the complete definition.
  514. if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
  515. getCanonicalDecl()->getDefinition() == getDefinition())
  516. HasCompleteDef = true;
  517. // Check for a complete definition and recover if not so.
  518. if (!HasCompleteDef)
  519. return false;
  520. if (data().ExternallyCompleted)
  521. LoadExternalDefinition();
  522. const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
  523. if (!IFace)
  524. return false;
  525. if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
  526. if (MD->isThisDeclarationADesignatedInitializer()) {
  527. if (InitMethod)
  528. *InitMethod = MD;
  529. return true;
  530. }
  531. }
  532. for (const auto *Ext : IFace->visible_extensions()) {
  533. if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
  534. if (MD->isThisDeclarationADesignatedInitializer()) {
  535. if (InitMethod)
  536. *InitMethod = MD;
  537. return true;
  538. }
  539. }
  540. }
  541. return false;
  542. }
  543. void ObjCInterfaceDecl::allocateDefinitionData() {
  544. assert(!hasDefinition() && "ObjC class already has a definition");
  545. Data.setPointer(new (getASTContext()) DefinitionData());
  546. Data.getPointer()->Definition = this;
  547. }
  548. void ObjCInterfaceDecl::startDefinition() {
  549. allocateDefinitionData();
  550. // Update all of the declarations with a pointer to the definition.
  551. for (auto *RD : redecls()) {
  552. if (RD != this)
  553. RD->Data = Data;
  554. }
  555. }
  556. ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
  557. ObjCInterfaceDecl *&clsDeclared) {
  558. // FIXME: Should make sure no callers ever do this.
  559. if (!hasDefinition())
  560. return nullptr;
  561. if (data().ExternallyCompleted)
  562. LoadExternalDefinition();
  563. ObjCInterfaceDecl* ClassDecl = this;
  564. while (ClassDecl != nullptr) {
  565. if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
  566. clsDeclared = ClassDecl;
  567. return I;
  568. }
  569. for (const auto *Ext : ClassDecl->visible_extensions()) {
  570. if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
  571. clsDeclared = ClassDecl;
  572. return I;
  573. }
  574. }
  575. ClassDecl = ClassDecl->getSuperClass();
  576. }
  577. return nullptr;
  578. }
  579. /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
  580. /// class whose name is passed as argument. If it is not one of the super classes
  581. /// the it returns NULL.
  582. ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
  583. const IdentifierInfo*ICName) {
  584. // FIXME: Should make sure no callers ever do this.
  585. if (!hasDefinition())
  586. return nullptr;
  587. if (data().ExternallyCompleted)
  588. LoadExternalDefinition();
  589. ObjCInterfaceDecl* ClassDecl = this;
  590. while (ClassDecl != nullptr) {
  591. if (ClassDecl->getIdentifier() == ICName)
  592. return ClassDecl;
  593. ClassDecl = ClassDecl->getSuperClass();
  594. }
  595. return nullptr;
  596. }
  597. ObjCProtocolDecl *
  598. ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
  599. for (auto *P : all_referenced_protocols())
  600. if (P->lookupProtocolNamed(Name))
  601. return P;
  602. ObjCInterfaceDecl *SuperClass = getSuperClass();
  603. return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
  604. }
  605. /// lookupMethod - This method returns an instance/class method by looking in
  606. /// the class, its categories, and its super classes (using a linear search).
  607. /// When argument category "C" is specified, any implicit method found
  608. /// in this category is ignored.
  609. ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
  610. bool isInstance,
  611. bool shallowCategoryLookup,
  612. bool followSuper,
  613. const ObjCCategoryDecl *C) const
  614. {
  615. // FIXME: Should make sure no callers ever do this.
  616. if (!hasDefinition())
  617. return nullptr;
  618. const ObjCInterfaceDecl* ClassDecl = this;
  619. ObjCMethodDecl *MethodDecl = nullptr;
  620. if (data().ExternallyCompleted)
  621. LoadExternalDefinition();
  622. while (ClassDecl) {
  623. // 1. Look through primary class.
  624. if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
  625. return MethodDecl;
  626. // 2. Didn't find one yet - now look through categories.
  627. for (const auto *Cat : ClassDecl->visible_categories())
  628. if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
  629. if (C != Cat || !MethodDecl->isImplicit())
  630. return MethodDecl;
  631. // 3. Didn't find one yet - look through primary class's protocols.
  632. for (const auto *I : ClassDecl->protocols())
  633. if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
  634. return MethodDecl;
  635. // 4. Didn't find one yet - now look through categories' protocols
  636. if (!shallowCategoryLookup)
  637. for (const auto *Cat : ClassDecl->visible_categories()) {
  638. // Didn't find one yet - look through protocols.
  639. const ObjCList<ObjCProtocolDecl> &Protocols =
  640. Cat->getReferencedProtocols();
  641. for (auto *Protocol : Protocols)
  642. if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
  643. if (C != Cat || !MethodDecl->isImplicit())
  644. return MethodDecl;
  645. }
  646. if (!followSuper)
  647. return nullptr;
  648. // 5. Get to the super class (if any).
  649. ClassDecl = ClassDecl->getSuperClass();
  650. }
  651. return nullptr;
  652. }
  653. // Will search "local" class/category implementations for a method decl.
  654. // If failed, then we search in class's root for an instance method.
  655. // Returns 0 if no method is found.
  656. ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
  657. const Selector &Sel,
  658. bool Instance) const {
  659. // FIXME: Should make sure no callers ever do this.
  660. if (!hasDefinition())
  661. return nullptr;
  662. if (data().ExternallyCompleted)
  663. LoadExternalDefinition();
  664. ObjCMethodDecl *Method = nullptr;
  665. if (ObjCImplementationDecl *ImpDecl = getImplementation())
  666. Method = Instance ? ImpDecl->getInstanceMethod(Sel)
  667. : ImpDecl->getClassMethod(Sel);
  668. // Look through local category implementations associated with the class.
  669. if (!Method)
  670. Method = getCategoryMethod(Sel, Instance);
  671. // Before we give up, check if the selector is an instance method.
  672. // But only in the root. This matches gcc's behavior and what the
  673. // runtime expects.
  674. if (!Instance && !Method && !getSuperClass()) {
  675. Method = lookupInstanceMethod(Sel);
  676. // Look through local category implementations associated
  677. // with the root class.
  678. if (!Method)
  679. Method = lookupPrivateMethod(Sel, true);
  680. }
  681. if (!Method && getSuperClass())
  682. return getSuperClass()->lookupPrivateMethod(Sel, Instance);
  683. return Method;
  684. }
  685. //===----------------------------------------------------------------------===//
  686. // ObjCMethodDecl
  687. //===----------------------------------------------------------------------===//
  688. ObjCMethodDecl::ObjCMethodDecl(
  689. SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
  690. QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
  691. bool isInstance, bool isVariadic, bool isPropertyAccessor,
  692. bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
  693. ImplementationControl impControl, bool HasRelatedResultType)
  694. : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
  695. DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
  696. DeclEndLoc(endLoc) {
  697. // Initialized the bits stored in DeclContext.
  698. ObjCMethodDeclBits.Family =
  699. static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
  700. setInstanceMethod(isInstance);
  701. setVariadic(isVariadic);
  702. setPropertyAccessor(isPropertyAccessor);
  703. setSynthesizedAccessorStub(isSynthesizedAccessorStub);
  704. setDefined(isDefined);
  705. setIsRedeclaration(false);
  706. setHasRedeclaration(false);
  707. setDeclImplementation(impControl);
  708. setObjCDeclQualifier(OBJC_TQ_None);
  709. setRelatedResultType(HasRelatedResultType);
  710. setSelLocsKind(SelLoc_StandardNoSpace);
  711. setOverriding(false);
  712. setHasSkippedBody(false);
  713. setImplicit(isImplicitlyDeclared);
  714. }
  715. ObjCMethodDecl *ObjCMethodDecl::Create(
  716. ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
  717. Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
  718. DeclContext *contextDecl, bool isInstance, bool isVariadic,
  719. bool isPropertyAccessor, bool isSynthesizedAccessorStub,
  720. bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
  721. bool HasRelatedResultType) {
  722. return new (C, contextDecl) ObjCMethodDecl(
  723. beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
  724. isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
  725. isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
  726. }
  727. ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  728. return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
  729. Selector(), QualType(), nullptr, nullptr);
  730. }
  731. bool ObjCMethodDecl::isDirectMethod() const {
  732. return hasAttr<ObjCDirectAttr>() &&
  733. !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting;
  734. }
  735. bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
  736. return getMethodFamily() == OMF_init &&
  737. hasAttr<ObjCDesignatedInitializerAttr>();
  738. }
  739. bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
  740. if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
  741. return PD->getIdentifier() == Ctx.getNSObjectName();
  742. if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
  743. return ID->getIdentifier() == Ctx.getNSObjectName();
  744. return false;
  745. }
  746. bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
  747. const ObjCMethodDecl **InitMethod) const {
  748. if (getMethodFamily() != OMF_init)
  749. return false;
  750. const DeclContext *DC = getDeclContext();
  751. if (isa<ObjCProtocolDecl>(DC))
  752. return false;
  753. if (const ObjCInterfaceDecl *ID = getClassInterface())
  754. return ID->isDesignatedInitializer(getSelector(), InitMethod);
  755. return false;
  756. }
  757. bool ObjCMethodDecl::hasParamDestroyedInCallee() const {
  758. for (auto param : parameters()) {
  759. if (param->isDestroyedInCallee())
  760. return true;
  761. }
  762. return false;
  763. }
  764. Stmt *ObjCMethodDecl::getBody() const {
  765. return Body.get(getASTContext().getExternalSource());
  766. }
  767. void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
  768. assert(PrevMethod);
  769. getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
  770. setIsRedeclaration(true);
  771. PrevMethod->setHasRedeclaration(true);
  772. }
  773. void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
  774. ArrayRef<ParmVarDecl*> Params,
  775. ArrayRef<SourceLocation> SelLocs) {
  776. ParamsAndSelLocs = nullptr;
  777. NumParams = Params.size();
  778. if (Params.empty() && SelLocs.empty())
  779. return;
  780. static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
  781. "Alignment not sufficient for SourceLocation");
  782. unsigned Size = sizeof(ParmVarDecl *) * NumParams +
  783. sizeof(SourceLocation) * SelLocs.size();
  784. ParamsAndSelLocs = C.Allocate(Size);
  785. std::copy(Params.begin(), Params.end(), getParams());
  786. std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
  787. }
  788. void ObjCMethodDecl::getSelectorLocs(
  789. SmallVectorImpl<SourceLocation> &SelLocs) const {
  790. for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
  791. SelLocs.push_back(getSelectorLoc(i));
  792. }
  793. void ObjCMethodDecl::setMethodParams(ASTContext &C,
  794. ArrayRef<ParmVarDecl*> Params,
  795. ArrayRef<SourceLocation> SelLocs) {
  796. assert((!SelLocs.empty() || isImplicit()) &&
  797. "No selector locs for non-implicit method");
  798. if (isImplicit())
  799. return setParamsAndSelLocs(C, Params, llvm::None);
  800. setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
  801. DeclEndLoc));
  802. if (getSelLocsKind() != SelLoc_NonStandard)
  803. return setParamsAndSelLocs(C, Params, llvm::None);
  804. setParamsAndSelLocs(C, Params, SelLocs);
  805. }
  806. /// A definition will return its interface declaration.
  807. /// An interface declaration will return its definition.
  808. /// Otherwise it will return itself.
  809. ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
  810. ASTContext &Ctx = getASTContext();
  811. ObjCMethodDecl *Redecl = nullptr;
  812. if (hasRedeclaration())
  813. Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
  814. if (Redecl)
  815. return Redecl;
  816. auto *CtxD = cast<Decl>(getDeclContext());
  817. if (!CtxD->isInvalidDecl()) {
  818. if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
  819. if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
  820. if (!ImplD->isInvalidDecl())
  821. Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
  822. } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
  823. if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
  824. if (!ImplD->isInvalidDecl())
  825. Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
  826. } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
  827. if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
  828. if (!IFD->isInvalidDecl())
  829. Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
  830. } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
  831. if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
  832. if (!CatD->isInvalidDecl())
  833. Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
  834. }
  835. }
  836. // Ensure that the discovered method redeclaration has a valid declaration
  837. // context. Used to prevent infinite loops when iterating redeclarations in
  838. // a partially invalid AST.
  839. if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
  840. Redecl = nullptr;
  841. if (!Redecl && isRedeclaration()) {
  842. // This is the last redeclaration, go back to the first method.
  843. return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
  844. isInstanceMethod(),
  845. /*AllowHidden=*/true);
  846. }
  847. return Redecl ? Redecl : this;
  848. }
  849. ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
  850. auto *CtxD = cast<Decl>(getDeclContext());
  851. const auto &Sel = getSelector();
  852. if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
  853. if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) {
  854. // When the container is the ObjCImplementationDecl (the primary
  855. // @implementation), then the canonical Decl is either in
  856. // the class Interface, or in any of its extension.
  857. //
  858. // So when we don't find it in the ObjCInterfaceDecl,
  859. // sift through extensions too.
  860. if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
  861. return MD;
  862. for (auto *Ext : IFD->known_extensions())
  863. if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
  864. return MD;
  865. }
  866. } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
  867. if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
  868. if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod()))
  869. return MD;
  870. }
  871. if (isRedeclaration()) {
  872. // It is possible that we have not done deserializing the ObjCMethod yet.
  873. ObjCMethodDecl *MD =
  874. cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod(),
  875. /*AllowHidden=*/true);
  876. return MD ? MD : this;
  877. }
  878. return this;
  879. }
  880. SourceLocation ObjCMethodDecl::getEndLoc() const {
  881. if (Stmt *Body = getBody())
  882. return Body->getEndLoc();
  883. return DeclEndLoc;
  884. }
  885. ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
  886. auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
  887. if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
  888. return family;
  889. // Check for an explicit attribute.
  890. if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
  891. // The unfortunate necessity of mapping between enums here is due
  892. // to the attributes framework.
  893. switch (attr->getFamily()) {
  894. case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
  895. case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
  896. case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
  897. case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
  898. case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
  899. case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
  900. }
  901. ObjCMethodDeclBits.Family = family;
  902. return family;
  903. }
  904. family = getSelector().getMethodFamily();
  905. switch (family) {
  906. case OMF_None: break;
  907. // init only has a conventional meaning for an instance method, and
  908. // it has to return an object.
  909. case OMF_init:
  910. if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
  911. family = OMF_None;
  912. break;
  913. // alloc/copy/new have a conventional meaning for both class and
  914. // instance methods, but they require an object return.
  915. case OMF_alloc:
  916. case OMF_copy:
  917. case OMF_mutableCopy:
  918. case OMF_new:
  919. if (!getReturnType()->isObjCObjectPointerType())
  920. family = OMF_None;
  921. break;
  922. // These selectors have a conventional meaning only for instance methods.
  923. case OMF_dealloc:
  924. case OMF_finalize:
  925. case OMF_retain:
  926. case OMF_release:
  927. case OMF_autorelease:
  928. case OMF_retainCount:
  929. case OMF_self:
  930. if (!isInstanceMethod())
  931. family = OMF_None;
  932. break;
  933. case OMF_initialize:
  934. if (isInstanceMethod() || !getReturnType()->isVoidType())
  935. family = OMF_None;
  936. break;
  937. case OMF_performSelector:
  938. if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
  939. family = OMF_None;
  940. else {
  941. unsigned noParams = param_size();
  942. if (noParams < 1 || noParams > 3)
  943. family = OMF_None;
  944. else {
  945. ObjCMethodDecl::param_type_iterator it = param_type_begin();
  946. QualType ArgT = (*it);
  947. if (!ArgT->isObjCSelType()) {
  948. family = OMF_None;
  949. break;
  950. }
  951. while (--noParams) {
  952. it++;
  953. ArgT = (*it);
  954. if (!ArgT->isObjCIdType()) {
  955. family = OMF_None;
  956. break;
  957. }
  958. }
  959. }
  960. }
  961. break;
  962. }
  963. // Cache the result.
  964. ObjCMethodDeclBits.Family = family;
  965. return family;
  966. }
  967. QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
  968. const ObjCInterfaceDecl *OID,
  969. bool &selfIsPseudoStrong,
  970. bool &selfIsConsumed) const {
  971. QualType selfTy;
  972. selfIsPseudoStrong = false;
  973. selfIsConsumed = false;
  974. if (isInstanceMethod()) {
  975. // There may be no interface context due to error in declaration
  976. // of the interface (which has been reported). Recover gracefully.
  977. if (OID) {
  978. selfTy = Context.getObjCInterfaceType(OID);
  979. selfTy = Context.getObjCObjectPointerType(selfTy);
  980. } else {
  981. selfTy = Context.getObjCIdType();
  982. }
  983. } else // we have a factory method.
  984. selfTy = Context.getObjCClassType();
  985. if (Context.getLangOpts().ObjCAutoRefCount) {
  986. if (isInstanceMethod()) {
  987. selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
  988. // 'self' is always __strong. It's actually pseudo-strong except
  989. // in init methods (or methods labeled ns_consumes_self), though.
  990. Qualifiers qs;
  991. qs.setObjCLifetime(Qualifiers::OCL_Strong);
  992. selfTy = Context.getQualifiedType(selfTy, qs);
  993. // In addition, 'self' is const unless this is an init method.
  994. if (getMethodFamily() != OMF_init && !selfIsConsumed) {
  995. selfTy = selfTy.withConst();
  996. selfIsPseudoStrong = true;
  997. }
  998. }
  999. else {
  1000. assert(isClassMethod());
  1001. // 'self' is always const in class methods.
  1002. selfTy = selfTy.withConst();
  1003. selfIsPseudoStrong = true;
  1004. }
  1005. }
  1006. return selfTy;
  1007. }
  1008. void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
  1009. const ObjCInterfaceDecl *OID) {
  1010. bool selfIsPseudoStrong, selfIsConsumed;
  1011. QualType selfTy =
  1012. getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
  1013. auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
  1014. &Context.Idents.get("self"), selfTy,
  1015. ImplicitParamDecl::ObjCSelf);
  1016. setSelfDecl(Self);
  1017. if (selfIsConsumed)
  1018. Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
  1019. if (selfIsPseudoStrong)
  1020. Self->setARCPseudoStrong(true);
  1021. setCmdDecl(ImplicitParamDecl::Create(
  1022. Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
  1023. Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
  1024. }
  1025. ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
  1026. if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
  1027. return ID;
  1028. if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
  1029. return CD->getClassInterface();
  1030. if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
  1031. return IMD->getClassInterface();
  1032. if (isa<ObjCProtocolDecl>(getDeclContext()))
  1033. return nullptr;
  1034. llvm_unreachable("unknown method context");
  1035. }
  1036. ObjCCategoryDecl *ObjCMethodDecl::getCategory() {
  1037. if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
  1038. return CD;
  1039. if (auto *IMD = dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
  1040. return IMD->getCategoryDecl();
  1041. return nullptr;
  1042. }
  1043. SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
  1044. const auto *TSI = getReturnTypeSourceInfo();
  1045. if (TSI)
  1046. return TSI->getTypeLoc().getSourceRange();
  1047. return SourceRange();
  1048. }
  1049. QualType ObjCMethodDecl::getSendResultType() const {
  1050. ASTContext &Ctx = getASTContext();
  1051. return getReturnType().getNonLValueExprType(Ctx)
  1052. .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
  1053. }
  1054. QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
  1055. // FIXME: Handle related result types here.
  1056. return getReturnType().getNonLValueExprType(getASTContext())
  1057. .substObjCMemberType(receiverType, getDeclContext(),
  1058. ObjCSubstitutionContext::Result);
  1059. }
  1060. static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
  1061. const ObjCMethodDecl *Method,
  1062. SmallVectorImpl<const ObjCMethodDecl *> &Methods,
  1063. bool MovedToSuper) {
  1064. if (!Container)
  1065. return;
  1066. // In categories look for overridden methods from protocols. A method from
  1067. // category is not "overridden" since it is considered as the "same" method
  1068. // (same USR) as the one from the interface.
  1069. if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
  1070. // Check whether we have a matching method at this category but only if we
  1071. // are at the super class level.
  1072. if (MovedToSuper)
  1073. if (ObjCMethodDecl *
  1074. Overridden = Container->getMethod(Method->getSelector(),
  1075. Method->isInstanceMethod(),
  1076. /*AllowHidden=*/true))
  1077. if (Method != Overridden) {
  1078. // We found an override at this category; there is no need to look
  1079. // into its protocols.
  1080. Methods.push_back(Overridden);
  1081. return;
  1082. }
  1083. for (const auto *P : Category->protocols())
  1084. CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
  1085. return;
  1086. }
  1087. // Check whether we have a matching method at this level.
  1088. if (const ObjCMethodDecl *
  1089. Overridden = Container->getMethod(Method->getSelector(),
  1090. Method->isInstanceMethod(),
  1091. /*AllowHidden=*/true))
  1092. if (Method != Overridden) {
  1093. // We found an override at this level; there is no need to look
  1094. // into other protocols or categories.
  1095. Methods.push_back(Overridden);
  1096. return;
  1097. }
  1098. if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
  1099. for (const auto *P : Protocol->protocols())
  1100. CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
  1101. }
  1102. if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
  1103. for (const auto *P : Interface->protocols())
  1104. CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
  1105. for (const auto *Cat : Interface->known_categories())
  1106. CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
  1107. if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
  1108. return CollectOverriddenMethodsRecurse(Super, Method, Methods,
  1109. /*MovedToSuper=*/true);
  1110. }
  1111. }
  1112. static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
  1113. const ObjCMethodDecl *Method,
  1114. SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
  1115. CollectOverriddenMethodsRecurse(Container, Method, Methods,
  1116. /*MovedToSuper=*/false);
  1117. }
  1118. static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
  1119. SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
  1120. assert(Method->isOverriding());
  1121. if (const auto *ProtD =
  1122. dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
  1123. CollectOverriddenMethods(ProtD, Method, overridden);
  1124. } else if (const auto *IMD =
  1125. dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
  1126. const ObjCInterfaceDecl *ID = IMD->getClassInterface();
  1127. if (!ID)
  1128. return;
  1129. // Start searching for overridden methods using the method from the
  1130. // interface as starting point.
  1131. if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
  1132. Method->isInstanceMethod(),
  1133. /*AllowHidden=*/true))
  1134. Method = IFaceMeth;
  1135. CollectOverriddenMethods(ID, Method, overridden);
  1136. } else if (const auto *CatD =
  1137. dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
  1138. const ObjCInterfaceDecl *ID = CatD->getClassInterface();
  1139. if (!ID)
  1140. return;
  1141. // Start searching for overridden methods using the method from the
  1142. // interface as starting point.
  1143. if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
  1144. Method->isInstanceMethod(),
  1145. /*AllowHidden=*/true))
  1146. Method = IFaceMeth;
  1147. CollectOverriddenMethods(ID, Method, overridden);
  1148. } else {
  1149. CollectOverriddenMethods(
  1150. dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
  1151. Method, overridden);
  1152. }
  1153. }
  1154. void ObjCMethodDecl::getOverriddenMethods(
  1155. SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
  1156. const ObjCMethodDecl *Method = this;
  1157. if (Method->isRedeclaration()) {
  1158. Method = cast<ObjCContainerDecl>(Method->getDeclContext())
  1159. ->getMethod(Method->getSelector(), Method->isInstanceMethod(),
  1160. /*AllowHidden=*/true);
  1161. }
  1162. if (Method->isOverriding()) {
  1163. collectOverriddenMethodsSlow(Method, Overridden);
  1164. assert(!Overridden.empty() &&
  1165. "ObjCMethodDecl's overriding bit is not as expected");
  1166. }
  1167. }
  1168. const ObjCPropertyDecl *
  1169. ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
  1170. Selector Sel = getSelector();
  1171. unsigned NumArgs = Sel.getNumArgs();
  1172. if (NumArgs > 1)
  1173. return nullptr;
  1174. if (isPropertyAccessor()) {
  1175. const auto *Container = cast<ObjCContainerDecl>(getParent());
  1176. // For accessor stubs, go back to the interface.
  1177. if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container))
  1178. if (isSynthesizedAccessorStub())
  1179. Container = ImplDecl->getClassInterface();
  1180. bool IsGetter = (NumArgs == 0);
  1181. bool IsInstance = isInstanceMethod();
  1182. /// Local function that attempts to find a matching property within the
  1183. /// given Objective-C container.
  1184. auto findMatchingProperty =
  1185. [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
  1186. if (IsInstance) {
  1187. for (const auto *I : Container->instance_properties()) {
  1188. Selector NextSel = IsGetter ? I->getGetterName()
  1189. : I->getSetterName();
  1190. if (NextSel == Sel)
  1191. return I;
  1192. }
  1193. } else {
  1194. for (const auto *I : Container->class_properties()) {
  1195. Selector NextSel = IsGetter ? I->getGetterName()
  1196. : I->getSetterName();
  1197. if (NextSel == Sel)
  1198. return I;
  1199. }
  1200. }
  1201. return nullptr;
  1202. };
  1203. // Look in the container we were given.
  1204. if (const auto *Found = findMatchingProperty(Container))
  1205. return Found;
  1206. // If we're in a category or extension, look in the main class.
  1207. const ObjCInterfaceDecl *ClassDecl = nullptr;
  1208. if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
  1209. ClassDecl = Category->getClassInterface();
  1210. if (const auto *Found = findMatchingProperty(ClassDecl))
  1211. return Found;
  1212. } else {
  1213. // Determine whether the container is a class.
  1214. ClassDecl = cast<ObjCInterfaceDecl>(Container);
  1215. }
  1216. assert(ClassDecl && "Failed to find main class");
  1217. // If we have a class, check its visible extensions.
  1218. for (const auto *Ext : ClassDecl->visible_extensions()) {
  1219. if (Ext == Container)
  1220. continue;
  1221. if (const auto *Found = findMatchingProperty(Ext))
  1222. return Found;
  1223. }
  1224. assert(isSynthesizedAccessorStub() && "expected an accessor stub");
  1225. for (const auto *Cat : ClassDecl->known_categories()) {
  1226. if (Cat == Container)
  1227. continue;
  1228. if (const auto *Found = findMatchingProperty(Cat))
  1229. return Found;
  1230. }
  1231. llvm_unreachable("Marked as a property accessor but no property found!");
  1232. }
  1233. if (!CheckOverrides)
  1234. return nullptr;
  1235. using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
  1236. OverridesTy Overrides;
  1237. getOverriddenMethods(Overrides);
  1238. for (const auto *Override : Overrides)
  1239. if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
  1240. return Prop;
  1241. return nullptr;
  1242. }
  1243. //===----------------------------------------------------------------------===//
  1244. // ObjCTypeParamDecl
  1245. //===----------------------------------------------------------------------===//
  1246. void ObjCTypeParamDecl::anchor() {}
  1247. ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
  1248. ObjCTypeParamVariance variance,
  1249. SourceLocation varianceLoc,
  1250. unsigned index,
  1251. SourceLocation nameLoc,
  1252. IdentifierInfo *name,
  1253. SourceLocation colonLoc,
  1254. TypeSourceInfo *boundInfo) {
  1255. auto *TPDecl =
  1256. new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
  1257. nameLoc, name, colonLoc, boundInfo);
  1258. QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
  1259. TPDecl->setTypeForDecl(TPType.getTypePtr());
  1260. return TPDecl;
  1261. }
  1262. ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
  1263. unsigned ID) {
  1264. return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
  1265. ObjCTypeParamVariance::Invariant,
  1266. SourceLocation(), 0, SourceLocation(),
  1267. nullptr, SourceLocation(), nullptr);
  1268. }
  1269. SourceRange ObjCTypeParamDecl::getSourceRange() const {
  1270. SourceLocation startLoc = VarianceLoc;
  1271. if (startLoc.isInvalid())
  1272. startLoc = getLocation();
  1273. if (hasExplicitBound()) {
  1274. return SourceRange(startLoc,
  1275. getTypeSourceInfo()->getTypeLoc().getEndLoc());
  1276. }
  1277. return SourceRange(startLoc);
  1278. }
  1279. //===----------------------------------------------------------------------===//
  1280. // ObjCTypeParamList
  1281. //===----------------------------------------------------------------------===//
  1282. ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
  1283. ArrayRef<ObjCTypeParamDecl *> typeParams,
  1284. SourceLocation rAngleLoc)
  1285. : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size()) {
  1286. std::copy(typeParams.begin(), typeParams.end(), begin());
  1287. }
  1288. ObjCTypeParamList *ObjCTypeParamList::create(
  1289. ASTContext &ctx,
  1290. SourceLocation lAngleLoc,
  1291. ArrayRef<ObjCTypeParamDecl *> typeParams,
  1292. SourceLocation rAngleLoc) {
  1293. void *mem =
  1294. ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
  1295. alignof(ObjCTypeParamList));
  1296. return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
  1297. }
  1298. void ObjCTypeParamList::gatherDefaultTypeArgs(
  1299. SmallVectorImpl<QualType> &typeArgs) const {
  1300. typeArgs.reserve(size());
  1301. for (auto typeParam : *this)
  1302. typeArgs.push_back(typeParam->getUnderlyingType());
  1303. }
  1304. //===----------------------------------------------------------------------===//
  1305. // ObjCInterfaceDecl
  1306. //===----------------------------------------------------------------------===//
  1307. ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
  1308. DeclContext *DC,
  1309. SourceLocation atLoc,
  1310. IdentifierInfo *Id,
  1311. ObjCTypeParamList *typeParamList,
  1312. ObjCInterfaceDecl *PrevDecl,
  1313. SourceLocation ClassLoc,
  1314. bool isInternal){
  1315. auto *Result = new (C, DC)
  1316. ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
  1317. isInternal);
  1318. Result->Data.setInt(!C.getLangOpts().Modules);
  1319. C.getObjCInterfaceType(Result, PrevDecl);
  1320. return Result;
  1321. }
  1322. ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
  1323. unsigned ID) {
  1324. auto *Result = new (C, ID)
  1325. ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
  1326. SourceLocation(), nullptr, false);
  1327. Result->Data.setInt(!C.getLangOpts().Modules);
  1328. return Result;
  1329. }
  1330. ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
  1331. SourceLocation AtLoc, IdentifierInfo *Id,
  1332. ObjCTypeParamList *typeParamList,
  1333. SourceLocation CLoc,
  1334. ObjCInterfaceDecl *PrevDecl,
  1335. bool IsInternal)
  1336. : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
  1337. redeclarable_base(C) {
  1338. setPreviousDecl(PrevDecl);
  1339. // Copy the 'data' pointer over.
  1340. if (PrevDecl)
  1341. Data = PrevDecl->Data;
  1342. setImplicit(IsInternal);
  1343. setTypeParamList(typeParamList);
  1344. }
  1345. void ObjCInterfaceDecl::LoadExternalDefinition() const {
  1346. assert(data().ExternallyCompleted && "Class is not externally completed");
  1347. data().ExternallyCompleted = false;
  1348. getASTContext().getExternalSource()->CompleteType(
  1349. const_cast<ObjCInterfaceDecl *>(this));
  1350. }
  1351. void ObjCInterfaceDecl::setExternallyCompleted() {
  1352. assert(getASTContext().getExternalSource() &&
  1353. "Class can't be externally completed without an external source");
  1354. assert(hasDefinition() &&
  1355. "Forward declarations can't be externally completed");
  1356. data().ExternallyCompleted = true;
  1357. }
  1358. void ObjCInterfaceDecl::setHasDesignatedInitializers() {
  1359. // Check for a complete definition and recover if not so.
  1360. if (!isThisDeclarationADefinition())
  1361. return;
  1362. data().HasDesignatedInitializers = true;
  1363. }
  1364. bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
  1365. // Check for a complete definition and recover if not so.
  1366. if (!isThisDeclarationADefinition())
  1367. return false;
  1368. if (data().ExternallyCompleted)
  1369. LoadExternalDefinition();
  1370. return data().HasDesignatedInitializers;
  1371. }
  1372. StringRef
  1373. ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
  1374. if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
  1375. return ObjCRTName->getMetadataName();
  1376. return getName();
  1377. }
  1378. StringRef
  1379. ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
  1380. if (ObjCInterfaceDecl *ID =
  1381. const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
  1382. return ID->getObjCRuntimeNameAsString();
  1383. return getName();
  1384. }
  1385. ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
  1386. if (const ObjCInterfaceDecl *Def = getDefinition()) {
  1387. if (data().ExternallyCompleted)
  1388. LoadExternalDefinition();
  1389. return getASTContext().getObjCImplementation(
  1390. const_cast<ObjCInterfaceDecl*>(Def));
  1391. }
  1392. // FIXME: Should make sure no callers ever do this.
  1393. return nullptr;
  1394. }
  1395. void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
  1396. getASTContext().setObjCImplementation(getDefinition(), ImplD);
  1397. }
  1398. namespace {
  1399. struct SynthesizeIvarChunk {
  1400. uint64_t Size;
  1401. ObjCIvarDecl *Ivar;
  1402. SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
  1403. : Size(size), Ivar(ivar) {}
  1404. };
  1405. bool operator<(const SynthesizeIvarChunk & LHS,
  1406. const SynthesizeIvarChunk &RHS) {
  1407. return LHS.Size < RHS.Size;
  1408. }
  1409. } // namespace
  1410. /// all_declared_ivar_begin - return first ivar declared in this class,
  1411. /// its extensions and its implementation. Lazily build the list on first
  1412. /// access.
  1413. ///
  1414. /// Caveat: The list returned by this method reflects the current
  1415. /// state of the parser. The cache will be updated for every ivar
  1416. /// added by an extension or the implementation when they are
  1417. /// encountered.
  1418. /// See also ObjCIvarDecl::Create().
  1419. ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
  1420. // FIXME: Should make sure no callers ever do this.
  1421. if (!hasDefinition())
  1422. return nullptr;
  1423. ObjCIvarDecl *curIvar = nullptr;
  1424. if (!data().IvarList) {
  1425. if (!ivar_empty()) {
  1426. ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
  1427. data().IvarList = *I; ++I;
  1428. for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
  1429. curIvar->setNextIvar(*I);
  1430. }
  1431. for (const auto *Ext : known_extensions()) {
  1432. if (!Ext->ivar_empty()) {
  1433. ObjCCategoryDecl::ivar_iterator
  1434. I = Ext->ivar_begin(),
  1435. E = Ext->ivar_end();
  1436. if (!data().IvarList) {
  1437. data().IvarList = *I; ++I;
  1438. curIvar = data().IvarList;
  1439. }
  1440. for ( ;I != E; curIvar = *I, ++I)
  1441. curIvar->setNextIvar(*I);
  1442. }
  1443. }
  1444. data().IvarListMissingImplementation = true;
  1445. }
  1446. // cached and complete!
  1447. if (!data().IvarListMissingImplementation)
  1448. return data().IvarList;
  1449. if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
  1450. data().IvarListMissingImplementation = false;
  1451. if (!ImplDecl->ivar_empty()) {
  1452. SmallVector<SynthesizeIvarChunk, 16> layout;
  1453. for (auto *IV : ImplDecl->ivars()) {
  1454. if (IV->getSynthesize() && !IV->isInvalidDecl()) {
  1455. layout.push_back(SynthesizeIvarChunk(
  1456. IV->getASTContext().getTypeSize(IV->getType()), IV));
  1457. continue;
  1458. }
  1459. if (!data().IvarList)
  1460. data().IvarList = IV;
  1461. else
  1462. curIvar->setNextIvar(IV);
  1463. curIvar = IV;
  1464. }
  1465. if (!layout.empty()) {
  1466. // Order synthesized ivars by their size.
  1467. llvm::stable_sort(layout);
  1468. unsigned Ix = 0, EIx = layout.size();
  1469. if (!data().IvarList) {
  1470. data().IvarList = layout[0].Ivar; Ix++;
  1471. curIvar = data().IvarList;
  1472. }
  1473. for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
  1474. curIvar->setNextIvar(layout[Ix].Ivar);
  1475. }
  1476. }
  1477. }
  1478. return data().IvarList;
  1479. }
  1480. /// FindCategoryDeclaration - Finds category declaration in the list of
  1481. /// categories for this class and returns it. Name of the category is passed
  1482. /// in 'CategoryId'. If category not found, return 0;
  1483. ///
  1484. ObjCCategoryDecl *
  1485. ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
  1486. // FIXME: Should make sure no callers ever do this.
  1487. if (!hasDefinition())
  1488. return nullptr;
  1489. if (data().ExternallyCompleted)
  1490. LoadExternalDefinition();
  1491. for (auto *Cat : visible_categories())
  1492. if (Cat->getIdentifier() == CategoryId)
  1493. return Cat;
  1494. return nullptr;
  1495. }
  1496. ObjCMethodDecl *
  1497. ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
  1498. for (const auto *Cat : visible_categories()) {
  1499. if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
  1500. if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
  1501. return MD;
  1502. }
  1503. return nullptr;
  1504. }
  1505. ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
  1506. for (const auto *Cat : visible_categories()) {
  1507. if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
  1508. if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
  1509. return MD;
  1510. }
  1511. return nullptr;
  1512. }
  1513. /// ClassImplementsProtocol - Checks that 'lProto' protocol
  1514. /// has been implemented in IDecl class, its super class or categories (if
  1515. /// lookupCategory is true).
  1516. bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
  1517. bool lookupCategory,
  1518. bool RHSIsQualifiedID) {
  1519. if (!hasDefinition())
  1520. return false;
  1521. ObjCInterfaceDecl *IDecl = this;
  1522. // 1st, look up the class.
  1523. for (auto *PI : IDecl->protocols()){
  1524. if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
  1525. return true;
  1526. // This is dubious and is added to be compatible with gcc. In gcc, it is
  1527. // also allowed assigning a protocol-qualified 'id' type to a LHS object
  1528. // when protocol in qualified LHS is in list of protocols in the rhs 'id'
  1529. // object. This IMO, should be a bug.
  1530. // FIXME: Treat this as an extension, and flag this as an error when GCC
  1531. // extensions are not enabled.
  1532. if (RHSIsQualifiedID &&
  1533. getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
  1534. return true;
  1535. }
  1536. // 2nd, look up the category.
  1537. if (lookupCategory)
  1538. for (const auto *Cat : visible_categories()) {
  1539. for (auto *PI : Cat->protocols())
  1540. if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
  1541. return true;
  1542. }
  1543. // 3rd, look up the super class(s)
  1544. if (IDecl->getSuperClass())
  1545. return
  1546. IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
  1547. RHSIsQualifiedID);
  1548. return false;
  1549. }
  1550. //===----------------------------------------------------------------------===//
  1551. // ObjCIvarDecl
  1552. //===----------------------------------------------------------------------===//
  1553. void ObjCIvarDecl::anchor() {}
  1554. ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
  1555. SourceLocation StartLoc,
  1556. SourceLocation IdLoc, IdentifierInfo *Id,
  1557. QualType T, TypeSourceInfo *TInfo,
  1558. AccessControl ac, Expr *BW,
  1559. bool synthesized) {
  1560. if (DC) {
  1561. // Ivar's can only appear in interfaces, implementations (via synthesized
  1562. // properties), and class extensions (via direct declaration, or synthesized
  1563. // properties).
  1564. //
  1565. // FIXME: This should really be asserting this:
  1566. // (isa<ObjCCategoryDecl>(DC) &&
  1567. // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
  1568. // but unfortunately we sometimes place ivars into non-class extension
  1569. // categories on error. This breaks an AST invariant, and should not be
  1570. // fixed.
  1571. assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
  1572. isa<ObjCCategoryDecl>(DC)) &&
  1573. "Invalid ivar decl context!");
  1574. // Once a new ivar is created in any of class/class-extension/implementation
  1575. // decl contexts, the previously built IvarList must be rebuilt.
  1576. auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
  1577. if (!ID) {
  1578. if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
  1579. ID = IM->getClassInterface();
  1580. else
  1581. ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
  1582. }
  1583. ID->setIvarList(nullptr);
  1584. }
  1585. return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
  1586. synthesized);
  1587. }
  1588. ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1589. return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
  1590. nullptr, QualType(), nullptr,
  1591. ObjCIvarDecl::None, nullptr, false);
  1592. }
  1593. const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
  1594. const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
  1595. switch (DC->getKind()) {
  1596. default:
  1597. case ObjCCategoryImpl:
  1598. case ObjCProtocol:
  1599. llvm_unreachable("invalid ivar container!");
  1600. // Ivars can only appear in class extension categories.
  1601. case ObjCCategory: {
  1602. const auto *CD = cast<ObjCCategoryDecl>(DC);
  1603. assert(CD->IsClassExtension() && "invalid container for ivar!");
  1604. return CD->getClassInterface();
  1605. }
  1606. case ObjCImplementation:
  1607. return cast<ObjCImplementationDecl>(DC)->getClassInterface();
  1608. case ObjCInterface:
  1609. return cast<ObjCInterfaceDecl>(DC);
  1610. }
  1611. }
  1612. QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
  1613. return getType().substObjCMemberType(objectType, getDeclContext(),
  1614. ObjCSubstitutionContext::Property);
  1615. }
  1616. //===----------------------------------------------------------------------===//
  1617. // ObjCAtDefsFieldDecl
  1618. //===----------------------------------------------------------------------===//
  1619. void ObjCAtDefsFieldDecl::anchor() {}
  1620. ObjCAtDefsFieldDecl
  1621. *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
  1622. SourceLocation StartLoc, SourceLocation IdLoc,
  1623. IdentifierInfo *Id, QualType T, Expr *BW) {
  1624. return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
  1625. }
  1626. ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
  1627. unsigned ID) {
  1628. return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
  1629. SourceLocation(), nullptr, QualType(),
  1630. nullptr);
  1631. }
  1632. //===----------------------------------------------------------------------===//
  1633. // ObjCProtocolDecl
  1634. //===----------------------------------------------------------------------===//
  1635. void ObjCProtocolDecl::anchor() {}
  1636. ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
  1637. IdentifierInfo *Id, SourceLocation nameLoc,
  1638. SourceLocation atStartLoc,
  1639. ObjCProtocolDecl *PrevDecl)
  1640. : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
  1641. redeclarable_base(C) {
  1642. setPreviousDecl(PrevDecl);
  1643. if (PrevDecl)
  1644. Data = PrevDecl->Data;
  1645. }
  1646. ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
  1647. IdentifierInfo *Id,
  1648. SourceLocation nameLoc,
  1649. SourceLocation atStartLoc,
  1650. ObjCProtocolDecl *PrevDecl) {
  1651. auto *Result =
  1652. new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
  1653. Result->Data.setInt(!C.getLangOpts().Modules);
  1654. return Result;
  1655. }
  1656. ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
  1657. unsigned ID) {
  1658. ObjCProtocolDecl *Result =
  1659. new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
  1660. SourceLocation(), nullptr);
  1661. Result->Data.setInt(!C.getLangOpts().Modules);
  1662. return Result;
  1663. }
  1664. bool ObjCProtocolDecl::isNonRuntimeProtocol() const {
  1665. return hasAttr<ObjCNonRuntimeProtocolAttr>();
  1666. }
  1667. void ObjCProtocolDecl::getImpliedProtocols(
  1668. llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const {
  1669. std::queue<const ObjCProtocolDecl *> WorkQueue;
  1670. WorkQueue.push(this);
  1671. while (!WorkQueue.empty()) {
  1672. const auto *PD = WorkQueue.front();
  1673. WorkQueue.pop();
  1674. for (const auto *Parent : PD->protocols()) {
  1675. const auto *Can = Parent->getCanonicalDecl();
  1676. auto Result = IPs.insert(Can);
  1677. if (Result.second)
  1678. WorkQueue.push(Parent);
  1679. }
  1680. }
  1681. }
  1682. ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
  1683. ObjCProtocolDecl *PDecl = this;
  1684. if (Name == getIdentifier())
  1685. return PDecl;
  1686. for (auto *I : protocols())
  1687. if ((PDecl = I->lookupProtocolNamed(Name)))
  1688. return PDecl;
  1689. return nullptr;
  1690. }
  1691. // lookupMethod - Lookup a instance/class method in the protocol and protocols
  1692. // it inherited.
  1693. ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
  1694. bool isInstance) const {
  1695. ObjCMethodDecl *MethodDecl = nullptr;
  1696. // If there is no definition or the definition is hidden, we don't find
  1697. // anything.
  1698. const ObjCProtocolDecl *Def = getDefinition();
  1699. if (!Def || !Def->isUnconditionallyVisible())
  1700. return nullptr;
  1701. if ((MethodDecl = getMethod(Sel, isInstance)))
  1702. return MethodDecl;
  1703. for (const auto *I : protocols())
  1704. if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
  1705. return MethodDecl;
  1706. return nullptr;
  1707. }
  1708. void ObjCProtocolDecl::allocateDefinitionData() {
  1709. assert(!Data.getPointer() && "Protocol already has a definition!");
  1710. Data.setPointer(new (getASTContext()) DefinitionData);
  1711. Data.getPointer()->Definition = this;
  1712. }
  1713. void ObjCProtocolDecl::startDefinition() {
  1714. allocateDefinitionData();
  1715. // Update all of the declarations with a pointer to the definition.
  1716. for (auto *RD : redecls())
  1717. RD->Data = this->Data;
  1718. }
  1719. void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
  1720. PropertyDeclOrder &PO) const {
  1721. if (const ObjCProtocolDecl *PDecl = getDefinition()) {
  1722. for (auto *Prop : PDecl->properties()) {
  1723. // Insert into PM if not there already.
  1724. PM.insert(std::make_pair(
  1725. std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
  1726. Prop));
  1727. PO.push_back(Prop);
  1728. }
  1729. // Scan through protocol's protocols.
  1730. for (const auto *PI : PDecl->protocols())
  1731. PI->collectPropertiesToImplement(PM, PO);
  1732. }
  1733. }
  1734. void ObjCProtocolDecl::collectInheritedProtocolProperties(
  1735. const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
  1736. PropertyDeclOrder &PO) const {
  1737. if (const ObjCProtocolDecl *PDecl = getDefinition()) {
  1738. if (!PS.insert(PDecl).second)
  1739. return;
  1740. for (auto *Prop : PDecl->properties()) {
  1741. if (Prop == Property)
  1742. continue;
  1743. if (Prop->getIdentifier() == Property->getIdentifier()) {
  1744. PO.push_back(Prop);
  1745. return;
  1746. }
  1747. }
  1748. // Scan through protocol's protocols which did not have a matching property.
  1749. for (const auto *PI : PDecl->protocols())
  1750. PI->collectInheritedProtocolProperties(Property, PS, PO);
  1751. }
  1752. }
  1753. StringRef
  1754. ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
  1755. if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
  1756. return ObjCRTName->getMetadataName();
  1757. return getName();
  1758. }
  1759. //===----------------------------------------------------------------------===//
  1760. // ObjCCategoryDecl
  1761. //===----------------------------------------------------------------------===//
  1762. void ObjCCategoryDecl::anchor() {}
  1763. ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
  1764. SourceLocation ClassNameLoc,
  1765. SourceLocation CategoryNameLoc,
  1766. IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
  1767. ObjCTypeParamList *typeParamList,
  1768. SourceLocation IvarLBraceLoc,
  1769. SourceLocation IvarRBraceLoc)
  1770. : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
  1771. ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
  1772. IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
  1773. setTypeParamList(typeParamList);
  1774. }
  1775. ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
  1776. SourceLocation AtLoc,
  1777. SourceLocation ClassNameLoc,
  1778. SourceLocation CategoryNameLoc,
  1779. IdentifierInfo *Id,
  1780. ObjCInterfaceDecl *IDecl,
  1781. ObjCTypeParamList *typeParamList,
  1782. SourceLocation IvarLBraceLoc,
  1783. SourceLocation IvarRBraceLoc) {
  1784. auto *CatDecl =
  1785. new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
  1786. IDecl, typeParamList, IvarLBraceLoc,
  1787. IvarRBraceLoc);
  1788. if (IDecl) {
  1789. // Link this category into its class's category list.
  1790. CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
  1791. if (IDecl->hasDefinition()) {
  1792. IDecl->setCategoryListRaw(CatDecl);
  1793. if (ASTMutationListener *L = C.getASTMutationListener())
  1794. L->AddedObjCCategoryToInterface(CatDecl, IDecl);
  1795. }
  1796. }
  1797. return CatDecl;
  1798. }
  1799. ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
  1800. unsigned ID) {
  1801. return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
  1802. SourceLocation(), SourceLocation(),
  1803. nullptr, nullptr, nullptr);
  1804. }
  1805. ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
  1806. return getASTContext().getObjCImplementation(
  1807. const_cast<ObjCCategoryDecl*>(this));
  1808. }
  1809. void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
  1810. getASTContext().setObjCImplementation(this, ImplD);
  1811. }
  1812. void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
  1813. TypeParamList = TPL;
  1814. if (!TPL)
  1815. return;
  1816. // Set the declaration context of each of the type parameters.
  1817. for (auto *typeParam : *TypeParamList)
  1818. typeParam->setDeclContext(this);
  1819. }
  1820. //===----------------------------------------------------------------------===//
  1821. // ObjCCategoryImplDecl
  1822. //===----------------------------------------------------------------------===//
  1823. void ObjCCategoryImplDecl::anchor() {}
  1824. ObjCCategoryImplDecl *
  1825. ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
  1826. IdentifierInfo *Id,
  1827. ObjCInterfaceDecl *ClassInterface,
  1828. SourceLocation nameLoc,
  1829. SourceLocation atStartLoc,
  1830. SourceLocation CategoryNameLoc) {
  1831. if (ClassInterface && ClassInterface->hasDefinition())
  1832. ClassInterface = ClassInterface->getDefinition();
  1833. return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
  1834. atStartLoc, CategoryNameLoc);
  1835. }
  1836. ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
  1837. unsigned ID) {
  1838. return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
  1839. SourceLocation(), SourceLocation(),
  1840. SourceLocation());
  1841. }
  1842. ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
  1843. // The class interface might be NULL if we are working with invalid code.
  1844. if (const ObjCInterfaceDecl *ID = getClassInterface())
  1845. return ID->FindCategoryDeclaration(getIdentifier());
  1846. return nullptr;
  1847. }
  1848. void ObjCImplDecl::anchor() {}
  1849. void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
  1850. // FIXME: The context should be correct before we get here.
  1851. property->setLexicalDeclContext(this);
  1852. addDecl(property);
  1853. }
  1854. void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
  1855. ASTContext &Ctx = getASTContext();
  1856. if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
  1857. if (IFace)
  1858. Ctx.setObjCImplementation(IFace, ImplD);
  1859. } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
  1860. if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
  1861. Ctx.setObjCImplementation(CD, ImplD);
  1862. }
  1863. ClassInterface = IFace;
  1864. }
  1865. /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
  1866. /// properties implemented in this \@implementation block and returns
  1867. /// the implemented property that uses it.
  1868. ObjCPropertyImplDecl *ObjCImplDecl::
  1869. FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
  1870. for (auto *PID : property_impls())
  1871. if (PID->getPropertyIvarDecl() &&
  1872. PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
  1873. return PID;
  1874. return nullptr;
  1875. }
  1876. /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
  1877. /// added to the list of those properties \@synthesized/\@dynamic in this
  1878. /// category \@implementation block.
  1879. ObjCPropertyImplDecl *ObjCImplDecl::
  1880. FindPropertyImplDecl(IdentifierInfo *Id,
  1881. ObjCPropertyQueryKind QueryKind) const {
  1882. ObjCPropertyImplDecl *ClassPropImpl = nullptr;
  1883. for (auto *PID : property_impls())
  1884. // If queryKind is unknown, we return the instance property if one
  1885. // exists; otherwise we return the class property.
  1886. if (PID->getPropertyDecl()->getIdentifier() == Id) {
  1887. if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
  1888. !PID->getPropertyDecl()->isClassProperty()) ||
  1889. (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
  1890. PID->getPropertyDecl()->isClassProperty()) ||
  1891. (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
  1892. !PID->getPropertyDecl()->isClassProperty()))
  1893. return PID;
  1894. if (PID->getPropertyDecl()->isClassProperty())
  1895. ClassPropImpl = PID;
  1896. }
  1897. if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
  1898. // We can't find the instance property, return the class property.
  1899. return ClassPropImpl;
  1900. return nullptr;
  1901. }
  1902. raw_ostream &clang::operator<<(raw_ostream &OS,
  1903. const ObjCCategoryImplDecl &CID) {
  1904. OS << CID.getName();
  1905. return OS;
  1906. }
  1907. //===----------------------------------------------------------------------===//
  1908. // ObjCImplementationDecl
  1909. //===----------------------------------------------------------------------===//
  1910. void ObjCImplementationDecl::anchor() {}
  1911. ObjCImplementationDecl *
  1912. ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
  1913. ObjCInterfaceDecl *ClassInterface,
  1914. ObjCInterfaceDecl *SuperDecl,
  1915. SourceLocation nameLoc,
  1916. SourceLocation atStartLoc,
  1917. SourceLocation superLoc,
  1918. SourceLocation IvarLBraceLoc,
  1919. SourceLocation IvarRBraceLoc) {
  1920. if (ClassInterface && ClassInterface->hasDefinition())
  1921. ClassInterface = ClassInterface->getDefinition();
  1922. return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
  1923. nameLoc, atStartLoc, superLoc,
  1924. IvarLBraceLoc, IvarRBraceLoc);
  1925. }
  1926. ObjCImplementationDecl *
  1927. ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1928. return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
  1929. SourceLocation(), SourceLocation());
  1930. }
  1931. void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
  1932. CXXCtorInitializer ** initializers,
  1933. unsigned numInitializers) {
  1934. if (numInitializers > 0) {
  1935. NumIvarInitializers = numInitializers;
  1936. auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
  1937. memcpy(ivarInitializers, initializers,
  1938. numInitializers * sizeof(CXXCtorInitializer*));
  1939. IvarInitializers = ivarInitializers;
  1940. }
  1941. }
  1942. ObjCImplementationDecl::init_const_iterator
  1943. ObjCImplementationDecl::init_begin() const {
  1944. return IvarInitializers.get(getASTContext().getExternalSource());
  1945. }
  1946. raw_ostream &clang::operator<<(raw_ostream &OS,
  1947. const ObjCImplementationDecl &ID) {
  1948. OS << ID.getName();
  1949. return OS;
  1950. }
  1951. //===----------------------------------------------------------------------===//
  1952. // ObjCCompatibleAliasDecl
  1953. //===----------------------------------------------------------------------===//
  1954. void ObjCCompatibleAliasDecl::anchor() {}
  1955. ObjCCompatibleAliasDecl *
  1956. ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
  1957. SourceLocation L,
  1958. IdentifierInfo *Id,
  1959. ObjCInterfaceDecl* AliasedClass) {
  1960. return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
  1961. }
  1962. ObjCCompatibleAliasDecl *
  1963. ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1964. return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
  1965. nullptr, nullptr);
  1966. }
  1967. //===----------------------------------------------------------------------===//
  1968. // ObjCPropertyDecl
  1969. //===----------------------------------------------------------------------===//
  1970. void ObjCPropertyDecl::anchor() {}
  1971. ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
  1972. SourceLocation L,
  1973. IdentifierInfo *Id,
  1974. SourceLocation AtLoc,
  1975. SourceLocation LParenLoc,
  1976. QualType T,
  1977. TypeSourceInfo *TSI,
  1978. PropertyControl propControl) {
  1979. return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
  1980. propControl);
  1981. }
  1982. ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
  1983. unsigned ID) {
  1984. return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
  1985. SourceLocation(), SourceLocation(),
  1986. QualType(), nullptr, None);
  1987. }
  1988. QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
  1989. return DeclType.substObjCMemberType(objectType, getDeclContext(),
  1990. ObjCSubstitutionContext::Property);
  1991. }
  1992. bool ObjCPropertyDecl::isDirectProperty() const {
  1993. return (PropertyAttributes & ObjCPropertyAttribute::kind_direct) &&
  1994. !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting;
  1995. }
  1996. //===----------------------------------------------------------------------===//
  1997. // ObjCPropertyImplDecl
  1998. //===----------------------------------------------------------------------===//
  1999. ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
  2000. DeclContext *DC,
  2001. SourceLocation atLoc,
  2002. SourceLocation L,
  2003. ObjCPropertyDecl *property,
  2004. Kind PK,
  2005. ObjCIvarDecl *ivar,
  2006. SourceLocation ivarLoc) {
  2007. return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
  2008. ivarLoc);
  2009. }
  2010. ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
  2011. unsigned ID) {
  2012. return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
  2013. SourceLocation(), nullptr, Dynamic,
  2014. nullptr, SourceLocation());
  2015. }
  2016. SourceRange ObjCPropertyImplDecl::getSourceRange() const {
  2017. SourceLocation EndLoc = getLocation();
  2018. if (IvarLoc.isValid())
  2019. EndLoc = IvarLoc;
  2020. return SourceRange(AtLoc, EndLoc);
  2021. }