DeclObjC.cpp 81 KB

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