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