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