DeclObjC.h 102 KB

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