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