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