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