DeclObjC.h 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclObjC.h - Classes for representing declarations -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the DeclObjC interface and subclasses.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_DECLOBJC_H
  18. #define LLVM_CLANG_AST_DECLOBJC_H
  19. #include "clang/AST/Decl.h"
  20. #include "clang/AST/DeclBase.h"
  21. #include "clang/AST/DeclObjCCommon.h"
  22. #include "clang/AST/ExternalASTSource.h"
  23. #include "clang/AST/Redeclarable.h"
  24. #include "clang/AST/SelectorLocationsKind.h"
  25. #include "clang/AST/Type.h"
  26. #include "clang/Basic/IdentifierTable.h"
  27. #include "clang/Basic/LLVM.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "clang/Basic/Specifiers.h"
  30. #include "llvm/ADT/ArrayRef.h"
  31. #include "llvm/ADT/DenseSet.h"
  32. #include "llvm/ADT/MapVector.h"
  33. #include "llvm/ADT/PointerIntPair.h"
  34. #include "llvm/ADT/STLExtras.h"
  35. #include "llvm/ADT/StringRef.h"
  36. #include "llvm/ADT/iterator_range.h"
  37. #include "llvm/Support/Compiler.h"
  38. #include "llvm/Support/TrailingObjects.h"
  39. #include <cassert>
  40. #include <cstddef>
  41. #include <cstdint>
  42. #include <iterator>
  43. #include <string>
  44. #include <utility>
  45. namespace clang {
  46. class ASTContext;
  47. class CompoundStmt;
  48. class CXXCtorInitializer;
  49. class Expr;
  50. class ObjCCategoryDecl;
  51. class ObjCCategoryImplDecl;
  52. class ObjCImplementationDecl;
  53. class ObjCInterfaceDecl;
  54. class ObjCIvarDecl;
  55. class ObjCPropertyDecl;
  56. class ObjCPropertyImplDecl;
  57. class ObjCProtocolDecl;
  58. class Stmt;
  59. class ObjCListBase {
  60. protected:
  61. /// List is an array of pointers to objects that are not owned by this object.
  62. void **List = nullptr;
  63. unsigned NumElts = 0;
  64. public:
  65. ObjCListBase() = default;
  66. ObjCListBase(const ObjCListBase &) = delete;
  67. ObjCListBase &operator=(const ObjCListBase &) = delete;
  68. unsigned size() const { return NumElts; }
  69. bool empty() const { return NumElts == 0; }
  70. protected:
  71. void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
  72. };
  73. /// ObjCList - This is a simple template class used to hold various lists of
  74. /// decls etc, which is heavily used by the ObjC front-end. This only use case
  75. /// this supports is setting the list all at once and then reading elements out
  76. /// of it.
  77. template <typename T>
  78. class ObjCList : public ObjCListBase {
  79. public:
  80. void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
  81. ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
  82. }
  83. using iterator = T* const *;
  84. iterator begin() const { return (iterator)List; }
  85. iterator end() const { return (iterator)List+NumElts; }
  86. T* operator[](unsigned Idx) const {
  87. assert(Idx < NumElts && "Invalid access");
  88. return (T*)List[Idx];
  89. }
  90. };
  91. /// A list of Objective-C protocols, along with the source
  92. /// locations at which they were referenced.
  93. class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
  94. SourceLocation *Locations = nullptr;
  95. using ObjCList<ObjCProtocolDecl>::set;
  96. public:
  97. ObjCProtocolList() = default;
  98. using loc_iterator = const SourceLocation *;
  99. loc_iterator loc_begin() const { return Locations; }
  100. loc_iterator loc_end() const { return Locations + size(); }
  101. void set(ObjCProtocolDecl* const* InList, unsigned Elts,
  102. const SourceLocation *Locs, ASTContext &Ctx);
  103. };
  104. /// ObjCMethodDecl - Represents an instance or class method declaration.
  105. /// ObjC methods can be declared within 4 contexts: class interfaces,
  106. /// categories, protocols, and class implementations. While C++ member
  107. /// functions leverage C syntax, Objective-C method syntax is modeled after
  108. /// Smalltalk (using colons to specify argument types/expressions).
  109. /// Here are some brief examples:
  110. ///
  111. /// Setter/getter instance methods:
  112. /// - (void)setMenu:(NSMenu *)menu;
  113. /// - (NSMenu *)menu;
  114. ///
  115. /// Instance method that takes 2 NSView arguments:
  116. /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
  117. ///
  118. /// Getter class method:
  119. /// + (NSMenu *)defaultMenu;
  120. ///
  121. /// A selector represents a unique name for a method. The selector names for
  122. /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
  123. ///
  124. class ObjCMethodDecl : public NamedDecl, public DeclContext {
  125. // This class stores some data in DeclContext::ObjCMethodDeclBits
  126. // to save some space. Use the provided accessors to access it.
  127. public:
  128. enum ImplementationControl { None, Required, Optional };
  129. private:
  130. /// Return type of this method.
  131. QualType MethodDeclType;
  132. /// Type source information for the return type.
  133. TypeSourceInfo *ReturnTInfo;
  134. /// Array of ParmVarDecls for the formal parameters of this method
  135. /// and optionally followed by selector locations.
  136. void *ParamsAndSelLocs = nullptr;
  137. unsigned NumParams = 0;
  138. /// List of attributes for this method declaration.
  139. SourceLocation DeclEndLoc; // the location of the ';' or '{'.
  140. /// The following are only used for method definitions, null otherwise.
  141. LazyDeclStmtPtr Body;
  142. /// SelfDecl - Decl for the implicit self parameter. This is lazily
  143. /// constructed by createImplicitParams.
  144. ImplicitParamDecl *SelfDecl = nullptr;
  145. /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
  146. /// constructed by createImplicitParams.
  147. ImplicitParamDecl *CmdDecl = nullptr;
  148. ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
  149. Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
  150. DeclContext *contextDecl, bool isInstance = true,
  151. bool isVariadic = false, bool isPropertyAccessor = false,
  152. bool isSynthesizedAccessorStub = false,
  153. bool isImplicitlyDeclared = false, bool isDefined = false,
  154. ImplementationControl impControl = None,
  155. bool HasRelatedResultType = false);
  156. SelectorLocationsKind getSelLocsKind() const {
  157. return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
  158. }
  159. void setSelLocsKind(SelectorLocationsKind Kind) {
  160. ObjCMethodDeclBits.SelLocsKind = Kind;
  161. }
  162. bool hasStandardSelLocs() const {
  163. return getSelLocsKind() != SelLoc_NonStandard;
  164. }
  165. /// Get a pointer to the stored selector identifiers locations array.
  166. /// No locations will be stored if HasStandardSelLocs is true.
  167. SourceLocation *getStoredSelLocs() {
  168. return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
  169. }
  170. const SourceLocation *getStoredSelLocs() const {
  171. return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
  172. }
  173. /// Get a pointer to the stored selector identifiers locations array.
  174. /// No locations will be stored if HasStandardSelLocs is true.
  175. ParmVarDecl **getParams() {
  176. return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
  177. }
  178. const ParmVarDecl *const *getParams() const {
  179. return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
  180. }
  181. /// Get the number of stored selector identifiers locations.
  182. /// No locations will be stored if HasStandardSelLocs is true.
  183. unsigned getNumStoredSelLocs() const {
  184. if (hasStandardSelLocs())
  185. return 0;
  186. return getNumSelectorLocs();
  187. }
  188. void setParamsAndSelLocs(ASTContext &C,
  189. ArrayRef<ParmVarDecl*> Params,
  190. ArrayRef<SourceLocation> SelLocs);
  191. /// A definition will return its interface declaration.
  192. /// An interface declaration will return its definition.
  193. /// Otherwise it will return itself.
  194. ObjCMethodDecl *getNextRedeclarationImpl() override;
  195. public:
  196. friend class ASTDeclReader;
  197. friend class ASTDeclWriter;
  198. static ObjCMethodDecl *
  199. Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
  200. Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
  201. DeclContext *contextDecl, bool isInstance = true,
  202. bool isVariadic = false, bool isPropertyAccessor = false,
  203. bool isSynthesizedAccessorStub = false,
  204. bool isImplicitlyDeclared = false, bool isDefined = false,
  205. ImplementationControl impControl = None,
  206. bool HasRelatedResultType = false);
  207. static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  208. ObjCMethodDecl *getCanonicalDecl() override;
  209. const ObjCMethodDecl *getCanonicalDecl() const {
  210. return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
  211. }
  212. ObjCDeclQualifier getObjCDeclQualifier() const {
  213. return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier);
  214. }
  215. void setObjCDeclQualifier(ObjCDeclQualifier QV) {
  216. ObjCMethodDeclBits.objcDeclQualifier = QV;
  217. }
  218. /// Determine whether this method has a result type that is related
  219. /// to the message receiver's type.
  220. bool hasRelatedResultType() const {
  221. return ObjCMethodDeclBits.RelatedResultType;
  222. }
  223. /// Note whether this method has a related result type.
  224. void setRelatedResultType(bool RRT = true) {
  225. ObjCMethodDeclBits.RelatedResultType = RRT;
  226. }
  227. /// True if this is a method redeclaration in the same interface.
  228. bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; }
  229. void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; }
  230. void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
  231. /// True if redeclared in the same interface.
  232. bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; }
  233. void setHasRedeclaration(bool HRD) const {
  234. ObjCMethodDeclBits.HasRedeclaration = HRD;
  235. }
  236. /// Returns the location where the declarator ends. It will be
  237. /// the location of ';' for a method declaration and the location of '{'
  238. /// for a method definition.
  239. SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
  240. // Location information, modeled after the Stmt API.
  241. SourceLocation getBeginLoc() const LLVM_READONLY { return getLocation(); }
  242. SourceLocation getEndLoc() const LLVM_READONLY;
  243. SourceRange getSourceRange() const override LLVM_READONLY {
  244. return SourceRange(getLocation(), getEndLoc());
  245. }
  246. SourceLocation getSelectorStartLoc() const {
  247. if (isImplicit())
  248. return getBeginLoc();
  249. return getSelectorLoc(0);
  250. }
  251. SourceLocation getSelectorLoc(unsigned Index) const {
  252. assert(Index < getNumSelectorLocs() && "Index out of range!");
  253. if (hasStandardSelLocs())
  254. return getStandardSelectorLoc(Index, getSelector(),
  255. getSelLocsKind() == SelLoc_StandardWithSpace,
  256. parameters(),
  257. DeclEndLoc);
  258. return getStoredSelLocs()[Index];
  259. }
  260. void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
  261. unsigned getNumSelectorLocs() const {
  262. if (isImplicit())
  263. return 0;
  264. Selector Sel = getSelector();
  265. if (Sel.isUnarySelector())
  266. return 1;
  267. return Sel.getNumArgs();
  268. }
  269. ObjCInterfaceDecl *getClassInterface();
  270. const ObjCInterfaceDecl *getClassInterface() const {
  271. return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
  272. }
  273. /// If this method is declared or implemented in a category, return
  274. /// that category.
  275. ObjCCategoryDecl *getCategory();
  276. const ObjCCategoryDecl *getCategory() const {
  277. return const_cast<ObjCMethodDecl*>(this)->getCategory();
  278. }
  279. Selector getSelector() const { return getDeclName().getObjCSelector(); }
  280. QualType getReturnType() const { return MethodDeclType; }
  281. void setReturnType(QualType T) { MethodDeclType = T; }
  282. SourceRange getReturnTypeSourceRange() const;
  283. /// Determine the type of an expression that sends a message to this
  284. /// function. This replaces the type parameters with the types they would
  285. /// get if the receiver was parameterless (e.g. it may replace the type
  286. /// parameter with 'id').
  287. QualType getSendResultType() const;
  288. /// Determine the type of an expression that sends a message to this
  289. /// function with the given receiver type.
  290. QualType getSendResultType(QualType receiverType) const;
  291. TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
  292. void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
  293. // Iterator access to formal parameters.
  294. unsigned param_size() const { return NumParams; }
  295. using param_const_iterator = const ParmVarDecl *const *;
  296. using param_iterator = ParmVarDecl *const *;
  297. using param_range = llvm::iterator_range<param_iterator>;
  298. using param_const_range = llvm::iterator_range<param_const_iterator>;
  299. param_const_iterator param_begin() const {
  300. return param_const_iterator(getParams());
  301. }
  302. param_const_iterator param_end() const {
  303. return param_const_iterator(getParams() + NumParams);
  304. }
  305. param_iterator param_begin() { return param_iterator(getParams()); }
  306. param_iterator param_end() { return param_iterator(getParams() + NumParams); }
  307. // This method returns and of the parameters which are part of the selector
  308. // name mangling requirements.
  309. param_const_iterator sel_param_end() const {
  310. return param_begin() + getSelector().getNumArgs();
  311. }
  312. // ArrayRef access to formal parameters. This should eventually
  313. // replace the iterator interface above.
  314. ArrayRef<ParmVarDecl*> parameters() const {
  315. return llvm::ArrayRef(const_cast<ParmVarDecl **>(getParams()), NumParams);
  316. }
  317. ParmVarDecl *getParamDecl(unsigned Idx) {
  318. assert(Idx < NumParams && "Index out of bounds!");
  319. return getParams()[Idx];
  320. }
  321. const ParmVarDecl *getParamDecl(unsigned Idx) const {
  322. return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx);
  323. }
  324. /// Sets the method's parameters and selector source locations.
  325. /// If the method is implicit (not coming from source) \p SelLocs is
  326. /// ignored.
  327. void setMethodParams(ASTContext &C, ArrayRef<ParmVarDecl *> Params,
  328. ArrayRef<SourceLocation> SelLocs = std::nullopt);
  329. // Iterator access to parameter types.
  330. struct GetTypeFn {
  331. QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); }
  332. };
  333. using param_type_iterator =
  334. llvm::mapped_iterator<param_const_iterator, GetTypeFn>;
  335. param_type_iterator param_type_begin() const {
  336. return llvm::map_iterator(param_begin(), GetTypeFn());
  337. }
  338. param_type_iterator param_type_end() const {
  339. return llvm::map_iterator(param_end(), GetTypeFn());
  340. }
  341. /// createImplicitParams - Used to lazily create the self and cmd
  342. /// implicit parameters. This must be called prior to using getSelfDecl()
  343. /// or getCmdDecl(). The call is ignored if the implicit parameters
  344. /// have already been created.
  345. void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
  346. /// \return the type for \c self and set \arg selfIsPseudoStrong and
  347. /// \arg selfIsConsumed accordingly.
  348. QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
  349. bool &selfIsPseudoStrong, bool &selfIsConsumed) const;
  350. ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
  351. void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
  352. ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
  353. void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
  354. /// Determines the family of this method.
  355. ObjCMethodFamily getMethodFamily() const;
  356. bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; }
  357. void setInstanceMethod(bool isInst) {
  358. ObjCMethodDeclBits.IsInstance = isInst;
  359. }
  360. bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; }
  361. void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; }
  362. bool isClassMethod() const { return !isInstanceMethod(); }
  363. bool isPropertyAccessor() const {
  364. return ObjCMethodDeclBits.IsPropertyAccessor;
  365. }
  366. void setPropertyAccessor(bool isAccessor) {
  367. ObjCMethodDeclBits.IsPropertyAccessor = isAccessor;
  368. }
  369. bool isSynthesizedAccessorStub() const {
  370. return ObjCMethodDeclBits.IsSynthesizedAccessorStub;
  371. }
  372. void setSynthesizedAccessorStub(bool isSynthesizedAccessorStub) {
  373. ObjCMethodDeclBits.IsSynthesizedAccessorStub = isSynthesizedAccessorStub;
  374. }
  375. bool isDefined() const { return ObjCMethodDeclBits.IsDefined; }
  376. void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; }
  377. /// Whether this method overrides any other in the class hierarchy.
  378. ///
  379. /// A method is said to override any method in the class's
  380. /// base classes, its protocols, or its categories' protocols, that has
  381. /// the same selector and is of the same kind (class or instance).
  382. /// A method in an implementation is not considered as overriding the same
  383. /// method in the interface or its categories.
  384. bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; }
  385. void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; }
  386. /// Return overridden methods for the given \p Method.
  387. ///
  388. /// An ObjC method is considered to override any method in the class's
  389. /// base classes (and base's categories), its protocols, or its categories'
  390. /// protocols, that has
  391. /// the same selector and is of the same kind (class or instance).
  392. /// A method in an implementation is not considered as overriding the same
  393. /// method in the interface or its categories.
  394. void getOverriddenMethods(
  395. SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
  396. /// True if the method was a definition but its body was skipped.
  397. bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; }
  398. void setHasSkippedBody(bool Skipped = true) {
  399. ObjCMethodDeclBits.HasSkippedBody = Skipped;
  400. }
  401. /// True if the method is tagged as objc_direct
  402. bool isDirectMethod() const;
  403. /// True if the method has a parameter that's destroyed in the callee.
  404. bool hasParamDestroyedInCallee() const;
  405. /// Returns the property associated with this method's selector.
  406. ///
  407. /// Note that even if this particular method is not marked as a property
  408. /// accessor, it is still possible for it to match a property declared in a
  409. /// superclass. Pass \c false if you only want to check the current class.
  410. const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
  411. // Related to protocols declared in \@protocol
  412. void setDeclImplementation(ImplementationControl ic) {
  413. ObjCMethodDeclBits.DeclImplementation = ic;
  414. }
  415. ImplementationControl getImplementationControl() const {
  416. return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
  417. }
  418. bool isOptional() const {
  419. return getImplementationControl() == Optional;
  420. }
  421. /// Returns true if this specific method declaration is marked with the
  422. /// designated initializer attribute.
  423. bool isThisDeclarationADesignatedInitializer() const;
  424. /// Returns true if the method selector resolves to a designated initializer
  425. /// in the class's interface.
  426. ///
  427. /// \param InitMethod if non-null and the function returns true, it receives
  428. /// the method declaration that was marked with the designated initializer
  429. /// attribute.
  430. bool isDesignatedInitializerForTheInterface(
  431. const ObjCMethodDecl **InitMethod = nullptr) const;
  432. /// Determine whether this method has a body.
  433. bool hasBody() const override { return Body.isValid(); }
  434. /// Retrieve the body of this method, if it has one.
  435. Stmt *getBody() const override;
  436. void setLazyBody(uint64_t Offset) { Body = Offset; }
  437. CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
  438. void setBody(Stmt *B) { Body = B; }
  439. /// Returns whether this specific method is a definition.
  440. bool isThisDeclarationADefinition() const { return hasBody(); }
  441. /// Is this method defined in the NSObject base class?
  442. bool definedInNSObject(const ASTContext &) const;
  443. // Implement isa/cast/dyncast/etc.
  444. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  445. static bool classofKind(Kind K) { return K == ObjCMethod; }
  446. static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
  447. return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
  448. }
  449. static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
  450. return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
  451. }
  452. };
  453. /// Describes the variance of a given generic parameter.
  454. enum class ObjCTypeParamVariance : uint8_t {
  455. /// The parameter is invariant: must match exactly.
  456. Invariant,
  457. /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
  458. /// the type parameter is covariant and T is a subtype of U.
  459. Covariant,
  460. /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
  461. /// when the type parameter is covariant and U is a subtype of T.
  462. Contravariant,
  463. };
  464. /// Represents the declaration of an Objective-C type parameter.
  465. ///
  466. /// \code
  467. /// @interface NSDictionary<Key : id<NSCopying>, Value>
  468. /// @end
  469. /// \endcode
  470. ///
  471. /// In the example above, both \c Key and \c Value are represented by
  472. /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
  473. /// while \c Value gets an implicit bound of \c id.
  474. ///
  475. /// Objective-C type parameters are typedef-names in the grammar,
  476. class ObjCTypeParamDecl : public TypedefNameDecl {
  477. /// Index of this type parameter in the type parameter list.
  478. unsigned Index : 14;
  479. /// The variance of the type parameter.
  480. unsigned Variance : 2;
  481. /// The location of the variance, if any.
  482. SourceLocation VarianceLoc;
  483. /// The location of the ':', which will be valid when the bound was
  484. /// explicitly specified.
  485. SourceLocation ColonLoc;
  486. ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
  487. ObjCTypeParamVariance variance, SourceLocation varianceLoc,
  488. unsigned index,
  489. SourceLocation nameLoc, IdentifierInfo *name,
  490. SourceLocation colonLoc, TypeSourceInfo *boundInfo)
  491. : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
  492. boundInfo),
  493. Index(index), Variance(static_cast<unsigned>(variance)),
  494. VarianceLoc(varianceLoc), ColonLoc(colonLoc) {}
  495. void anchor() override;
  496. public:
  497. friend class ASTDeclReader;
  498. friend class ASTDeclWriter;
  499. static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
  500. ObjCTypeParamVariance variance,
  501. SourceLocation varianceLoc,
  502. unsigned index,
  503. SourceLocation nameLoc,
  504. IdentifierInfo *name,
  505. SourceLocation colonLoc,
  506. TypeSourceInfo *boundInfo);
  507. static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
  508. SourceRange getSourceRange() const override LLVM_READONLY;
  509. /// Determine the variance of this type parameter.
  510. ObjCTypeParamVariance getVariance() const {
  511. return static_cast<ObjCTypeParamVariance>(Variance);
  512. }
  513. /// Set the variance of this type parameter.
  514. void setVariance(ObjCTypeParamVariance variance) {
  515. Variance = static_cast<unsigned>(variance);
  516. }
  517. /// Retrieve the location of the variance keyword.
  518. SourceLocation getVarianceLoc() const { return VarianceLoc; }
  519. /// Retrieve the index into its type parameter list.
  520. unsigned getIndex() const { return Index; }
  521. /// Whether this type parameter has an explicitly-written type bound, e.g.,
  522. /// "T : NSView".
  523. bool hasExplicitBound() const { return ColonLoc.isValid(); }
  524. /// Retrieve the location of the ':' separating the type parameter name
  525. /// from the explicitly-specified bound.
  526. SourceLocation getColonLoc() const { return ColonLoc; }
  527. // Implement isa/cast/dyncast/etc.
  528. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  529. static bool classofKind(Kind K) { return K == ObjCTypeParam; }
  530. };
  531. /// Stores a list of Objective-C type parameters for a parameterized class
  532. /// or a category/extension thereof.
  533. ///
  534. /// \code
  535. /// @interface NSArray<T> // stores the <T>
  536. /// @end
  537. /// \endcode
  538. class ObjCTypeParamList final
  539. : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
  540. /// Location of the left and right angle brackets.
  541. SourceRange Brackets;
  542. /// The number of parameters in the list, which are tail-allocated.
  543. unsigned NumParams;
  544. ObjCTypeParamList(SourceLocation lAngleLoc,
  545. ArrayRef<ObjCTypeParamDecl *> typeParams,
  546. SourceLocation rAngleLoc);
  547. public:
  548. friend TrailingObjects;
  549. /// Create a new Objective-C type parameter list.
  550. static ObjCTypeParamList *create(ASTContext &ctx,
  551. SourceLocation lAngleLoc,
  552. ArrayRef<ObjCTypeParamDecl *> typeParams,
  553. SourceLocation rAngleLoc);
  554. /// Iterate through the type parameters in the list.
  555. using iterator = ObjCTypeParamDecl **;
  556. iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
  557. iterator end() { return begin() + size(); }
  558. /// Determine the number of type parameters in this list.
  559. unsigned size() const { return NumParams; }
  560. // Iterate through the type parameters in the list.
  561. using const_iterator = ObjCTypeParamDecl * const *;
  562. const_iterator begin() const {
  563. return getTrailingObjects<ObjCTypeParamDecl *>();
  564. }
  565. const_iterator end() const {
  566. return begin() + size();
  567. }
  568. ObjCTypeParamDecl *front() const {
  569. assert(size() > 0 && "empty Objective-C type parameter list");
  570. return *begin();
  571. }
  572. ObjCTypeParamDecl *back() const {
  573. assert(size() > 0 && "empty Objective-C type parameter list");
  574. return *(end() - 1);
  575. }
  576. SourceLocation getLAngleLoc() const { return Brackets.getBegin(); }
  577. SourceLocation getRAngleLoc() const { return Brackets.getEnd(); }
  578. SourceRange getSourceRange() const { return Brackets; }
  579. /// Gather the default set of type arguments to be substituted for
  580. /// these type parameters when dealing with an unspecialized type.
  581. void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
  582. };
  583. enum class ObjCPropertyQueryKind : uint8_t {
  584. OBJC_PR_query_unknown = 0x00,
  585. OBJC_PR_query_instance,
  586. OBJC_PR_query_class
  587. };
  588. /// Represents one property declaration in an Objective-C interface.
  589. ///
  590. /// For example:
  591. /// \code{.mm}
  592. /// \@property (assign, readwrite) int MyProperty;
  593. /// \endcode
  594. class ObjCPropertyDecl : public NamedDecl {
  595. void anchor() override;
  596. public:
  597. enum SetterKind { Assign, Retain, Copy, Weak };
  598. enum PropertyControl { None, Required, Optional };
  599. private:
  600. // location of \@property
  601. SourceLocation AtLoc;
  602. // location of '(' starting attribute list or null.
  603. SourceLocation LParenLoc;
  604. QualType DeclType;
  605. TypeSourceInfo *DeclTypeSourceInfo;
  606. unsigned PropertyAttributes : NumObjCPropertyAttrsBits;
  607. unsigned PropertyAttributesAsWritten : NumObjCPropertyAttrsBits;
  608. // \@required/\@optional
  609. unsigned PropertyImplementation : 2;
  610. // getter name of NULL if no getter
  611. Selector GetterName;
  612. // setter name of NULL if no setter
  613. Selector SetterName;
  614. // location of the getter attribute's value
  615. SourceLocation GetterNameLoc;
  616. // location of the setter attribute's value
  617. SourceLocation SetterNameLoc;
  618. // Declaration of getter instance method
  619. ObjCMethodDecl *GetterMethodDecl = nullptr;
  620. // Declaration of setter instance method
  621. ObjCMethodDecl *SetterMethodDecl = nullptr;
  622. // Synthesize ivar for this property
  623. ObjCIvarDecl *PropertyIvarDecl = nullptr;
  624. ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
  625. SourceLocation AtLocation, SourceLocation LParenLocation,
  626. QualType T, TypeSourceInfo *TSI, PropertyControl propControl)
  627. : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
  628. LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
  629. PropertyAttributes(ObjCPropertyAttribute::kind_noattr),
  630. PropertyAttributesAsWritten(ObjCPropertyAttribute::kind_noattr),
  631. PropertyImplementation(propControl) {}
  632. public:
  633. static ObjCPropertyDecl *
  634. Create(ASTContext &C, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
  635. SourceLocation AtLocation, SourceLocation LParenLocation, QualType T,
  636. TypeSourceInfo *TSI, PropertyControl propControl = None);
  637. static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  638. SourceLocation getAtLoc() const { return AtLoc; }
  639. void setAtLoc(SourceLocation L) { AtLoc = L; }
  640. SourceLocation getLParenLoc() const { return LParenLoc; }
  641. void setLParenLoc(SourceLocation L) { LParenLoc = L; }
  642. TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
  643. QualType getType() const { return DeclType; }
  644. void setType(QualType T, TypeSourceInfo *TSI) {
  645. DeclType = T;
  646. DeclTypeSourceInfo = TSI;
  647. }
  648. /// Retrieve the type when this property is used with a specific base object
  649. /// type.
  650. QualType getUsageType(QualType objectType) const;
  651. ObjCPropertyAttribute::Kind getPropertyAttributes() const {
  652. return ObjCPropertyAttribute::Kind(PropertyAttributes);
  653. }
  654. void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) {
  655. PropertyAttributes |= PRVal;
  656. }
  657. void overwritePropertyAttributes(unsigned PRVal) {
  658. PropertyAttributes = PRVal;
  659. }
  660. ObjCPropertyAttribute::Kind getPropertyAttributesAsWritten() const {
  661. return ObjCPropertyAttribute::Kind(PropertyAttributesAsWritten);
  662. }
  663. void setPropertyAttributesAsWritten(ObjCPropertyAttribute::Kind PRVal) {
  664. PropertyAttributesAsWritten = PRVal;
  665. }
  666. // Helper methods for accessing attributes.
  667. /// isReadOnly - Return true iff the property has a setter.
  668. bool isReadOnly() const {
  669. return (PropertyAttributes & ObjCPropertyAttribute::kind_readonly);
  670. }
  671. /// isAtomic - Return true if the property is atomic.
  672. bool isAtomic() const {
  673. return (PropertyAttributes & ObjCPropertyAttribute::kind_atomic);
  674. }
  675. /// isRetaining - Return true if the property retains its value.
  676. bool isRetaining() const {
  677. return (PropertyAttributes & (ObjCPropertyAttribute::kind_retain |
  678. ObjCPropertyAttribute::kind_strong |
  679. ObjCPropertyAttribute::kind_copy));
  680. }
  681. bool isInstanceProperty() const { return !isClassProperty(); }
  682. bool isClassProperty() const {
  683. return PropertyAttributes & ObjCPropertyAttribute::kind_class;
  684. }
  685. bool isDirectProperty() const;
  686. ObjCPropertyQueryKind getQueryKind() const {
  687. return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
  688. ObjCPropertyQueryKind::OBJC_PR_query_instance;
  689. }
  690. static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
  691. return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
  692. ObjCPropertyQueryKind::OBJC_PR_query_instance;
  693. }
  694. /// getSetterKind - Return the method used for doing assignment in
  695. /// the property setter. This is only valid if the property has been
  696. /// defined to have a setter.
  697. SetterKind getSetterKind() const {
  698. if (PropertyAttributes & ObjCPropertyAttribute::kind_strong)
  699. return getType()->isBlockPointerType() ? Copy : Retain;
  700. if (PropertyAttributes & ObjCPropertyAttribute::kind_retain)
  701. return Retain;
  702. if (PropertyAttributes & ObjCPropertyAttribute::kind_copy)
  703. return Copy;
  704. if (PropertyAttributes & ObjCPropertyAttribute::kind_weak)
  705. return Weak;
  706. return Assign;
  707. }
  708. Selector getGetterName() const { return GetterName; }
  709. SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
  710. void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
  711. GetterName = Sel;
  712. GetterNameLoc = Loc;
  713. }
  714. Selector getSetterName() const { return SetterName; }
  715. SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
  716. void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
  717. SetterName = Sel;
  718. SetterNameLoc = Loc;
  719. }
  720. ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
  721. void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
  722. ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
  723. void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
  724. // Related to \@optional/\@required declared in \@protocol
  725. void setPropertyImplementation(PropertyControl pc) {
  726. PropertyImplementation = pc;
  727. }
  728. PropertyControl getPropertyImplementation() const {
  729. return PropertyControl(PropertyImplementation);
  730. }
  731. bool isOptional() const {
  732. return getPropertyImplementation() == PropertyControl::Optional;
  733. }
  734. void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
  735. PropertyIvarDecl = Ivar;
  736. }
  737. ObjCIvarDecl *getPropertyIvarDecl() const {
  738. return PropertyIvarDecl;
  739. }
  740. SourceRange getSourceRange() const override LLVM_READONLY {
  741. return SourceRange(AtLoc, getLocation());
  742. }
  743. /// Get the default name of the synthesized ivar.
  744. IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
  745. /// Lookup a property by name in the specified DeclContext.
  746. static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
  747. const IdentifierInfo *propertyID,
  748. ObjCPropertyQueryKind queryKind);
  749. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  750. static bool classofKind(Kind K) { return K == ObjCProperty; }
  751. };
  752. /// ObjCContainerDecl - Represents a container for method declarations.
  753. /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
  754. /// ObjCProtocolDecl, and ObjCImplDecl.
  755. ///
  756. class ObjCContainerDecl : public NamedDecl, public DeclContext {
  757. // This class stores some data in DeclContext::ObjCContainerDeclBits
  758. // to save some space. Use the provided accessors to access it.
  759. // These two locations in the range mark the end of the method container.
  760. // The first points to the '@' token, and the second to the 'end' token.
  761. SourceRange AtEnd;
  762. void anchor() override;
  763. public:
  764. ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id,
  765. SourceLocation nameLoc, SourceLocation atStartLoc);
  766. // Iterator access to instance/class properties.
  767. using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>;
  768. using prop_range =
  769. llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>;
  770. prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
  771. prop_iterator prop_begin() const {
  772. return prop_iterator(decls_begin());
  773. }
  774. prop_iterator prop_end() const {
  775. return prop_iterator(decls_end());
  776. }
  777. using instprop_iterator =
  778. filtered_decl_iterator<ObjCPropertyDecl,
  779. &ObjCPropertyDecl::isInstanceProperty>;
  780. using instprop_range = llvm::iterator_range<instprop_iterator>;
  781. instprop_range instance_properties() const {
  782. return instprop_range(instprop_begin(), instprop_end());
  783. }
  784. instprop_iterator instprop_begin() const {
  785. return instprop_iterator(decls_begin());
  786. }
  787. instprop_iterator instprop_end() const {
  788. return instprop_iterator(decls_end());
  789. }
  790. using classprop_iterator =
  791. filtered_decl_iterator<ObjCPropertyDecl,
  792. &ObjCPropertyDecl::isClassProperty>;
  793. using classprop_range = llvm::iterator_range<classprop_iterator>;
  794. classprop_range class_properties() const {
  795. return classprop_range(classprop_begin(), classprop_end());
  796. }
  797. classprop_iterator classprop_begin() const {
  798. return classprop_iterator(decls_begin());
  799. }
  800. classprop_iterator classprop_end() const {
  801. return classprop_iterator(decls_end());
  802. }
  803. // Iterator access to instance/class methods.
  804. using method_iterator = specific_decl_iterator<ObjCMethodDecl>;
  805. using method_range =
  806. llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>;
  807. method_range methods() const {
  808. return method_range(meth_begin(), meth_end());
  809. }
  810. method_iterator meth_begin() const {
  811. return method_iterator(decls_begin());
  812. }
  813. method_iterator meth_end() const {
  814. return method_iterator(decls_end());
  815. }
  816. using instmeth_iterator =
  817. filtered_decl_iterator<ObjCMethodDecl,
  818. &ObjCMethodDecl::isInstanceMethod>;
  819. using instmeth_range = llvm::iterator_range<instmeth_iterator>;
  820. instmeth_range instance_methods() const {
  821. return instmeth_range(instmeth_begin(), instmeth_end());
  822. }
  823. instmeth_iterator instmeth_begin() const {
  824. return instmeth_iterator(decls_begin());
  825. }
  826. instmeth_iterator instmeth_end() const {
  827. return instmeth_iterator(decls_end());
  828. }
  829. using classmeth_iterator =
  830. filtered_decl_iterator<ObjCMethodDecl,
  831. &ObjCMethodDecl::isClassMethod>;
  832. using classmeth_range = llvm::iterator_range<classmeth_iterator>;
  833. classmeth_range class_methods() const {
  834. return classmeth_range(classmeth_begin(), classmeth_end());
  835. }
  836. classmeth_iterator classmeth_begin() const {
  837. return classmeth_iterator(decls_begin());
  838. }
  839. classmeth_iterator classmeth_end() const {
  840. return classmeth_iterator(decls_end());
  841. }
  842. // Get the local instance/class method declared in this interface.
  843. ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
  844. bool AllowHidden = false) const;
  845. ObjCMethodDecl *getInstanceMethod(Selector Sel,
  846. bool AllowHidden = false) const {
  847. return getMethod(Sel, true/*isInstance*/, AllowHidden);
  848. }
  849. ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
  850. return getMethod(Sel, false/*isInstance*/, AllowHidden);
  851. }
  852. bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
  853. ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
  854. ObjCPropertyDecl *getProperty(const IdentifierInfo *Id,
  855. bool IsInstance) const;
  856. ObjCPropertyDecl *
  857. FindPropertyDeclaration(const IdentifierInfo *PropertyId,
  858. ObjCPropertyQueryKind QueryKind) const;
  859. using PropertyMap =
  860. llvm::MapVector<std::pair<IdentifierInfo *, unsigned /*isClassProperty*/>,
  861. ObjCPropertyDecl *>;
  862. using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>;
  863. using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>;
  864. /// This routine collects list of properties to be implemented in the class.
  865. /// This includes, class's and its conforming protocols' properties.
  866. /// Note, the superclass's properties are not included in the list.
  867. virtual void collectPropertiesToImplement(PropertyMap &PM) const {}
  868. SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
  869. void setAtStartLoc(SourceLocation Loc) {
  870. ObjCContainerDeclBits.AtStart = Loc;
  871. }
  872. // Marks the end of the container.
  873. SourceRange getAtEndRange() const { return AtEnd; }
  874. void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; }
  875. SourceRange getSourceRange() const override LLVM_READONLY {
  876. return SourceRange(getAtStartLoc(), getAtEndRange().getEnd());
  877. }
  878. // Implement isa/cast/dyncast/etc.
  879. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  880. static bool classofKind(Kind K) {
  881. return K >= firstObjCContainer &&
  882. K <= lastObjCContainer;
  883. }
  884. static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
  885. return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
  886. }
  887. static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
  888. return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
  889. }
  890. };
  891. /// Represents an ObjC class declaration.
  892. ///
  893. /// For example:
  894. ///
  895. /// \code
  896. /// // MostPrimitive declares no super class (not particularly useful).
  897. /// \@interface MostPrimitive
  898. /// // no instance variables or methods.
  899. /// \@end
  900. ///
  901. /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
  902. /// \@interface NSResponder : NSObject \<NSCoding>
  903. /// { // instance variables are represented by ObjCIvarDecl.
  904. /// id nextResponder; // nextResponder instance variable.
  905. /// }
  906. /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
  907. /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
  908. /// \@end // to an NSEvent.
  909. /// \endcode
  910. ///
  911. /// Unlike C/C++, forward class declarations are accomplished with \@class.
  912. /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
  913. /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
  914. /// typically inherit from NSObject (an exception is NSProxy).
  915. ///
  916. class ObjCInterfaceDecl : public ObjCContainerDecl
  917. , public Redeclarable<ObjCInterfaceDecl> {
  918. friend class ASTContext;
  919. friend class ODRDiagsEmitter;
  920. /// TypeForDecl - This indicates the Type object that represents this
  921. /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
  922. mutable const Type *TypeForDecl = nullptr;
  923. struct DefinitionData {
  924. /// The definition of this class, for quick access from any
  925. /// declaration.
  926. ObjCInterfaceDecl *Definition = nullptr;
  927. /// When non-null, this is always an ObjCObjectType.
  928. TypeSourceInfo *SuperClassTInfo = nullptr;
  929. /// Protocols referenced in the \@interface declaration
  930. ObjCProtocolList ReferencedProtocols;
  931. /// Protocols reference in both the \@interface and class extensions.
  932. ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
  933. /// List of categories and class extensions defined for this class.
  934. ///
  935. /// Categories are stored as a linked list in the AST, since the categories
  936. /// and class extensions come long after the initial interface declaration,
  937. /// and we avoid dynamically-resized arrays in the AST wherever possible.
  938. ObjCCategoryDecl *CategoryList = nullptr;
  939. /// IvarList - List of all ivars defined by this class; including class
  940. /// extensions and implementation. This list is built lazily.
  941. ObjCIvarDecl *IvarList = nullptr;
  942. /// Indicates that the contents of this Objective-C class will be
  943. /// completed by the external AST source when required.
  944. mutable unsigned ExternallyCompleted : 1;
  945. /// Indicates that the ivar cache does not yet include ivars
  946. /// declared in the implementation.
  947. mutable unsigned IvarListMissingImplementation : 1;
  948. /// Indicates that this interface decl contains at least one initializer
  949. /// marked with the 'objc_designated_initializer' attribute.
  950. unsigned HasDesignatedInitializers : 1;
  951. enum InheritedDesignatedInitializersState {
  952. /// We didn't calculate whether the designated initializers should be
  953. /// inherited or not.
  954. IDI_Unknown = 0,
  955. /// Designated initializers are inherited for the super class.
  956. IDI_Inherited = 1,
  957. /// The class does not inherit designated initializers.
  958. IDI_NotInherited = 2
  959. };
  960. /// One of the \c InheritedDesignatedInitializersState enumeratos.
  961. mutable unsigned InheritedDesignatedInitializers : 2;
  962. /// Tracks whether a ODR hash has been computed for this interface.
  963. unsigned HasODRHash : 1;
  964. /// A hash of parts of the class to help in ODR checking.
  965. unsigned ODRHash = 0;
  966. /// The location of the last location in this declaration, before
  967. /// the properties/methods. For example, this will be the '>', '}', or
  968. /// identifier,
  969. SourceLocation EndLoc;
  970. DefinitionData()
  971. : ExternallyCompleted(false), IvarListMissingImplementation(true),
  972. HasDesignatedInitializers(false),
  973. InheritedDesignatedInitializers(IDI_Unknown), HasODRHash(false) {}
  974. };
  975. /// The type parameters associated with this class, if any.
  976. ObjCTypeParamList *TypeParamList = nullptr;
  977. /// Contains a pointer to the data associated with this class,
  978. /// which will be NULL if this class has not yet been defined.
  979. ///
  980. /// The bit indicates when we don't need to check for out-of-date
  981. /// declarations. It will be set unless modules are enabled.
  982. llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
  983. ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
  984. IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
  985. SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
  986. bool IsInternal);
  987. void anchor() override;
  988. void LoadExternalDefinition() const;
  989. DefinitionData &data() const {
  990. assert(Data.getPointer() && "Declaration has no definition!");
  991. return *Data.getPointer();
  992. }
  993. /// Allocate the definition data for this class.
  994. void allocateDefinitionData();
  995. using redeclarable_base = Redeclarable<ObjCInterfaceDecl>;
  996. ObjCInterfaceDecl *getNextRedeclarationImpl() override {
  997. return getNextRedeclaration();
  998. }
  999. ObjCInterfaceDecl *getPreviousDeclImpl() override {
  1000. return getPreviousDecl();
  1001. }
  1002. ObjCInterfaceDecl *getMostRecentDeclImpl() override {
  1003. return getMostRecentDecl();
  1004. }
  1005. public:
  1006. static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
  1007. SourceLocation atLoc,
  1008. IdentifierInfo *Id,
  1009. ObjCTypeParamList *typeParamList,
  1010. ObjCInterfaceDecl *PrevDecl,
  1011. SourceLocation ClassLoc = SourceLocation(),
  1012. bool isInternal = false);
  1013. static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
  1014. /// Retrieve the type parameters of this class.
  1015. ///
  1016. /// This function looks for a type parameter list for the given
  1017. /// class; if the class has been declared (with \c \@class) but not
  1018. /// defined (with \c \@interface), it will search for a declaration that
  1019. /// has type parameters, skipping any declarations that do not.
  1020. ObjCTypeParamList *getTypeParamList() const;
  1021. /// Set the type parameters of this class.
  1022. ///
  1023. /// This function is used by the AST importer, which must import the type
  1024. /// parameters after creating their DeclContext to avoid loops.
  1025. void setTypeParamList(ObjCTypeParamList *TPL);
  1026. /// Retrieve the type parameters written on this particular declaration of
  1027. /// the class.
  1028. ObjCTypeParamList *getTypeParamListAsWritten() const {
  1029. return TypeParamList;
  1030. }
  1031. SourceRange getSourceRange() const override LLVM_READONLY {
  1032. if (isThisDeclarationADefinition())
  1033. return ObjCContainerDecl::getSourceRange();
  1034. return SourceRange(getAtStartLoc(), getLocation());
  1035. }
  1036. /// Indicate that this Objective-C class is complete, but that
  1037. /// the external AST source will be responsible for filling in its contents
  1038. /// when a complete class is required.
  1039. void setExternallyCompleted();
  1040. /// Indicate that this interface decl contains at least one initializer
  1041. /// marked with the 'objc_designated_initializer' attribute.
  1042. void setHasDesignatedInitializers();
  1043. /// Returns true if this interface decl contains at least one initializer
  1044. /// marked with the 'objc_designated_initializer' attribute.
  1045. bool hasDesignatedInitializers() const;
  1046. /// Returns true if this interface decl declares a designated initializer
  1047. /// or it inherites one from its super class.
  1048. bool declaresOrInheritsDesignatedInitializers() const {
  1049. return hasDesignatedInitializers() || inheritsDesignatedInitializers();
  1050. }
  1051. const ObjCProtocolList &getReferencedProtocols() const {
  1052. assert(hasDefinition() && "Caller did not check for forward reference!");
  1053. if (data().ExternallyCompleted)
  1054. LoadExternalDefinition();
  1055. return data().ReferencedProtocols;
  1056. }
  1057. ObjCImplementationDecl *getImplementation() const;
  1058. void setImplementation(ObjCImplementationDecl *ImplD);
  1059. ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
  1060. // Get the local instance/class method declared in a category.
  1061. ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
  1062. ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
  1063. ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
  1064. return isInstance ? getCategoryInstanceMethod(Sel)
  1065. : getCategoryClassMethod(Sel);
  1066. }
  1067. using protocol_iterator = ObjCProtocolList::iterator;
  1068. using protocol_range = llvm::iterator_range<protocol_iterator>;
  1069. protocol_range protocols() const {
  1070. return protocol_range(protocol_begin(), protocol_end());
  1071. }
  1072. protocol_iterator protocol_begin() const {
  1073. // FIXME: Should make sure no callers ever do this.
  1074. if (!hasDefinition())
  1075. return protocol_iterator();
  1076. if (data().ExternallyCompleted)
  1077. LoadExternalDefinition();
  1078. return data().ReferencedProtocols.begin();
  1079. }
  1080. protocol_iterator protocol_end() const {
  1081. // FIXME: Should make sure no callers ever do this.
  1082. if (!hasDefinition())
  1083. return protocol_iterator();
  1084. if (data().ExternallyCompleted)
  1085. LoadExternalDefinition();
  1086. return data().ReferencedProtocols.end();
  1087. }
  1088. using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
  1089. using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
  1090. protocol_loc_range protocol_locs() const {
  1091. return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
  1092. }
  1093. protocol_loc_iterator protocol_loc_begin() const {
  1094. // FIXME: Should make sure no callers ever do this.
  1095. if (!hasDefinition())
  1096. return protocol_loc_iterator();
  1097. if (data().ExternallyCompleted)
  1098. LoadExternalDefinition();
  1099. return data().ReferencedProtocols.loc_begin();
  1100. }
  1101. protocol_loc_iterator protocol_loc_end() const {
  1102. // FIXME: Should make sure no callers ever do this.
  1103. if (!hasDefinition())
  1104. return protocol_loc_iterator();
  1105. if (data().ExternallyCompleted)
  1106. LoadExternalDefinition();
  1107. return data().ReferencedProtocols.loc_end();
  1108. }
  1109. using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator;
  1110. using all_protocol_range = llvm::iterator_range<all_protocol_iterator>;
  1111. all_protocol_range all_referenced_protocols() const {
  1112. return all_protocol_range(all_referenced_protocol_begin(),
  1113. all_referenced_protocol_end());
  1114. }
  1115. all_protocol_iterator all_referenced_protocol_begin() const {
  1116. // FIXME: Should make sure no callers ever do this.
  1117. if (!hasDefinition())
  1118. return all_protocol_iterator();
  1119. if (data().ExternallyCompleted)
  1120. LoadExternalDefinition();
  1121. return data().AllReferencedProtocols.empty()
  1122. ? protocol_begin()
  1123. : data().AllReferencedProtocols.begin();
  1124. }
  1125. all_protocol_iterator all_referenced_protocol_end() const {
  1126. // FIXME: Should make sure no callers ever do this.
  1127. if (!hasDefinition())
  1128. return all_protocol_iterator();
  1129. if (data().ExternallyCompleted)
  1130. LoadExternalDefinition();
  1131. return data().AllReferencedProtocols.empty()
  1132. ? protocol_end()
  1133. : data().AllReferencedProtocols.end();
  1134. }
  1135. using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
  1136. using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
  1137. ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
  1138. ivar_iterator ivar_begin() const {
  1139. if (const ObjCInterfaceDecl *Def = getDefinition())
  1140. return ivar_iterator(Def->decls_begin());
  1141. // FIXME: Should make sure no callers ever do this.
  1142. return ivar_iterator();
  1143. }
  1144. ivar_iterator ivar_end() const {
  1145. if (const ObjCInterfaceDecl *Def = getDefinition())
  1146. return ivar_iterator(Def->decls_end());
  1147. // FIXME: Should make sure no callers ever do this.
  1148. return ivar_iterator();
  1149. }
  1150. unsigned ivar_size() const {
  1151. return std::distance(ivar_begin(), ivar_end());
  1152. }
  1153. bool ivar_empty() const { return ivar_begin() == ivar_end(); }
  1154. ObjCIvarDecl *all_declared_ivar_begin();
  1155. const ObjCIvarDecl *all_declared_ivar_begin() const {
  1156. // Even though this modifies IvarList, it's conceptually const:
  1157. // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
  1158. return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
  1159. }
  1160. void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
  1161. /// setProtocolList - Set the list of protocols that this interface
  1162. /// implements.
  1163. void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
  1164. const SourceLocation *Locs, ASTContext &C) {
  1165. data().ReferencedProtocols.set(List, Num, Locs, C);
  1166. }
  1167. /// mergeClassExtensionProtocolList - Merge class extension's protocol list
  1168. /// into the protocol list for this class.
  1169. void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
  1170. unsigned Num,
  1171. ASTContext &C);
  1172. /// Produce a name to be used for class's metadata. It comes either via
  1173. /// objc_runtime_name attribute or class name.
  1174. StringRef getObjCRuntimeNameAsString() const;
  1175. /// Returns the designated initializers for the interface.
  1176. ///
  1177. /// If this declaration does not have methods marked as designated
  1178. /// initializers then the interface inherits the designated initializers of
  1179. /// its super class.
  1180. void getDesignatedInitializers(
  1181. llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
  1182. /// Returns true if the given selector is a designated initializer for the
  1183. /// interface.
  1184. ///
  1185. /// If this declaration does not have methods marked as designated
  1186. /// initializers then the interface inherits the designated initializers of
  1187. /// its super class.
  1188. ///
  1189. /// \param InitMethod if non-null and the function returns true, it receives
  1190. /// the method that was marked as a designated initializer.
  1191. bool
  1192. isDesignatedInitializer(Selector Sel,
  1193. const ObjCMethodDecl **InitMethod = nullptr) const;
  1194. /// Determine whether this particular declaration of this class is
  1195. /// actually also a definition.
  1196. bool isThisDeclarationADefinition() const {
  1197. return getDefinition() == this;
  1198. }
  1199. /// Determine whether this class has been defined.
  1200. bool hasDefinition() const {
  1201. // If the name of this class is out-of-date, bring it up-to-date, which
  1202. // might bring in a definition.
  1203. // Note: a null value indicates that we don't have a definition and that
  1204. // modules are enabled.
  1205. if (!Data.getOpaqueValue())
  1206. getMostRecentDecl();
  1207. return Data.getPointer();
  1208. }
  1209. /// Retrieve the definition of this class, or NULL if this class
  1210. /// has been forward-declared (with \@class) but not yet defined (with
  1211. /// \@interface).
  1212. ObjCInterfaceDecl *getDefinition() {
  1213. return hasDefinition()? Data.getPointer()->Definition : nullptr;
  1214. }
  1215. /// Retrieve the definition of this class, or NULL if this class
  1216. /// has been forward-declared (with \@class) but not yet defined (with
  1217. /// \@interface).
  1218. const ObjCInterfaceDecl *getDefinition() const {
  1219. return hasDefinition()? Data.getPointer()->Definition : nullptr;
  1220. }
  1221. /// Starts the definition of this Objective-C class, taking it from
  1222. /// a forward declaration (\@class) to a definition (\@interface).
  1223. void startDefinition();
  1224. /// Starts the definition without sharing it with other redeclarations.
  1225. /// Such definition shouldn't be used for anything but only to compare if
  1226. /// a duplicate is compatible with previous definition or if it is
  1227. /// a distinct duplicate.
  1228. void startDuplicateDefinitionForComparison();
  1229. void mergeDuplicateDefinitionWithCommon(const ObjCInterfaceDecl *Definition);
  1230. /// Retrieve the superclass type.
  1231. const ObjCObjectType *getSuperClassType() const {
  1232. if (TypeSourceInfo *TInfo = getSuperClassTInfo())
  1233. return TInfo->getType()->castAs<ObjCObjectType>();
  1234. return nullptr;
  1235. }
  1236. // Retrieve the type source information for the superclass.
  1237. TypeSourceInfo *getSuperClassTInfo() const {
  1238. // FIXME: Should make sure no callers ever do this.
  1239. if (!hasDefinition())
  1240. return nullptr;
  1241. if (data().ExternallyCompleted)
  1242. LoadExternalDefinition();
  1243. return data().SuperClassTInfo;
  1244. }
  1245. // Retrieve the declaration for the superclass of this class, which
  1246. // does not include any type arguments that apply to the superclass.
  1247. ObjCInterfaceDecl *getSuperClass() const;
  1248. void setSuperClass(TypeSourceInfo *superClass) {
  1249. data().SuperClassTInfo = superClass;
  1250. }
  1251. /// Iterator that walks over the list of categories, filtering out
  1252. /// those that do not meet specific criteria.
  1253. ///
  1254. /// This class template is used for the various permutations of category
  1255. /// and extension iterators.
  1256. template<bool (*Filter)(ObjCCategoryDecl *)>
  1257. class filtered_category_iterator {
  1258. ObjCCategoryDecl *Current = nullptr;
  1259. void findAcceptableCategory();
  1260. public:
  1261. using value_type = ObjCCategoryDecl *;
  1262. using reference = value_type;
  1263. using pointer = value_type;
  1264. using difference_type = std::ptrdiff_t;
  1265. using iterator_category = std::input_iterator_tag;
  1266. filtered_category_iterator() = default;
  1267. explicit filtered_category_iterator(ObjCCategoryDecl *Current)
  1268. : Current(Current) {
  1269. findAcceptableCategory();
  1270. }
  1271. reference operator*() const { return Current; }
  1272. pointer operator->() const { return Current; }
  1273. filtered_category_iterator &operator++();
  1274. filtered_category_iterator operator++(int) {
  1275. filtered_category_iterator Tmp = *this;
  1276. ++(*this);
  1277. return Tmp;
  1278. }
  1279. friend bool operator==(filtered_category_iterator X,
  1280. filtered_category_iterator Y) {
  1281. return X.Current == Y.Current;
  1282. }
  1283. friend bool operator!=(filtered_category_iterator X,
  1284. filtered_category_iterator Y) {
  1285. return X.Current != Y.Current;
  1286. }
  1287. };
  1288. private:
  1289. /// Test whether the given category is visible.
  1290. ///
  1291. /// Used in the \c visible_categories_iterator.
  1292. static bool isVisibleCategory(ObjCCategoryDecl *Cat);
  1293. public:
  1294. /// Iterator that walks over the list of categories and extensions
  1295. /// that are visible, i.e., not hidden in a non-imported submodule.
  1296. using visible_categories_iterator =
  1297. filtered_category_iterator<isVisibleCategory>;
  1298. using visible_categories_range =
  1299. llvm::iterator_range<visible_categories_iterator>;
  1300. visible_categories_range visible_categories() const {
  1301. return visible_categories_range(visible_categories_begin(),
  1302. visible_categories_end());
  1303. }
  1304. /// Retrieve an iterator to the beginning of the visible-categories
  1305. /// list.
  1306. visible_categories_iterator visible_categories_begin() const {
  1307. return visible_categories_iterator(getCategoryListRaw());
  1308. }
  1309. /// Retrieve an iterator to the end of the visible-categories list.
  1310. visible_categories_iterator visible_categories_end() const {
  1311. return visible_categories_iterator();
  1312. }
  1313. /// Determine whether the visible-categories list is empty.
  1314. bool visible_categories_empty() const {
  1315. return visible_categories_begin() == visible_categories_end();
  1316. }
  1317. private:
  1318. /// Test whether the given category... is a category.
  1319. ///
  1320. /// Used in the \c known_categories_iterator.
  1321. static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
  1322. public:
  1323. /// Iterator that walks over all of the known categories and
  1324. /// extensions, including those that are hidden.
  1325. using known_categories_iterator = filtered_category_iterator<isKnownCategory>;
  1326. using known_categories_range =
  1327. llvm::iterator_range<known_categories_iterator>;
  1328. known_categories_range known_categories() const {
  1329. return known_categories_range(known_categories_begin(),
  1330. known_categories_end());
  1331. }
  1332. /// Retrieve an iterator to the beginning of the known-categories
  1333. /// list.
  1334. known_categories_iterator known_categories_begin() const {
  1335. return known_categories_iterator(getCategoryListRaw());
  1336. }
  1337. /// Retrieve an iterator to the end of the known-categories list.
  1338. known_categories_iterator known_categories_end() const {
  1339. return known_categories_iterator();
  1340. }
  1341. /// Determine whether the known-categories list is empty.
  1342. bool known_categories_empty() const {
  1343. return known_categories_begin() == known_categories_end();
  1344. }
  1345. private:
  1346. /// Test whether the given category is a visible extension.
  1347. ///
  1348. /// Used in the \c visible_extensions_iterator.
  1349. static bool isVisibleExtension(ObjCCategoryDecl *Cat);
  1350. public:
  1351. /// Iterator that walks over all of the visible extensions, skipping
  1352. /// any that are known but hidden.
  1353. using visible_extensions_iterator =
  1354. filtered_category_iterator<isVisibleExtension>;
  1355. using visible_extensions_range =
  1356. llvm::iterator_range<visible_extensions_iterator>;
  1357. visible_extensions_range visible_extensions() const {
  1358. return visible_extensions_range(visible_extensions_begin(),
  1359. visible_extensions_end());
  1360. }
  1361. /// Retrieve an iterator to the beginning of the visible-extensions
  1362. /// list.
  1363. visible_extensions_iterator visible_extensions_begin() const {
  1364. return visible_extensions_iterator(getCategoryListRaw());
  1365. }
  1366. /// Retrieve an iterator to the end of the visible-extensions list.
  1367. visible_extensions_iterator visible_extensions_end() const {
  1368. return visible_extensions_iterator();
  1369. }
  1370. /// Determine whether the visible-extensions list is empty.
  1371. bool visible_extensions_empty() const {
  1372. return visible_extensions_begin() == visible_extensions_end();
  1373. }
  1374. private:
  1375. /// Test whether the given category is an extension.
  1376. ///
  1377. /// Used in the \c known_extensions_iterator.
  1378. static bool isKnownExtension(ObjCCategoryDecl *Cat);
  1379. public:
  1380. friend class ASTDeclReader;
  1381. friend class ASTDeclWriter;
  1382. friend class ASTReader;
  1383. /// Iterator that walks over all of the known extensions.
  1384. using known_extensions_iterator =
  1385. filtered_category_iterator<isKnownExtension>;
  1386. using known_extensions_range =
  1387. llvm::iterator_range<known_extensions_iterator>;
  1388. known_extensions_range known_extensions() const {
  1389. return known_extensions_range(known_extensions_begin(),
  1390. known_extensions_end());
  1391. }
  1392. /// Retrieve an iterator to the beginning of the known-extensions
  1393. /// list.
  1394. known_extensions_iterator known_extensions_begin() const {
  1395. return known_extensions_iterator(getCategoryListRaw());
  1396. }
  1397. /// Retrieve an iterator to the end of the known-extensions list.
  1398. known_extensions_iterator known_extensions_end() const {
  1399. return known_extensions_iterator();
  1400. }
  1401. /// Determine whether the known-extensions list is empty.
  1402. bool known_extensions_empty() const {
  1403. return known_extensions_begin() == known_extensions_end();
  1404. }
  1405. /// Retrieve the raw pointer to the start of the category/extension
  1406. /// list.
  1407. ObjCCategoryDecl* getCategoryListRaw() const {
  1408. // FIXME: Should make sure no callers ever do this.
  1409. if (!hasDefinition())
  1410. return nullptr;
  1411. if (data().ExternallyCompleted)
  1412. LoadExternalDefinition();
  1413. return data().CategoryList;
  1414. }
  1415. /// Set the raw pointer to the start of the category/extension
  1416. /// list.
  1417. void setCategoryListRaw(ObjCCategoryDecl *category) {
  1418. data().CategoryList = category;
  1419. }
  1420. ObjCPropertyDecl
  1421. *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
  1422. ObjCPropertyQueryKind QueryKind) const;
  1423. void collectPropertiesToImplement(PropertyMap &PM) const override;
  1424. /// isSuperClassOf - Return true if this class is the specified class or is a
  1425. /// super class of the specified interface class.
  1426. bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
  1427. // If RHS is derived from LHS it is OK; else it is not OK.
  1428. while (I != nullptr) {
  1429. if (declaresSameEntity(this, I))
  1430. return true;
  1431. I = I->getSuperClass();
  1432. }
  1433. return false;
  1434. }
  1435. /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
  1436. /// to be incompatible with __weak references. Returns true if it is.
  1437. bool isArcWeakrefUnavailable() const;
  1438. /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
  1439. /// classes must not be auto-synthesized. Returns class decl. if it must not
  1440. /// be; 0, otherwise.
  1441. const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
  1442. ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
  1443. ObjCInterfaceDecl *&ClassDeclared);
  1444. ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
  1445. ObjCInterfaceDecl *ClassDeclared;
  1446. return lookupInstanceVariable(IVarName, ClassDeclared);
  1447. }
  1448. ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
  1449. // Lookup a method. First, we search locally. If a method isn't
  1450. // found, we search referenced protocols and class categories.
  1451. ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
  1452. bool shallowCategoryLookup = false,
  1453. bool followSuper = true,
  1454. const ObjCCategoryDecl *C = nullptr) const;
  1455. /// Lookup an instance method for a given selector.
  1456. ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
  1457. return lookupMethod(Sel, true/*isInstance*/);
  1458. }
  1459. /// Lookup a class method for a given selector.
  1460. ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
  1461. return lookupMethod(Sel, false/*isInstance*/);
  1462. }
  1463. ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
  1464. /// Lookup a method in the classes implementation hierarchy.
  1465. ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
  1466. bool Instance=true) const;
  1467. ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
  1468. return lookupPrivateMethod(Sel, false);
  1469. }
  1470. /// Lookup a setter or getter in the class hierarchy,
  1471. /// including in all categories except for category passed
  1472. /// as argument.
  1473. ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
  1474. const ObjCCategoryDecl *Cat,
  1475. bool IsClassProperty) const {
  1476. return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
  1477. false/*shallowCategoryLookup*/,
  1478. true /* followsSuper */,
  1479. Cat);
  1480. }
  1481. SourceLocation getEndOfDefinitionLoc() const {
  1482. if (!hasDefinition())
  1483. return getLocation();
  1484. return data().EndLoc;
  1485. }
  1486. void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
  1487. /// Retrieve the starting location of the superclass.
  1488. SourceLocation getSuperClassLoc() const;
  1489. /// isImplicitInterfaceDecl - check that this is an implicitly declared
  1490. /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
  1491. /// declaration without an \@interface declaration.
  1492. bool isImplicitInterfaceDecl() const {
  1493. return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
  1494. }
  1495. /// ClassImplementsProtocol - Checks that 'lProto' protocol
  1496. /// has been implemented in IDecl class, its super class or categories (if
  1497. /// lookupCategory is true).
  1498. bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
  1499. bool lookupCategory,
  1500. bool RHSIsQualifiedID = false);
  1501. using redecl_range = redeclarable_base::redecl_range;
  1502. using redecl_iterator = redeclarable_base::redecl_iterator;
  1503. using redeclarable_base::redecls_begin;
  1504. using redeclarable_base::redecls_end;
  1505. using redeclarable_base::redecls;
  1506. using redeclarable_base::getPreviousDecl;
  1507. using redeclarable_base::getMostRecentDecl;
  1508. using redeclarable_base::isFirstDecl;
  1509. /// Retrieves the canonical declaration of this Objective-C class.
  1510. ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
  1511. const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
  1512. // Low-level accessor
  1513. const Type *getTypeForDecl() const { return TypeForDecl; }
  1514. void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
  1515. /// Get precomputed ODRHash or add a new one.
  1516. unsigned getODRHash();
  1517. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1518. static bool classofKind(Kind K) { return K == ObjCInterface; }
  1519. private:
  1520. /// True if a valid hash is stored in ODRHash.
  1521. bool hasODRHash() const;
  1522. void setHasODRHash(bool HasHash);
  1523. const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
  1524. bool inheritsDesignatedInitializers() const;
  1525. };
  1526. /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
  1527. /// instance variables are identical to C. The only exception is Objective-C
  1528. /// supports C++ style access control. For example:
  1529. ///
  1530. /// \@interface IvarExample : NSObject
  1531. /// {
  1532. /// id defaultToProtected;
  1533. /// \@public:
  1534. /// id canBePublic; // same as C++.
  1535. /// \@protected:
  1536. /// id canBeProtected; // same as C++.
  1537. /// \@package:
  1538. /// id canBePackage; // framework visibility (not available in C++).
  1539. /// }
  1540. ///
  1541. class ObjCIvarDecl : public FieldDecl {
  1542. void anchor() override;
  1543. public:
  1544. enum AccessControl {
  1545. None, Private, Protected, Public, Package
  1546. };
  1547. private:
  1548. ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
  1549. SourceLocation IdLoc, IdentifierInfo *Id,
  1550. QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
  1551. bool synthesized)
  1552. : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
  1553. /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
  1554. DeclAccess(ac), Synthesized(synthesized) {}
  1555. public:
  1556. static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
  1557. SourceLocation StartLoc, SourceLocation IdLoc,
  1558. IdentifierInfo *Id, QualType T,
  1559. TypeSourceInfo *TInfo,
  1560. AccessControl ac, Expr *BW = nullptr,
  1561. bool synthesized=false);
  1562. static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1563. /// Return the class interface that this ivar is logically contained
  1564. /// in; this is either the interface where the ivar was declared, or the
  1565. /// interface the ivar is conceptually a part of in the case of synthesized
  1566. /// ivars.
  1567. ObjCInterfaceDecl *getContainingInterface();
  1568. const ObjCInterfaceDecl *getContainingInterface() const {
  1569. return const_cast<ObjCIvarDecl *>(this)->getContainingInterface();
  1570. }
  1571. ObjCIvarDecl *getNextIvar() { return NextIvar; }
  1572. const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
  1573. void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
  1574. ObjCIvarDecl *getCanonicalDecl() override {
  1575. return cast<ObjCIvarDecl>(FieldDecl::getCanonicalDecl());
  1576. }
  1577. const ObjCIvarDecl *getCanonicalDecl() const {
  1578. return const_cast<ObjCIvarDecl *>(this)->getCanonicalDecl();
  1579. }
  1580. void setAccessControl(AccessControl ac) { DeclAccess = ac; }
  1581. AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
  1582. AccessControl getCanonicalAccessControl() const {
  1583. return DeclAccess == None ? Protected : AccessControl(DeclAccess);
  1584. }
  1585. void setSynthesize(bool synth) { Synthesized = synth; }
  1586. bool getSynthesize() const { return Synthesized; }
  1587. /// Retrieve the type of this instance variable when viewed as a member of a
  1588. /// specific object type.
  1589. QualType getUsageType(QualType objectType) const;
  1590. // Implement isa/cast/dyncast/etc.
  1591. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1592. static bool classofKind(Kind K) { return K == ObjCIvar; }
  1593. private:
  1594. /// NextIvar - Next Ivar in the list of ivars declared in class; class's
  1595. /// extensions and class's implementation
  1596. ObjCIvarDecl *NextIvar = nullptr;
  1597. // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
  1598. unsigned DeclAccess : 3;
  1599. unsigned Synthesized : 1;
  1600. };
  1601. /// Represents a field declaration created by an \@defs(...).
  1602. class ObjCAtDefsFieldDecl : public FieldDecl {
  1603. ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
  1604. SourceLocation IdLoc, IdentifierInfo *Id,
  1605. QualType T, Expr *BW)
  1606. : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
  1607. /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
  1608. BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
  1609. void anchor() override;
  1610. public:
  1611. static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
  1612. SourceLocation StartLoc,
  1613. SourceLocation IdLoc, IdentifierInfo *Id,
  1614. QualType T, Expr *BW);
  1615. static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1616. // Implement isa/cast/dyncast/etc.
  1617. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1618. static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
  1619. };
  1620. /// Represents an Objective-C protocol declaration.
  1621. ///
  1622. /// Objective-C protocols declare a pure abstract type (i.e., no instance
  1623. /// variables are permitted). Protocols originally drew inspiration from
  1624. /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
  1625. /// syntax:-). Here is an example:
  1626. ///
  1627. /// \code
  1628. /// \@protocol NSDraggingInfo <refproto1, refproto2>
  1629. /// - (NSWindow *)draggingDestinationWindow;
  1630. /// - (NSImage *)draggedImage;
  1631. /// \@end
  1632. /// \endcode
  1633. ///
  1634. /// This says that NSDraggingInfo requires two methods and requires everything
  1635. /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
  1636. /// well.
  1637. ///
  1638. /// \code
  1639. /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
  1640. /// \@end
  1641. /// \endcode
  1642. ///
  1643. /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
  1644. /// protocols are in distinct namespaces. For example, Cocoa defines both
  1645. /// an NSObject protocol and class (which isn't allowed in Java). As a result,
  1646. /// protocols are referenced using angle brackets as follows:
  1647. ///
  1648. /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
  1649. class ObjCProtocolDecl : public ObjCContainerDecl,
  1650. public Redeclarable<ObjCProtocolDecl> {
  1651. struct DefinitionData {
  1652. // The declaration that defines this protocol.
  1653. ObjCProtocolDecl *Definition;
  1654. /// Referenced protocols
  1655. ObjCProtocolList ReferencedProtocols;
  1656. /// Tracks whether a ODR hash has been computed for this protocol.
  1657. unsigned HasODRHash : 1;
  1658. /// A hash of parts of the class to help in ODR checking.
  1659. unsigned ODRHash = 0;
  1660. };
  1661. /// Contains a pointer to the data associated with this class,
  1662. /// which will be NULL if this class has not yet been defined.
  1663. ///
  1664. /// The bit indicates when we don't need to check for out-of-date
  1665. /// declarations. It will be set unless modules are enabled.
  1666. llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
  1667. ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
  1668. SourceLocation nameLoc, SourceLocation atStartLoc,
  1669. ObjCProtocolDecl *PrevDecl);
  1670. void anchor() override;
  1671. DefinitionData &data() const {
  1672. assert(Data.getPointer() && "Objective-C protocol has no definition!");
  1673. return *Data.getPointer();
  1674. }
  1675. void allocateDefinitionData();
  1676. using redeclarable_base = Redeclarable<ObjCProtocolDecl>;
  1677. ObjCProtocolDecl *getNextRedeclarationImpl() override {
  1678. return getNextRedeclaration();
  1679. }
  1680. ObjCProtocolDecl *getPreviousDeclImpl() override {
  1681. return getPreviousDecl();
  1682. }
  1683. ObjCProtocolDecl *getMostRecentDeclImpl() override {
  1684. return getMostRecentDecl();
  1685. }
  1686. /// True if a valid hash is stored in ODRHash.
  1687. bool hasODRHash() const;
  1688. void setHasODRHash(bool HasHash);
  1689. public:
  1690. friend class ASTDeclReader;
  1691. friend class ASTDeclWriter;
  1692. friend class ASTReader;
  1693. friend class ODRDiagsEmitter;
  1694. static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
  1695. IdentifierInfo *Id,
  1696. SourceLocation nameLoc,
  1697. SourceLocation atStartLoc,
  1698. ObjCProtocolDecl *PrevDecl);
  1699. static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1700. const ObjCProtocolList &getReferencedProtocols() const {
  1701. assert(hasDefinition() && "No definition available!");
  1702. return data().ReferencedProtocols;
  1703. }
  1704. using protocol_iterator = ObjCProtocolList::iterator;
  1705. using protocol_range = llvm::iterator_range<protocol_iterator>;
  1706. protocol_range protocols() const {
  1707. return protocol_range(protocol_begin(), protocol_end());
  1708. }
  1709. protocol_iterator protocol_begin() const {
  1710. if (!hasDefinition())
  1711. return protocol_iterator();
  1712. return data().ReferencedProtocols.begin();
  1713. }
  1714. protocol_iterator protocol_end() const {
  1715. if (!hasDefinition())
  1716. return protocol_iterator();
  1717. return data().ReferencedProtocols.end();
  1718. }
  1719. using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
  1720. using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
  1721. protocol_loc_range protocol_locs() const {
  1722. return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
  1723. }
  1724. protocol_loc_iterator protocol_loc_begin() const {
  1725. if (!hasDefinition())
  1726. return protocol_loc_iterator();
  1727. return data().ReferencedProtocols.loc_begin();
  1728. }
  1729. protocol_loc_iterator protocol_loc_end() const {
  1730. if (!hasDefinition())
  1731. return protocol_loc_iterator();
  1732. return data().ReferencedProtocols.loc_end();
  1733. }
  1734. unsigned protocol_size() const {
  1735. if (!hasDefinition())
  1736. return 0;
  1737. return data().ReferencedProtocols.size();
  1738. }
  1739. /// setProtocolList - Set the list of protocols that this interface
  1740. /// implements.
  1741. void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
  1742. const SourceLocation *Locs, ASTContext &C) {
  1743. assert(hasDefinition() && "Protocol is not defined");
  1744. data().ReferencedProtocols.set(List, Num, Locs, C);
  1745. }
  1746. /// This is true iff the protocol is tagged with the
  1747. /// `objc_non_runtime_protocol` attribute.
  1748. bool isNonRuntimeProtocol() const;
  1749. /// Get the set of all protocols implied by this protocols inheritance
  1750. /// hierarchy.
  1751. void getImpliedProtocols(llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const;
  1752. ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
  1753. // Lookup a method. First, we search locally. If a method isn't
  1754. // found, we search referenced protocols and class categories.
  1755. ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
  1756. ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
  1757. return lookupMethod(Sel, true/*isInstance*/);
  1758. }
  1759. ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
  1760. return lookupMethod(Sel, false/*isInstance*/);
  1761. }
  1762. /// Determine whether this protocol has a definition.
  1763. bool hasDefinition() const {
  1764. // If the name of this protocol is out-of-date, bring it up-to-date, which
  1765. // might bring in a definition.
  1766. // Note: a null value indicates that we don't have a definition and that
  1767. // modules are enabled.
  1768. if (!Data.getOpaqueValue())
  1769. getMostRecentDecl();
  1770. return Data.getPointer();
  1771. }
  1772. /// Retrieve the definition of this protocol, if any.
  1773. ObjCProtocolDecl *getDefinition() {
  1774. return hasDefinition()? Data.getPointer()->Definition : nullptr;
  1775. }
  1776. /// Retrieve the definition of this protocol, if any.
  1777. const ObjCProtocolDecl *getDefinition() const {
  1778. return hasDefinition()? Data.getPointer()->Definition : nullptr;
  1779. }
  1780. /// Determine whether this particular declaration is also the
  1781. /// definition.
  1782. bool isThisDeclarationADefinition() const {
  1783. return getDefinition() == this;
  1784. }
  1785. /// Starts the definition of this Objective-C protocol.
  1786. void startDefinition();
  1787. /// Starts the definition without sharing it with other redeclarations.
  1788. /// Such definition shouldn't be used for anything but only to compare if
  1789. /// a duplicate is compatible with previous definition or if it is
  1790. /// a distinct duplicate.
  1791. void startDuplicateDefinitionForComparison();
  1792. void mergeDuplicateDefinitionWithCommon(const ObjCProtocolDecl *Definition);
  1793. /// Produce a name to be used for protocol's metadata. It comes either via
  1794. /// objc_runtime_name attribute or protocol name.
  1795. StringRef getObjCRuntimeNameAsString() const;
  1796. SourceRange getSourceRange() const override LLVM_READONLY {
  1797. if (isThisDeclarationADefinition())
  1798. return ObjCContainerDecl::getSourceRange();
  1799. return SourceRange(getAtStartLoc(), getLocation());
  1800. }
  1801. using redecl_range = redeclarable_base::redecl_range;
  1802. using redecl_iterator = redeclarable_base::redecl_iterator;
  1803. using redeclarable_base::redecls_begin;
  1804. using redeclarable_base::redecls_end;
  1805. using redeclarable_base::redecls;
  1806. using redeclarable_base::getPreviousDecl;
  1807. using redeclarable_base::getMostRecentDecl;
  1808. using redeclarable_base::isFirstDecl;
  1809. /// Retrieves the canonical declaration of this Objective-C protocol.
  1810. ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
  1811. const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
  1812. void collectPropertiesToImplement(PropertyMap &PM) const override;
  1813. void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
  1814. ProtocolPropertySet &PS,
  1815. PropertyDeclOrder &PO) const;
  1816. /// Get precomputed ODRHash or add a new one.
  1817. unsigned getODRHash();
  1818. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1819. static bool classofKind(Kind K) { return K == ObjCProtocol; }
  1820. };
  1821. /// ObjCCategoryDecl - Represents a category declaration. A category allows
  1822. /// you to add methods to an existing class (without subclassing or modifying
  1823. /// the original class interface or implementation:-). Categories don't allow
  1824. /// you to add instance data. The following example adds "myMethod" to all
  1825. /// NSView's within a process:
  1826. ///
  1827. /// \@interface NSView (MyViewMethods)
  1828. /// - myMethod;
  1829. /// \@end
  1830. ///
  1831. /// Categories also allow you to split the implementation of a class across
  1832. /// several files (a feature more naturally supported in C++).
  1833. ///
  1834. /// Categories were originally inspired by dynamic languages such as Common
  1835. /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
  1836. /// don't support this level of dynamism, which is both powerful and dangerous.
  1837. class ObjCCategoryDecl : public ObjCContainerDecl {
  1838. /// Interface belonging to this category
  1839. ObjCInterfaceDecl *ClassInterface;
  1840. /// The type parameters associated with this category, if any.
  1841. ObjCTypeParamList *TypeParamList = nullptr;
  1842. /// referenced protocols in this category.
  1843. ObjCProtocolList ReferencedProtocols;
  1844. /// Next category belonging to this class.
  1845. /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
  1846. ObjCCategoryDecl *NextClassCategory = nullptr;
  1847. /// The location of the category name in this declaration.
  1848. SourceLocation CategoryNameLoc;
  1849. /// class extension may have private ivars.
  1850. SourceLocation IvarLBraceLoc;
  1851. SourceLocation IvarRBraceLoc;
  1852. ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
  1853. SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
  1854. IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
  1855. ObjCTypeParamList *typeParamList,
  1856. SourceLocation IvarLBraceLoc = SourceLocation(),
  1857. SourceLocation IvarRBraceLoc = SourceLocation());
  1858. void anchor() override;
  1859. public:
  1860. friend class ASTDeclReader;
  1861. friend class ASTDeclWriter;
  1862. static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
  1863. SourceLocation AtLoc,
  1864. SourceLocation ClassNameLoc,
  1865. SourceLocation CategoryNameLoc,
  1866. IdentifierInfo *Id,
  1867. ObjCInterfaceDecl *IDecl,
  1868. ObjCTypeParamList *typeParamList,
  1869. SourceLocation IvarLBraceLoc=SourceLocation(),
  1870. SourceLocation IvarRBraceLoc=SourceLocation());
  1871. static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1872. ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
  1873. const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
  1874. /// Retrieve the type parameter list associated with this category or
  1875. /// extension.
  1876. ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
  1877. /// Set the type parameters of this category.
  1878. ///
  1879. /// This function is used by the AST importer, which must import the type
  1880. /// parameters after creating their DeclContext to avoid loops.
  1881. void setTypeParamList(ObjCTypeParamList *TPL);
  1882. ObjCCategoryImplDecl *getImplementation() const;
  1883. void setImplementation(ObjCCategoryImplDecl *ImplD);
  1884. /// setProtocolList - Set the list of protocols that this interface
  1885. /// implements.
  1886. void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
  1887. const SourceLocation *Locs, ASTContext &C) {
  1888. ReferencedProtocols.set(List, Num, Locs, C);
  1889. }
  1890. const ObjCProtocolList &getReferencedProtocols() const {
  1891. return ReferencedProtocols;
  1892. }
  1893. using protocol_iterator = ObjCProtocolList::iterator;
  1894. using protocol_range = llvm::iterator_range<protocol_iterator>;
  1895. protocol_range protocols() const {
  1896. return protocol_range(protocol_begin(), protocol_end());
  1897. }
  1898. protocol_iterator protocol_begin() const {
  1899. return ReferencedProtocols.begin();
  1900. }
  1901. protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
  1902. unsigned protocol_size() const { return ReferencedProtocols.size(); }
  1903. using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
  1904. using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
  1905. protocol_loc_range protocol_locs() const {
  1906. return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
  1907. }
  1908. protocol_loc_iterator protocol_loc_begin() const {
  1909. return ReferencedProtocols.loc_begin();
  1910. }
  1911. protocol_loc_iterator protocol_loc_end() const {
  1912. return ReferencedProtocols.loc_end();
  1913. }
  1914. ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
  1915. /// Retrieve the pointer to the next stored category (or extension),
  1916. /// which may be hidden.
  1917. ObjCCategoryDecl *getNextClassCategoryRaw() const {
  1918. return NextClassCategory;
  1919. }
  1920. bool IsClassExtension() const { return getIdentifier() == nullptr; }
  1921. using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
  1922. using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
  1923. ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
  1924. ivar_iterator ivar_begin() const {
  1925. return ivar_iterator(decls_begin());
  1926. }
  1927. ivar_iterator ivar_end() const {
  1928. return ivar_iterator(decls_end());
  1929. }
  1930. unsigned ivar_size() const {
  1931. return std::distance(ivar_begin(), ivar_end());
  1932. }
  1933. bool ivar_empty() const {
  1934. return ivar_begin() == ivar_end();
  1935. }
  1936. SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
  1937. void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
  1938. void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
  1939. SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
  1940. void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
  1941. SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
  1942. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1943. static bool classofKind(Kind K) { return K == ObjCCategory; }
  1944. };
  1945. class ObjCImplDecl : public ObjCContainerDecl {
  1946. /// Class interface for this class/category implementation
  1947. ObjCInterfaceDecl *ClassInterface;
  1948. void anchor() override;
  1949. protected:
  1950. ObjCImplDecl(Kind DK, DeclContext *DC,
  1951. ObjCInterfaceDecl *classInterface,
  1952. IdentifierInfo *Id,
  1953. SourceLocation nameLoc, SourceLocation atStartLoc)
  1954. : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc),
  1955. ClassInterface(classInterface) {}
  1956. public:
  1957. const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
  1958. ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
  1959. void setClassInterface(ObjCInterfaceDecl *IFace);
  1960. void addInstanceMethod(ObjCMethodDecl *method) {
  1961. // FIXME: Context should be set correctly before we get here.
  1962. method->setLexicalDeclContext(this);
  1963. addDecl(method);
  1964. }
  1965. void addClassMethod(ObjCMethodDecl *method) {
  1966. // FIXME: Context should be set correctly before we get here.
  1967. method->setLexicalDeclContext(this);
  1968. addDecl(method);
  1969. }
  1970. void addPropertyImplementation(ObjCPropertyImplDecl *property);
  1971. ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
  1972. ObjCPropertyQueryKind queryKind) const;
  1973. ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
  1974. // Iterator access to properties.
  1975. using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>;
  1976. using propimpl_range =
  1977. llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>;
  1978. propimpl_range property_impls() const {
  1979. return propimpl_range(propimpl_begin(), propimpl_end());
  1980. }
  1981. propimpl_iterator propimpl_begin() const {
  1982. return propimpl_iterator(decls_begin());
  1983. }
  1984. propimpl_iterator propimpl_end() const {
  1985. return propimpl_iterator(decls_end());
  1986. }
  1987. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1988. static bool classofKind(Kind K) {
  1989. return K >= firstObjCImpl && K <= lastObjCImpl;
  1990. }
  1991. };
  1992. /// ObjCCategoryImplDecl - An object of this class encapsulates a category
  1993. /// \@implementation declaration. If a category class has declaration of a
  1994. /// property, its implementation must be specified in the category's
  1995. /// \@implementation declaration. Example:
  1996. /// \@interface I \@end
  1997. /// \@interface I(CATEGORY)
  1998. /// \@property int p1, d1;
  1999. /// \@end
  2000. /// \@implementation I(CATEGORY)
  2001. /// \@dynamic p1,d1;
  2002. /// \@end
  2003. ///
  2004. /// ObjCCategoryImplDecl
  2005. class ObjCCategoryImplDecl : public ObjCImplDecl {
  2006. // Category name location
  2007. SourceLocation CategoryNameLoc;
  2008. ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
  2009. ObjCInterfaceDecl *classInterface,
  2010. SourceLocation nameLoc, SourceLocation atStartLoc,
  2011. SourceLocation CategoryNameLoc)
  2012. : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id,
  2013. nameLoc, atStartLoc),
  2014. CategoryNameLoc(CategoryNameLoc) {}
  2015. void anchor() override;
  2016. public:
  2017. friend class ASTDeclReader;
  2018. friend class ASTDeclWriter;
  2019. static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
  2020. IdentifierInfo *Id,
  2021. ObjCInterfaceDecl *classInterface,
  2022. SourceLocation nameLoc,
  2023. SourceLocation atStartLoc,
  2024. SourceLocation CategoryNameLoc);
  2025. static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2026. ObjCCategoryDecl *getCategoryDecl() const;
  2027. SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
  2028. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2029. static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
  2030. };
  2031. raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
  2032. /// ObjCImplementationDecl - Represents a class definition - this is where
  2033. /// method definitions are specified. For example:
  2034. ///
  2035. /// @code
  2036. /// \@implementation MyClass
  2037. /// - (void)myMethod { /* do something */ }
  2038. /// \@end
  2039. /// @endcode
  2040. ///
  2041. /// In a non-fragile runtime, instance variables can appear in the class
  2042. /// interface, class extensions (nameless categories), and in the implementation
  2043. /// itself, as well as being synthesized as backing storage for properties.
  2044. ///
  2045. /// In a fragile runtime, instance variables are specified in the class
  2046. /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
  2047. /// we allow instance variables to be specified in the implementation. When
  2048. /// specified, they need to be \em identical to the interface.
  2049. class ObjCImplementationDecl : public ObjCImplDecl {
  2050. /// Implementation Class's super class.
  2051. ObjCInterfaceDecl *SuperClass;
  2052. SourceLocation SuperLoc;
  2053. /// \@implementation may have private ivars.
  2054. SourceLocation IvarLBraceLoc;
  2055. SourceLocation IvarRBraceLoc;
  2056. /// Support for ivar initialization.
  2057. /// The arguments used to initialize the ivars
  2058. LazyCXXCtorInitializersPtr IvarInitializers;
  2059. unsigned NumIvarInitializers = 0;
  2060. /// Do the ivars of this class require initialization other than
  2061. /// zero-initialization?
  2062. bool HasNonZeroConstructors : 1;
  2063. /// Do the ivars of this class require non-trivial destruction?
  2064. bool HasDestructors : 1;
  2065. ObjCImplementationDecl(DeclContext *DC,
  2066. ObjCInterfaceDecl *classInterface,
  2067. ObjCInterfaceDecl *superDecl,
  2068. SourceLocation nameLoc, SourceLocation atStartLoc,
  2069. SourceLocation superLoc = SourceLocation(),
  2070. SourceLocation IvarLBraceLoc=SourceLocation(),
  2071. SourceLocation IvarRBraceLoc=SourceLocation())
  2072. : ObjCImplDecl(ObjCImplementation, DC, classInterface,
  2073. classInterface ? classInterface->getIdentifier()
  2074. : nullptr,
  2075. nameLoc, atStartLoc),
  2076. SuperClass(superDecl), SuperLoc(superLoc),
  2077. IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc),
  2078. HasNonZeroConstructors(false), HasDestructors(false) {}
  2079. void anchor() override;
  2080. public:
  2081. friend class ASTDeclReader;
  2082. friend class ASTDeclWriter;
  2083. static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
  2084. ObjCInterfaceDecl *classInterface,
  2085. ObjCInterfaceDecl *superDecl,
  2086. SourceLocation nameLoc,
  2087. SourceLocation atStartLoc,
  2088. SourceLocation superLoc = SourceLocation(),
  2089. SourceLocation IvarLBraceLoc=SourceLocation(),
  2090. SourceLocation IvarRBraceLoc=SourceLocation());
  2091. static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2092. /// init_iterator - Iterates through the ivar initializer list.
  2093. using init_iterator = CXXCtorInitializer **;
  2094. /// init_const_iterator - Iterates through the ivar initializer list.
  2095. using init_const_iterator = CXXCtorInitializer * const *;
  2096. using init_range = llvm::iterator_range<init_iterator>;
  2097. using init_const_range = llvm::iterator_range<init_const_iterator>;
  2098. init_range inits() { return init_range(init_begin(), init_end()); }
  2099. init_const_range inits() const {
  2100. return init_const_range(init_begin(), init_end());
  2101. }
  2102. /// init_begin() - Retrieve an iterator to the first initializer.
  2103. init_iterator init_begin() {
  2104. const auto *ConstThis = this;
  2105. return const_cast<init_iterator>(ConstThis->init_begin());
  2106. }
  2107. /// begin() - Retrieve an iterator to the first initializer.
  2108. init_const_iterator init_begin() const;
  2109. /// init_end() - Retrieve an iterator past the last initializer.
  2110. init_iterator init_end() {
  2111. return init_begin() + NumIvarInitializers;
  2112. }
  2113. /// end() - Retrieve an iterator past the last initializer.
  2114. init_const_iterator init_end() const {
  2115. return init_begin() + NumIvarInitializers;
  2116. }
  2117. /// getNumArgs - Number of ivars which must be initialized.
  2118. unsigned getNumIvarInitializers() const {
  2119. return NumIvarInitializers;
  2120. }
  2121. void setNumIvarInitializers(unsigned numNumIvarInitializers) {
  2122. NumIvarInitializers = numNumIvarInitializers;
  2123. }
  2124. void setIvarInitializers(ASTContext &C,
  2125. CXXCtorInitializer ** initializers,
  2126. unsigned numInitializers);
  2127. /// Do any of the ivars of this class (not counting its base classes)
  2128. /// require construction other than zero-initialization?
  2129. bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
  2130. void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
  2131. /// Do any of the ivars of this class (not counting its base classes)
  2132. /// require non-trivial destruction?
  2133. bool hasDestructors() const { return HasDestructors; }
  2134. void setHasDestructors(bool val) { HasDestructors = val; }
  2135. /// getIdentifier - Get the identifier that names the class
  2136. /// interface associated with this implementation.
  2137. IdentifierInfo *getIdentifier() const {
  2138. return getClassInterface()->getIdentifier();
  2139. }
  2140. /// getName - Get the name of identifier for the class interface associated
  2141. /// with this implementation as a StringRef.
  2142. //
  2143. // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
  2144. // meaning.
  2145. StringRef getName() const {
  2146. assert(getIdentifier() && "Name is not a simple identifier");
  2147. return getIdentifier()->getName();
  2148. }
  2149. /// Get the name of the class associated with this interface.
  2150. //
  2151. // FIXME: Move to StringRef API.
  2152. std::string getNameAsString() const { return std::string(getName()); }
  2153. /// Produce a name to be used for class's metadata. It comes either via
  2154. /// class's objc_runtime_name attribute or class name.
  2155. StringRef getObjCRuntimeNameAsString() const;
  2156. const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
  2157. ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
  2158. SourceLocation getSuperClassLoc() const { return SuperLoc; }
  2159. void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
  2160. void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
  2161. SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
  2162. void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
  2163. SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
  2164. using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
  2165. using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
  2166. ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
  2167. ivar_iterator ivar_begin() const {
  2168. return ivar_iterator(decls_begin());
  2169. }
  2170. ivar_iterator ivar_end() const {
  2171. return ivar_iterator(decls_end());
  2172. }
  2173. unsigned ivar_size() const {
  2174. return std::distance(ivar_begin(), ivar_end());
  2175. }
  2176. bool ivar_empty() const {
  2177. return ivar_begin() == ivar_end();
  2178. }
  2179. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2180. static bool classofKind(Kind K) { return K == ObjCImplementation; }
  2181. };
  2182. raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
  2183. /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
  2184. /// declared as \@compatibility_alias alias class.
  2185. class ObjCCompatibleAliasDecl : public NamedDecl {
  2186. /// Class that this is an alias of.
  2187. ObjCInterfaceDecl *AliasedClass;
  2188. ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
  2189. ObjCInterfaceDecl* aliasedClass)
  2190. : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
  2191. void anchor() override;
  2192. public:
  2193. static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
  2194. SourceLocation L, IdentifierInfo *Id,
  2195. ObjCInterfaceDecl* aliasedClass);
  2196. static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
  2197. unsigned ID);
  2198. const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
  2199. ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
  2200. void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
  2201. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2202. static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
  2203. };
  2204. /// ObjCPropertyImplDecl - Represents implementation declaration of a property
  2205. /// in a class or category implementation block. For example:
  2206. /// \@synthesize prop1 = ivar1;
  2207. ///
  2208. class ObjCPropertyImplDecl : public Decl {
  2209. public:
  2210. enum Kind {
  2211. Synthesize,
  2212. Dynamic
  2213. };
  2214. private:
  2215. SourceLocation AtLoc; // location of \@synthesize or \@dynamic
  2216. /// For \@synthesize, the location of the ivar, if it was written in
  2217. /// the source code.
  2218. ///
  2219. /// \code
  2220. /// \@synthesize int a = b
  2221. /// \endcode
  2222. SourceLocation IvarLoc;
  2223. /// Property declaration being implemented
  2224. ObjCPropertyDecl *PropertyDecl;
  2225. /// Null for \@dynamic. Required for \@synthesize.
  2226. ObjCIvarDecl *PropertyIvarDecl;
  2227. /// The getter's definition, which has an empty body if synthesized.
  2228. ObjCMethodDecl *GetterMethodDecl = nullptr;
  2229. /// The getter's definition, which has an empty body if synthesized.
  2230. ObjCMethodDecl *SetterMethodDecl = nullptr;
  2231. /// Null for \@dynamic. Non-null if property must be copy-constructed in
  2232. /// getter.
  2233. Expr *GetterCXXConstructor = nullptr;
  2234. /// Null for \@dynamic. Non-null if property has assignment operator to call
  2235. /// in Setter synthesis.
  2236. Expr *SetterCXXAssignment = nullptr;
  2237. ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
  2238. ObjCPropertyDecl *property,
  2239. Kind PK,
  2240. ObjCIvarDecl *ivarDecl,
  2241. SourceLocation ivarLoc)
  2242. : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
  2243. IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) {
  2244. assert(PK == Dynamic || PropertyIvarDecl);
  2245. }
  2246. public:
  2247. friend class ASTDeclReader;
  2248. static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
  2249. SourceLocation atLoc, SourceLocation L,
  2250. ObjCPropertyDecl *property,
  2251. Kind PK,
  2252. ObjCIvarDecl *ivarDecl,
  2253. SourceLocation ivarLoc);
  2254. static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2255. SourceRange getSourceRange() const override LLVM_READONLY;
  2256. SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
  2257. void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
  2258. ObjCPropertyDecl *getPropertyDecl() const {
  2259. return PropertyDecl;
  2260. }
  2261. void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
  2262. Kind getPropertyImplementation() const {
  2263. return PropertyIvarDecl ? Synthesize : Dynamic;
  2264. }
  2265. ObjCIvarDecl *getPropertyIvarDecl() const {
  2266. return PropertyIvarDecl;
  2267. }
  2268. SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
  2269. void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
  2270. SourceLocation IvarLoc) {
  2271. PropertyIvarDecl = Ivar;
  2272. this->IvarLoc = IvarLoc;
  2273. }
  2274. /// For \@synthesize, returns true if an ivar name was explicitly
  2275. /// specified.
  2276. ///
  2277. /// \code
  2278. /// \@synthesize int a = b; // true
  2279. /// \@synthesize int a; // false
  2280. /// \endcode
  2281. bool isIvarNameSpecified() const {
  2282. return IvarLoc.isValid() && IvarLoc != getLocation();
  2283. }
  2284. ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
  2285. void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; }
  2286. ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
  2287. void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; }
  2288. Expr *getGetterCXXConstructor() const {
  2289. return GetterCXXConstructor;
  2290. }
  2291. void setGetterCXXConstructor(Expr *getterCXXConstructor) {
  2292. GetterCXXConstructor = getterCXXConstructor;
  2293. }
  2294. Expr *getSetterCXXAssignment() const {
  2295. return SetterCXXAssignment;
  2296. }
  2297. void setSetterCXXAssignment(Expr *setterCXXAssignment) {
  2298. SetterCXXAssignment = setterCXXAssignment;
  2299. }
  2300. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2301. static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
  2302. };
  2303. template<bool (*Filter)(ObjCCategoryDecl *)>
  2304. void
  2305. ObjCInterfaceDecl::filtered_category_iterator<Filter>::
  2306. findAcceptableCategory() {
  2307. while (Current && !Filter(Current))
  2308. Current = Current->getNextClassCategoryRaw();
  2309. }
  2310. template<bool (*Filter)(ObjCCategoryDecl *)>
  2311. inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
  2312. ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
  2313. Current = Current->getNextClassCategoryRaw();
  2314. findAcceptableCategory();
  2315. return *this;
  2316. }
  2317. inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
  2318. return !Cat->isInvalidDecl() && Cat->isUnconditionallyVisible();
  2319. }
  2320. inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
  2321. return !Cat->isInvalidDecl() && Cat->IsClassExtension() &&
  2322. Cat->isUnconditionallyVisible();
  2323. }
  2324. inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
  2325. return !Cat->isInvalidDecl() && Cat->IsClassExtension();
  2326. }
  2327. } // namespace clang
  2328. #endif // LLVM_CLANG_AST_DECLOBJC_H
  2329. #ifdef __GNUC__
  2330. #pragma GCC diagnostic pop
  2331. #endif