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