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