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