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