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