DeclObjC.h revision a54fbf2499c7cc999e22abb9be484ce976ed9689
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
21namespace clang {
22class Expr;
23class Stmt;
24class FunctionDecl;
25class RecordDecl;
26class ObjCIvarDecl;
27class ObjCMethodDecl;
28class ObjCProtocolDecl;
29class ObjCCategoryDecl;
30class ObjCPropertyDecl;
31class ObjCPropertyImplDecl;
32class CXXCtorInitializer;
33
34class ObjCListBase {
35  void operator=(const ObjCListBase &);     // DO NOT IMPLEMENT
36  ObjCListBase(const ObjCListBase&);        // DO NOT IMPLEMENT
37protected:
38  /// List is an array of pointers to objects that are not owned by this object.
39  void **List;
40  unsigned NumElts;
41
42public:
43  ObjCListBase() : List(0), NumElts(0) {}
44  unsigned size() const { return NumElts; }
45  bool empty() const { return NumElts == 0; }
46
47protected:
48  void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
49};
50
51
52/// ObjCList - This is a simple template class used to hold various lists of
53/// decls etc, which is heavily used by the ObjC front-end.  This only use case
54/// this supports is setting the list all at once and then reading elements out
55/// of it.
56template <typename T>
57class ObjCList : public ObjCListBase {
58public:
59  void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
60    ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
61  }
62
63  typedef T* const * iterator;
64  iterator begin() const { return (iterator)List; }
65  iterator end() const { return (iterator)List+NumElts; }
66
67  T* operator[](unsigned Idx) const {
68    assert(Idx < NumElts && "Invalid access");
69    return (T*)List[Idx];
70  }
71};
72
73/// \brief A list of Objective-C protocols, along with the source
74/// locations at which they were referenced.
75class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
76  SourceLocation *Locations;
77
78  using ObjCList<ObjCProtocolDecl>::set;
79
80public:
81  ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(0) { }
82
83  typedef const SourceLocation *loc_iterator;
84  loc_iterator loc_begin() const { return Locations; }
85  loc_iterator loc_end() const { return Locations + size(); }
86
87  void set(ObjCProtocolDecl* const* InList, unsigned Elts,
88           const SourceLocation *Locs, ASTContext &Ctx);
89};
90
91
92/// ObjCMethodDecl - Represents an instance or class method declaration.
93/// ObjC methods can be declared within 4 contexts: class interfaces,
94/// categories, protocols, and class implementations. While C++ member
95/// functions leverage C syntax, Objective-C method syntax is modeled after
96/// Smalltalk (using colons to specify argument types/expressions).
97/// Here are some brief examples:
98///
99/// Setter/getter instance methods:
100/// - (void)setMenu:(NSMenu *)menu;
101/// - (NSMenu *)menu;
102///
103/// Instance method that takes 2 NSView arguments:
104/// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
105///
106/// Getter class method:
107/// + (NSMenu *)defaultMenu;
108///
109/// A selector represents a unique name for a method. The selector names for
110/// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
111///
112class ObjCMethodDecl : public NamedDecl, public DeclContext {
113public:
114  enum ImplementationControl { None, Required, Optional };
115private:
116  // The conventional meaning of this method; an ObjCMethodFamily.
117  // This is not serialized; instead, it is computed on demand and
118  // cached.
119  mutable unsigned Family : ObjCMethodFamilyBitWidth;
120
121  /// instance (true) or class (false) method.
122  unsigned IsInstance : 1;
123  unsigned IsVariadic : 1;
124
125  // Synthesized declaration method for a property setter/getter
126  unsigned IsSynthesized : 1;
127
128  // Method has a definition.
129  unsigned IsDefined : 1;
130
131  /// \brief Method redeclaration in the same interface.
132  unsigned IsRedeclaration : 1;
133
134  /// \brief Is redeclared in the same interface.
135  mutable unsigned HasRedeclaration : 1;
136
137  // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
138  /// @required/@optional
139  unsigned DeclImplementation : 2;
140
141  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
142  /// in, inout, etc.
143  unsigned objcDeclQualifier : 6;
144
145  /// \brief Indicates whether this method has a related result type.
146  unsigned RelatedResultType : 1;
147
148  /// \brief Whether the locations of the selector identifiers are in a
149  /// "standard" position, a enum SelectorLocationsKind.
150  unsigned SelLocsKind : 2;
151
152  // Result type of this method.
153  QualType MethodDeclType;
154
155  // Type source information for the result type.
156  TypeSourceInfo *ResultTInfo;
157
158  /// \brief Array of ParmVarDecls for the formal parameters of this method
159  /// and optionally followed by selector locations.
160  void *ParamsAndSelLocs;
161  unsigned NumParams;
162
163  /// List of attributes for this method declaration.
164  SourceLocation EndLoc; // the location of the ';' or '}'.
165
166  // The following are only used for method definitions, null otherwise.
167  // FIXME: space savings opportunity, consider a sub-class.
168  Stmt *Body;
169
170  /// SelfDecl - Decl for the implicit self parameter. This is lazily
171  /// constructed by createImplicitParams.
172  ImplicitParamDecl *SelfDecl;
173  /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
174  /// constructed by createImplicitParams.
175  ImplicitParamDecl *CmdDecl;
176
177  SelectorLocationsKind getSelLocsKind() const {
178    return (SelectorLocationsKind)SelLocsKind;
179  }
180  bool hasStandardSelLocs() const {
181    return getSelLocsKind() != SelLoc_NonStandard;
182  }
183
184  /// \brief Get a pointer to the stored selector identifiers locations array.
185  /// No locations will be stored if HasStandardSelLocs is true.
186  SourceLocation *getStoredSelLocs() {
187    return reinterpret_cast<SourceLocation*>(getParams() + NumParams);
188  }
189  const SourceLocation *getStoredSelLocs() const {
190    return reinterpret_cast<const SourceLocation*>(getParams() + NumParams);
191  }
192
193  /// \brief Get a pointer to the stored selector identifiers locations array.
194  /// No locations will be stored if HasStandardSelLocs is true.
195  ParmVarDecl **getParams() {
196    return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
197  }
198  const ParmVarDecl *const *getParams() const {
199    return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
200  }
201
202  /// \brief Get the number of stored selector identifiers locations.
203  /// No locations will be stored if HasStandardSelLocs is true.
204  unsigned getNumStoredSelLocs() const {
205    if (hasStandardSelLocs())
206      return 0;
207    return getNumSelectorLocs();
208  }
209
210  void setParamsAndSelLocs(ASTContext &C,
211                           ArrayRef<ParmVarDecl*> Params,
212                           ArrayRef<SourceLocation> SelLocs);
213
214  ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
215                 Selector SelInfo, QualType T,
216                 TypeSourceInfo *ResultTInfo,
217                 DeclContext *contextDecl,
218                 bool isInstance = true,
219                 bool isVariadic = false,
220                 bool isSynthesized = false,
221                 bool isImplicitlyDeclared = false,
222                 bool isDefined = false,
223                 ImplementationControl impControl = None,
224                 bool HasRelatedResultType = false)
225  : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
226    DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily),
227    IsInstance(isInstance), IsVariadic(isVariadic),
228    IsSynthesized(isSynthesized),
229    IsDefined(isDefined), IsRedeclaration(0), HasRedeclaration(0),
230    DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
231    RelatedResultType(HasRelatedResultType),
232    SelLocsKind(SelLoc_StandardNoSpace),
233    MethodDeclType(T), ResultTInfo(ResultTInfo),
234    ParamsAndSelLocs(0), NumParams(0),
235    EndLoc(endLoc), Body(0), SelfDecl(0), CmdDecl(0) {
236    setImplicit(isImplicitlyDeclared);
237  }
238
239  /// \brief A definition will return its interface declaration.
240  /// An interface declaration will return its definition.
241  /// Otherwise it will return itself.
242  virtual ObjCMethodDecl *getNextRedeclaration();
243
244public:
245  static ObjCMethodDecl *Create(ASTContext &C,
246                                SourceLocation beginLoc,
247                                SourceLocation endLoc,
248                                Selector SelInfo,
249                                QualType T,
250                                TypeSourceInfo *ResultTInfo,
251                                DeclContext *contextDecl,
252                                bool isInstance = true,
253                                bool isVariadic = false,
254                                bool isSynthesized = false,
255                                bool isImplicitlyDeclared = false,
256                                bool isDefined = false,
257                                ImplementationControl impControl = None,
258                                bool HasRelatedResultType = false);
259
260  static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
261
262  virtual ObjCMethodDecl *getCanonicalDecl();
263  const ObjCMethodDecl *getCanonicalDecl() const {
264    return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
265  }
266
267  ObjCDeclQualifier getObjCDeclQualifier() const {
268    return ObjCDeclQualifier(objcDeclQualifier);
269  }
270  void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
271
272  /// \brief Determine whether this method has a result type that is related
273  /// to the message receiver's type.
274  bool hasRelatedResultType() const { return RelatedResultType; }
275
276  /// \brief Note whether this method has a related result type.
277  void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; }
278
279  /// \brief True if this is a method redeclaration in the same interface.
280  bool isRedeclaration() const { return IsRedeclaration; }
281  void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
282
283  // Location information, modeled after the Stmt API.
284  SourceLocation getLocStart() const { return getLocation(); }
285  SourceLocation getLocEnd() const { return EndLoc; }
286  void setEndLoc(SourceLocation Loc) { EndLoc = Loc; }
287  virtual SourceRange getSourceRange() const {
288    return SourceRange(getLocation(), EndLoc);
289  }
290
291  SourceLocation getSelectorStartLoc() const {
292    if (isImplicit())
293      return getLocStart();
294    return getSelectorLoc(0);
295  }
296  SourceLocation getSelectorLoc(unsigned Index) const {
297    assert(Index < getNumSelectorLocs() && "Index out of range!");
298    if (hasStandardSelLocs())
299      return getStandardSelectorLoc(Index, getSelector(),
300                                   getSelLocsKind() == SelLoc_StandardWithSpace,
301                      llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
302                                         NumParams),
303                                   EndLoc);
304    return getStoredSelLocs()[Index];
305  }
306
307  void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
308
309  unsigned getNumSelectorLocs() const {
310    if (isImplicit())
311      return 0;
312    Selector Sel = getSelector();
313    if (Sel.isUnarySelector())
314      return 1;
315    return Sel.getNumArgs();
316  }
317
318  ObjCInterfaceDecl *getClassInterface();
319  const ObjCInterfaceDecl *getClassInterface() const {
320    return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
321  }
322
323  Selector getSelector() const { return getDeclName().getObjCSelector(); }
324
325  QualType getResultType() const { return MethodDeclType; }
326  void setResultType(QualType T) { MethodDeclType = T; }
327
328  /// \brief Determine the type of an expression that sends a message to this
329  /// function.
330  QualType getSendResultType() const {
331    return getResultType().getNonLValueExprType(getASTContext());
332  }
333
334  TypeSourceInfo *getResultTypeSourceInfo() const { return ResultTInfo; }
335  void setResultTypeSourceInfo(TypeSourceInfo *TInfo) { ResultTInfo = TInfo; }
336
337  // Iterator access to formal parameters.
338  unsigned param_size() const { return NumParams; }
339  typedef const ParmVarDecl *const *param_const_iterator;
340  typedef ParmVarDecl *const *param_iterator;
341  param_const_iterator param_begin() const { return getParams(); }
342  param_const_iterator param_end() const { return getParams() + NumParams; }
343  param_iterator param_begin() { return getParams(); }
344  param_iterator param_end() { return getParams() + NumParams; }
345  // This method returns and of the parameters which are part of the selector
346  // name mangling requirements.
347  param_const_iterator sel_param_end() const {
348    return param_begin() + getSelector().getNumArgs();
349  }
350
351  /// \brief Sets the method's parameters and selector source locations.
352  /// If the method is implicit (not coming from source) \arg SelLocs is
353  /// ignored.
354  void setMethodParams(ASTContext &C,
355                       ArrayRef<ParmVarDecl*> Params,
356                       ArrayRef<SourceLocation> SelLocs =
357                           ArrayRef<SourceLocation>());
358
359  // Iterator access to parameter types.
360  typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
361  typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
362      arg_type_iterator;
363
364  arg_type_iterator arg_type_begin() const {
365    return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
366  }
367  arg_type_iterator arg_type_end() const {
368    return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
369  }
370
371  /// createImplicitParams - Used to lazily create the self and cmd
372  /// implict parameters. This must be called prior to using getSelfDecl()
373  /// or getCmdDecl(). The call is ignored if the implicit paramters
374  /// have already been created.
375  void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
376
377  ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
378  void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
379  ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
380  void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
381
382  /// Determines the family of this method.
383  ObjCMethodFamily getMethodFamily() const;
384
385  bool isInstanceMethod() const { return IsInstance; }
386  void setInstanceMethod(bool isInst) { IsInstance = isInst; }
387  bool isVariadic() const { return IsVariadic; }
388  void setVariadic(bool isVar) { IsVariadic = isVar; }
389
390  bool isClassMethod() const { return !IsInstance; }
391
392  bool isSynthesized() const { return IsSynthesized; }
393  void setSynthesized(bool isSynth) { IsSynthesized = isSynth; }
394
395  bool isDefined() const { return IsDefined; }
396  void setDefined(bool isDefined) { IsDefined = isDefined; }
397
398  // Related to protocols declared in  @protocol
399  void setDeclImplementation(ImplementationControl ic) {
400    DeclImplementation = ic;
401  }
402  ImplementationControl getImplementationControl() const {
403    return ImplementationControl(DeclImplementation);
404  }
405
406  virtual Stmt *getBody() const {
407    return (Stmt*) Body;
408  }
409  CompoundStmt *getCompoundBody() { return (CompoundStmt*)Body; }
410  void setBody(Stmt *B) { Body = B; }
411
412  /// \brief Returns whether this specific method is a definition.
413  bool isThisDeclarationADefinition() const { return Body; }
414
415  // Implement isa/cast/dyncast/etc.
416  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
417  static bool classof(const ObjCMethodDecl *D) { return true; }
418  static bool classofKind(Kind K) { return K == ObjCMethod; }
419  static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
420    return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
421  }
422  static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
423    return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
424  }
425
426  friend class ASTDeclReader;
427  friend class ASTDeclWriter;
428};
429
430/// ObjCContainerDecl - Represents a container for method declarations.
431/// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
432/// ObjCProtocolDecl, and ObjCImplDecl.
433///
434class ObjCContainerDecl : public NamedDecl, public DeclContext {
435  virtual void anchor();
436
437  SourceLocation AtStart;
438
439  // These two locations in the range mark the end of the method container.
440  // The first points to the '@' token, and the second to the 'end' token.
441  SourceRange AtEnd;
442public:
443
444  ObjCContainerDecl(Kind DK, DeclContext *DC,
445                    IdentifierInfo *Id, SourceLocation nameLoc,
446                    SourceLocation atStartLoc)
447    : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
448
449  // Iterator access to properties.
450  typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
451  prop_iterator prop_begin() const {
452    return prop_iterator(decls_begin());
453  }
454  prop_iterator prop_end() const {
455    return prop_iterator(decls_end());
456  }
457
458  // Iterator access to instance/class methods.
459  typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
460  method_iterator meth_begin() const {
461    return method_iterator(decls_begin());
462  }
463  method_iterator meth_end() const {
464    return method_iterator(decls_end());
465  }
466
467  typedef filtered_decl_iterator<ObjCMethodDecl,
468                                 &ObjCMethodDecl::isInstanceMethod>
469    instmeth_iterator;
470  instmeth_iterator instmeth_begin() const {
471    return instmeth_iterator(decls_begin());
472  }
473  instmeth_iterator instmeth_end() const {
474    return instmeth_iterator(decls_end());
475  }
476
477  typedef filtered_decl_iterator<ObjCMethodDecl,
478                                 &ObjCMethodDecl::isClassMethod>
479    classmeth_iterator;
480  classmeth_iterator classmeth_begin() const {
481    return classmeth_iterator(decls_begin());
482  }
483  classmeth_iterator classmeth_end() const {
484    return classmeth_iterator(decls_end());
485  }
486
487  // Get the local instance/class method declared in this interface.
488  ObjCMethodDecl *getMethod(Selector Sel, bool isInstance) const;
489  ObjCMethodDecl *getInstanceMethod(Selector Sel) const {
490    return getMethod(Sel, true/*isInstance*/);
491  }
492  ObjCMethodDecl *getClassMethod(Selector Sel) const {
493    return getMethod(Sel, false/*isInstance*/);
494  }
495  ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
496
497  ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
498
499  SourceLocation getAtStartLoc() const { return AtStart; }
500  void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
501
502  // Marks the end of the container.
503  SourceRange getAtEndRange() const {
504    return AtEnd;
505  }
506  void setAtEndRange(SourceRange atEnd) {
507    AtEnd = atEnd;
508  }
509
510  virtual SourceRange getSourceRange() const {
511    return SourceRange(AtStart, getAtEndRange().getEnd());
512  }
513
514  // Implement isa/cast/dyncast/etc.
515  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
516  static bool classof(const ObjCContainerDecl *D) { return true; }
517  static bool classofKind(Kind K) {
518    return K >= firstObjCContainer &&
519           K <= lastObjCContainer;
520  }
521
522  static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
523    return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
524  }
525  static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
526    return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
527  }
528};
529
530/// ObjCInterfaceDecl - Represents an ObjC class declaration. For example:
531///
532///   // MostPrimitive declares no super class (not particularly useful).
533///   @interface MostPrimitive
534///     // no instance variables or methods.
535///   @end
536///
537///   // NSResponder inherits from NSObject & implements NSCoding (a protocol).
538///   @interface NSResponder : NSObject <NSCoding>
539///   { // instance variables are represented by ObjCIvarDecl.
540///     id nextResponder; // nextResponder instance variable.
541///   }
542///   - (NSResponder *)nextResponder; // return a pointer to NSResponder.
543///   - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
544///   @end                                    // to an NSEvent.
545///
546///   Unlike C/C++, forward class declarations are accomplished with @class.
547///   Unlike C/C++, @class allows for a list of classes to be forward declared.
548///   Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
549///   typically inherit from NSObject (an exception is NSProxy).
550///
551class ObjCInterfaceDecl : public ObjCContainerDecl
552                        , public Redeclarable<ObjCInterfaceDecl> {
553  virtual void anchor();
554
555  /// TypeForDecl - This indicates the Type object that represents this
556  /// TypeDecl.  It is a cache maintained by ASTContext::getObjCInterfaceType
557  mutable const Type *TypeForDecl;
558  friend class ASTContext;
559
560  struct DefinitionData {
561    /// \brief The definition of this class, for quick access from any
562    /// declaration.
563    ObjCInterfaceDecl *Definition;
564
565    /// Class's super class.
566    ObjCInterfaceDecl *SuperClass;
567
568    /// Protocols referenced in the @interface  declaration
569    ObjCProtocolList ReferencedProtocols;
570
571    /// Protocols reference in both the @interface and class extensions.
572    ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
573
574    /// \brief List of categories and class extensions defined for this class.
575    ///
576    /// Categories are stored as a linked list in the AST, since the categories
577    /// and class extensions come long after the initial interface declaration,
578    /// and we avoid dynamically-resized arrays in the AST wherever possible.
579    ObjCCategoryDecl *CategoryList;
580
581    /// IvarList - List of all ivars defined by this class; including class
582    /// extensions and implementation. This list is built lazily.
583    ObjCIvarDecl *IvarList;
584
585    /// \brief Indicates that the contents of this Objective-C class will be
586    /// completed by the external AST source when required.
587    mutable bool ExternallyCompleted : 1;
588
589    /// \brief The location of the superclass, if any.
590    SourceLocation SuperClassLoc;
591
592    /// \brief The location of the last location in this declaration, before
593    /// the properties/methods. For example, this will be the '>', '}', or
594    /// identifier,
595    SourceLocation EndLoc;
596
597    DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(),
598                       ExternallyCompleted() { }
599  };
600
601  ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
602                    SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
603                    bool isInternal);
604
605  void LoadExternalDefinition() const;
606
607  /// \brief Contains a pointer to the data associated with this class,
608  /// which will be NULL if this class has not yet been defined.
609  DefinitionData *Data;
610
611  DefinitionData &data() const {
612    assert(Data != 0 && "Declaration has no definition!");
613    return *Data;
614  }
615
616  /// \brief Allocate the definition data for this class.
617  void allocateDefinitionData();
618
619  typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
620  virtual ObjCInterfaceDecl *getNextRedeclaration() {
621    return RedeclLink.getNext();
622  }
623  virtual ObjCInterfaceDecl *getPreviousDeclImpl() {
624    return getPreviousDecl();
625  }
626  virtual ObjCInterfaceDecl *getMostRecentDeclImpl() {
627    return getMostRecentDecl();
628  }
629
630public:
631  static ObjCInterfaceDecl *Create(ASTContext &C, DeclContext *DC,
632                                   SourceLocation atLoc,
633                                   IdentifierInfo *Id,
634                                   ObjCInterfaceDecl *PrevDecl,
635                                   SourceLocation ClassLoc = SourceLocation(),
636                                   bool isInternal = false);
637
638  static ObjCInterfaceDecl *CreateDeserialized(ASTContext &C, unsigned ID);
639
640  virtual SourceRange getSourceRange() const {
641    if (isThisDeclarationADefinition())
642      return ObjCContainerDecl::getSourceRange();
643
644    return SourceRange(getAtStartLoc(), getLocation());
645  }
646
647  /// \brief Indicate that this Objective-C class is complete, but that
648  /// the external AST source will be responsible for filling in its contents
649  /// when a complete class is required.
650  void setExternallyCompleted();
651
652  const ObjCProtocolList &getReferencedProtocols() const {
653    if (data().ExternallyCompleted)
654      LoadExternalDefinition();
655
656    return data().ReferencedProtocols;
657  }
658
659  ObjCImplementationDecl *getImplementation() const;
660  void setImplementation(ObjCImplementationDecl *ImplD);
661
662  ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
663
664  // Get the local instance/class method declared in a category.
665  ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
666  ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
667  ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
668    return isInstance ? getInstanceMethod(Sel)
669                      : getClassMethod(Sel);
670  }
671
672  typedef ObjCProtocolList::iterator protocol_iterator;
673
674  protocol_iterator protocol_begin() const {
675    // FIXME: Should make sure no callers ever do this.
676    if (!hasDefinition())
677      return protocol_iterator();
678
679    if (data().ExternallyCompleted)
680      LoadExternalDefinition();
681
682    return data().ReferencedProtocols.begin();
683  }
684  protocol_iterator protocol_end() const {
685    // FIXME: Should make sure no callers ever do this.
686    if (!hasDefinition())
687      return protocol_iterator();
688
689    if (data().ExternallyCompleted)
690      LoadExternalDefinition();
691
692    return data().ReferencedProtocols.end();
693  }
694
695  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
696
697  protocol_loc_iterator protocol_loc_begin() const {
698    // FIXME: Should make sure no callers ever do this.
699    if (!hasDefinition())
700      return protocol_loc_iterator();
701
702    if (data().ExternallyCompleted)
703      LoadExternalDefinition();
704
705    return data().ReferencedProtocols.loc_begin();
706  }
707
708  protocol_loc_iterator protocol_loc_end() const {
709    // FIXME: Should make sure no callers ever do this.
710    if (!hasDefinition())
711      return protocol_loc_iterator();
712
713    if (data().ExternallyCompleted)
714      LoadExternalDefinition();
715
716    return data().ReferencedProtocols.loc_end();
717  }
718
719  typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
720
721  all_protocol_iterator all_referenced_protocol_begin() const {
722    // FIXME: Should make sure no callers ever do this.
723    if (!hasDefinition())
724      return all_protocol_iterator();
725
726    if (data().ExternallyCompleted)
727      LoadExternalDefinition();
728
729    return data().AllReferencedProtocols.empty()
730             ? protocol_begin()
731             : data().AllReferencedProtocols.begin();
732  }
733  all_protocol_iterator all_referenced_protocol_end() const {
734    // FIXME: Should make sure no callers ever do this.
735    if (!hasDefinition())
736      return all_protocol_iterator();
737
738    if (data().ExternallyCompleted)
739      LoadExternalDefinition();
740
741    return data().AllReferencedProtocols.empty()
742             ? protocol_end()
743             : data().AllReferencedProtocols.end();
744  }
745
746  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
747
748  ivar_iterator ivar_begin() const {
749    if (const ObjCInterfaceDecl *Def = getDefinition())
750      return ivar_iterator(Def->decls_begin());
751
752    // FIXME: Should make sure no callers ever do this.
753    return ivar_iterator();
754  }
755  ivar_iterator ivar_end() const {
756    if (const ObjCInterfaceDecl *Def = getDefinition())
757      return ivar_iterator(Def->decls_end());
758
759    // FIXME: Should make sure no callers ever do this.
760    return ivar_iterator();
761  }
762
763  unsigned ivar_size() const {
764    return std::distance(ivar_begin(), ivar_end());
765  }
766
767  bool ivar_empty() const { return ivar_begin() == ivar_end(); }
768
769  ObjCIvarDecl *all_declared_ivar_begin();
770  const ObjCIvarDecl *all_declared_ivar_begin() const {
771    // Even though this modifies IvarList, it's conceptually const:
772    // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
773    return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
774  }
775  void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
776
777  /// setProtocolList - Set the list of protocols that this interface
778  /// implements.
779  void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
780                       const SourceLocation *Locs, ASTContext &C) {
781    data().ReferencedProtocols.set(List, Num, Locs, C);
782  }
783
784  /// mergeClassExtensionProtocolList - Merge class extension's protocol list
785  /// into the protocol list for this class.
786  void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
787                                       unsigned Num,
788                                       ASTContext &C);
789
790  /// \brief Determine whether this particular declaration of this class is
791  /// actually also a definition.
792  bool isThisDeclarationADefinition() const {
793    return Data && Data->Definition == this;
794  }
795
796  /// \brief Determine whether this class has been defined.
797  bool hasDefinition() const { return Data; }
798
799  /// \brief Retrieve the definition of this class, or NULL if this class
800  /// has been forward-declared (with @class) but not yet defined (with
801  /// @interface).
802  ObjCInterfaceDecl *getDefinition() {
803    return hasDefinition()? Data->Definition : 0;
804  }
805
806  /// \brief Retrieve the definition of this class, or NULL if this class
807  /// has been forward-declared (with @class) but not yet defined (with
808  /// @interface).
809  const ObjCInterfaceDecl *getDefinition() const {
810    return hasDefinition()? Data->Definition : 0;
811  }
812
813  /// \brief Starts the definition of this Objective-C class, taking it from
814  /// a forward declaration (@class) to a definition (@interface).
815  void startDefinition();
816
817  ObjCInterfaceDecl *getSuperClass() const {
818    // FIXME: Should make sure no callers ever do this.
819    if (!hasDefinition())
820      return 0;
821
822    if (data().ExternallyCompleted)
823      LoadExternalDefinition();
824
825    return data().SuperClass;
826  }
827
828  void setSuperClass(ObjCInterfaceDecl * superCls) {
829    data().SuperClass =
830      (superCls && superCls->hasDefinition()) ? superCls->getDefinition()
831                                              : superCls;
832  }
833
834  ObjCCategoryDecl* getCategoryList() const {
835    // FIXME: Should make sure no callers ever do this.
836    if (!hasDefinition())
837      return 0;
838
839    if (data().ExternallyCompleted)
840      LoadExternalDefinition();
841
842    return data().CategoryList;
843  }
844
845  void setCategoryList(ObjCCategoryDecl *category) {
846    data().CategoryList = category;
847  }
848
849  ObjCCategoryDecl* getFirstClassExtension() const;
850
851  ObjCPropertyDecl
852    *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
853
854  /// isSuperClassOf - Return true if this class is the specified class or is a
855  /// super class of the specified interface class.
856  bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
857    // If RHS is derived from LHS it is OK; else it is not OK.
858    while (I != NULL) {
859      if (declaresSameEntity(this, I))
860        return true;
861
862      I = I->getSuperClass();
863    }
864    return false;
865  }
866
867  /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
868  /// to be incompatible with __weak references. Returns true if it is.
869  bool isArcWeakrefUnavailable() const {
870    const ObjCInterfaceDecl *Class = this;
871    while (Class) {
872      if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
873        return true;
874      Class = Class->getSuperClass();
875   }
876   return false;
877  }
878
879  /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
880  /// classes must not be auto-synthesized. Returns class decl. if it must not be;
881  /// 0, otherwise.
882  const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const {
883    const ObjCInterfaceDecl *Class = this;
884    while (Class) {
885      if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
886        return Class;
887      Class = Class->getSuperClass();
888   }
889   return 0;
890  }
891
892  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
893                                       ObjCInterfaceDecl *&ClassDeclared);
894  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
895    ObjCInterfaceDecl *ClassDeclared;
896    return lookupInstanceVariable(IVarName, ClassDeclared);
897  }
898
899  // Lookup a method. First, we search locally. If a method isn't
900  // found, we search referenced protocols and class categories.
901  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
902  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
903    return lookupMethod(Sel, true/*isInstance*/);
904  }
905  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
906    return lookupMethod(Sel, false/*isInstance*/);
907  }
908  ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
909
910  // Lookup a method in the classes implementation hierarchy.
911  ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true);
912
913  SourceLocation getEndOfDefinitionLoc() const {
914    if (!hasDefinition())
915      return getLocation();
916
917    return data().EndLoc;
918  }
919
920  void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
921
922  void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
923  SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
924
925  /// isImplicitInterfaceDecl - check that this is an implicitly declared
926  /// ObjCInterfaceDecl node. This is for legacy objective-c @implementation
927  /// declaration without an @interface declaration.
928  bool isImplicitInterfaceDecl() const {
929    return hasDefinition() ? Data->Definition->isImplicit() : isImplicit();
930  }
931
932  /// ClassImplementsProtocol - Checks that 'lProto' protocol
933  /// has been implemented in IDecl class, its super class or categories (if
934  /// lookupCategory is true).
935  bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
936                               bool lookupCategory,
937                               bool RHSIsQualifiedID = false);
938
939  typedef redeclarable_base::redecl_iterator redecl_iterator;
940  using redeclarable_base::redecls_begin;
941  using redeclarable_base::redecls_end;
942  using redeclarable_base::getPreviousDecl;
943  using redeclarable_base::getMostRecentDecl;
944
945  /// Retrieves the canonical declaration of this Objective-C class.
946  ObjCInterfaceDecl *getCanonicalDecl() {
947    return getFirstDeclaration();
948  }
949  const ObjCInterfaceDecl *getCanonicalDecl() const {
950    return getFirstDeclaration();
951  }
952
953  // Low-level accessor
954  const Type *getTypeForDecl() const { return TypeForDecl; }
955  void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
956
957  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
958  static bool classof(const ObjCInterfaceDecl *D) { return true; }
959  static bool classofKind(Kind K) { return K == ObjCInterface; }
960
961  friend class ASTReader;
962  friend class ASTDeclReader;
963  friend class ASTDeclWriter;
964};
965
966/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
967/// instance variables are identical to C. The only exception is Objective-C
968/// supports C++ style access control. For example:
969///
970///   @interface IvarExample : NSObject
971///   {
972///     id defaultToProtected;
973///   @public:
974///     id canBePublic; // same as C++.
975///   @protected:
976///     id canBeProtected; // same as C++.
977///   @package:
978///     id canBePackage; // framework visibility (not available in C++).
979///   }
980///
981class ObjCIvarDecl : public FieldDecl {
982  virtual void anchor();
983
984public:
985  enum AccessControl {
986    None, Private, Protected, Public, Package
987  };
988
989private:
990  ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
991               SourceLocation IdLoc, IdentifierInfo *Id,
992               QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
993               bool synthesized)
994    : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
995                /*Mutable=*/false, /*HasInit=*/false),
996      NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {}
997
998public:
999  static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1000                              SourceLocation StartLoc, SourceLocation IdLoc,
1001                              IdentifierInfo *Id, QualType T,
1002                              TypeSourceInfo *TInfo,
1003                              AccessControl ac, Expr *BW = NULL,
1004                              bool synthesized=false);
1005
1006  static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1007
1008  /// \brief Return the class interface that this ivar is logically contained
1009  /// in; this is either the interface where the ivar was declared, or the
1010  /// interface the ivar is conceptually a part of in the case of synthesized
1011  /// ivars.
1012  const ObjCInterfaceDecl *getContainingInterface() const;
1013
1014  ObjCIvarDecl *getNextIvar() { return NextIvar; }
1015  const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1016  void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1017
1018  void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1019
1020  AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1021
1022  AccessControl getCanonicalAccessControl() const {
1023    return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1024  }
1025
1026  void setSynthesize(bool synth) { Synthesized = synth; }
1027  bool getSynthesize() const { return Synthesized; }
1028
1029  // Implement isa/cast/dyncast/etc.
1030  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1031  static bool classof(const ObjCIvarDecl *D) { return true; }
1032  static bool classofKind(Kind K) { return K == ObjCIvar; }
1033private:
1034  /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1035  /// extensions and class's implementation
1036  ObjCIvarDecl *NextIvar;
1037
1038  // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1039  unsigned DeclAccess : 3;
1040  unsigned Synthesized : 1;
1041};
1042
1043
1044/// ObjCAtDefsFieldDecl - Represents a field declaration created by an
1045///  @defs(...).
1046class ObjCAtDefsFieldDecl : public FieldDecl {
1047  virtual void anchor();
1048  ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1049                      SourceLocation IdLoc, IdentifierInfo *Id,
1050                      QualType T, Expr *BW)
1051    : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1052                /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
1053                BW, /*Mutable=*/false, /*HasInit=*/false) {}
1054
1055public:
1056  static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1057                                     SourceLocation StartLoc,
1058                                     SourceLocation IdLoc, IdentifierInfo *Id,
1059                                     QualType T, Expr *BW);
1060
1061  static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1062
1063  // Implement isa/cast/dyncast/etc.
1064  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1065  static bool classof(const ObjCAtDefsFieldDecl *D) { return true; }
1066  static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1067};
1068
1069/// ObjCProtocolDecl - Represents a protocol declaration. ObjC protocols
1070/// declare a pure abstract type (i.e no instance variables are permitted).
1071/// Protocols originally drew inspiration from C++ pure virtual functions (a C++
1072/// feature with nice semantics and lousy syntax:-). Here is an example:
1073///
1074/// @protocol NSDraggingInfo <refproto1, refproto2>
1075/// - (NSWindow *)draggingDestinationWindow;
1076/// - (NSImage *)draggedImage;
1077/// @end
1078///
1079/// This says that NSDraggingInfo requires two methods and requires everything
1080/// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1081/// well.
1082///
1083/// @interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo>
1084/// @end
1085///
1086/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1087/// protocols are in distinct namespaces. For example, Cocoa defines both
1088/// an NSObject protocol and class (which isn't allowed in Java). As a result,
1089/// protocols are referenced using angle brackets as follows:
1090///
1091/// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1092///
1093class ObjCProtocolDecl : public ObjCContainerDecl,
1094                         public Redeclarable<ObjCProtocolDecl> {
1095  virtual void anchor();
1096
1097  struct DefinitionData {
1098    // \brief The declaration that defines this protocol.
1099    ObjCProtocolDecl *Definition;
1100
1101    /// \brief Referenced protocols
1102    ObjCProtocolList ReferencedProtocols;
1103  };
1104
1105  DefinitionData *Data;
1106
1107  DefinitionData &data() const {
1108    assert(Data && "Objective-C protocol has no definition!");
1109    return *Data;
1110  }
1111
1112  ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1113                   SourceLocation nameLoc, SourceLocation atStartLoc,
1114                   ObjCProtocolDecl *PrevDecl);
1115
1116  void allocateDefinitionData();
1117
1118  typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1119  virtual ObjCProtocolDecl *getNextRedeclaration() {
1120    return RedeclLink.getNext();
1121  }
1122  virtual ObjCProtocolDecl *getPreviousDeclImpl() {
1123    return getPreviousDecl();
1124  }
1125  virtual ObjCProtocolDecl *getMostRecentDeclImpl() {
1126    return getMostRecentDecl();
1127  }
1128
1129public:
1130  static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1131                                  IdentifierInfo *Id,
1132                                  SourceLocation nameLoc,
1133                                  SourceLocation atStartLoc,
1134                                  ObjCProtocolDecl *PrevDecl);
1135
1136  static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1137
1138  const ObjCProtocolList &getReferencedProtocols() const {
1139    assert(hasDefinition() && "No definition available!");
1140    return data().ReferencedProtocols;
1141  }
1142  typedef ObjCProtocolList::iterator protocol_iterator;
1143  protocol_iterator protocol_begin() const {
1144    if (!hasDefinition())
1145      return protocol_iterator();
1146
1147    return data().ReferencedProtocols.begin();
1148  }
1149  protocol_iterator protocol_end() const {
1150    if (!hasDefinition())
1151      return protocol_iterator();
1152
1153    return data().ReferencedProtocols.end();
1154  }
1155  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1156  protocol_loc_iterator protocol_loc_begin() const {
1157    if (!hasDefinition())
1158      return protocol_loc_iterator();
1159
1160    return data().ReferencedProtocols.loc_begin();
1161  }
1162  protocol_loc_iterator protocol_loc_end() const {
1163    if (!hasDefinition())
1164      return protocol_loc_iterator();
1165
1166    return data().ReferencedProtocols.loc_end();
1167  }
1168  unsigned protocol_size() const {
1169    if (!hasDefinition())
1170      return 0;
1171
1172    return data().ReferencedProtocols.size();
1173  }
1174
1175  /// setProtocolList - Set the list of protocols that this interface
1176  /// implements.
1177  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1178                       const SourceLocation *Locs, ASTContext &C) {
1179    assert(Data && "Protocol is not defined");
1180    data().ReferencedProtocols.set(List, Num, Locs, C);
1181  }
1182
1183  ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1184
1185  // Lookup a method. First, we search locally. If a method isn't
1186  // found, we search referenced protocols and class categories.
1187  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1188  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1189    return lookupMethod(Sel, true/*isInstance*/);
1190  }
1191  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1192    return lookupMethod(Sel, false/*isInstance*/);
1193  }
1194
1195  /// \brief Determine whether this protocol has a definition.
1196  bool hasDefinition() const { return Data != 0; }
1197
1198  /// \brief Retrieve the definition of this protocol, if any.
1199  ObjCProtocolDecl *getDefinition() {
1200    return Data? Data->Definition : 0;
1201  }
1202
1203  /// \brief Retrieve the definition of this protocol, if any.
1204  const ObjCProtocolDecl *getDefinition() const {
1205    return Data? Data->Definition : 0;
1206  }
1207
1208  /// \brief Determine whether this particular declaration is also the
1209  /// definition.
1210  bool isThisDeclarationADefinition() const {
1211    return getDefinition() == this;
1212  }
1213
1214  /// \brief Starts the definition of this Objective-C protocol.
1215  void startDefinition();
1216
1217  virtual SourceRange getSourceRange() const {
1218    if (isThisDeclarationADefinition())
1219      return ObjCContainerDecl::getSourceRange();
1220
1221    return SourceRange(getAtStartLoc(), getLocation());
1222  }
1223
1224  typedef redeclarable_base::redecl_iterator redecl_iterator;
1225  using redeclarable_base::redecls_begin;
1226  using redeclarable_base::redecls_end;
1227  using redeclarable_base::getPreviousDecl;
1228  using redeclarable_base::getMostRecentDecl;
1229
1230  /// Retrieves the canonical declaration of this Objective-C protocol.
1231  ObjCProtocolDecl *getCanonicalDecl() {
1232    return getFirstDeclaration();
1233  }
1234  const ObjCProtocolDecl *getCanonicalDecl() const {
1235    return getFirstDeclaration();
1236  }
1237
1238  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1239  static bool classof(const ObjCProtocolDecl *D) { return true; }
1240  static bool classofKind(Kind K) { return K == ObjCProtocol; }
1241
1242  friend class ASTReader;
1243  friend class ASTDeclReader;
1244  friend class ASTDeclWriter;
1245};
1246
1247/// ObjCCategoryDecl - Represents a category declaration. A category allows
1248/// you to add methods to an existing class (without subclassing or modifying
1249/// the original class interface or implementation:-). Categories don't allow
1250/// you to add instance data. The following example adds "myMethod" to all
1251/// NSView's within a process:
1252///
1253/// @interface NSView (MyViewMethods)
1254/// - myMethod;
1255/// @end
1256///
1257/// Categories also allow you to split the implementation of a class across
1258/// several files (a feature more naturally supported in C++).
1259///
1260/// Categories were originally inspired by dynamic languages such as Common
1261/// Lisp and Smalltalk.  More traditional class-based languages (C++, Java)
1262/// don't support this level of dynamism, which is both powerful and dangerous.
1263///
1264class ObjCCategoryDecl : public ObjCContainerDecl {
1265  virtual void anchor();
1266
1267  /// Interface belonging to this category
1268  ObjCInterfaceDecl *ClassInterface;
1269
1270  /// referenced protocols in this category.
1271  ObjCProtocolList ReferencedProtocols;
1272
1273  /// Next category belonging to this class.
1274  /// FIXME: this should not be a singly-linked list.  Move storage elsewhere.
1275  ObjCCategoryDecl *NextClassCategory;
1276
1277  /// true of class extension has at least one bitfield ivar.
1278  bool HasSynthBitfield : 1;
1279
1280  /// \brief The location of the category name in this declaration.
1281  SourceLocation CategoryNameLoc;
1282
1283  ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1284                   SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1285                   IdentifierInfo *Id, ObjCInterfaceDecl *IDecl)
1286    : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1287      ClassInterface(IDecl), NextClassCategory(0), HasSynthBitfield(false),
1288      CategoryNameLoc(CategoryNameLoc) {
1289  }
1290public:
1291
1292  static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1293                                  SourceLocation AtLoc,
1294                                  SourceLocation ClassNameLoc,
1295                                  SourceLocation CategoryNameLoc,
1296                                  IdentifierInfo *Id,
1297                                  ObjCInterfaceDecl *IDecl);
1298  static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1299
1300  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1301  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1302
1303  ObjCCategoryImplDecl *getImplementation() const;
1304  void setImplementation(ObjCCategoryImplDecl *ImplD);
1305
1306  /// setProtocolList - Set the list of protocols that this interface
1307  /// implements.
1308  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1309                       const SourceLocation *Locs, ASTContext &C) {
1310    ReferencedProtocols.set(List, Num, Locs, C);
1311  }
1312
1313  const ObjCProtocolList &getReferencedProtocols() const {
1314    return ReferencedProtocols;
1315  }
1316
1317  typedef ObjCProtocolList::iterator protocol_iterator;
1318  protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1319  protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1320  unsigned protocol_size() const { return ReferencedProtocols.size(); }
1321  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1322  protocol_loc_iterator protocol_loc_begin() const {
1323    return ReferencedProtocols.loc_begin();
1324  }
1325  protocol_loc_iterator protocol_loc_end() const {
1326    return ReferencedProtocols.loc_end();
1327  }
1328
1329  ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1330
1331  bool IsClassExtension() const { return getIdentifier() == 0; }
1332  const ObjCCategoryDecl *getNextClassExtension() const;
1333
1334  bool hasSynthBitfield() const { return HasSynthBitfield; }
1335  void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1336
1337  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1338  ivar_iterator ivar_begin() const {
1339    return ivar_iterator(decls_begin());
1340  }
1341  ivar_iterator ivar_end() const {
1342    return ivar_iterator(decls_end());
1343  }
1344  unsigned ivar_size() const {
1345    return std::distance(ivar_begin(), ivar_end());
1346  }
1347  bool ivar_empty() const {
1348    return ivar_begin() == ivar_end();
1349  }
1350
1351  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1352  void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1353
1354  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1355  static bool classof(const ObjCCategoryDecl *D) { return true; }
1356  static bool classofKind(Kind K) { return K == ObjCCategory; }
1357
1358  friend class ASTDeclReader;
1359  friend class ASTDeclWriter;
1360};
1361
1362class ObjCImplDecl : public ObjCContainerDecl {
1363  virtual void anchor();
1364
1365  /// Class interface for this class/category implementation
1366  ObjCInterfaceDecl *ClassInterface;
1367
1368protected:
1369  ObjCImplDecl(Kind DK, DeclContext *DC,
1370               ObjCInterfaceDecl *classInterface,
1371               SourceLocation nameLoc, SourceLocation atStartLoc)
1372    : ObjCContainerDecl(DK, DC,
1373                        classInterface? classInterface->getIdentifier() : 0,
1374                        nameLoc, atStartLoc),
1375      ClassInterface(classInterface) {}
1376
1377public:
1378  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1379  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1380  void setClassInterface(ObjCInterfaceDecl *IFace);
1381
1382  void addInstanceMethod(ObjCMethodDecl *method) {
1383    // FIXME: Context should be set correctly before we get here.
1384    method->setLexicalDeclContext(this);
1385    addDecl(method);
1386  }
1387  void addClassMethod(ObjCMethodDecl *method) {
1388    // FIXME: Context should be set correctly before we get here.
1389    method->setLexicalDeclContext(this);
1390    addDecl(method);
1391  }
1392
1393  void addPropertyImplementation(ObjCPropertyImplDecl *property);
1394
1395  ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1396  ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1397
1398  // Iterator access to properties.
1399  typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1400  propimpl_iterator propimpl_begin() const {
1401    return propimpl_iterator(decls_begin());
1402  }
1403  propimpl_iterator propimpl_end() const {
1404    return propimpl_iterator(decls_end());
1405  }
1406
1407  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1408  static bool classof(const ObjCImplDecl *D) { return true; }
1409  static bool classofKind(Kind K) {
1410    return K >= firstObjCImpl && K <= lastObjCImpl;
1411  }
1412};
1413
1414/// ObjCCategoryImplDecl - An object of this class encapsulates a category
1415/// @implementation declaration. If a category class has declaration of a
1416/// property, its implementation must be specified in the category's
1417/// @implementation declaration. Example:
1418/// @interface I @end
1419/// @interface I(CATEGORY)
1420///    @property int p1, d1;
1421/// @end
1422/// @implementation I(CATEGORY)
1423///  @dynamic p1,d1;
1424/// @end
1425///
1426/// ObjCCategoryImplDecl
1427class ObjCCategoryImplDecl : public ObjCImplDecl {
1428  virtual void anchor();
1429
1430  // Category name
1431  IdentifierInfo *Id;
1432
1433  // Category name location
1434  SourceLocation CategoryNameLoc;
1435
1436  ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1437                       ObjCInterfaceDecl *classInterface,
1438                       SourceLocation nameLoc, SourceLocation atStartLoc,
1439                       SourceLocation CategoryNameLoc)
1440    : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1441      Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1442public:
1443  static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1444                                      IdentifierInfo *Id,
1445                                      ObjCInterfaceDecl *classInterface,
1446                                      SourceLocation nameLoc,
1447                                      SourceLocation atStartLoc,
1448                                      SourceLocation CategoryNameLoc);
1449  static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1450
1451  /// getIdentifier - Get the identifier that names the category
1452  /// interface associated with this implementation.
1453  /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1454  /// to mean something different. For example:
1455  /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1456  /// returns the class interface name, whereas
1457  /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1458  /// returns the category name.
1459  IdentifierInfo *getIdentifier() const {
1460    return Id;
1461  }
1462  void setIdentifier(IdentifierInfo *II) { Id = II; }
1463
1464  ObjCCategoryDecl *getCategoryDecl() const;
1465
1466  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1467
1468  /// getName - Get the name of identifier for the class interface associated
1469  /// with this implementation as a StringRef.
1470  //
1471  // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1472  // something different.
1473  StringRef getName() const {
1474    return Id ? Id->getNameStart() : "";
1475  }
1476
1477  /// getNameAsCString - Get the name of identifier for the class
1478  /// interface associated with this implementation as a C string
1479  /// (const char*).
1480  //
1481  // FIXME: Deprecated, move clients to getName().
1482  const char *getNameAsCString() const {
1483    return Id ? Id->getNameStart() : "";
1484  }
1485
1486  /// @brief Get the name of the class associated with this interface.
1487  //
1488  // FIXME: Deprecated, move clients to getName().
1489  std::string getNameAsString() const {
1490    return getName();
1491  }
1492
1493  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1494  static bool classof(const ObjCCategoryImplDecl *D) { return true; }
1495  static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1496
1497  friend class ASTDeclReader;
1498  friend class ASTDeclWriter;
1499};
1500
1501raw_ostream &operator<<(raw_ostream &OS,
1502                              const ObjCCategoryImplDecl *CID);
1503
1504/// ObjCImplementationDecl - Represents a class definition - this is where
1505/// method definitions are specified. For example:
1506///
1507/// @code
1508/// @implementation MyClass
1509/// - (void)myMethod { /* do something */ }
1510/// @end
1511/// @endcode
1512///
1513/// Typically, instance variables are specified in the class interface,
1514/// *not* in the implementation. Nevertheless (for legacy reasons), we
1515/// allow instance variables to be specified in the implementation.  When
1516/// specified, they need to be *identical* to the interface.
1517///
1518class ObjCImplementationDecl : public ObjCImplDecl {
1519  virtual void anchor();
1520  /// Implementation Class's super class.
1521  ObjCInterfaceDecl *SuperClass;
1522  /// Support for ivar initialization.
1523  /// IvarInitializers - The arguments used to initialize the ivars
1524  CXXCtorInitializer **IvarInitializers;
1525  unsigned NumIvarInitializers;
1526
1527  /// true if class has a .cxx_[construct,destruct] method.
1528  bool HasCXXStructors : 1;
1529
1530  /// true of class extension has at least one bitfield ivar.
1531  bool HasSynthBitfield : 1;
1532
1533  ObjCImplementationDecl(DeclContext *DC,
1534                         ObjCInterfaceDecl *classInterface,
1535                         ObjCInterfaceDecl *superDecl,
1536                         SourceLocation nameLoc, SourceLocation atStartLoc)
1537    : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
1538       SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0),
1539       HasCXXStructors(false), HasSynthBitfield(false) {}
1540public:
1541  static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1542                                        ObjCInterfaceDecl *classInterface,
1543                                        ObjCInterfaceDecl *superDecl,
1544                                        SourceLocation nameLoc,
1545                                        SourceLocation atStartLoc);
1546
1547  static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1548
1549  /// init_iterator - Iterates through the ivar initializer list.
1550  typedef CXXCtorInitializer **init_iterator;
1551
1552  /// init_const_iterator - Iterates through the ivar initializer list.
1553  typedef CXXCtorInitializer * const * init_const_iterator;
1554
1555  /// init_begin() - Retrieve an iterator to the first initializer.
1556  init_iterator       init_begin()       { return IvarInitializers; }
1557  /// begin() - Retrieve an iterator to the first initializer.
1558  init_const_iterator init_begin() const { return IvarInitializers; }
1559
1560  /// init_end() - Retrieve an iterator past the last initializer.
1561  init_iterator       init_end()       {
1562    return IvarInitializers + NumIvarInitializers;
1563  }
1564  /// end() - Retrieve an iterator past the last initializer.
1565  init_const_iterator init_end() const {
1566    return IvarInitializers + NumIvarInitializers;
1567  }
1568  /// getNumArgs - Number of ivars which must be initialized.
1569  unsigned getNumIvarInitializers() const {
1570    return NumIvarInitializers;
1571  }
1572
1573  void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1574    NumIvarInitializers = numNumIvarInitializers;
1575  }
1576
1577  void setIvarInitializers(ASTContext &C,
1578                           CXXCtorInitializer ** initializers,
1579                           unsigned numInitializers);
1580
1581  bool hasCXXStructors() const { return HasCXXStructors; }
1582  void setHasCXXStructors(bool val) { HasCXXStructors = val; }
1583
1584  bool hasSynthBitfield() const { return HasSynthBitfield; }
1585  void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1586
1587  /// getIdentifier - Get the identifier that names the class
1588  /// interface associated with this implementation.
1589  IdentifierInfo *getIdentifier() const {
1590    return getClassInterface()->getIdentifier();
1591  }
1592
1593  /// getName - Get the name of identifier for the class interface associated
1594  /// with this implementation as a StringRef.
1595  //
1596  // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1597  // something different.
1598  StringRef getName() const {
1599    assert(getIdentifier() && "Name is not a simple identifier");
1600    return getIdentifier()->getName();
1601  }
1602
1603  /// getNameAsCString - Get the name of identifier for the class
1604  /// interface associated with this implementation as a C string
1605  /// (const char*).
1606  //
1607  // FIXME: Move to StringRef API.
1608  const char *getNameAsCString() const {
1609    return getName().data();
1610  }
1611
1612  /// @brief Get the name of the class associated with this interface.
1613  //
1614  // FIXME: Move to StringRef API.
1615  std::string getNameAsString() const {
1616    return getName();
1617  }
1618
1619  const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1620  ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1621
1622  void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1623
1624  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1625  ivar_iterator ivar_begin() const {
1626    return ivar_iterator(decls_begin());
1627  }
1628  ivar_iterator ivar_end() const {
1629    return ivar_iterator(decls_end());
1630  }
1631  unsigned ivar_size() const {
1632    return std::distance(ivar_begin(), ivar_end());
1633  }
1634  bool ivar_empty() const {
1635    return ivar_begin() == ivar_end();
1636  }
1637
1638  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1639  static bool classof(const ObjCImplementationDecl *D) { return true; }
1640  static bool classofKind(Kind K) { return K == ObjCImplementation; }
1641
1642  friend class ASTDeclReader;
1643  friend class ASTDeclWriter;
1644};
1645
1646raw_ostream &operator<<(raw_ostream &OS,
1647                              const ObjCImplementationDecl *ID);
1648
1649/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1650/// declared as @compatibility_alias alias class.
1651class ObjCCompatibleAliasDecl : public NamedDecl {
1652  virtual void anchor();
1653  /// Class that this is an alias of.
1654  ObjCInterfaceDecl *AliasedClass;
1655
1656  ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1657                          ObjCInterfaceDecl* aliasedClass)
1658    : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1659public:
1660  static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1661                                         SourceLocation L, IdentifierInfo *Id,
1662                                         ObjCInterfaceDecl* aliasedClass);
1663
1664  static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
1665                                                     unsigned ID);
1666
1667  const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1668  ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1669  void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1670
1671  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1672  static bool classof(const ObjCCompatibleAliasDecl *D) { return true; }
1673  static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1674
1675};
1676
1677/// ObjCPropertyDecl - Represents one property declaration in an interface.
1678/// For example:
1679/// @property (assign, readwrite) int MyProperty;
1680///
1681class ObjCPropertyDecl : public NamedDecl {
1682  virtual void anchor();
1683public:
1684  enum PropertyAttributeKind {
1685    OBJC_PR_noattr    = 0x00,
1686    OBJC_PR_readonly  = 0x01,
1687    OBJC_PR_getter    = 0x02,
1688    OBJC_PR_assign    = 0x04,
1689    OBJC_PR_readwrite = 0x08,
1690    OBJC_PR_retain    = 0x10,
1691    OBJC_PR_copy      = 0x20,
1692    OBJC_PR_nonatomic = 0x40,
1693    OBJC_PR_setter    = 0x80,
1694    OBJC_PR_atomic    = 0x100,
1695    OBJC_PR_weak      = 0x200,
1696    OBJC_PR_strong    = 0x400,
1697    OBJC_PR_unsafe_unretained = 0x800
1698    // Adding a property should change NumPropertyAttrsBits
1699  };
1700
1701  enum {
1702    /// \brief Number of bits fitting all the property attributes.
1703    NumPropertyAttrsBits = 12
1704  };
1705
1706  enum SetterKind { Assign, Retain, Copy, Weak };
1707  enum PropertyControl { None, Required, Optional };
1708private:
1709  SourceLocation AtLoc;   // location of @property
1710  TypeSourceInfo *DeclType;
1711  unsigned PropertyAttributes : NumPropertyAttrsBits;
1712  unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
1713  // @required/@optional
1714  unsigned PropertyImplementation : 2;
1715
1716  Selector GetterName;    // getter name of NULL if no getter
1717  Selector SetterName;    // setter name of NULL if no setter
1718
1719  ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
1720  ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
1721  ObjCIvarDecl *PropertyIvarDecl;   // Synthesize ivar for this property
1722
1723  ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1724                   SourceLocation AtLocation, TypeSourceInfo *T)
1725    : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), DeclType(T),
1726      PropertyAttributes(OBJC_PR_noattr),
1727      PropertyAttributesAsWritten(OBJC_PR_noattr),
1728      PropertyImplementation(None),
1729      GetterName(Selector()),
1730      SetterName(Selector()),
1731      GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
1732public:
1733  static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
1734                                  SourceLocation L,
1735                                  IdentifierInfo *Id, SourceLocation AtLocation,
1736                                  TypeSourceInfo *T,
1737                                  PropertyControl propControl = None);
1738
1739  static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1740
1741  SourceLocation getAtLoc() const { return AtLoc; }
1742  void setAtLoc(SourceLocation L) { AtLoc = L; }
1743
1744  TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
1745  QualType getType() const { return DeclType->getType(); }
1746  void setType(TypeSourceInfo *T) { DeclType = T; }
1747
1748  PropertyAttributeKind getPropertyAttributes() const {
1749    return PropertyAttributeKind(PropertyAttributes);
1750  }
1751  void setPropertyAttributes(PropertyAttributeKind PRVal) {
1752    PropertyAttributes |= PRVal;
1753  }
1754
1755  PropertyAttributeKind getPropertyAttributesAsWritten() const {
1756    return PropertyAttributeKind(PropertyAttributesAsWritten);
1757  }
1758
1759  bool hasWrittenStorageAttribute() const {
1760    return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
1761        OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
1762        OBJC_PR_weak);
1763  }
1764
1765  void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
1766    PropertyAttributesAsWritten = PRVal;
1767  }
1768
1769 void makeitReadWriteAttribute(void) {
1770    PropertyAttributes &= ~OBJC_PR_readonly;
1771    PropertyAttributes |= OBJC_PR_readwrite;
1772 }
1773
1774  // Helper methods for accessing attributes.
1775
1776  /// isReadOnly - Return true iff the property has a setter.
1777  bool isReadOnly() const {
1778    return (PropertyAttributes & OBJC_PR_readonly);
1779  }
1780
1781  /// isAtomic - Return true if the property is atomic.
1782  bool isAtomic() const {
1783    return (PropertyAttributes & OBJC_PR_atomic);
1784  }
1785
1786  /// isRetaining - Return true if the property retains its value.
1787  bool isRetaining() const {
1788    return (PropertyAttributes &
1789            (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
1790  }
1791
1792  /// getSetterKind - Return the method used for doing assignment in
1793  /// the property setter. This is only valid if the property has been
1794  /// defined to have a setter.
1795  SetterKind getSetterKind() const {
1796    if (PropertyAttributes & OBJC_PR_strong)
1797      return getType()->isBlockPointerType() ? Copy : Retain;
1798    if (PropertyAttributes & OBJC_PR_retain)
1799      return Retain;
1800    if (PropertyAttributes & OBJC_PR_copy)
1801      return Copy;
1802    if (PropertyAttributes & OBJC_PR_weak)
1803      return Weak;
1804    return Assign;
1805  }
1806
1807  Selector getGetterName() const { return GetterName; }
1808  void setGetterName(Selector Sel) { GetterName = Sel; }
1809
1810  Selector getSetterName() const { return SetterName; }
1811  void setSetterName(Selector Sel) { SetterName = Sel; }
1812
1813  ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
1814  void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
1815
1816  ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
1817  void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
1818
1819  // Related to @optional/@required declared in @protocol
1820  void setPropertyImplementation(PropertyControl pc) {
1821    PropertyImplementation = pc;
1822  }
1823  PropertyControl getPropertyImplementation() const {
1824    return PropertyControl(PropertyImplementation);
1825  }
1826
1827  void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
1828    PropertyIvarDecl = Ivar;
1829  }
1830  ObjCIvarDecl *getPropertyIvarDecl() const {
1831    return PropertyIvarDecl;
1832  }
1833
1834  virtual SourceRange getSourceRange() const {
1835    return SourceRange(AtLoc, getLocation());
1836  }
1837
1838  /// Lookup a property by name in the specified DeclContext.
1839  static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
1840                                            IdentifierInfo *propertyID);
1841
1842  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1843  static bool classof(const ObjCPropertyDecl *D) { return true; }
1844  static bool classofKind(Kind K) { return K == ObjCProperty; }
1845};
1846
1847/// ObjCPropertyImplDecl - Represents implementation declaration of a property
1848/// in a class or category implementation block. For example:
1849/// @synthesize prop1 = ivar1;
1850///
1851class ObjCPropertyImplDecl : public Decl {
1852public:
1853  enum Kind {
1854    Synthesize,
1855    Dynamic
1856  };
1857private:
1858  SourceLocation AtLoc;   // location of @synthesize or @dynamic
1859
1860  /// \brief For @synthesize, the location of the ivar, if it was written in
1861  /// the source code.
1862  ///
1863  /// \code
1864  /// @synthesize int a = b
1865  /// \endcode
1866  SourceLocation IvarLoc;
1867
1868  /// Property declaration being implemented
1869  ObjCPropertyDecl *PropertyDecl;
1870
1871  /// Null for @dynamic. Required for @synthesize.
1872  ObjCIvarDecl *PropertyIvarDecl;
1873
1874  /// Null for @dynamic. Non-null if property must be copy-constructed in getter
1875  Expr *GetterCXXConstructor;
1876
1877  /// Null for @dynamic. Non-null if property has assignment operator to call
1878  /// in Setter synthesis.
1879  Expr *SetterCXXAssignment;
1880
1881  ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
1882                       ObjCPropertyDecl *property,
1883                       Kind PK,
1884                       ObjCIvarDecl *ivarDecl,
1885                       SourceLocation ivarLoc)
1886    : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
1887      IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
1888      GetterCXXConstructor(0), SetterCXXAssignment(0) {
1889    assert (PK == Dynamic || PropertyIvarDecl);
1890  }
1891
1892public:
1893  static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
1894                                      SourceLocation atLoc, SourceLocation L,
1895                                      ObjCPropertyDecl *property,
1896                                      Kind PK,
1897                                      ObjCIvarDecl *ivarDecl,
1898                                      SourceLocation ivarLoc);
1899
1900  static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1901
1902  virtual SourceRange getSourceRange() const;
1903
1904  SourceLocation getLocStart() const { return AtLoc; }
1905  void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
1906
1907  ObjCPropertyDecl *getPropertyDecl() const {
1908    return PropertyDecl;
1909  }
1910  void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
1911
1912  Kind getPropertyImplementation() const {
1913    return PropertyIvarDecl ? Synthesize : Dynamic;
1914  }
1915
1916  ObjCIvarDecl *getPropertyIvarDecl() const {
1917    return PropertyIvarDecl;
1918  }
1919  SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
1920
1921  void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
1922                           SourceLocation IvarLoc) {
1923    PropertyIvarDecl = Ivar;
1924    this->IvarLoc = IvarLoc;
1925  }
1926
1927  Expr *getGetterCXXConstructor() const {
1928    return GetterCXXConstructor;
1929  }
1930  void setGetterCXXConstructor(Expr *getterCXXConstructor) {
1931    GetterCXXConstructor = getterCXXConstructor;
1932  }
1933
1934  Expr *getSetterCXXAssignment() const {
1935    return SetterCXXAssignment;
1936  }
1937  void setSetterCXXAssignment(Expr *setterCXXAssignment) {
1938    SetterCXXAssignment = setterCXXAssignment;
1939  }
1940
1941  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1942  static bool classof(const ObjCPropertyImplDecl *D) { return true; }
1943  static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
1944
1945  friend class ASTDeclReader;
1946};
1947
1948}  // end namespace clang
1949#endif
1950