DeclObjC.h revision 05c272fed899b8d3142cf080e86a235fc6168862
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    /// \brief The location of the superclass, if any.
580    SourceLocation SuperClassLoc;
581
582    /// \brief The location of the last location in this declaration, before
583    /// the properties/methods. For example, this will be the '>', '}', or
584    /// identifier,
585    SourceLocation EndLoc;
586
587    DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(),
588                       ExternallyCompleted() { }
589  };
590
591  ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
592                    SourceLocation CLoc, bool isInternal);
593
594  void LoadExternalDefinition() const;
595
596  /// \brief Contains a pointer to the data associated with this class,
597  /// which will be NULL if this class has not yet been defined.
598  DefinitionData *Data;
599
600  DefinitionData &data() const {
601    assert(Data != 0 && "Declaration has no definition!");
602    return *Data;
603  }
604
605  /// \brief Allocate the definition data for this class.
606  void allocateDefinitionData();
607
608  typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
609  virtual ObjCInterfaceDecl *getNextRedeclaration() {
610    return RedeclLink.getNext();
611  }
612
613public:
614  static ObjCInterfaceDecl *Create(ASTContext &C, DeclContext *DC,
615                                   SourceLocation atLoc,
616                                   IdentifierInfo *Id,
617                                   SourceLocation ClassLoc = SourceLocation(),
618                                   bool isInternal = false);
619
620  virtual SourceRange getSourceRange() const {
621    if (isThisDeclarationADefinition())
622      return ObjCContainerDecl::getSourceRange();
623
624    return SourceRange(getAtStartLoc(), getLocation());
625  }
626
627  /// \brief Indicate that this Objective-C class is complete, but that
628  /// the external AST source will be responsible for filling in its contents
629  /// when a complete class is required.
630  void setExternallyCompleted();
631
632  const ObjCProtocolList &getReferencedProtocols() const {
633    if (data().ExternallyCompleted)
634      LoadExternalDefinition();
635
636    return data().ReferencedProtocols;
637  }
638
639  ObjCImplementationDecl *getImplementation() const;
640  void setImplementation(ObjCImplementationDecl *ImplD);
641
642  ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
643
644  // Get the local instance/class method declared in a category.
645  ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
646  ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
647  ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
648    return isInstance ? getInstanceMethod(Sel)
649                      : getClassMethod(Sel);
650  }
651
652  typedef ObjCProtocolList::iterator protocol_iterator;
653
654  protocol_iterator protocol_begin() const {
655    // FIXME: Should make sure no callers ever do this.
656    if (!hasDefinition())
657      return protocol_iterator();
658
659    if (data().ExternallyCompleted)
660      LoadExternalDefinition();
661
662    return data().ReferencedProtocols.begin();
663  }
664  protocol_iterator protocol_end() const {
665    // FIXME: Should make sure no callers ever do this.
666    if (!hasDefinition())
667      return protocol_iterator();
668
669    if (data().ExternallyCompleted)
670      LoadExternalDefinition();
671
672    return data().ReferencedProtocols.end();
673  }
674
675  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
676
677  protocol_loc_iterator protocol_loc_begin() const {
678    // FIXME: Should make sure no callers ever do this.
679    if (!hasDefinition())
680      return protocol_loc_iterator();
681
682    if (data().ExternallyCompleted)
683      LoadExternalDefinition();
684
685    return data().ReferencedProtocols.loc_begin();
686  }
687
688  protocol_loc_iterator protocol_loc_end() const {
689    // FIXME: Should make sure no callers ever do this.
690    if (!hasDefinition())
691      return protocol_loc_iterator();
692
693    if (data().ExternallyCompleted)
694      LoadExternalDefinition();
695
696    return data().ReferencedProtocols.loc_end();
697  }
698
699  typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
700
701  all_protocol_iterator all_referenced_protocol_begin() const {
702    // FIXME: Should make sure no callers ever do this.
703    if (!hasDefinition())
704      return all_protocol_iterator();
705
706    if (data().ExternallyCompleted)
707      LoadExternalDefinition();
708
709    return data().AllReferencedProtocols.empty()
710             ? protocol_begin()
711             : data().AllReferencedProtocols.begin();
712  }
713  all_protocol_iterator all_referenced_protocol_end() const {
714    // FIXME: Should make sure no callers ever do this.
715    if (!hasDefinition())
716      return all_protocol_iterator();
717
718    if (data().ExternallyCompleted)
719      LoadExternalDefinition();
720
721    return data().AllReferencedProtocols.empty()
722             ? protocol_end()
723             : data().AllReferencedProtocols.end();
724  }
725
726  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
727
728  ivar_iterator ivar_begin() const {
729    if (const ObjCInterfaceDecl *Def = getDefinition())
730      return ivar_iterator(Def->decls_begin());
731
732    // FIXME: Should make sure no callers ever do this.
733    return ivar_iterator();
734  }
735  ivar_iterator ivar_end() const {
736    if (const ObjCInterfaceDecl *Def = getDefinition())
737      return ivar_iterator(Def->decls_end());
738
739    // FIXME: Should make sure no callers ever do this.
740    return ivar_iterator();
741  }
742
743  unsigned ivar_size() const {
744    return std::distance(ivar_begin(), ivar_end());
745  }
746
747  bool ivar_empty() const { return ivar_begin() == ivar_end(); }
748
749  ObjCIvarDecl *all_declared_ivar_begin();
750  const ObjCIvarDecl *all_declared_ivar_begin() const {
751    // Even though this modifies IvarList, it's conceptually const:
752    // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
753    return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
754  }
755  void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
756
757  /// setProtocolList - Set the list of protocols that this interface
758  /// implements.
759  void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
760                       const SourceLocation *Locs, ASTContext &C) {
761    data().ReferencedProtocols.set(List, Num, Locs, C);
762  }
763
764  /// mergeClassExtensionProtocolList - Merge class extension's protocol list
765  /// into the protocol list for this class.
766  void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
767                                       unsigned Num,
768                                       ASTContext &C);
769
770  /// \brief Determine whether this particular declaration of this class is
771  /// actually also a definition.
772  bool isThisDeclarationADefinition() const {
773    return Data && Data->Definition == this;
774  }
775
776  /// \brief Determine whether this class has been defined.
777  bool hasDefinition() const { return Data; }
778
779  /// \brief Retrieve the definition of this class, or NULL if this class
780  /// has been forward-declared (with @class) but not yet defined (with
781  /// @interface).
782  ObjCInterfaceDecl *getDefinition() {
783    return hasDefinition()? Data->Definition : 0;
784  }
785
786  /// \brief Retrieve the definition of this class, or NULL if this class
787  /// has been forward-declared (with @class) but not yet defined (with
788  /// @interface).
789  const ObjCInterfaceDecl *getDefinition() const {
790    return hasDefinition()? Data->Definition : 0;
791  }
792
793  /// \brief Starts the definition of this Objective-C class, taking it from
794  /// a forward declaration (@class) to a definition (@interface).
795  void startDefinition();
796
797  ObjCInterfaceDecl *getSuperClass() const {
798    // FIXME: Should make sure no callers ever do this.
799    if (!hasDefinition())
800      return 0;
801
802    if (data().ExternallyCompleted)
803      LoadExternalDefinition();
804
805    return data().SuperClass;
806  }
807
808  void setSuperClass(ObjCInterfaceDecl * superCls) {
809    data().SuperClass = superCls;
810  }
811
812  ObjCCategoryDecl* getCategoryList() 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().CategoryList;
821  }
822
823  void setCategoryList(ObjCCategoryDecl *category) {
824    data().CategoryList = category;
825  }
826
827  ObjCCategoryDecl* getFirstClassExtension() const;
828
829  ObjCPropertyDecl
830    *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
831
832  /// isSuperClassOf - Return true if this class is the specified class or is a
833  /// super class of the specified interface class.
834  bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
835    // If RHS is derived from LHS it is OK; else it is not OK.
836    while (I != NULL) {
837      if (declaresSameEntity(this, I))
838        return true;
839
840      I = I->getSuperClass();
841    }
842    return false;
843  }
844
845  /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
846  /// to be incompatible with __weak references. Returns true if it is.
847  bool isArcWeakrefUnavailable() const {
848    const ObjCInterfaceDecl *Class = this;
849    while (Class) {
850      if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
851        return true;
852      Class = Class->getSuperClass();
853   }
854   return false;
855  }
856
857  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
858                                       ObjCInterfaceDecl *&ClassDeclared);
859  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
860    ObjCInterfaceDecl *ClassDeclared;
861    return lookupInstanceVariable(IVarName, ClassDeclared);
862  }
863
864  // Lookup a method. First, we search locally. If a method isn't
865  // found, we search referenced protocols and class categories.
866  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
867  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
868    return lookupMethod(Sel, true/*isInstance*/);
869  }
870  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
871    return lookupMethod(Sel, false/*isInstance*/);
872  }
873  ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
874
875  // Lookup a method in the classes implementation hierarchy.
876  ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true);
877
878  SourceLocation getEndOfDefinitionLoc() const {
879    if (!hasDefinition())
880      return getLocation();
881
882    return data().EndLoc;
883  }
884
885  void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
886
887  void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
888  SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
889
890  /// isImplicitInterfaceDecl - check that this is an implicitly declared
891  /// ObjCInterfaceDecl node. This is for legacy objective-c @implementation
892  /// declaration without an @interface declaration.
893  bool isImplicitInterfaceDecl() const { return isImplicit(); }
894  void setImplicitInterfaceDecl(bool val) { setImplicit(val); }
895
896  /// ClassImplementsProtocol - Checks that 'lProto' protocol
897  /// has been implemented in IDecl class, its super class or categories (if
898  /// lookupCategory is true).
899  bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
900                               bool lookupCategory,
901                               bool RHSIsQualifiedID = false);
902
903  typedef redeclarable_base::redecl_iterator redecl_iterator;
904  redecl_iterator redecls_begin() const {
905    return redeclarable_base::redecls_begin();
906  }
907  redecl_iterator redecls_end() const {
908    return redeclarable_base::redecls_end();
909  }
910
911  /// Retrieves the canonical declaration of this Objective-C class.
912  ObjCInterfaceDecl *getCanonicalDecl() {
913    return getFirstDeclaration();
914  }
915  const ObjCInterfaceDecl *getCanonicalDecl() const {
916    return getFirstDeclaration();
917  }
918
919  void setPreviousDeclaration(ObjCInterfaceDecl *PrevDecl);
920
921  // Low-level accessor
922  const Type *getTypeForDecl() const { return TypeForDecl; }
923  void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
924
925  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
926  static bool classof(const ObjCInterfaceDecl *D) { return true; }
927  static bool classofKind(Kind K) { return K == ObjCInterface; }
928
929  friend class ASTDeclReader;
930  friend class ASTDeclWriter;
931};
932
933/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
934/// instance variables are identical to C. The only exception is Objective-C
935/// supports C++ style access control. For example:
936///
937///   @interface IvarExample : NSObject
938///   {
939///     id defaultToProtected;
940///   @public:
941///     id canBePublic; // same as C++.
942///   @protected:
943///     id canBeProtected; // same as C++.
944///   @package:
945///     id canBePackage; // framework visibility (not available in C++).
946///   }
947///
948class ObjCIvarDecl : public FieldDecl {
949public:
950  enum AccessControl {
951    None, Private, Protected, Public, Package
952  };
953
954private:
955  ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
956               SourceLocation IdLoc, IdentifierInfo *Id,
957               QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
958               bool synthesized)
959    : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
960                /*Mutable=*/false, /*HasInit=*/false),
961      NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {}
962
963public:
964  static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
965                              SourceLocation StartLoc, SourceLocation IdLoc,
966                              IdentifierInfo *Id, QualType T,
967                              TypeSourceInfo *TInfo,
968                              AccessControl ac, Expr *BW = NULL,
969                              bool synthesized=false);
970
971  /// \brief Return the class interface that this ivar is logically contained
972  /// in; this is either the interface where the ivar was declared, or the
973  /// interface the ivar is conceptually a part of in the case of synthesized
974  /// ivars.
975  const ObjCInterfaceDecl *getContainingInterface() const;
976
977  ObjCIvarDecl *getNextIvar() { return NextIvar; }
978  const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
979  void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
980
981  void setAccessControl(AccessControl ac) { DeclAccess = ac; }
982
983  AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
984
985  AccessControl getCanonicalAccessControl() const {
986    return DeclAccess == None ? Protected : AccessControl(DeclAccess);
987  }
988
989  void setSynthesize(bool synth) { Synthesized = synth; }
990  bool getSynthesize() const { return Synthesized; }
991
992  // Implement isa/cast/dyncast/etc.
993  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
994  static bool classof(const ObjCIvarDecl *D) { return true; }
995  static bool classofKind(Kind K) { return K == ObjCIvar; }
996private:
997  /// NextIvar - Next Ivar in the list of ivars declared in class; class's
998  /// extensions and class's implementation
999  ObjCIvarDecl *NextIvar;
1000
1001  // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1002  unsigned DeclAccess : 3;
1003  unsigned Synthesized : 1;
1004};
1005
1006
1007/// ObjCAtDefsFieldDecl - Represents a field declaration created by an
1008///  @defs(...).
1009class ObjCAtDefsFieldDecl : public FieldDecl {
1010private:
1011  ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1012                      SourceLocation IdLoc, IdentifierInfo *Id,
1013                      QualType T, Expr *BW)
1014    : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1015                /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
1016                BW, /*Mutable=*/false, /*HasInit=*/false) {}
1017
1018public:
1019  static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1020                                     SourceLocation StartLoc,
1021                                     SourceLocation IdLoc, IdentifierInfo *Id,
1022                                     QualType T, Expr *BW);
1023
1024  // Implement isa/cast/dyncast/etc.
1025  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1026  static bool classof(const ObjCAtDefsFieldDecl *D) { return true; }
1027  static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1028};
1029
1030/// ObjCProtocolDecl - Represents a protocol declaration. ObjC protocols
1031/// declare a pure abstract type (i.e no instance variables are permitted).
1032/// Protocols originally drew inspiration from C++ pure virtual functions (a C++
1033/// feature with nice semantics and lousy syntax:-). Here is an example:
1034///
1035/// @protocol NSDraggingInfo <refproto1, refproto2>
1036/// - (NSWindow *)draggingDestinationWindow;
1037/// - (NSImage *)draggedImage;
1038/// @end
1039///
1040/// This says that NSDraggingInfo requires two methods and requires everything
1041/// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1042/// well.
1043///
1044/// @interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo>
1045/// @end
1046///
1047/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1048/// protocols are in distinct namespaces. For example, Cocoa defines both
1049/// an NSObject protocol and class (which isn't allowed in Java). As a result,
1050/// protocols are referenced using angle brackets as follows:
1051///
1052/// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1053///
1054class ObjCProtocolDecl : public ObjCContainerDecl {
1055  /// Referenced protocols
1056  ObjCProtocolList ReferencedProtocols;
1057
1058  bool InitiallyForwardDecl : 1;
1059  bool isForwardProtoDecl : 1; // declared with @protocol.
1060
1061  SourceLocation EndLoc; // marks the '>' or identifier.
1062
1063  ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1064                   SourceLocation nameLoc, SourceLocation atStartLoc,
1065                   bool isForwardDecl)
1066    : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1067      InitiallyForwardDecl(isForwardDecl),
1068      isForwardProtoDecl(isForwardDecl) {
1069  }
1070
1071public:
1072  static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1073                                  IdentifierInfo *Id,
1074                                  SourceLocation nameLoc,
1075                                  SourceLocation atStartLoc,
1076                                  bool isForwardDecl);
1077
1078  const ObjCProtocolList &getReferencedProtocols() const {
1079    return ReferencedProtocols;
1080  }
1081  typedef ObjCProtocolList::iterator protocol_iterator;
1082  protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1083  protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1084  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1085  protocol_loc_iterator protocol_loc_begin() const {
1086    return ReferencedProtocols.loc_begin();
1087  }
1088  protocol_loc_iterator protocol_loc_end() const {
1089    return ReferencedProtocols.loc_end();
1090  }
1091  unsigned protocol_size() const { return ReferencedProtocols.size(); }
1092
1093  /// setProtocolList - Set the list of protocols that this interface
1094  /// implements.
1095  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1096                       const SourceLocation *Locs, ASTContext &C) {
1097    ReferencedProtocols.set(List, Num, Locs, C);
1098  }
1099
1100  ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1101
1102  // Lookup a method. First, we search locally. If a method isn't
1103  // found, we search referenced protocols and class categories.
1104  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1105  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1106    return lookupMethod(Sel, true/*isInstance*/);
1107  }
1108  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1109    return lookupMethod(Sel, false/*isInstance*/);
1110  }
1111
1112  /// \brief True if it was initially a forward reference.
1113  /// Differs with \see isForwardDecl in that \see isForwardDecl will change to
1114  /// false when we see the definition, but this will remain true.
1115  bool isInitiallyForwardDecl() const { return InitiallyForwardDecl; }
1116
1117  bool isForwardDecl() const { return isForwardProtoDecl; }
1118
1119  void completedForwardDecl();
1120
1121  // Location information, modeled after the Stmt API.
1122  SourceLocation getLocStart() const { return getAtStartLoc(); } // '@'protocol
1123  SourceLocation getLocEnd() const { return EndLoc; }
1124  void setLocEnd(SourceLocation LE) { EndLoc = LE; }
1125
1126  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1127  static bool classof(const ObjCProtocolDecl *D) { return true; }
1128  static bool classofKind(Kind K) { return K == ObjCProtocol; }
1129
1130  friend class ASTDeclReader;
1131  friend class ASTDeclWriter;
1132};
1133
1134/// ObjCClassDecl - Specifies a list of forward class declarations. For example:
1135///
1136/// @class NSCursor, NSImage, NSPasteboard, NSWindow;
1137///
1138class ObjCClassDecl : public Decl {
1139  ObjCInterfaceDecl *Interface;
1140  SourceLocation InterfaceLoc;
1141
1142  ObjCClassDecl(DeclContext *DC, SourceLocation L,
1143                ObjCInterfaceDecl *Interface, SourceLocation InterfaceLoc);
1144
1145  friend class ASTDeclReader;
1146  friend class ASTDeclWriter;
1147
1148public:
1149  static ObjCClassDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1150                               ObjCInterfaceDecl *Interface = 0,
1151                               SourceLocation InterfaceLoc = SourceLocation());
1152
1153  ObjCInterfaceDecl *getForwardInterfaceDecl() const {
1154    return Interface;
1155  }
1156
1157  /// \brief Retrieve the location of the class name.
1158  SourceLocation getNameLoc() const { return InterfaceLoc; }
1159
1160  virtual SourceRange getSourceRange() const;
1161
1162  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1163  static bool classof(const ObjCClassDecl *D) { return true; }
1164  static bool classofKind(Kind K) { return K == ObjCClass; }
1165};
1166
1167/// ObjCForwardProtocolDecl - Specifies a list of forward protocol declarations.
1168/// For example:
1169///
1170/// @protocol NSTextInput, NSChangeSpelling, NSDraggingInfo;
1171///
1172class ObjCForwardProtocolDecl : public Decl {
1173  ObjCProtocolList ReferencedProtocols;
1174
1175  ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
1176                          ObjCProtocolDecl *const *Elts, unsigned nElts,
1177                          const SourceLocation *Locs, ASTContext &C);
1178
1179public:
1180  static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1181                                         SourceLocation L,
1182                                         ObjCProtocolDecl *const *Elts,
1183                                         unsigned Num,
1184                                         const SourceLocation *Locs);
1185
1186  static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1187                                         SourceLocation L) {
1188    return Create(C, DC, L, 0, 0, 0);
1189  }
1190
1191  typedef ObjCProtocolList::iterator protocol_iterator;
1192  protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1193  protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1194  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1195  protocol_loc_iterator protocol_loc_begin() const {
1196    return ReferencedProtocols.loc_begin();
1197  }
1198  protocol_loc_iterator protocol_loc_end() const {
1199    return ReferencedProtocols.loc_end();
1200  }
1201
1202  unsigned protocol_size() const { return ReferencedProtocols.size(); }
1203
1204  /// setProtocolList - Set the list of forward protocols.
1205  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1206                       const SourceLocation *Locs, ASTContext &C) {
1207    ReferencedProtocols.set(List, Num, Locs, C);
1208  }
1209  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1210  static bool classof(const ObjCForwardProtocolDecl *D) { return true; }
1211  static bool classofKind(Kind K) { return K == ObjCForwardProtocol; }
1212};
1213
1214/// ObjCCategoryDecl - Represents a category declaration. A category allows
1215/// you to add methods to an existing class (without subclassing or modifying
1216/// the original class interface or implementation:-). Categories don't allow
1217/// you to add instance data. The following example adds "myMethod" to all
1218/// NSView's within a process:
1219///
1220/// @interface NSView (MyViewMethods)
1221/// - myMethod;
1222/// @end
1223///
1224/// Categories also allow you to split the implementation of a class across
1225/// several files (a feature more naturally supported in C++).
1226///
1227/// Categories were originally inspired by dynamic languages such as Common
1228/// Lisp and Smalltalk.  More traditional class-based languages (C++, Java)
1229/// don't support this level of dynamism, which is both powerful and dangerous.
1230///
1231class ObjCCategoryDecl : public ObjCContainerDecl {
1232  /// Interface belonging to this category
1233  ObjCInterfaceDecl *ClassInterface;
1234
1235  /// referenced protocols in this category.
1236  ObjCProtocolList ReferencedProtocols;
1237
1238  /// Next category belonging to this class.
1239  /// FIXME: this should not be a singly-linked list.  Move storage elsewhere.
1240  ObjCCategoryDecl *NextClassCategory;
1241
1242  /// true of class extension has at least one bitfield ivar.
1243  bool HasSynthBitfield : 1;
1244
1245  /// \brief The location of the category name in this declaration.
1246  SourceLocation CategoryNameLoc;
1247
1248  ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1249                   SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1250                   IdentifierInfo *Id, ObjCInterfaceDecl *IDecl)
1251    : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1252      ClassInterface(IDecl), NextClassCategory(0), HasSynthBitfield(false),
1253      CategoryNameLoc(CategoryNameLoc) {
1254  }
1255public:
1256
1257  static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1258                                  SourceLocation AtLoc,
1259                                  SourceLocation ClassNameLoc,
1260                                  SourceLocation CategoryNameLoc,
1261                                  IdentifierInfo *Id,
1262                                  ObjCInterfaceDecl *IDecl);
1263  static ObjCCategoryDecl *Create(ASTContext &C, EmptyShell Empty);
1264
1265  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1266  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1267
1268  ObjCCategoryImplDecl *getImplementation() const;
1269  void setImplementation(ObjCCategoryImplDecl *ImplD);
1270
1271  /// setProtocolList - Set the list of protocols that this interface
1272  /// implements.
1273  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1274                       const SourceLocation *Locs, ASTContext &C) {
1275    ReferencedProtocols.set(List, Num, Locs, C);
1276  }
1277
1278  const ObjCProtocolList &getReferencedProtocols() const {
1279    return ReferencedProtocols;
1280  }
1281
1282  typedef ObjCProtocolList::iterator protocol_iterator;
1283  protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1284  protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1285  unsigned protocol_size() const { return ReferencedProtocols.size(); }
1286  typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1287  protocol_loc_iterator protocol_loc_begin() const {
1288    return ReferencedProtocols.loc_begin();
1289  }
1290  protocol_loc_iterator protocol_loc_end() const {
1291    return ReferencedProtocols.loc_end();
1292  }
1293
1294  ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1295
1296  bool IsClassExtension() const { return getIdentifier() == 0; }
1297  const ObjCCategoryDecl *getNextClassExtension() const;
1298
1299  bool hasSynthBitfield() const { return HasSynthBitfield; }
1300  void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1301
1302  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1303  ivar_iterator ivar_begin() const {
1304    return ivar_iterator(decls_begin());
1305  }
1306  ivar_iterator ivar_end() const {
1307    return ivar_iterator(decls_end());
1308  }
1309  unsigned ivar_size() const {
1310    return std::distance(ivar_begin(), ivar_end());
1311  }
1312  bool ivar_empty() const {
1313    return ivar_begin() == ivar_end();
1314  }
1315
1316  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1317  void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1318
1319  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1320  static bool classof(const ObjCCategoryDecl *D) { return true; }
1321  static bool classofKind(Kind K) { return K == ObjCCategory; }
1322
1323  friend class ASTDeclReader;
1324  friend class ASTDeclWriter;
1325};
1326
1327class ObjCImplDecl : public ObjCContainerDecl {
1328  /// Class interface for this class/category implementation
1329  ObjCInterfaceDecl *ClassInterface;
1330
1331protected:
1332  ObjCImplDecl(Kind DK, DeclContext *DC,
1333               ObjCInterfaceDecl *classInterface,
1334               SourceLocation nameLoc, SourceLocation atStartLoc)
1335    : ObjCContainerDecl(DK, DC,
1336                        classInterface? classInterface->getIdentifier() : 0,
1337                        nameLoc, atStartLoc),
1338      ClassInterface(classInterface) {}
1339
1340public:
1341  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1342  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1343  void setClassInterface(ObjCInterfaceDecl *IFace);
1344
1345  void addInstanceMethod(ObjCMethodDecl *method) {
1346    // FIXME: Context should be set correctly before we get here.
1347    method->setLexicalDeclContext(this);
1348    addDecl(method);
1349  }
1350  void addClassMethod(ObjCMethodDecl *method) {
1351    // FIXME: Context should be set correctly before we get here.
1352    method->setLexicalDeclContext(this);
1353    addDecl(method);
1354  }
1355
1356  void addPropertyImplementation(ObjCPropertyImplDecl *property);
1357
1358  ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1359  ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1360
1361  // Iterator access to properties.
1362  typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1363  propimpl_iterator propimpl_begin() const {
1364    return propimpl_iterator(decls_begin());
1365  }
1366  propimpl_iterator propimpl_end() const {
1367    return propimpl_iterator(decls_end());
1368  }
1369
1370  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1371  static bool classof(const ObjCImplDecl *D) { return true; }
1372  static bool classofKind(Kind K) {
1373    return K >= firstObjCImpl && K <= lastObjCImpl;
1374  }
1375};
1376
1377/// ObjCCategoryImplDecl - An object of this class encapsulates a category
1378/// @implementation declaration. If a category class has declaration of a
1379/// property, its implementation must be specified in the category's
1380/// @implementation declaration. Example:
1381/// @interface I @end
1382/// @interface I(CATEGORY)
1383///    @property int p1, d1;
1384/// @end
1385/// @implementation I(CATEGORY)
1386///  @dynamic p1,d1;
1387/// @end
1388///
1389/// ObjCCategoryImplDecl
1390class ObjCCategoryImplDecl : public ObjCImplDecl {
1391  // Category name
1392  IdentifierInfo *Id;
1393
1394  // Category name location
1395  SourceLocation CategoryNameLoc;
1396
1397  ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1398                       ObjCInterfaceDecl *classInterface,
1399                       SourceLocation nameLoc, SourceLocation atStartLoc,
1400                       SourceLocation CategoryNameLoc)
1401    : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1402      Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1403public:
1404  static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1405                                      IdentifierInfo *Id,
1406                                      ObjCInterfaceDecl *classInterface,
1407                                      SourceLocation nameLoc,
1408                                      SourceLocation atStartLoc,
1409                                      SourceLocation CategoryNameLoc);
1410
1411  /// getIdentifier - Get the identifier that names the category
1412  /// interface associated with this implementation.
1413  /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1414  /// to mean something different. For example:
1415  /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1416  /// returns the class interface name, whereas
1417  /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1418  /// returns the category name.
1419  IdentifierInfo *getIdentifier() const {
1420    return Id;
1421  }
1422  void setIdentifier(IdentifierInfo *II) { Id = II; }
1423
1424  ObjCCategoryDecl *getCategoryDecl() const;
1425
1426  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1427
1428  /// getName - Get the name of identifier for the class interface associated
1429  /// with this implementation as a StringRef.
1430  //
1431  // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1432  // something different.
1433  StringRef getName() const {
1434    return Id ? Id->getNameStart() : "";
1435  }
1436
1437  /// getNameAsCString - Get the name of identifier for the class
1438  /// interface associated with this implementation as a C string
1439  /// (const char*).
1440  //
1441  // FIXME: Deprecated, move clients to getName().
1442  const char *getNameAsCString() const {
1443    return Id ? Id->getNameStart() : "";
1444  }
1445
1446  /// @brief Get the name of the class associated with this interface.
1447  //
1448  // FIXME: Deprecated, move clients to getName().
1449  std::string getNameAsString() const {
1450    return getName();
1451  }
1452
1453  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1454  static bool classof(const ObjCCategoryImplDecl *D) { return true; }
1455  static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1456
1457  friend class ASTDeclReader;
1458  friend class ASTDeclWriter;
1459};
1460
1461raw_ostream &operator<<(raw_ostream &OS,
1462                              const ObjCCategoryImplDecl *CID);
1463
1464/// ObjCImplementationDecl - Represents a class definition - this is where
1465/// method definitions are specified. For example:
1466///
1467/// @code
1468/// @implementation MyClass
1469/// - (void)myMethod { /* do something */ }
1470/// @end
1471/// @endcode
1472///
1473/// Typically, instance variables are specified in the class interface,
1474/// *not* in the implementation. Nevertheless (for legacy reasons), we
1475/// allow instance variables to be specified in the implementation.  When
1476/// specified, they need to be *identical* to the interface.
1477///
1478class ObjCImplementationDecl : public ObjCImplDecl {
1479  /// Implementation Class's super class.
1480  ObjCInterfaceDecl *SuperClass;
1481  /// Support for ivar initialization.
1482  /// IvarInitializers - The arguments used to initialize the ivars
1483  CXXCtorInitializer **IvarInitializers;
1484  unsigned NumIvarInitializers;
1485
1486  /// true if class has a .cxx_[construct,destruct] method.
1487  bool HasCXXStructors : 1;
1488
1489  /// true of class extension has at least one bitfield ivar.
1490  bool HasSynthBitfield : 1;
1491
1492  ObjCImplementationDecl(DeclContext *DC,
1493                         ObjCInterfaceDecl *classInterface,
1494                         ObjCInterfaceDecl *superDecl,
1495                         SourceLocation nameLoc, SourceLocation atStartLoc)
1496    : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
1497       SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0),
1498       HasCXXStructors(false), HasSynthBitfield(false) {}
1499public:
1500  static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1501                                        ObjCInterfaceDecl *classInterface,
1502                                        ObjCInterfaceDecl *superDecl,
1503                                        SourceLocation nameLoc,
1504                                        SourceLocation atStartLoc);
1505
1506  /// init_iterator - Iterates through the ivar initializer list.
1507  typedef CXXCtorInitializer **init_iterator;
1508
1509  /// init_const_iterator - Iterates through the ivar initializer list.
1510  typedef CXXCtorInitializer * const * init_const_iterator;
1511
1512  /// init_begin() - Retrieve an iterator to the first initializer.
1513  init_iterator       init_begin()       { return IvarInitializers; }
1514  /// begin() - Retrieve an iterator to the first initializer.
1515  init_const_iterator init_begin() const { return IvarInitializers; }
1516
1517  /// init_end() - Retrieve an iterator past the last initializer.
1518  init_iterator       init_end()       {
1519    return IvarInitializers + NumIvarInitializers;
1520  }
1521  /// end() - Retrieve an iterator past the last initializer.
1522  init_const_iterator init_end() const {
1523    return IvarInitializers + NumIvarInitializers;
1524  }
1525  /// getNumArgs - Number of ivars which must be initialized.
1526  unsigned getNumIvarInitializers() const {
1527    return NumIvarInitializers;
1528  }
1529
1530  void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1531    NumIvarInitializers = numNumIvarInitializers;
1532  }
1533
1534  void setIvarInitializers(ASTContext &C,
1535                           CXXCtorInitializer ** initializers,
1536                           unsigned numInitializers);
1537
1538  bool hasCXXStructors() const { return HasCXXStructors; }
1539  void setHasCXXStructors(bool val) { HasCXXStructors = val; }
1540
1541  bool hasSynthBitfield() const { return HasSynthBitfield; }
1542  void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1543
1544  /// getIdentifier - Get the identifier that names the class
1545  /// interface associated with this implementation.
1546  IdentifierInfo *getIdentifier() const {
1547    return getClassInterface()->getIdentifier();
1548  }
1549
1550  /// getName - Get the name of identifier for the class interface associated
1551  /// with this implementation as a StringRef.
1552  //
1553  // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1554  // something different.
1555  StringRef getName() const {
1556    assert(getIdentifier() && "Name is not a simple identifier");
1557    return getIdentifier()->getName();
1558  }
1559
1560  /// getNameAsCString - Get the name of identifier for the class
1561  /// interface associated with this implementation as a C string
1562  /// (const char*).
1563  //
1564  // FIXME: Move to StringRef API.
1565  const char *getNameAsCString() const {
1566    return getName().data();
1567  }
1568
1569  /// @brief Get the name of the class associated with this interface.
1570  //
1571  // FIXME: Move to StringRef API.
1572  std::string getNameAsString() const {
1573    return getName();
1574  }
1575
1576  const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1577  ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1578
1579  void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1580
1581  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1582  ivar_iterator ivar_begin() const {
1583    return ivar_iterator(decls_begin());
1584  }
1585  ivar_iterator ivar_end() const {
1586    return ivar_iterator(decls_end());
1587  }
1588  unsigned ivar_size() const {
1589    return std::distance(ivar_begin(), ivar_end());
1590  }
1591  bool ivar_empty() const {
1592    return ivar_begin() == ivar_end();
1593  }
1594
1595  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1596  static bool classof(const ObjCImplementationDecl *D) { return true; }
1597  static bool classofKind(Kind K) { return K == ObjCImplementation; }
1598
1599  friend class ASTDeclReader;
1600  friend class ASTDeclWriter;
1601};
1602
1603raw_ostream &operator<<(raw_ostream &OS,
1604                              const ObjCImplementationDecl *ID);
1605
1606/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1607/// declared as @compatibility_alias alias class.
1608class ObjCCompatibleAliasDecl : public NamedDecl {
1609  /// Class that this is an alias of.
1610  ObjCInterfaceDecl *AliasedClass;
1611
1612  ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1613                          ObjCInterfaceDecl* aliasedClass)
1614    : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1615public:
1616  static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1617                                         SourceLocation L, IdentifierInfo *Id,
1618                                         ObjCInterfaceDecl* aliasedClass);
1619
1620  const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1621  ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1622  void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1623
1624  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1625  static bool classof(const ObjCCompatibleAliasDecl *D) { return true; }
1626  static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1627
1628};
1629
1630/// ObjCPropertyDecl - Represents one property declaration in an interface.
1631/// For example:
1632/// @property (assign, readwrite) int MyProperty;
1633///
1634class ObjCPropertyDecl : public NamedDecl {
1635public:
1636  enum PropertyAttributeKind {
1637    OBJC_PR_noattr    = 0x00,
1638    OBJC_PR_readonly  = 0x01,
1639    OBJC_PR_getter    = 0x02,
1640    OBJC_PR_assign    = 0x04,
1641    OBJC_PR_readwrite = 0x08,
1642    OBJC_PR_retain    = 0x10,
1643    OBJC_PR_copy      = 0x20,
1644    OBJC_PR_nonatomic = 0x40,
1645    OBJC_PR_setter    = 0x80,
1646    OBJC_PR_atomic    = 0x100,
1647    OBJC_PR_weak      = 0x200,
1648    OBJC_PR_strong    = 0x400,
1649    OBJC_PR_unsafe_unretained = 0x800
1650    // Adding a property should change NumPropertyAttrsBits
1651  };
1652
1653  enum {
1654    /// \brief Number of bits fitting all the property attributes.
1655    NumPropertyAttrsBits = 12
1656  };
1657
1658  enum SetterKind { Assign, Retain, Copy, Weak };
1659  enum PropertyControl { None, Required, Optional };
1660private:
1661  SourceLocation AtLoc;   // location of @property
1662  TypeSourceInfo *DeclType;
1663  unsigned PropertyAttributes : NumPropertyAttrsBits;
1664  unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
1665  // @required/@optional
1666  unsigned PropertyImplementation : 2;
1667
1668  Selector GetterName;    // getter name of NULL if no getter
1669  Selector SetterName;    // setter name of NULL if no setter
1670
1671  ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
1672  ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
1673  ObjCIvarDecl *PropertyIvarDecl;   // Synthesize ivar for this property
1674
1675  ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1676                   SourceLocation AtLocation, TypeSourceInfo *T)
1677    : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), DeclType(T),
1678      PropertyAttributes(OBJC_PR_noattr),
1679      PropertyAttributesAsWritten(OBJC_PR_noattr),
1680      PropertyImplementation(None),
1681      GetterName(Selector()),
1682      SetterName(Selector()),
1683      GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
1684public:
1685  static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
1686                                  SourceLocation L,
1687                                  IdentifierInfo *Id, SourceLocation AtLocation,
1688                                  TypeSourceInfo *T,
1689                                  PropertyControl propControl = None);
1690  SourceLocation getAtLoc() const { return AtLoc; }
1691  void setAtLoc(SourceLocation L) { AtLoc = L; }
1692
1693  TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
1694  QualType getType() const { return DeclType->getType(); }
1695  void setType(TypeSourceInfo *T) { DeclType = T; }
1696
1697  PropertyAttributeKind getPropertyAttributes() const {
1698    return PropertyAttributeKind(PropertyAttributes);
1699  }
1700  void setPropertyAttributes(PropertyAttributeKind PRVal) {
1701    PropertyAttributes |= PRVal;
1702  }
1703
1704  PropertyAttributeKind getPropertyAttributesAsWritten() const {
1705    return PropertyAttributeKind(PropertyAttributesAsWritten);
1706  }
1707
1708  bool hasWrittenStorageAttribute() const {
1709    return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
1710        OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
1711        OBJC_PR_weak);
1712  }
1713
1714  void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
1715    PropertyAttributesAsWritten = PRVal;
1716  }
1717
1718 void makeitReadWriteAttribute(void) {
1719    PropertyAttributes &= ~OBJC_PR_readonly;
1720    PropertyAttributes |= OBJC_PR_readwrite;
1721 }
1722
1723  // Helper methods for accessing attributes.
1724
1725  /// isReadOnly - Return true iff the property has a setter.
1726  bool isReadOnly() const {
1727    return (PropertyAttributes & OBJC_PR_readonly);
1728  }
1729
1730  /// isAtomic - Return true if the property is atomic.
1731  bool isAtomic() const {
1732    return (PropertyAttributes & OBJC_PR_atomic);
1733  }
1734
1735  /// isRetaining - Return true if the property retains its value.
1736  bool isRetaining() const {
1737    return (PropertyAttributes &
1738            (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
1739  }
1740
1741  /// getSetterKind - Return the method used for doing assignment in
1742  /// the property setter. This is only valid if the property has been
1743  /// defined to have a setter.
1744  SetterKind getSetterKind() const {
1745    if (PropertyAttributes & OBJC_PR_strong)
1746      return getType()->isBlockPointerType() ? Copy : Retain;
1747    if (PropertyAttributes & OBJC_PR_retain)
1748      return Retain;
1749    if (PropertyAttributes & OBJC_PR_copy)
1750      return Copy;
1751    if (PropertyAttributes & OBJC_PR_weak)
1752      return Weak;
1753    return Assign;
1754  }
1755
1756  Selector getGetterName() const { return GetterName; }
1757  void setGetterName(Selector Sel) { GetterName = Sel; }
1758
1759  Selector getSetterName() const { return SetterName; }
1760  void setSetterName(Selector Sel) { SetterName = Sel; }
1761
1762  ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
1763  void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
1764
1765  ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
1766  void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
1767
1768  // Related to @optional/@required declared in @protocol
1769  void setPropertyImplementation(PropertyControl pc) {
1770    PropertyImplementation = pc;
1771  }
1772  PropertyControl getPropertyImplementation() const {
1773    return PropertyControl(PropertyImplementation);
1774  }
1775
1776  void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
1777    PropertyIvarDecl = Ivar;
1778  }
1779  ObjCIvarDecl *getPropertyIvarDecl() const {
1780    return PropertyIvarDecl;
1781  }
1782
1783  virtual SourceRange getSourceRange() const {
1784    return SourceRange(AtLoc, getLocation());
1785  }
1786
1787  /// Lookup a property by name in the specified DeclContext.
1788  static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
1789                                            IdentifierInfo *propertyID);
1790
1791  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1792  static bool classof(const ObjCPropertyDecl *D) { return true; }
1793  static bool classofKind(Kind K) { return K == ObjCProperty; }
1794};
1795
1796/// ObjCPropertyImplDecl - Represents implementation declaration of a property
1797/// in a class or category implementation block. For example:
1798/// @synthesize prop1 = ivar1;
1799///
1800class ObjCPropertyImplDecl : public Decl {
1801public:
1802  enum Kind {
1803    Synthesize,
1804    Dynamic
1805  };
1806private:
1807  SourceLocation AtLoc;   // location of @synthesize or @dynamic
1808
1809  /// \brief For @synthesize, the location of the ivar, if it was written in
1810  /// the source code.
1811  ///
1812  /// \code
1813  /// @synthesize int a = b
1814  /// \endcode
1815  SourceLocation IvarLoc;
1816
1817  /// Property declaration being implemented
1818  ObjCPropertyDecl *PropertyDecl;
1819
1820  /// Null for @dynamic. Required for @synthesize.
1821  ObjCIvarDecl *PropertyIvarDecl;
1822
1823  /// Null for @dynamic. Non-null if property must be copy-constructed in getter
1824  Expr *GetterCXXConstructor;
1825
1826  /// Null for @dynamic. Non-null if property has assignment operator to call
1827  /// in Setter synthesis.
1828  Expr *SetterCXXAssignment;
1829
1830  ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
1831                       ObjCPropertyDecl *property,
1832                       Kind PK,
1833                       ObjCIvarDecl *ivarDecl,
1834                       SourceLocation ivarLoc)
1835    : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
1836      IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
1837      GetterCXXConstructor(0), SetterCXXAssignment(0) {
1838    assert (PK == Dynamic || PropertyIvarDecl);
1839  }
1840
1841public:
1842  static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
1843                                      SourceLocation atLoc, SourceLocation L,
1844                                      ObjCPropertyDecl *property,
1845                                      Kind PK,
1846                                      ObjCIvarDecl *ivarDecl,
1847                                      SourceLocation ivarLoc);
1848
1849  virtual SourceRange getSourceRange() const;
1850
1851  SourceLocation getLocStart() const { return AtLoc; }
1852  void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
1853
1854  ObjCPropertyDecl *getPropertyDecl() const {
1855    return PropertyDecl;
1856  }
1857  void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
1858
1859  Kind getPropertyImplementation() const {
1860    return PropertyIvarDecl ? Synthesize : Dynamic;
1861  }
1862
1863  ObjCIvarDecl *getPropertyIvarDecl() const {
1864    return PropertyIvarDecl;
1865  }
1866  SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
1867
1868  void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
1869                           SourceLocation IvarLoc) {
1870    PropertyIvarDecl = Ivar;
1871    this->IvarLoc = IvarLoc;
1872  }
1873
1874  Expr *getGetterCXXConstructor() const {
1875    return GetterCXXConstructor;
1876  }
1877  void setGetterCXXConstructor(Expr *getterCXXConstructor) {
1878    GetterCXXConstructor = getterCXXConstructor;
1879  }
1880
1881  Expr *getSetterCXXAssignment() const {
1882    return SetterCXXAssignment;
1883  }
1884  void setSetterCXXAssignment(Expr *setterCXXAssignment) {
1885    SetterCXXAssignment = setterCXXAssignment;
1886  }
1887
1888  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1889  static bool classof(const ObjCPropertyImplDecl *D) { return true; }
1890  static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
1891
1892  friend class ASTDeclReader;
1893};
1894
1895}  // end namespace clang
1896#endif
1897