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