]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclObjC.h
Merge ^/head r275685 through r275714.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / DeclObjC.h
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 &) LLVM_DELETED_FUNCTION;
37   void operator=(const ObjCListBase &) LLVM_DELETED_FUNCTION;
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:
44   ObjCListBase() : List(nullptr), NumElts(0) {}
45   unsigned size() const { return NumElts; }
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:
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;
65   iterator begin() const { return (iterator)List; }
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:
82   ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(nullptr) { }
83
84   typedef const SourceLocation *loc_iterator;
85   loc_iterator loc_begin() const { return Locations; }
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 : 6;
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
189   SelectorLocationsKind getSelLocsKind() const {
190     return (SelectorLocationsKind)SelLocsKind;
191   }
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.
198   SourceLocation *getStoredSelLocs() {
199     return reinterpret_cast<SourceLocation*>(getParams() + NumParams);
200   }
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.
207   ParmVarDecl **getParams() {
208     return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
209   }
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.
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)
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;
265   const ObjCMethodDecl *getCanonicalDecl() const {
266     return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
267   }
268
269   ObjCDeclQualifier getObjCDeclQualifier() const {
270     return ObjCDeclQualifier(objcDeclQualifier);
271   }
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.
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.
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.
288   SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
289
290   // Location information, modeled after the Stmt API.
291   SourceLocation getLocStart() const LLVM_READONLY { return getLocation(); }
292   SourceLocation getLocEnd() const LLVM_READONLY;
293   SourceRange getSourceRange() const override LLVM_READONLY {
294     return SourceRange(getLocation(), getLocEnd());
295   }
296
297   SourceLocation getSelectorStartLoc() const {
298     if (isImplicit())
299       return getLocStart();
300     return getSelectorLoc(0);
301   }
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
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();
324   const ObjCInterfaceDecl *getClassInterface() const {
325     return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
326   }
327
328   Selector getSelector() const { return getDeclName().getObjCSelector(); }
329
330   QualType getReturnType() const { return MethodDeclType; }
331   void setReturnType(QualType T) { MethodDeclType = T; }
332
333   /// \brief Determine the type of an expression that sends a message to this
334   /// function.
335   QualType getSendResultType() const {
336     return getReturnType().getNonLValueExprType(getASTContext());
337   }
338
339   TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
340   void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
341
342   // Iterator access to formal parameters.
343   unsigned param_size() const { return NumParams; }
344   typedef const ParmVarDecl *const *param_const_iterator;
345   typedef ParmVarDecl *const *param_iterator;
346   typedef llvm::iterator_range<param_iterator> param_range;
347   typedef llvm::iterator_range<param_const_iterator> param_const_range;
348
349   param_range params() { return param_range(param_begin(), param_end()); }
350   param_const_range params() const {
351     return param_const_range(param_begin(), param_end());
352   }
353
354   param_const_iterator param_begin() const {
355     return param_const_iterator(getParams());
356   }
357   param_const_iterator param_end() const {
358     return param_const_iterator(getParams() + NumParams);
359   }
360   param_iterator param_begin() { return param_iterator(getParams()); }
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.
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.
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 =
382                            ArrayRef<SourceLocation>());
383
384   // Iterator access to parameter types.
385   typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
386   typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
387   param_type_iterator;
388
389   param_type_iterator param_type_begin() const {
390     return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
391   }
392   param_type_iterator param_type_end() const {
393     return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
394   }
395
396   /// createImplicitParams - Used to lazily create the self and cmd
397   /// implict parameters. This must be called prior to using getSelfDecl()
398   /// or getCmdDecl(). The call is ignored if the implicit paramters
399   /// have already been created.
400   void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
401
402   ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
403   void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
404   ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
405   void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
406
407   /// Determines the family of this method.
408   ObjCMethodFamily getMethodFamily() const;
409
410   bool isInstanceMethod() const { return IsInstance; }
411   void setInstanceMethod(bool isInst) { IsInstance = isInst; }
412   bool isVariadic() const { return IsVariadic; }
413   void setVariadic(bool isVar) { IsVariadic = isVar; }
414
415   bool isClassMethod() const { return !IsInstance; }
416
417   bool isPropertyAccessor() const { return IsPropertyAccessor; }
418   void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; }
419
420   bool isDefined() const { return IsDefined; }
421   void setDefined(bool isDefined) { IsDefined = isDefined; }
422
423   /// \brief Whether this method overrides any other in the class hierarchy.
424   ///
425   /// A method is said to override any method in the class's
426   /// base classes, its protocols, or its categories' protocols, that has
427   /// the same selector and is of the same kind (class or instance).
428   /// A method in an implementation is not considered as overriding the same
429   /// method in the interface or its categories.
430   bool isOverriding() const { return IsOverriding; }
431   void setOverriding(bool isOverriding) { IsOverriding = isOverriding; }
432
433   /// \brief Return overridden methods for the given \p Method.
434   ///
435   /// An ObjC method is considered to override any method in the class's
436   /// base classes (and base's categories), its protocols, or its categories'
437   /// protocols, that has
438   /// the same selector and is of the same kind (class or instance).
439   /// A method in an implementation is not considered as overriding the same
440   /// method in the interface or its categories.
441   void getOverriddenMethods(
442                      SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
443
444   /// \brief True if the method was a definition but its body was skipped.
445   bool hasSkippedBody() const { return HasSkippedBody; }
446   void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
447
448   /// \brief Returns the property associated with this method's selector.
449   ///
450   /// Note that even if this particular method is not marked as a property
451   /// accessor, it is still possible for it to match a property declared in a
452   /// superclass. Pass \c false if you only want to check the current class.
453   const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
454
455   // Related to protocols declared in  \@protocol
456   void setDeclImplementation(ImplementationControl ic) {
457     DeclImplementation = ic;
458   }
459   ImplementationControl getImplementationControl() const {
460     return ImplementationControl(DeclImplementation);
461   }
462
463   /// Returns true if this specific method declaration is marked with the
464   /// designated initializer attribute.
465   bool isThisDeclarationADesignatedInitializer() const;
466
467   /// Returns true if the method selector resolves to a designated initializer
468   /// in the class's interface.
469   ///
470   /// \param InitMethod if non-null and the function returns true, it receives
471   /// the method declaration that was marked with the designated initializer
472   /// attribute.
473   bool isDesignatedInitializerForTheInterface(
474       const ObjCMethodDecl **InitMethod = nullptr) const;
475
476   /// \brief Determine whether this method has a body.
477   bool hasBody() const override { return Body.isValid(); }
478
479   /// \brief Retrieve the body of this method, if it has one.
480   Stmt *getBody() const override;
481
482   void setLazyBody(uint64_t Offset) { Body = Offset; }
483
484   CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
485   void setBody(Stmt *B) { Body = B; }
486
487   /// \brief Returns whether this specific method is a definition.
488   bool isThisDeclarationADefinition() const { return hasBody(); }
489
490   // Implement isa/cast/dyncast/etc.
491   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
492   static bool classofKind(Kind K) { return K == ObjCMethod; }
493   static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
494     return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
495   }
496   static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
497     return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
498   }
499
500   friend class ASTDeclReader;
501   friend class ASTDeclWriter;
502 };
503
504 /// ObjCContainerDecl - Represents a container for method declarations.
505 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
506 /// ObjCProtocolDecl, and ObjCImplDecl.
507 ///
508 class ObjCContainerDecl : public NamedDecl, public DeclContext {
509   void anchor() override;
510
511   SourceLocation AtStart;
512
513   // These two locations in the range mark the end of the method container.
514   // The first points to the '@' token, and the second to the 'end' token.
515   SourceRange AtEnd;
516 public:
517
518   ObjCContainerDecl(Kind DK, DeclContext *DC,
519                     IdentifierInfo *Id, SourceLocation nameLoc,
520                     SourceLocation atStartLoc)
521     : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
522
523   // Iterator access to properties.
524   typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
525   typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>
526     prop_range;
527
528   prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
529   prop_iterator prop_begin() const {
530     return prop_iterator(decls_begin());
531   }
532   prop_iterator prop_end() const {
533     return prop_iterator(decls_end());
534   }
535
536   // Iterator access to instance/class methods.
537   typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
538   typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>
539     method_range;
540
541   method_range methods() const {
542     return method_range(meth_begin(), meth_end());
543   }
544   method_iterator meth_begin() const {
545     return method_iterator(decls_begin());
546   }
547   method_iterator meth_end() const {
548     return method_iterator(decls_end());
549   }
550
551   typedef filtered_decl_iterator<ObjCMethodDecl,
552                                  &ObjCMethodDecl::isInstanceMethod>
553     instmeth_iterator;
554   typedef llvm::iterator_range<instmeth_iterator> instmeth_range;
555
556   instmeth_range instance_methods() const {
557     return instmeth_range(instmeth_begin(), instmeth_end());
558   }
559   instmeth_iterator instmeth_begin() const {
560     return instmeth_iterator(decls_begin());
561   }
562   instmeth_iterator instmeth_end() const {
563     return instmeth_iterator(decls_end());
564   }
565
566   typedef filtered_decl_iterator<ObjCMethodDecl,
567                                  &ObjCMethodDecl::isClassMethod>
568     classmeth_iterator;
569   typedef llvm::iterator_range<classmeth_iterator> classmeth_range;
570
571   classmeth_range class_methods() const {
572     return classmeth_range(classmeth_begin(), classmeth_end());
573   }
574   classmeth_iterator classmeth_begin() const {
575     return classmeth_iterator(decls_begin());
576   }
577   classmeth_iterator classmeth_end() const {
578     return classmeth_iterator(decls_end());
579   }
580
581   // Get the local instance/class method declared in this interface.
582   ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
583                             bool AllowHidden = false) const;
584   ObjCMethodDecl *getInstanceMethod(Selector Sel,
585                                     bool AllowHidden = false) const {
586     return getMethod(Sel, true/*isInstance*/, AllowHidden);
587   }
588   ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
589     return getMethod(Sel, false/*isInstance*/, AllowHidden);
590   }
591   bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
592   ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
593
594   ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
595
596   typedef llvm::DenseMap<IdentifierInfo*, ObjCPropertyDecl*> PropertyMap;
597   
598   typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*>
599             ProtocolPropertyMap;
600   
601   typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
602   
603   /// This routine collects list of properties to be implemented in the class.
604   /// This includes, class's and its conforming protocols' properties.
605   /// Note, the superclass's properties are not included in the list.
606   virtual void collectPropertiesToImplement(PropertyMap &PM,
607                                             PropertyDeclOrder &PO) const {}
608
609   SourceLocation getAtStartLoc() const { return AtStart; }
610   void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
611
612   // Marks the end of the container.
613   SourceRange getAtEndRange() const {
614     return AtEnd;
615   }
616   void setAtEndRange(SourceRange atEnd) {
617     AtEnd = atEnd;
618   }
619
620   SourceRange getSourceRange() const override LLVM_READONLY {
621     return SourceRange(AtStart, getAtEndRange().getEnd());
622   }
623
624   // Implement isa/cast/dyncast/etc.
625   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
626   static bool classofKind(Kind K) {
627     return K >= firstObjCContainer &&
628            K <= lastObjCContainer;
629   }
630
631   static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
632     return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
633   }
634   static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
635     return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
636   }
637 };
638
639 /// \brief Represents an ObjC class declaration.
640 ///
641 /// For example:
642 ///
643 /// \code
644 ///   // MostPrimitive declares no super class (not particularly useful).
645 ///   \@interface MostPrimitive
646 ///     // no instance variables or methods.
647 ///   \@end
648 ///
649 ///   // NSResponder inherits from NSObject & implements NSCoding (a protocol).
650 ///   \@interface NSResponder : NSObject \<NSCoding>
651 ///   { // instance variables are represented by ObjCIvarDecl.
652 ///     id nextResponder; // nextResponder instance variable.
653 ///   }
654 ///   - (NSResponder *)nextResponder; // return a pointer to NSResponder.
655 ///   - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
656 ///   \@end                                    // to an NSEvent.
657 /// \endcode
658 ///
659 ///   Unlike C/C++, forward class declarations are accomplished with \@class.
660 ///   Unlike C/C++, \@class allows for a list of classes to be forward declared.
661 ///   Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
662 ///   typically inherit from NSObject (an exception is NSProxy).
663 ///
664 class ObjCInterfaceDecl : public ObjCContainerDecl
665                         , public Redeclarable<ObjCInterfaceDecl> {
666   void anchor() override;
667
668   /// TypeForDecl - This indicates the Type object that represents this
669   /// TypeDecl.  It is a cache maintained by ASTContext::getObjCInterfaceType
670   mutable const Type *TypeForDecl;
671   friend class ASTContext;
672   
673   struct DefinitionData {
674     /// \brief The definition of this class, for quick access from any 
675     /// declaration.
676     ObjCInterfaceDecl *Definition;
677     
678     /// Class's super class.
679     ObjCInterfaceDecl *SuperClass;
680
681     /// Protocols referenced in the \@interface  declaration
682     ObjCProtocolList ReferencedProtocols;
683
684     /// Protocols reference in both the \@interface and class extensions.
685     ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
686
687     /// \brief List of categories and class extensions defined for this class.
688     ///
689     /// Categories are stored as a linked list in the AST, since the categories
690     /// and class extensions come long after the initial interface declaration,
691     /// and we avoid dynamically-resized arrays in the AST wherever possible.
692     ObjCCategoryDecl *CategoryList;
693
694     /// IvarList - List of all ivars defined by this class; including class
695     /// extensions and implementation. This list is built lazily.
696     ObjCIvarDecl *IvarList;
697
698     /// \brief Indicates that the contents of this Objective-C class will be
699     /// completed by the external AST source when required.
700     mutable bool ExternallyCompleted : 1;
701
702     /// \brief Indicates that the ivar cache does not yet include ivars
703     /// declared in the implementation.
704     mutable bool IvarListMissingImplementation : 1;
705
706     /// Indicates that this interface decl contains at least one initializer
707     /// marked with the 'objc_designated_initializer' attribute.
708     bool HasDesignatedInitializers : 1;
709
710     enum InheritedDesignatedInitializersState {
711       /// We didn't calculate whether the designated initializers should be
712       /// inherited or not.
713       IDI_Unknown = 0,
714       /// Designated initializers are inherited for the super class.
715       IDI_Inherited = 1,
716       /// The class does not inherit designated initializers.
717       IDI_NotInherited = 2
718     };
719     /// One of the \c InheritedDesignatedInitializersState enumeratos.
720     mutable unsigned InheritedDesignatedInitializers : 2;
721
722     /// \brief The location of the superclass, if any.
723     SourceLocation SuperClassLoc;
724     
725     /// \brief The location of the last location in this declaration, before
726     /// the properties/methods. For example, this will be the '>', '}', or 
727     /// identifier, 
728     SourceLocation EndLoc; 
729
730     DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(), 
731                        ExternallyCompleted(),
732                        IvarListMissingImplementation(true),
733                        HasDesignatedInitializers(),
734                        InheritedDesignatedInitializers(IDI_Unknown) { }
735   };
736
737   ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
738                     IdentifierInfo *Id, SourceLocation CLoc,
739                     ObjCInterfaceDecl *PrevDecl, bool IsInternal);
740
741   void LoadExternalDefinition() const;
742
743   /// \brief Contains a pointer to the data associated with this class,
744   /// which will be NULL if this class has not yet been defined.
745   ///
746   /// The bit indicates when we don't need to check for out-of-date
747   /// declarations. It will be set unless modules are enabled.
748   llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
749
750   DefinitionData &data() const {
751     assert(Data.getPointer() && "Declaration has no definition!");
752     return *Data.getPointer();
753   }
754
755   /// \brief Allocate the definition data for this class.
756   void allocateDefinitionData();
757   
758   typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
759   ObjCInterfaceDecl *getNextRedeclarationImpl() override {
760     return getNextRedeclaration();
761   }
762   ObjCInterfaceDecl *getPreviousDeclImpl() override {
763     return getPreviousDecl();
764   }
765   ObjCInterfaceDecl *getMostRecentDeclImpl() override {
766     return getMostRecentDecl();
767   }
768
769 public:
770   static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
771                                    SourceLocation atLoc,
772                                    IdentifierInfo *Id,
773                                    ObjCInterfaceDecl *PrevDecl,
774                                    SourceLocation ClassLoc = SourceLocation(),
775                                    bool isInternal = false);
776
777   static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
778
779   SourceRange getSourceRange() const override LLVM_READONLY {
780     if (isThisDeclarationADefinition())
781       return ObjCContainerDecl::getSourceRange();
782     
783     return SourceRange(getAtStartLoc(), getLocation());
784   }
785
786   /// \brief Indicate that this Objective-C class is complete, but that
787   /// the external AST source will be responsible for filling in its contents
788   /// when a complete class is required.
789   void setExternallyCompleted();
790
791   /// Indicate that this interface decl contains at least one initializer
792   /// marked with the 'objc_designated_initializer' attribute.
793   void setHasDesignatedInitializers();
794
795   /// Returns true if this interface decl contains at least one initializer
796   /// marked with the 'objc_designated_initializer' attribute.
797   bool hasDesignatedInitializers() const;
798
799   /// Returns true if this interface decl declares a designated initializer
800   /// or it inherites one from its super class.
801   bool declaresOrInheritsDesignatedInitializers() const {
802     return hasDesignatedInitializers() || inheritsDesignatedInitializers();
803   }
804
805   const ObjCProtocolList &getReferencedProtocols() const {
806     assert(hasDefinition() && "Caller did not check for forward reference!");
807     if (data().ExternallyCompleted)
808       LoadExternalDefinition();
809
810     return data().ReferencedProtocols;
811   }
812
813   ObjCImplementationDecl *getImplementation() const;
814   void setImplementation(ObjCImplementationDecl *ImplD);
815
816   ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
817
818   // Get the local instance/class method declared in a category.
819   ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
820   ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
821   ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
822     return isInstance ? getInstanceMethod(Sel)
823                       : getClassMethod(Sel);
824   }
825
826   typedef ObjCProtocolList::iterator protocol_iterator;
827   typedef llvm::iterator_range<protocol_iterator> protocol_range;
828
829   protocol_range protocols() const {
830     return protocol_range(protocol_begin(), protocol_end());
831   }
832   protocol_iterator protocol_begin() const {
833     // FIXME: Should make sure no callers ever do this.
834     if (!hasDefinition())
835       return protocol_iterator();
836     
837     if (data().ExternallyCompleted)
838       LoadExternalDefinition();
839
840     return data().ReferencedProtocols.begin();
841   }
842   protocol_iterator protocol_end() const {
843     // FIXME: Should make sure no callers ever do this.
844     if (!hasDefinition())
845       return protocol_iterator();
846
847     if (data().ExternallyCompleted)
848       LoadExternalDefinition();
849
850     return data().ReferencedProtocols.end();
851   }
852
853   typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
854   typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
855
856   protocol_loc_range protocol_locs() const {
857     return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
858   }
859   protocol_loc_iterator protocol_loc_begin() const {
860     // FIXME: Should make sure no callers ever do this.
861     if (!hasDefinition())
862       return protocol_loc_iterator();
863
864     if (data().ExternallyCompleted)
865       LoadExternalDefinition();
866
867     return data().ReferencedProtocols.loc_begin();
868   }
869
870   protocol_loc_iterator protocol_loc_end() const {
871     // FIXME: Should make sure no callers ever do this.
872     if (!hasDefinition())
873       return protocol_loc_iterator();
874
875     if (data().ExternallyCompleted)
876       LoadExternalDefinition();
877
878     return data().ReferencedProtocols.loc_end();
879   }
880
881   typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
882   typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range;
883
884   all_protocol_range all_referenced_protocols() const {
885     return all_protocol_range(all_referenced_protocol_begin(),
886                               all_referenced_protocol_end());
887   }
888   all_protocol_iterator all_referenced_protocol_begin() const {
889     // FIXME: Should make sure no callers ever do this.
890     if (!hasDefinition())
891       return all_protocol_iterator();
892
893     if (data().ExternallyCompleted)
894       LoadExternalDefinition();
895
896     return data().AllReferencedProtocols.empty()  
897              ? protocol_begin()
898              : data().AllReferencedProtocols.begin();
899   }
900   all_protocol_iterator all_referenced_protocol_end() const {
901     // FIXME: Should make sure no callers ever do this.
902     if (!hasDefinition())
903       return all_protocol_iterator();
904     
905     if (data().ExternallyCompleted)
906       LoadExternalDefinition();
907
908     return data().AllReferencedProtocols.empty() 
909              ? protocol_end()
910              : data().AllReferencedProtocols.end();
911   }
912
913   typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
914   typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
915
916   ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
917   ivar_iterator ivar_begin() const { 
918     if (const ObjCInterfaceDecl *Def = getDefinition())
919       return ivar_iterator(Def->decls_begin()); 
920     
921     // FIXME: Should make sure no callers ever do this.
922     return ivar_iterator();
923   }
924   ivar_iterator ivar_end() const { 
925     if (const ObjCInterfaceDecl *Def = getDefinition())
926       return ivar_iterator(Def->decls_end()); 
927
928     // FIXME: Should make sure no callers ever do this.
929     return ivar_iterator();
930   }
931
932   unsigned ivar_size() const {
933     return std::distance(ivar_begin(), ivar_end());
934   }
935
936   bool ivar_empty() const { return ivar_begin() == ivar_end(); }
937
938   ObjCIvarDecl *all_declared_ivar_begin();
939   const ObjCIvarDecl *all_declared_ivar_begin() const {
940     // Even though this modifies IvarList, it's conceptually const:
941     // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
942     return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
943   }
944   void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
945
946   /// setProtocolList - Set the list of protocols that this interface
947   /// implements.
948   void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
949                        const SourceLocation *Locs, ASTContext &C) {
950     data().ReferencedProtocols.set(List, Num, Locs, C);
951   }
952
953   /// mergeClassExtensionProtocolList - Merge class extension's protocol list
954   /// into the protocol list for this class.
955   void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
956                                        unsigned Num,
957                                        ASTContext &C);
958
959   /// Produce a name to be used for class's metadata. It comes either via
960   /// objc_runtime_name attribute or class name.
961   StringRef getObjCRuntimeNameAsString() const;
962
963   /// Returns the designated initializers for the interface.
964   ///
965   /// If this declaration does not have methods marked as designated
966   /// initializers then the interface inherits the designated initializers of
967   /// its super class.
968   void getDesignatedInitializers(
969                   llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
970
971   /// Returns true if the given selector is a designated initializer for the
972   /// interface.
973   ///
974   /// If this declaration does not have methods marked as designated
975   /// initializers then the interface inherits the designated initializers of
976   /// its super class.
977   ///
978   /// \param InitMethod if non-null and the function returns true, it receives
979   /// the method that was marked as a designated initializer.
980   bool
981   isDesignatedInitializer(Selector Sel,
982                           const ObjCMethodDecl **InitMethod = nullptr) const;
983
984   /// \brief Determine whether this particular declaration of this class is
985   /// actually also a definition.
986   bool isThisDeclarationADefinition() const { 
987     return getDefinition() == this;
988   }
989                           
990   /// \brief Determine whether this class has been defined.
991   bool hasDefinition() const {
992     // If the name of this class is out-of-date, bring it up-to-date, which
993     // might bring in a definition.
994     // Note: a null value indicates that we don't have a definition and that
995     // modules are enabled.
996     if (!Data.getOpaqueValue()) {
997       if (IdentifierInfo *II = getIdentifier()) {
998         if (II->isOutOfDate()) {
999           updateOutOfDate(*II);
1000         }
1001       }
1002     }
1003
1004     return Data.getPointer();
1005   }
1006                         
1007   /// \brief Retrieve the definition of this class, or NULL if this class 
1008   /// has been forward-declared (with \@class) but not yet defined (with 
1009   /// \@interface).
1010   ObjCInterfaceDecl *getDefinition() {
1011     return hasDefinition()? Data.getPointer()->Definition : nullptr;
1012   }
1013
1014   /// \brief Retrieve the definition of this class, or NULL if this class 
1015   /// has been forward-declared (with \@class) but not yet defined (with 
1016   /// \@interface).
1017   const ObjCInterfaceDecl *getDefinition() const {
1018     return hasDefinition()? Data.getPointer()->Definition : nullptr;
1019   }
1020
1021   /// \brief Starts the definition of this Objective-C class, taking it from
1022   /// a forward declaration (\@class) to a definition (\@interface).
1023   void startDefinition();
1024   
1025   ObjCInterfaceDecl *getSuperClass() const {
1026     // FIXME: Should make sure no callers ever do this.
1027     if (!hasDefinition())
1028       return nullptr;
1029     
1030     if (data().ExternallyCompleted)
1031       LoadExternalDefinition();
1032
1033     return data().SuperClass;
1034   }
1035
1036   void setSuperClass(ObjCInterfaceDecl * superCls) { 
1037     data().SuperClass = 
1038       (superCls && superCls->hasDefinition()) ? superCls->getDefinition() 
1039                                               : superCls; 
1040   }
1041
1042   /// \brief Iterator that walks over the list of categories, filtering out
1043   /// those that do not meet specific criteria.
1044   ///
1045   /// This class template is used for the various permutations of category
1046   /// and extension iterators.
1047   template<bool (*Filter)(ObjCCategoryDecl *)>
1048   class filtered_category_iterator {
1049     ObjCCategoryDecl *Current;
1050
1051     void findAcceptableCategory();
1052     
1053   public:
1054     typedef ObjCCategoryDecl *      value_type;
1055     typedef value_type              reference;
1056     typedef value_type              pointer;
1057     typedef std::ptrdiff_t          difference_type;
1058     typedef std::input_iterator_tag iterator_category;
1059
1060     filtered_category_iterator() : Current(nullptr) { }
1061     explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1062       : Current(Current)
1063     {
1064       findAcceptableCategory();
1065     }
1066
1067     reference operator*() const { return Current; }
1068     pointer operator->() const { return Current; }
1069
1070     filtered_category_iterator &operator++();
1071
1072     filtered_category_iterator operator++(int) {
1073       filtered_category_iterator Tmp = *this;
1074       ++(*this);
1075       return Tmp;
1076     }
1077
1078     friend bool operator==(filtered_category_iterator X,
1079                            filtered_category_iterator Y) {
1080       return X.Current == Y.Current;
1081     }
1082
1083     friend bool operator!=(filtered_category_iterator X,
1084                            filtered_category_iterator Y) {
1085       return X.Current != Y.Current;
1086     }
1087   };
1088
1089 private:
1090   /// \brief Test whether the given category is visible.
1091   ///
1092   /// Used in the \c visible_categories_iterator.
1093   static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1094                         
1095 public:
1096   /// \brief Iterator that walks over the list of categories and extensions
1097   /// that are visible, i.e., not hidden in a non-imported submodule.
1098   typedef filtered_category_iterator<isVisibleCategory>
1099     visible_categories_iterator;
1100
1101   typedef llvm::iterator_range<visible_categories_iterator>
1102     visible_categories_range;
1103
1104   visible_categories_range visible_categories() const {
1105     return visible_categories_range(visible_categories_begin(),
1106                                     visible_categories_end());
1107   }
1108
1109   /// \brief Retrieve an iterator to the beginning of the visible-categories
1110   /// list.
1111   visible_categories_iterator visible_categories_begin() const {
1112     return visible_categories_iterator(getCategoryListRaw());
1113   }
1114
1115   /// \brief Retrieve an iterator to the end of the visible-categories list.
1116   visible_categories_iterator visible_categories_end() const {
1117     return visible_categories_iterator();
1118   }
1119
1120   /// \brief Determine whether the visible-categories list is empty.
1121   bool visible_categories_empty() const {
1122     return visible_categories_begin() == visible_categories_end();
1123   }
1124
1125 private:
1126   /// \brief Test whether the given category... is a category.
1127   ///
1128   /// Used in the \c known_categories_iterator.
1129   static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1130
1131 public:
1132   /// \brief Iterator that walks over all of the known categories and
1133   /// extensions, including those that are hidden.
1134   typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1135   typedef llvm::iterator_range<known_categories_iterator>
1136     known_categories_range;
1137
1138   known_categories_range known_categories() const {
1139     return known_categories_range(known_categories_begin(),
1140                                   known_categories_end());
1141   }
1142
1143   /// \brief Retrieve an iterator to the beginning of the known-categories
1144   /// list.
1145   known_categories_iterator known_categories_begin() const {
1146     return known_categories_iterator(getCategoryListRaw());
1147   }
1148
1149   /// \brief Retrieve an iterator to the end of the known-categories list.
1150   known_categories_iterator known_categories_end() const {
1151     return known_categories_iterator();
1152   }
1153
1154   /// \brief Determine whether the known-categories list is empty.
1155   bool known_categories_empty() const {
1156     return known_categories_begin() == known_categories_end();
1157   }
1158
1159 private:
1160   /// \brief Test whether the given category is a visible extension.
1161   ///
1162   /// Used in the \c visible_extensions_iterator.
1163   static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1164
1165 public:
1166   /// \brief Iterator that walks over all of the visible extensions, skipping
1167   /// any that are known but hidden.
1168   typedef filtered_category_iterator<isVisibleExtension>
1169     visible_extensions_iterator;
1170
1171   typedef llvm::iterator_range<visible_extensions_iterator>
1172     visible_extensions_range;
1173
1174   visible_extensions_range visible_extensions() const {
1175     return visible_extensions_range(visible_extensions_begin(),
1176                                     visible_extensions_end());
1177   }
1178
1179   /// \brief Retrieve an iterator to the beginning of the visible-extensions
1180   /// list.
1181   visible_extensions_iterator visible_extensions_begin() const {
1182     return visible_extensions_iterator(getCategoryListRaw());
1183   }
1184
1185   /// \brief Retrieve an iterator to the end of the visible-extensions list.
1186   visible_extensions_iterator visible_extensions_end() const {
1187     return visible_extensions_iterator();
1188   }
1189
1190   /// \brief Determine whether the visible-extensions list is empty.
1191   bool visible_extensions_empty() const {
1192     return visible_extensions_begin() == visible_extensions_end();
1193   }
1194
1195 private:
1196   /// \brief Test whether the given category is an extension.
1197   ///
1198   /// Used in the \c known_extensions_iterator.
1199   static bool isKnownExtension(ObjCCategoryDecl *Cat);
1200   
1201 public:
1202   /// \brief Iterator that walks over all of the known extensions.
1203   typedef filtered_category_iterator<isKnownExtension>
1204     known_extensions_iterator;
1205   typedef llvm::iterator_range<known_extensions_iterator>
1206     known_extensions_range;
1207
1208   known_extensions_range known_extensions() const {
1209     return known_extensions_range(known_extensions_begin(),
1210                                   known_extensions_end());
1211   }
1212
1213   /// \brief Retrieve an iterator to the beginning of the known-extensions
1214   /// list.
1215   known_extensions_iterator known_extensions_begin() const {
1216     return known_extensions_iterator(getCategoryListRaw());
1217   }
1218   
1219   /// \brief Retrieve an iterator to the end of the known-extensions list.
1220   known_extensions_iterator known_extensions_end() const {
1221     return known_extensions_iterator();
1222   }
1223
1224   /// \brief Determine whether the known-extensions list is empty.
1225   bool known_extensions_empty() const {
1226     return known_extensions_begin() == known_extensions_end();
1227   }
1228
1229   /// \brief Retrieve the raw pointer to the start of the category/extension
1230   /// list.
1231   ObjCCategoryDecl* getCategoryListRaw() const {
1232     // FIXME: Should make sure no callers ever do this.
1233     if (!hasDefinition())
1234       return nullptr;
1235     
1236     if (data().ExternallyCompleted)
1237       LoadExternalDefinition();
1238
1239     return data().CategoryList;
1240   }
1241
1242   /// \brief Set the raw pointer to the start of the category/extension
1243   /// list.
1244   void setCategoryListRaw(ObjCCategoryDecl *category) {
1245     data().CategoryList = category;
1246   }
1247
1248   ObjCPropertyDecl
1249     *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
1250
1251   void collectPropertiesToImplement(PropertyMap &PM,
1252                                     PropertyDeclOrder &PO) const override;
1253
1254   /// isSuperClassOf - Return true if this class is the specified class or is a
1255   /// super class of the specified interface class.
1256   bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1257     // If RHS is derived from LHS it is OK; else it is not OK.
1258     while (I != nullptr) {
1259       if (declaresSameEntity(this, I))
1260         return true;
1261       
1262       I = I->getSuperClass();
1263     }
1264     return false;
1265   }
1266
1267   /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1268   /// to be incompatible with __weak references. Returns true if it is.
1269   bool isArcWeakrefUnavailable() const;
1270
1271   /// isObjCRequiresPropertyDefs - Checks that a class or one of its super 
1272   /// classes must not be auto-synthesized. Returns class decl. if it must not
1273   /// be; 0, otherwise.
1274   const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1275
1276   ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1277                                        ObjCInterfaceDecl *&ClassDeclared);
1278   ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1279     ObjCInterfaceDecl *ClassDeclared;
1280     return lookupInstanceVariable(IVarName, ClassDeclared);
1281   }
1282
1283   ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1284                           
1285   // Lookup a method. First, we search locally. If a method isn't
1286   // found, we search referenced protocols and class categories.
1287   ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1288                                bool shallowCategoryLookup = false,
1289                                bool followSuper = true,
1290                                const ObjCCategoryDecl *C = nullptr) const;
1291
1292   /// Lookup an instance method for a given selector.
1293   ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1294     return lookupMethod(Sel, true/*isInstance*/);
1295   }
1296
1297   /// Lookup a class method for a given selector.
1298   ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1299     return lookupMethod(Sel, false/*isInstance*/);
1300   }
1301   ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1302
1303   /// \brief Lookup a method in the classes implementation hierarchy.
1304   ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1305                                       bool Instance=true) const;
1306
1307   ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1308     return lookupPrivateMethod(Sel, false);
1309   }
1310
1311   /// \brief Lookup a setter or getter in the class hierarchy,
1312   /// including in all categories except for category passed
1313   /// as argument.
1314   ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1315                                          const ObjCCategoryDecl *Cat) const {
1316     return lookupMethod(Sel, true/*isInstance*/,
1317                         false/*shallowCategoryLookup*/,
1318                         true /* followsSuper */,
1319                         Cat);
1320   }
1321                           
1322   SourceLocation getEndOfDefinitionLoc() const { 
1323     if (!hasDefinition())
1324       return getLocation();
1325     
1326     return data().EndLoc; 
1327   }
1328                           
1329   void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1330
1331   void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
1332   SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
1333
1334   /// isImplicitInterfaceDecl - check that this is an implicitly declared
1335   /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1336   /// declaration without an \@interface declaration.
1337   bool isImplicitInterfaceDecl() const { 
1338     return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1339   }
1340
1341   /// ClassImplementsProtocol - Checks that 'lProto' protocol
1342   /// has been implemented in IDecl class, its super class or categories (if
1343   /// lookupCategory is true).
1344   bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1345                                bool lookupCategory,
1346                                bool RHSIsQualifiedID = false);
1347
1348   typedef redeclarable_base::redecl_range redecl_range;
1349   typedef redeclarable_base::redecl_iterator redecl_iterator;
1350   using redeclarable_base::redecls_begin;
1351   using redeclarable_base::redecls_end;
1352   using redeclarable_base::redecls;
1353   using redeclarable_base::getPreviousDecl;
1354   using redeclarable_base::getMostRecentDecl;
1355   using redeclarable_base::isFirstDecl;
1356
1357   /// Retrieves the canonical declaration of this Objective-C class.
1358   ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1359   const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1360
1361   // Low-level accessor
1362   const Type *getTypeForDecl() const { return TypeForDecl; }
1363   void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1364
1365   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1366   static bool classofKind(Kind K) { return K == ObjCInterface; }
1367
1368   friend class ASTReader;
1369   friend class ASTDeclReader;
1370   friend class ASTDeclWriter;
1371
1372 private:
1373   const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1374   bool inheritsDesignatedInitializers() const;
1375 };
1376
1377 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1378 /// instance variables are identical to C. The only exception is Objective-C
1379 /// supports C++ style access control. For example:
1380 ///
1381 ///   \@interface IvarExample : NSObject
1382 ///   {
1383 ///     id defaultToProtected;
1384 ///   \@public:
1385 ///     id canBePublic; // same as C++.
1386 ///   \@protected:
1387 ///     id canBeProtected; // same as C++.
1388 ///   \@package:
1389 ///     id canBePackage; // framework visibility (not available in C++).
1390 ///   }
1391 ///
1392 class ObjCIvarDecl : public FieldDecl {
1393   void anchor() override;
1394
1395 public:
1396   enum AccessControl {
1397     None, Private, Protected, Public, Package
1398   };
1399
1400 private:
1401   ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1402                SourceLocation IdLoc, IdentifierInfo *Id,
1403                QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1404                bool synthesized)
1405     : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1406                 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1407       NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {}
1408
1409 public:
1410   static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1411                               SourceLocation StartLoc, SourceLocation IdLoc,
1412                               IdentifierInfo *Id, QualType T,
1413                               TypeSourceInfo *TInfo,
1414                               AccessControl ac, Expr *BW = nullptr,
1415                               bool synthesized=false);
1416
1417   static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1418   
1419   /// \brief Return the class interface that this ivar is logically contained
1420   /// in; this is either the interface where the ivar was declared, or the
1421   /// interface the ivar is conceptually a part of in the case of synthesized
1422   /// ivars.
1423   const ObjCInterfaceDecl *getContainingInterface() const;
1424
1425   ObjCIvarDecl *getNextIvar() { return NextIvar; }
1426   const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1427   void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1428
1429   void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1430
1431   AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1432
1433   AccessControl getCanonicalAccessControl() const {
1434     return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1435   }
1436
1437   void setSynthesize(bool synth) { Synthesized = synth; }
1438   bool getSynthesize() const { return Synthesized; }
1439
1440   // Implement isa/cast/dyncast/etc.
1441   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1442   static bool classofKind(Kind K) { return K == ObjCIvar; }
1443 private:
1444   /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1445   /// extensions and class's implementation
1446   ObjCIvarDecl *NextIvar;
1447
1448   // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1449   unsigned DeclAccess : 3;
1450   unsigned Synthesized : 1;
1451 };
1452
1453
1454 /// \brief Represents a field declaration created by an \@defs(...).
1455 class ObjCAtDefsFieldDecl : public FieldDecl {
1456   void anchor() override;
1457   ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1458                       SourceLocation IdLoc, IdentifierInfo *Id,
1459                       QualType T, Expr *BW)
1460     : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1461                 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
1462                 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1463
1464 public:
1465   static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1466                                      SourceLocation StartLoc,
1467                                      SourceLocation IdLoc, IdentifierInfo *Id,
1468                                      QualType T, Expr *BW);
1469
1470   static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1471   
1472   // Implement isa/cast/dyncast/etc.
1473   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1474   static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1475 };
1476
1477 /// \brief Represents an Objective-C protocol declaration.
1478 ///
1479 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1480 /// variables are permitted).  Protocols originally drew inspiration from
1481 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1482 /// syntax:-). Here is an example:
1483 ///
1484 /// \code
1485 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1486 /// - (NSWindow *)draggingDestinationWindow;
1487 /// - (NSImage *)draggedImage;
1488 /// \@end
1489 /// \endcode
1490 ///
1491 /// This says that NSDraggingInfo requires two methods and requires everything
1492 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1493 /// well.
1494 ///
1495 /// \code
1496 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1497 /// \@end
1498 /// \endcode
1499 ///
1500 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1501 /// protocols are in distinct namespaces. For example, Cocoa defines both
1502 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1503 /// protocols are referenced using angle brackets as follows:
1504 ///
1505 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1506 ///
1507 class ObjCProtocolDecl : public ObjCContainerDecl,
1508                          public Redeclarable<ObjCProtocolDecl> {
1509   void anchor() override;
1510
1511   struct DefinitionData {
1512     // \brief The declaration that defines this protocol.
1513     ObjCProtocolDecl *Definition;
1514
1515     /// \brief Referenced protocols
1516     ObjCProtocolList ReferencedProtocols;    
1517   };
1518
1519   /// \brief Contains a pointer to the data associated with this class,
1520   /// which will be NULL if this class has not yet been defined.
1521   ///
1522   /// The bit indicates when we don't need to check for out-of-date
1523   /// declarations. It will be set unless modules are enabled.
1524   llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1525
1526   DefinitionData &data() const {
1527     assert(Data.getPointer() && "Objective-C protocol has no definition!");
1528     return *Data.getPointer();
1529   }
1530   
1531   ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
1532                    SourceLocation nameLoc, SourceLocation atStartLoc,
1533                    ObjCProtocolDecl *PrevDecl);
1534
1535   void allocateDefinitionData();
1536
1537   typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1538   ObjCProtocolDecl *getNextRedeclarationImpl() override {
1539     return getNextRedeclaration();
1540   }
1541   ObjCProtocolDecl *getPreviousDeclImpl() override {
1542     return getPreviousDecl();
1543   }
1544   ObjCProtocolDecl *getMostRecentDeclImpl() override {
1545     return getMostRecentDecl();
1546   }
1547
1548 public:
1549   static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1550                                   IdentifierInfo *Id,
1551                                   SourceLocation nameLoc,
1552                                   SourceLocation atStartLoc,
1553                                   ObjCProtocolDecl *PrevDecl);
1554
1555   static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1556
1557   const ObjCProtocolList &getReferencedProtocols() const {
1558     assert(hasDefinition() && "No definition available!");
1559     return data().ReferencedProtocols;
1560   }
1561   typedef ObjCProtocolList::iterator protocol_iterator;
1562   typedef llvm::iterator_range<protocol_iterator> protocol_range;
1563
1564   protocol_range protocols() const {
1565     return protocol_range(protocol_begin(), protocol_end());
1566   }
1567   protocol_iterator protocol_begin() const {
1568     if (!hasDefinition())
1569       return protocol_iterator();
1570     
1571     return data().ReferencedProtocols.begin();
1572   }
1573   protocol_iterator protocol_end() const { 
1574     if (!hasDefinition())
1575       return protocol_iterator();
1576     
1577     return data().ReferencedProtocols.end(); 
1578   }
1579   typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1580   typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1581
1582   protocol_loc_range protocol_locs() const {
1583     return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1584   }
1585   protocol_loc_iterator protocol_loc_begin() const {
1586     if (!hasDefinition())
1587       return protocol_loc_iterator();
1588     
1589     return data().ReferencedProtocols.loc_begin();
1590   }
1591   protocol_loc_iterator protocol_loc_end() const {
1592     if (!hasDefinition())
1593       return protocol_loc_iterator();
1594     
1595     return data().ReferencedProtocols.loc_end();
1596   }
1597   unsigned protocol_size() const { 
1598     if (!hasDefinition())
1599       return 0;
1600     
1601     return data().ReferencedProtocols.size(); 
1602   }
1603
1604   /// setProtocolList - Set the list of protocols that this interface
1605   /// implements.
1606   void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1607                        const SourceLocation *Locs, ASTContext &C) {
1608     assert(hasDefinition() && "Protocol is not defined");
1609     data().ReferencedProtocols.set(List, Num, Locs, C);
1610   }
1611
1612   ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1613
1614   // Lookup a method. First, we search locally. If a method isn't
1615   // found, we search referenced protocols and class categories.
1616   ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1617   ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1618     return lookupMethod(Sel, true/*isInstance*/);
1619   }
1620   ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1621     return lookupMethod(Sel, false/*isInstance*/);
1622   }
1623
1624   /// \brief Determine whether this protocol has a definition.
1625   bool hasDefinition() const {
1626     // If the name of this protocol is out-of-date, bring it up-to-date, which
1627     // might bring in a definition.
1628     // Note: a null value indicates that we don't have a definition and that
1629     // modules are enabled.
1630     if (!Data.getOpaqueValue()) {
1631       if (IdentifierInfo *II = getIdentifier()) {
1632         if (II->isOutOfDate()) {
1633           updateOutOfDate(*II);
1634         }
1635       }
1636     }
1637
1638     return Data.getPointer();
1639   }
1640
1641   /// \brief Retrieve the definition of this protocol, if any.
1642   ObjCProtocolDecl *getDefinition() {
1643     return hasDefinition()? Data.getPointer()->Definition : nullptr;
1644   }
1645
1646   /// \brief Retrieve the definition of this protocol, if any.
1647   const ObjCProtocolDecl *getDefinition() const {
1648     return hasDefinition()? Data.getPointer()->Definition : nullptr;
1649   }
1650
1651   /// \brief Determine whether this particular declaration is also the 
1652   /// definition.
1653   bool isThisDeclarationADefinition() const {
1654     return getDefinition() == this;
1655   }
1656   
1657   /// \brief Starts the definition of this Objective-C protocol.
1658   void startDefinition();
1659
1660   /// Produce a name to be used for protocol's metadata. It comes either via
1661   /// objc_runtime_name attribute or protocol name.
1662   StringRef getObjCRuntimeNameAsString() const;
1663
1664   SourceRange getSourceRange() const override LLVM_READONLY {
1665     if (isThisDeclarationADefinition())
1666       return ObjCContainerDecl::getSourceRange();
1667    
1668     return SourceRange(getAtStartLoc(), getLocation());
1669   }
1670    
1671   typedef redeclarable_base::redecl_range redecl_range;
1672   typedef redeclarable_base::redecl_iterator redecl_iterator;
1673   using redeclarable_base::redecls_begin;
1674   using redeclarable_base::redecls_end;
1675   using redeclarable_base::redecls;
1676   using redeclarable_base::getPreviousDecl;
1677   using redeclarable_base::getMostRecentDecl;
1678   using redeclarable_base::isFirstDecl;
1679
1680   /// Retrieves the canonical declaration of this Objective-C protocol.
1681   ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
1682   const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
1683
1684   void collectPropertiesToImplement(PropertyMap &PM,
1685                                     PropertyDeclOrder &PO) const override;
1686
1687   void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
1688                                           ProtocolPropertyMap &PM) const;
1689
1690   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1691   static bool classofKind(Kind K) { return K == ObjCProtocol; }
1692
1693   friend class ASTReader;
1694   friend class ASTDeclReader;
1695   friend class ASTDeclWriter;
1696 };
1697
1698 /// ObjCCategoryDecl - Represents a category declaration. A category allows
1699 /// you to add methods to an existing class (without subclassing or modifying
1700 /// the original class interface or implementation:-). Categories don't allow
1701 /// you to add instance data. The following example adds "myMethod" to all
1702 /// NSView's within a process:
1703 ///
1704 /// \@interface NSView (MyViewMethods)
1705 /// - myMethod;
1706 /// \@end
1707 ///
1708 /// Categories also allow you to split the implementation of a class across
1709 /// several files (a feature more naturally supported in C++).
1710 ///
1711 /// Categories were originally inspired by dynamic languages such as Common
1712 /// Lisp and Smalltalk.  More traditional class-based languages (C++, Java)
1713 /// don't support this level of dynamism, which is both powerful and dangerous.
1714 ///
1715 class ObjCCategoryDecl : public ObjCContainerDecl {
1716   void anchor() override;
1717
1718   /// Interface belonging to this category
1719   ObjCInterfaceDecl *ClassInterface;
1720
1721   /// referenced protocols in this category.
1722   ObjCProtocolList ReferencedProtocols;
1723
1724   /// Next category belonging to this class.
1725   /// FIXME: this should not be a singly-linked list.  Move storage elsewhere.
1726   ObjCCategoryDecl *NextClassCategory;
1727
1728   /// \brief The location of the category name in this declaration.
1729   SourceLocation CategoryNameLoc;
1730
1731   /// class extension may have private ivars.
1732   SourceLocation IvarLBraceLoc;
1733   SourceLocation IvarRBraceLoc;
1734   
1735   ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1736                    SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1737                    IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1738                    SourceLocation IvarLBraceLoc=SourceLocation(),
1739                    SourceLocation IvarRBraceLoc=SourceLocation())
1740     : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1741       ClassInterface(IDecl), NextClassCategory(nullptr),
1742       CategoryNameLoc(CategoryNameLoc),
1743       IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
1744   }
1745
1746 public:
1747
1748   static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1749                                   SourceLocation AtLoc,
1750                                   SourceLocation ClassNameLoc,
1751                                   SourceLocation CategoryNameLoc,
1752                                   IdentifierInfo *Id,
1753                                   ObjCInterfaceDecl *IDecl,
1754                                   SourceLocation IvarLBraceLoc=SourceLocation(),
1755                                   SourceLocation IvarRBraceLoc=SourceLocation());
1756   static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1757
1758   ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1759   const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1760
1761   ObjCCategoryImplDecl *getImplementation() const;
1762   void setImplementation(ObjCCategoryImplDecl *ImplD);
1763
1764   /// setProtocolList - Set the list of protocols that this interface
1765   /// implements.
1766   void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1767                        const SourceLocation *Locs, ASTContext &C) {
1768     ReferencedProtocols.set(List, Num, Locs, C);
1769   }
1770
1771   const ObjCProtocolList &getReferencedProtocols() const {
1772     return ReferencedProtocols;
1773   }
1774
1775   typedef ObjCProtocolList::iterator protocol_iterator;
1776   typedef llvm::iterator_range<protocol_iterator> protocol_range;
1777
1778   protocol_range protocols() const {
1779     return protocol_range(protocol_begin(), protocol_end());
1780   }
1781   protocol_iterator protocol_begin() const {
1782     return ReferencedProtocols.begin();
1783   }
1784   protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1785   unsigned protocol_size() const { return ReferencedProtocols.size(); }
1786   typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1787   typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1788
1789   protocol_loc_range protocol_locs() const {
1790     return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1791   }
1792   protocol_loc_iterator protocol_loc_begin() const {
1793     return ReferencedProtocols.loc_begin();
1794   }
1795   protocol_loc_iterator protocol_loc_end() const {
1796     return ReferencedProtocols.loc_end();
1797   }
1798
1799   ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1800
1801   /// \brief Retrieve the pointer to the next stored category (or extension),
1802   /// which may be hidden.
1803   ObjCCategoryDecl *getNextClassCategoryRaw() const {
1804     return NextClassCategory;
1805   }
1806
1807   bool IsClassExtension() const { return getIdentifier() == nullptr; }
1808
1809   typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1810   typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
1811
1812   ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1813   ivar_iterator ivar_begin() const {
1814     return ivar_iterator(decls_begin());
1815   }
1816   ivar_iterator ivar_end() const {
1817     return ivar_iterator(decls_end());
1818   }
1819   unsigned ivar_size() const {
1820     return std::distance(ivar_begin(), ivar_end());
1821   }
1822   bool ivar_empty() const {
1823     return ivar_begin() == ivar_end();
1824   }
1825
1826   SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1827   void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1828   
1829   void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1830   SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1831   void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1832   SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1833
1834   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1835   static bool classofKind(Kind K) { return K == ObjCCategory; }
1836
1837   friend class ASTDeclReader;
1838   friend class ASTDeclWriter;
1839 };
1840
1841 class ObjCImplDecl : public ObjCContainerDecl {
1842   void anchor() override;
1843
1844   /// Class interface for this class/category implementation
1845   ObjCInterfaceDecl *ClassInterface;
1846
1847 protected:
1848   ObjCImplDecl(Kind DK, DeclContext *DC,
1849                ObjCInterfaceDecl *classInterface,
1850                SourceLocation nameLoc, SourceLocation atStartLoc)
1851     : ObjCContainerDecl(DK, DC,
1852                         classInterface? classInterface->getIdentifier()
1853                                       : nullptr,
1854                         nameLoc, atStartLoc),
1855       ClassInterface(classInterface) {}
1856
1857 public:
1858   const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1859   ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1860   void setClassInterface(ObjCInterfaceDecl *IFace);
1861
1862   void addInstanceMethod(ObjCMethodDecl *method) {
1863     // FIXME: Context should be set correctly before we get here.
1864     method->setLexicalDeclContext(this);
1865     addDecl(method);
1866   }
1867   void addClassMethod(ObjCMethodDecl *method) {
1868     // FIXME: Context should be set correctly before we get here.
1869     method->setLexicalDeclContext(this);
1870     addDecl(method);
1871   }
1872
1873   void addPropertyImplementation(ObjCPropertyImplDecl *property);
1874
1875   ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1876   ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1877
1878   // Iterator access to properties.
1879   typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1880   typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>
1881     propimpl_range;
1882
1883   propimpl_range property_impls() const {
1884     return propimpl_range(propimpl_begin(), propimpl_end());
1885   }
1886   propimpl_iterator propimpl_begin() const {
1887     return propimpl_iterator(decls_begin());
1888   }
1889   propimpl_iterator propimpl_end() const {
1890     return propimpl_iterator(decls_end());
1891   }
1892
1893   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1894   static bool classofKind(Kind K) {
1895     return K >= firstObjCImpl && K <= lastObjCImpl;
1896   }
1897 };
1898
1899 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1900 /// \@implementation declaration. If a category class has declaration of a
1901 /// property, its implementation must be specified in the category's
1902 /// \@implementation declaration. Example:
1903 /// \@interface I \@end
1904 /// \@interface I(CATEGORY)
1905 ///    \@property int p1, d1;
1906 /// \@end
1907 /// \@implementation I(CATEGORY)
1908 ///  \@dynamic p1,d1;
1909 /// \@end
1910 ///
1911 /// ObjCCategoryImplDecl
1912 class ObjCCategoryImplDecl : public ObjCImplDecl {
1913   void anchor() override;
1914
1915   // Category name
1916   IdentifierInfo *Id;
1917
1918   // Category name location
1919   SourceLocation CategoryNameLoc;
1920
1921   ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1922                        ObjCInterfaceDecl *classInterface,
1923                        SourceLocation nameLoc, SourceLocation atStartLoc,
1924                        SourceLocation CategoryNameLoc)
1925     : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1926       Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1927 public:
1928   static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1929                                       IdentifierInfo *Id,
1930                                       ObjCInterfaceDecl *classInterface,
1931                                       SourceLocation nameLoc,
1932                                       SourceLocation atStartLoc,
1933                                       SourceLocation CategoryNameLoc);
1934   static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1935
1936   /// getIdentifier - Get the identifier that names the category
1937   /// interface associated with this implementation.
1938   /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier()
1939   /// with a different meaning. For example:
1940   /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1941   /// returns the class interface name, whereas
1942   /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1943   /// returns the category name.
1944   IdentifierInfo *getIdentifier() const {
1945     return Id;
1946   }
1947   void setIdentifier(IdentifierInfo *II) { Id = II; }
1948
1949   ObjCCategoryDecl *getCategoryDecl() const;
1950
1951   SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1952
1953   /// getName - Get the name of identifier for the class interface associated
1954   /// with this implementation as a StringRef.
1955   //
1956   // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
1957   // meaning.
1958   StringRef getName() const { return Id ? Id->getName() : StringRef(); }
1959
1960   /// @brief Get the name of the class associated with this interface.
1961   //
1962   // FIXME: Deprecated, move clients to getName().
1963   std::string getNameAsString() const {
1964     return getName();
1965   }
1966
1967   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1968   static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1969
1970   friend class ASTDeclReader;
1971   friend class ASTDeclWriter;
1972 };
1973
1974 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
1975
1976 /// ObjCImplementationDecl - Represents a class definition - this is where
1977 /// method definitions are specified. For example:
1978 ///
1979 /// @code
1980 /// \@implementation MyClass
1981 /// - (void)myMethod { /* do something */ }
1982 /// \@end
1983 /// @endcode
1984 ///
1985 /// In a non-fragile runtime, instance variables can appear in the class
1986 /// interface, class extensions (nameless categories), and in the implementation
1987 /// itself, as well as being synthesized as backing storage for properties.
1988 ///
1989 /// In a fragile runtime, instance variables are specified in the class
1990 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
1991 /// we allow instance variables to be specified in the implementation. When
1992 /// specified, they need to be \em identical to the interface.
1993 class ObjCImplementationDecl : public ObjCImplDecl {
1994   void anchor() override;
1995   /// Implementation Class's super class.
1996   ObjCInterfaceDecl *SuperClass;
1997   SourceLocation SuperLoc;
1998
1999   /// \@implementation may have private ivars.
2000   SourceLocation IvarLBraceLoc;
2001   SourceLocation IvarRBraceLoc;
2002   
2003   /// Support for ivar initialization.
2004   /// IvarInitializers - The arguments used to initialize the ivars
2005   CXXCtorInitializer **IvarInitializers;
2006   unsigned NumIvarInitializers;
2007
2008   /// Do the ivars of this class require initialization other than
2009   /// zero-initialization?
2010   bool HasNonZeroConstructors : 1;
2011
2012   /// Do the ivars of this class require non-trivial destruction?
2013   bool HasDestructors : 1;
2014
2015   ObjCImplementationDecl(DeclContext *DC,
2016                          ObjCInterfaceDecl *classInterface,
2017                          ObjCInterfaceDecl *superDecl,
2018                          SourceLocation nameLoc, SourceLocation atStartLoc,
2019                          SourceLocation superLoc = SourceLocation(),
2020                          SourceLocation IvarLBraceLoc=SourceLocation(), 
2021                          SourceLocation IvarRBraceLoc=SourceLocation())
2022     : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
2023        SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
2024        IvarRBraceLoc(IvarRBraceLoc),
2025        IvarInitializers(nullptr), NumIvarInitializers(0),
2026        HasNonZeroConstructors(false), HasDestructors(false) {}
2027 public:
2028   static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2029                                         ObjCInterfaceDecl *classInterface,
2030                                         ObjCInterfaceDecl *superDecl,
2031                                         SourceLocation nameLoc,
2032                                         SourceLocation atStartLoc,
2033                                      SourceLocation superLoc = SourceLocation(),
2034                                         SourceLocation IvarLBraceLoc=SourceLocation(), 
2035                                         SourceLocation IvarRBraceLoc=SourceLocation());
2036
2037   static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2038
2039   /// init_iterator - Iterates through the ivar initializer list.
2040   typedef CXXCtorInitializer **init_iterator;
2041
2042   /// init_const_iterator - Iterates through the ivar initializer list.
2043   typedef CXXCtorInitializer * const * init_const_iterator;
2044
2045   typedef llvm::iterator_range<init_iterator> init_range;
2046   typedef llvm::iterator_range<init_const_iterator> init_const_range;
2047
2048   init_range inits() { return init_range(init_begin(), init_end()); }
2049   init_const_range inits() const {
2050     return init_const_range(init_begin(), init_end());
2051   }
2052
2053   /// init_begin() - Retrieve an iterator to the first initializer.
2054   init_iterator       init_begin()       { return IvarInitializers; }
2055   /// begin() - Retrieve an iterator to the first initializer.
2056   init_const_iterator init_begin() const { return IvarInitializers; }
2057
2058   /// init_end() - Retrieve an iterator past the last initializer.
2059   init_iterator       init_end()       {
2060     return IvarInitializers + NumIvarInitializers;
2061   }
2062   /// end() - Retrieve an iterator past the last initializer.
2063   init_const_iterator init_end() const {
2064     return IvarInitializers + NumIvarInitializers;
2065   }
2066   /// getNumArgs - Number of ivars which must be initialized.
2067   unsigned getNumIvarInitializers() const {
2068     return NumIvarInitializers;
2069   }
2070
2071   void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2072     NumIvarInitializers = numNumIvarInitializers;
2073   }
2074
2075   void setIvarInitializers(ASTContext &C,
2076                            CXXCtorInitializer ** initializers,
2077                            unsigned numInitializers);
2078
2079   /// Do any of the ivars of this class (not counting its base classes)
2080   /// require construction other than zero-initialization?
2081   bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2082   void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2083
2084   /// Do any of the ivars of this class (not counting its base classes)
2085   /// require non-trivial destruction?
2086   bool hasDestructors() const { return HasDestructors; }
2087   void setHasDestructors(bool val) { HasDestructors = val; }
2088
2089   /// getIdentifier - Get the identifier that names the class
2090   /// interface associated with this implementation.
2091   IdentifierInfo *getIdentifier() const {
2092     return getClassInterface()->getIdentifier();
2093   }
2094
2095   /// getName - Get the name of identifier for the class interface associated
2096   /// with this implementation as a StringRef.
2097   //
2098   // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2099   // meaning.
2100   StringRef getName() const {
2101     assert(getIdentifier() && "Name is not a simple identifier");
2102     return getIdentifier()->getName();
2103   }
2104
2105   /// @brief Get the name of the class associated with this interface.
2106   //
2107   // FIXME: Move to StringRef API.
2108   std::string getNameAsString() const {
2109     return getName();
2110   }
2111     
2112   /// Produce a name to be used for class's metadata. It comes either via
2113   /// class's objc_runtime_name attribute or class name.
2114   StringRef getObjCRuntimeNameAsString() const;
2115
2116   const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2117   ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2118   SourceLocation getSuperClassLoc() const { return SuperLoc; }
2119
2120   void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2121
2122   void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2123   SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2124   void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2125   SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2126   
2127   typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2128   typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2129
2130   ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2131   ivar_iterator ivar_begin() const {
2132     return ivar_iterator(decls_begin());
2133   }
2134   ivar_iterator ivar_end() const {
2135     return ivar_iterator(decls_end());
2136   }
2137   unsigned ivar_size() const {
2138     return std::distance(ivar_begin(), ivar_end());
2139   }
2140   bool ivar_empty() const {
2141     return ivar_begin() == ivar_end();
2142   }
2143
2144   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2145   static bool classofKind(Kind K) { return K == ObjCImplementation; }
2146
2147   friend class ASTDeclReader;
2148   friend class ASTDeclWriter;
2149 };
2150
2151 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2152
2153 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2154 /// declared as \@compatibility_alias alias class.
2155 class ObjCCompatibleAliasDecl : public NamedDecl {
2156   void anchor() override;
2157   /// Class that this is an alias of.
2158   ObjCInterfaceDecl *AliasedClass;
2159
2160   ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2161                           ObjCInterfaceDecl* aliasedClass)
2162     : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2163 public:
2164   static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2165                                          SourceLocation L, IdentifierInfo *Id,
2166                                          ObjCInterfaceDecl* aliasedClass);
2167
2168   static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, 
2169                                                      unsigned ID);
2170   
2171   const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2172   ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2173   void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2174
2175   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2176   static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2177
2178 };
2179
2180 /// \brief Represents one property declaration in an Objective-C interface.
2181 ///
2182 /// For example:
2183 /// \code{.mm}
2184 /// \@property (assign, readwrite) int MyProperty;
2185 /// \endcode
2186 class ObjCPropertyDecl : public NamedDecl {
2187   void anchor() override;
2188 public:
2189   enum PropertyAttributeKind {
2190     OBJC_PR_noattr    = 0x00,
2191     OBJC_PR_readonly  = 0x01,
2192     OBJC_PR_getter    = 0x02,
2193     OBJC_PR_assign    = 0x04,
2194     OBJC_PR_readwrite = 0x08,
2195     OBJC_PR_retain    = 0x10,
2196     OBJC_PR_copy      = 0x20,
2197     OBJC_PR_nonatomic = 0x40,
2198     OBJC_PR_setter    = 0x80,
2199     OBJC_PR_atomic    = 0x100,
2200     OBJC_PR_weak      = 0x200,
2201     OBJC_PR_strong    = 0x400,
2202     OBJC_PR_unsafe_unretained = 0x800
2203     // Adding a property should change NumPropertyAttrsBits
2204   };
2205
2206   enum {
2207     /// \brief Number of bits fitting all the property attributes.
2208     NumPropertyAttrsBits = 12
2209   };
2210
2211   enum SetterKind { Assign, Retain, Copy, Weak };
2212   enum PropertyControl { None, Required, Optional };
2213 private:
2214   SourceLocation AtLoc;   // location of \@property
2215   SourceLocation LParenLoc; // location of '(' starting attribute list or null.
2216   TypeSourceInfo *DeclType;
2217   unsigned PropertyAttributes : NumPropertyAttrsBits;
2218   unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
2219   // \@required/\@optional
2220   unsigned PropertyImplementation : 2;
2221
2222   Selector GetterName;    // getter name of NULL if no getter
2223   Selector SetterName;    // setter name of NULL if no setter
2224
2225   ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
2226   ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
2227   ObjCIvarDecl *PropertyIvarDecl;   // Synthesize ivar for this property
2228
2229   ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2230                    SourceLocation AtLocation,  SourceLocation LParenLocation,
2231                    TypeSourceInfo *T)
2232     : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), 
2233       LParenLoc(LParenLocation), DeclType(T),
2234       PropertyAttributes(OBJC_PR_noattr),
2235       PropertyAttributesAsWritten(OBJC_PR_noattr),
2236       PropertyImplementation(None),
2237       GetterName(Selector()),
2238       SetterName(Selector()),
2239       GetterMethodDecl(nullptr), SetterMethodDecl(nullptr),
2240       PropertyIvarDecl(nullptr) {}
2241
2242 public:
2243   static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
2244                                   SourceLocation L,
2245                                   IdentifierInfo *Id, SourceLocation AtLocation,
2246                                   SourceLocation LParenLocation,
2247                                   TypeSourceInfo *T,
2248                                   PropertyControl propControl = None);
2249   
2250   static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2251   
2252   SourceLocation getAtLoc() const { return AtLoc; }
2253   void setAtLoc(SourceLocation L) { AtLoc = L; }
2254   
2255   SourceLocation getLParenLoc() const { return LParenLoc; }
2256   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2257
2258   TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
2259   QualType getType() const { return DeclType->getType(); }
2260   void setType(TypeSourceInfo *T) { DeclType = T; }
2261
2262   PropertyAttributeKind getPropertyAttributes() const {
2263     return PropertyAttributeKind(PropertyAttributes);
2264   }
2265   void setPropertyAttributes(PropertyAttributeKind PRVal) {
2266     PropertyAttributes |= PRVal;
2267   }
2268
2269   PropertyAttributeKind getPropertyAttributesAsWritten() const {
2270     return PropertyAttributeKind(PropertyAttributesAsWritten);
2271   }
2272
2273   bool hasWrittenStorageAttribute() const {
2274     return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
2275         OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
2276         OBJC_PR_weak);
2277   }
2278
2279   void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
2280     PropertyAttributesAsWritten = PRVal;
2281   }
2282
2283  void makeitReadWriteAttribute() {
2284     PropertyAttributes &= ~OBJC_PR_readonly;
2285     PropertyAttributes |= OBJC_PR_readwrite;
2286  }
2287
2288   // Helper methods for accessing attributes.
2289
2290   /// isReadOnly - Return true iff the property has a setter.
2291   bool isReadOnly() const {
2292     return (PropertyAttributes & OBJC_PR_readonly);
2293   }
2294
2295   /// isAtomic - Return true if the property is atomic.
2296   bool isAtomic() const {
2297     return (PropertyAttributes & OBJC_PR_atomic);
2298   }
2299
2300   /// isRetaining - Return true if the property retains its value.
2301   bool isRetaining() const {
2302     return (PropertyAttributes &
2303             (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
2304   }
2305
2306   /// getSetterKind - Return the method used for doing assignment in
2307   /// the property setter. This is only valid if the property has been
2308   /// defined to have a setter.
2309   SetterKind getSetterKind() const {
2310     if (PropertyAttributes & OBJC_PR_strong)
2311       return getType()->isBlockPointerType() ? Copy : Retain;
2312     if (PropertyAttributes & OBJC_PR_retain)
2313       return Retain;
2314     if (PropertyAttributes & OBJC_PR_copy)
2315       return Copy;
2316     if (PropertyAttributes & OBJC_PR_weak)
2317       return Weak;
2318     return Assign;
2319   }
2320
2321   Selector getGetterName() const { return GetterName; }
2322   void setGetterName(Selector Sel) { GetterName = Sel; }
2323
2324   Selector getSetterName() const { return SetterName; }
2325   void setSetterName(Selector Sel) { SetterName = Sel; }
2326
2327   ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2328   void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
2329
2330   ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2331   void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
2332
2333   // Related to \@optional/\@required declared in \@protocol
2334   void setPropertyImplementation(PropertyControl pc) {
2335     PropertyImplementation = pc;
2336   }
2337   PropertyControl getPropertyImplementation() const {
2338     return PropertyControl(PropertyImplementation);
2339   }
2340
2341   void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
2342     PropertyIvarDecl = Ivar;
2343   }
2344   ObjCIvarDecl *getPropertyIvarDecl() const {
2345     return PropertyIvarDecl;
2346   }
2347
2348   SourceRange getSourceRange() const override LLVM_READONLY {
2349     return SourceRange(AtLoc, getLocation());
2350   }
2351   
2352   /// Get the default name of the synthesized ivar.
2353   IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
2354
2355   /// Lookup a property by name in the specified DeclContext.
2356   static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
2357                                             IdentifierInfo *propertyID);
2358
2359   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2360   static bool classofKind(Kind K) { return K == ObjCProperty; }
2361 };
2362
2363 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2364 /// in a class or category implementation block. For example:
2365 /// \@synthesize prop1 = ivar1;
2366 ///
2367 class ObjCPropertyImplDecl : public Decl {
2368 public:
2369   enum Kind {
2370     Synthesize,
2371     Dynamic
2372   };
2373 private:
2374   SourceLocation AtLoc;   // location of \@synthesize or \@dynamic
2375
2376   /// \brief For \@synthesize, the location of the ivar, if it was written in
2377   /// the source code.
2378   ///
2379   /// \code
2380   /// \@synthesize int a = b
2381   /// \endcode
2382   SourceLocation IvarLoc;
2383
2384   /// Property declaration being implemented
2385   ObjCPropertyDecl *PropertyDecl;
2386
2387   /// Null for \@dynamic. Required for \@synthesize.
2388   ObjCIvarDecl *PropertyIvarDecl;
2389
2390   /// Null for \@dynamic. Non-null if property must be copy-constructed in
2391   /// getter.
2392   Expr *GetterCXXConstructor;
2393
2394   /// Null for \@dynamic. Non-null if property has assignment operator to call
2395   /// in Setter synthesis.
2396   Expr *SetterCXXAssignment;
2397
2398   ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2399                        ObjCPropertyDecl *property,
2400                        Kind PK,
2401                        ObjCIvarDecl *ivarDecl,
2402                        SourceLocation ivarLoc)
2403     : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2404       IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2405       GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) {
2406     assert (PK == Dynamic || PropertyIvarDecl);
2407   }
2408
2409 public:
2410   static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2411                                       SourceLocation atLoc, SourceLocation L,
2412                                       ObjCPropertyDecl *property,
2413                                       Kind PK,
2414                                       ObjCIvarDecl *ivarDecl,
2415                                       SourceLocation ivarLoc);
2416
2417   static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2418
2419   SourceRange getSourceRange() const override LLVM_READONLY;
2420
2421   SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2422   void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2423
2424   ObjCPropertyDecl *getPropertyDecl() const {
2425     return PropertyDecl;
2426   }
2427   void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2428
2429   Kind getPropertyImplementation() const {
2430     return PropertyIvarDecl ? Synthesize : Dynamic;
2431   }
2432
2433   ObjCIvarDecl *getPropertyIvarDecl() const {
2434     return PropertyIvarDecl;
2435   }
2436   SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2437
2438   void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2439                            SourceLocation IvarLoc) {
2440     PropertyIvarDecl = Ivar;
2441     this->IvarLoc = IvarLoc;
2442   }
2443
2444   /// \brief For \@synthesize, returns true if an ivar name was explicitly
2445   /// specified.
2446   ///
2447   /// \code
2448   /// \@synthesize int a = b; // true
2449   /// \@synthesize int a; // false
2450   /// \endcode
2451   bool isIvarNameSpecified() const {
2452     return IvarLoc.isValid() && IvarLoc != getLocation();
2453   }
2454
2455   Expr *getGetterCXXConstructor() const {
2456     return GetterCXXConstructor;
2457   }
2458   void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2459     GetterCXXConstructor = getterCXXConstructor;
2460   }
2461
2462   Expr *getSetterCXXAssignment() const {
2463     return SetterCXXAssignment;
2464   }
2465   void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2466     SetterCXXAssignment = setterCXXAssignment;
2467   }
2468
2469   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2470   static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2471
2472   friend class ASTDeclReader;
2473 };
2474
2475 template<bool (*Filter)(ObjCCategoryDecl *)>
2476 void
2477 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2478 findAcceptableCategory() {
2479   while (Current && !Filter(Current))
2480     Current = Current->getNextClassCategoryRaw();
2481 }
2482
2483 template<bool (*Filter)(ObjCCategoryDecl *)>
2484 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2485 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2486   Current = Current->getNextClassCategoryRaw();
2487   findAcceptableCategory();
2488   return *this;
2489 }
2490
2491 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2492   return !Cat->isHidden();
2493 }
2494
2495 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2496   return Cat->IsClassExtension() && !Cat->isHidden();
2497 }
2498
2499 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2500   return Cat->IsClassExtension();
2501 }
2502
2503 }  // end namespace clang
2504 #endif