DeclObjC.h revision 75c9cae5f85c72cbb1649e93849e16ede3f07522
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/Basic/IdentifierTable.h"
19
20namespace clang {
21class Expr;
22class Stmt;
23class FunctionDecl;
24class AttributeList;
25class ObjCIvarDecl;
26class ObjCMethodDecl;
27class ObjCProtocolDecl;
28class ObjCCategoryDecl;
29class ObjCPropertyDecl;
30
31/// ObjCMethodDecl - Represents an instance or class method declaration.
32/// ObjC methods can be declared within 4 contexts: class interfaces,
33/// categories, protocols, and class implementations. While C++ member
34/// functions leverage C syntax, Objective-C method syntax is modeled after
35/// Smalltalk (using colons to specify argument types/expressions).
36/// Here are some brief examples:
37///
38/// Setter/getter instance methods:
39/// - (void)setMenu:(NSMenu *)menu;
40/// - (NSMenu *)menu;
41///
42/// Instance method that takes 2 NSView arguments:
43/// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
44///
45/// Getter class method:
46/// + (NSMenu *)defaultMenu;
47///
48/// A selector represents a unique name for a method. The selector names for
49/// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
50///
51class ObjCMethodDecl : public Decl {
52public:
53  enum ImplementationControl { None, Required, Optional };
54private:
55  /// Bitfields must be first fields in this class so they pack with those
56  /// declared in class Decl.
57  /// instance (true) or class (false) method.
58  bool IsInstance : 1;
59  bool IsVariadic : 1;
60
61  // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
62  /// @required/@optional
63  unsigned DeclImplementation : 2;
64
65  // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
66  /// in, inout, etc.
67  unsigned objcDeclQualifier : 6;
68
69  // Context this method is declared in.
70  NamedDecl *MethodContext;
71
72  // A unigue name for this method.
73  Selector SelName;
74
75  // Type of this method.
76  QualType MethodDeclType;
77  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
78  /// parameters of this Method.  This is null if there are no formals.
79  ParmVarDecl **ParamInfo;
80  unsigned NumMethodParams;
81
82  /// List of attributes for this method declaration.
83  AttributeList *MethodAttrs;
84
85  SourceLocation EndLoc; // the location of the ';' or '{'.
86
87  // The following are only used for method definitions, null otherwise.
88  // FIXME: space savings opportunity, consider a sub-class.
89  Stmt *Body;
90  ParmVarDecl *SelfDecl;
91
92  ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
93                 Selector SelInfo, QualType T,
94                 Decl *contextDecl,
95                 AttributeList *M = 0, bool isInstance = true,
96                 bool isVariadic = false,
97                 ImplementationControl impControl = None)
98  : Decl(ObjCMethod, beginLoc),
99    IsInstance(isInstance), IsVariadic(isVariadic),
100    DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
101    MethodContext(static_cast<NamedDecl*>(contextDecl)),
102    SelName(SelInfo), MethodDeclType(T),
103    ParamInfo(0), NumMethodParams(0),
104    MethodAttrs(M), EndLoc(endLoc), Body(0), SelfDecl(0) {}
105  virtual ~ObjCMethodDecl();
106public:
107
108  static ObjCMethodDecl *Create(ASTContext &C, SourceLocation beginLoc,
109                                SourceLocation endLoc, Selector SelInfo,
110                                QualType T, Decl *contextDecl,
111                                AttributeList *M = 0, bool isInstance = true,
112                                bool isVariadic = false,
113                                ImplementationControl impControl = None);
114
115  ObjCDeclQualifier getObjCDeclQualifier() const {
116    return ObjCDeclQualifier(objcDeclQualifier);
117  }
118  void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
119
120  // Location information, modeled after the Stmt API.
121  SourceLocation getLocStart() const { return getLocation(); }
122  SourceLocation getLocEnd() const { return EndLoc; }
123
124  NamedDecl *getMethodContext() const { return MethodContext; }
125
126  ObjCInterfaceDecl *const getClassInterface() const;
127
128  Selector getSelector() const { return SelName; }
129  unsigned getSynthesizedMethodSize() const;
130  QualType getResultType() const { return MethodDeclType; }
131
132  // Iterator access to formal parameters.
133  unsigned param_size() const { return NumMethodParams; }
134  typedef ParmVarDecl **param_iterator;
135  typedef ParmVarDecl * const *param_const_iterator;
136  param_iterator param_begin() { return ParamInfo; }
137  param_iterator param_end() { return ParamInfo+param_size(); }
138  param_const_iterator param_begin() const { return ParamInfo; }
139  param_const_iterator param_end() const { return ParamInfo+param_size(); }
140
141  unsigned getNumParams() const { return NumMethodParams; }
142  ParmVarDecl *getParamDecl(unsigned i) const {
143    assert(i < getNumParams() && "Illegal param #");
144    return ParamInfo[i];
145  }
146  void setParamDecl(int i, ParmVarDecl *pDecl) {
147    ParamInfo[i] = pDecl;
148  }
149  void setMethodParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
150
151  AttributeList *getMethodAttrs() const {return MethodAttrs;}
152  bool isInstance() const { return IsInstance; }
153  bool isVariadic() const { return IsVariadic; }
154
155  // Related to protocols declared in  @protocol
156  void setDeclImplementation(ImplementationControl ic) {
157    DeclImplementation = ic;
158  }
159  ImplementationControl getImplementationControl() const {
160    return ImplementationControl(DeclImplementation);
161  }
162  Stmt *const getBody() const { return Body; }
163  void setBody(Stmt *B) { Body = B; }
164
165  ParmVarDecl *const getSelfDecl() const { return SelfDecl; }
166  void setSelfDecl(ParmVarDecl *PVD) { SelfDecl = PVD; }
167
168  // Implement isa/cast/dyncast/etc.
169  static bool classof(const Decl *D) { return D->getKind() == ObjCMethod; }
170  static bool classof(const ObjCMethodDecl *D) { return true; }
171};
172
173/// ObjCInterfaceDecl - Represents an ObjC class declaration. For example:
174///
175///   // MostPrimitive declares no super class (not particularly useful).
176///   @interface MostPrimitive
177///     // no instance variables or methods.
178///   @end
179///
180///   // NSResponder inherits from NSObject & implements NSCoding (a protocol).
181///   @interface NSResponder : NSObject <NSCoding>
182///   { // instance variables are represented by ObjCIvarDecl.
183///     id nextResponder; // nextResponder instance variable.
184///   }
185///   - (NSResponder *)nextResponder; // return a pointer to NSResponder.
186///   - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
187///   @end                                    // to an NSEvent.
188///
189///   Unlike C/C++, forward class declarations are accomplished with @class.
190///   Unlike C/C++, @class allows for a list of classes to be forward declared.
191///   Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
192///   typically inherit from NSObject (an exception is NSProxy).
193///
194class ObjCInterfaceDecl : public TypeDecl {
195
196  /// Class's super class.
197  ObjCInterfaceDecl *SuperClass;
198
199  /// Protocols referenced in interface header declaration
200  ObjCProtocolDecl **ReferencedProtocols;  // Null if none
201  unsigned NumReferencedProtocols;  // 0 if none
202
203  /// Ivars/NumIvars - This is a new[]'d array of pointers to Decls.
204  ObjCIvarDecl **Ivars;   // Null if not defined.
205  int NumIvars;   // -1 if not defined.
206
207  /// instance methods
208  ObjCMethodDecl **InstanceMethods;  // Null if not defined
209  int NumInstanceMethods;  // -1 if not defined
210
211  /// class methods
212  ObjCMethodDecl **ClassMethods;  // Null if not defined
213  unsigned NumClassMethods;  // 0 if none
214
215  /// List of categories defined for this class.
216  ObjCCategoryDecl *CategoryList;
217
218  /// class properties
219  ObjCPropertyDecl **PropertyDecl;  // Null if no property
220  int NumPropertyDecl;  // -1 if no property
221
222  bool ForwardDecl:1; // declared with @class.
223  bool InternalInterface:1; // true - no @interface for @implementation
224
225  SourceLocation EndLoc; // marks the '>', '}', or identifier.
226  SourceLocation AtEndLoc; // marks the end of the entire interface.
227
228  ObjCInterfaceDecl(SourceLocation atLoc, unsigned numRefProtos,
229                    IdentifierInfo *Id, bool FD, bool isInternal)
230    : TypeDecl(ObjCInterface, atLoc, Id, 0), SuperClass(0),
231      ReferencedProtocols(0), NumReferencedProtocols(0), Ivars(0),
232      NumIvars(-1),
233      InstanceMethods(0), NumInstanceMethods(-1),
234      ClassMethods(0), NumClassMethods(0),
235      CategoryList(0), PropertyDecl(0), NumPropertyDecl(-1),
236      ForwardDecl(FD), InternalInterface(isInternal) {
237        AllocIntfRefProtocols(numRefProtos);
238      }
239public:
240
241  static ObjCInterfaceDecl *Create(ASTContext &C, SourceLocation atLoc,
242                                   unsigned numRefProtos, IdentifierInfo *Id,
243                                   bool ForwardDecl = false,
244                                   bool isInternal = false);
245
246  // This is necessary when converting a forward declaration to a definition.
247  void AllocIntfRefProtocols(unsigned numRefProtos) {
248    if (numRefProtos) {
249      ReferencedProtocols = new ObjCProtocolDecl*[numRefProtos];
250      memset(ReferencedProtocols, '\0',
251             numRefProtos*sizeof(ObjCProtocolDecl*));
252      NumReferencedProtocols = numRefProtos;
253    }
254  }
255
256  ObjCProtocolDecl **getReferencedProtocols() const {
257    return ReferencedProtocols;
258  }
259  unsigned getNumIntfRefProtocols() const { return NumReferencedProtocols; }
260
261  int getNumInstanceVariables() const { return NumIvars; }
262
263  typedef ObjCIvarDecl * const *ivar_iterator;
264  unsigned ivar_size() const { return NumIvars == -1 ?0 : NumIvars; }
265  ivar_iterator ivar_begin() const { return Ivars; }
266  ivar_iterator ivar_end() const { return Ivars + ivar_size();}
267
268  int getNumInstanceMethods() const { return NumInstanceMethods; }
269  unsigned getNumClassMethods() const { return NumClassMethods; }
270
271  typedef ObjCMethodDecl * const * instmeth_iterator;
272  instmeth_iterator instmeth_begin() const { return InstanceMethods; }
273  instmeth_iterator instmeth_end() const {
274    return InstanceMethods+(NumInstanceMethods == -1 ? 0 : NumInstanceMethods);
275  }
276
277  typedef ObjCMethodDecl * const * classmeth_iterator;
278  classmeth_iterator classmeth_begin() const { return ClassMethods; }
279  classmeth_iterator classmeth_end() const {
280    return ClassMethods+NumClassMethods;
281  }
282
283  void addInstanceVariablesToClass(ObjCIvarDecl **ivars, unsigned numIvars,
284                                   SourceLocation RBracLoc);
285
286  void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers,
287                  ObjCMethodDecl **clsMethods, unsigned numClsMembers,
288                  SourceLocation AtEnd);
289
290  bool isForwardDecl() const { return ForwardDecl; }
291  void setForwardDecl(bool val) { ForwardDecl = val; }
292
293  void setIntfRefProtocols(unsigned idx, ObjCProtocolDecl *OID) {
294    assert((idx < NumReferencedProtocols) && "index out of range");
295    ReferencedProtocols[idx] = OID;
296  }
297
298  ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
299  void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
300
301  ObjCCategoryDecl* getCategoryList() const { return CategoryList; }
302  void setCategoryList(ObjCCategoryDecl *category) {
303         CategoryList = category;
304  }
305  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *ivarName,
306                                       ObjCInterfaceDecl *&clsDeclared);
307
308  // Get the local instance method declared in this interface.
309  ObjCMethodDecl *getInstanceMethod(Selector &Sel) {
310    for (instmeth_iterator I = instmeth_begin(), E = instmeth_end();
311         I != E; ++I) {
312      if ((*I)->getSelector() == Sel)
313        return *I;
314    }
315    return 0;
316  }
317  // Get the local class method declared in this interface.
318  ObjCMethodDecl *getClassMethod(Selector &Sel) {
319    for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
320         I != E; ++I) {
321      if ((*I)->getSelector() == Sel)
322        return *I;
323    }
324    return 0;
325  }
326  // Lookup a method. First, we search locally. If a method isn't
327  // found, we search referenced protocols and class categories.
328  ObjCMethodDecl *lookupInstanceMethod(Selector Sel);
329  ObjCMethodDecl *lookupClassMethod(Selector Sel);
330
331  // Location information, modeled after the Stmt API.
332  SourceLocation getLocStart() const { return getLocation(); } // '@'interface
333  SourceLocation getLocEnd() const { return EndLoc; }
334  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
335
336  // We also need to record the @end location.
337  SourceLocation getAtEndLoc() const { return AtEndLoc; }
338
339  int getNumPropertyDecl() const { return NumPropertyDecl; }
340  void setNumPropertyDecl(int num) { NumPropertyDecl = num; }
341
342  ObjCPropertyDecl **const getPropertyDecl() const { return PropertyDecl; }
343  ObjCPropertyDecl **getPropertyDecl() { return PropertyDecl; }
344  void setPropertyDecls(ObjCPropertyDecl **properties) {
345    PropertyDecl = properties;
346  }
347
348  /// ImplicitInterfaceDecl - check that this is an implicitely declared
349  /// ObjCInterfaceDecl node. This is for legacy objective-c @implementation
350  /// declaration without an @interface declaration.
351  bool ImplicitInterfaceDecl() const { return InternalInterface; }
352
353  static bool classof(const Decl *D) { return D->getKind() == ObjCInterface; }
354  static bool classof(const ObjCInterfaceDecl *D) { return true; }
355};
356
357/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
358/// instance variables are identical to C. The only exception is Objective-C
359/// supports C++ style access control. For example:
360///
361///   @interface IvarExample : NSObject
362///   {
363///     id defaultToPrivate; // same as C++.
364///   @public:
365///     id canBePublic; // same as C++.
366///   @protected:
367///     id canBeProtected; // same as C++.
368///   @package:
369///     id canBePackage; // framework visibility (not available in C++).
370///   }
371///
372class ObjCIvarDecl : public FieldDecl {
373  ObjCIvarDecl(SourceLocation L, IdentifierInfo *Id, QualType T)
374    : FieldDecl(ObjCIvar, L, Id, T) {}
375public:
376  static ObjCIvarDecl *Create(ASTContext &C, SourceLocation L,
377                              IdentifierInfo *Id, QualType T);
378
379  enum AccessControl {
380    None, Private, Protected, Public, Package
381  };
382  void setAccessControl(AccessControl ac) { DeclAccess = ac; }
383  AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
384
385  // Implement isa/cast/dyncast/etc.
386  static bool classof(const Decl *D) { return D->getKind() == ObjCIvar; }
387  static bool classof(const ObjCIvarDecl *D) { return true; }
388private:
389  // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
390  unsigned DeclAccess : 3;
391};
392
393
394/// ObjCProtocolDecl - Represents a protocol declaration. ObjC protocols
395/// declare a pure abstract type (i.e no instance variables are permitted).
396/// Protocols orginally drew inspiration from C++ pure virtual functions (a C++
397/// feature with nice semantics and lousy syntax:-). Here is an example:
398///
399/// @protocol NSDraggingInfo
400/// - (NSWindow *)draggingDestinationWindow;
401/// - (NSImage *)draggedImage;
402/// @end
403///
404/// @interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo>
405/// @end
406///
407/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
408/// protocols are in distinct namespaces. For example, Cocoa defines both
409/// an NSObject protocol and class (which isn't allowed in Java). As a result,
410/// protocols are referenced using angle brackets as follows:
411///
412/// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
413///
414class ObjCProtocolDecl : public NamedDecl {
415  /// referenced protocols
416  ObjCProtocolDecl **ReferencedProtocols;  // Null if none
417  unsigned NumReferencedProtocols;  // 0 if none
418
419  /// protocol instance methods
420  ObjCMethodDecl **InstanceMethods;  // Null if not defined
421  unsigned NumInstanceMethods;  // 0 if none
422
423  /// protocol class methods
424  ObjCMethodDecl **ClassMethods;  // Null if not defined
425  unsigned NumClassMethods;  // 0 if none
426
427  bool isForwardProtoDecl; // declared with @protocol.
428
429  SourceLocation EndLoc; // marks the '>' or identifier.
430  SourceLocation AtEndLoc; // marks the end of the entire interface.
431
432  ObjCProtocolDecl(SourceLocation L, unsigned numRefProtos, IdentifierInfo *Id)
433    : NamedDecl(ObjCProtocol, L, Id),
434      ReferencedProtocols(0), NumReferencedProtocols(0),
435      InstanceMethods(0), NumInstanceMethods(0),
436      ClassMethods(0), NumClassMethods(0),
437      isForwardProtoDecl(true) {
438    AllocReferencedProtocols(numRefProtos);
439  }
440public:
441  static ObjCProtocolDecl *Create(ASTContext &C, SourceLocation L,
442                                  unsigned numRefProtos, IdentifierInfo *Id);
443
444  void AllocReferencedProtocols(unsigned numRefProtos) {
445    if (numRefProtos) {
446      ReferencedProtocols = new ObjCProtocolDecl*[numRefProtos];
447      memset(ReferencedProtocols, '\0',
448             numRefProtos*sizeof(ObjCProtocolDecl*));
449      NumReferencedProtocols = numRefProtos;
450    }
451  }
452  void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers,
453                  ObjCMethodDecl **clsMethods, unsigned numClsMembers,
454                  SourceLocation AtEndLoc);
455
456  void setReferencedProtocols(unsigned idx, ObjCProtocolDecl *OID) {
457    assert((idx < NumReferencedProtocols) && "index out of range");
458    ReferencedProtocols[idx] = OID;
459  }
460
461  ObjCProtocolDecl** getReferencedProtocols() const {
462    return ReferencedProtocols;
463  }
464  unsigned getNumReferencedProtocols() const { return NumReferencedProtocols; }
465  unsigned getNumInstanceMethods() const { return NumInstanceMethods; }
466  unsigned getNumClassMethods() const { return NumClassMethods; }
467
468  typedef ObjCMethodDecl * const * instmeth_iterator;
469  instmeth_iterator instmeth_begin() const { return InstanceMethods; }
470  instmeth_iterator instmeth_end() const {
471    return InstanceMethods+NumInstanceMethods;
472  }
473
474  typedef ObjCMethodDecl * const * classmeth_iterator;
475  classmeth_iterator classmeth_begin() const { return ClassMethods; }
476  classmeth_iterator classmeth_end() const {
477    return ClassMethods+NumClassMethods;
478  }
479
480  // Get the local instance method declared in this interface.
481  ObjCMethodDecl *getInstanceMethod(Selector Sel) {
482    for (instmeth_iterator I = instmeth_begin(), E = instmeth_end();
483         I != E; ++I) {
484      if ((*I)->getSelector() == Sel)
485        return *I;
486    }
487    return 0;
488  }
489  // Get the local class method declared in this interface.
490  ObjCMethodDecl *getClassMethod(Selector Sel) {
491    for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
492         I != E; ++I) {
493      if ((*I)->getSelector() == Sel)
494        return *I;
495    }
496    return 0;
497  }
498
499  // Lookup a method. First, we search locally. If a method isn't
500  // found, we search referenced protocols and class categories.
501  ObjCMethodDecl *lookupInstanceMethod(Selector Sel);
502  ObjCMethodDecl *lookupClassMethod(Selector Sel);
503
504  bool isForwardDecl() const { return isForwardProtoDecl; }
505  void setForwardDecl(bool val) { isForwardProtoDecl = val; }
506
507  // Location information, modeled after the Stmt API.
508  SourceLocation getLocStart() const { return getLocation(); } // '@'protocol
509  SourceLocation getLocEnd() const { return EndLoc; }
510  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
511
512  // We also need to record the @end location.
513  SourceLocation getAtEndLoc() const { return AtEndLoc; }
514
515  static bool classof(const Decl *D) { return D->getKind() == ObjCProtocol; }
516  static bool classof(const ObjCProtocolDecl *D) { return true; }
517};
518
519/// ObjCClassDecl - Specifies a list of forward class declarations. For example:
520///
521/// @class NSCursor, NSImage, NSPasteboard, NSWindow;
522///
523class ObjCClassDecl : public Decl {
524  ObjCInterfaceDecl **ForwardDecls;
525  unsigned NumForwardDecls;
526
527  ObjCClassDecl(SourceLocation L, ObjCInterfaceDecl **Elts, unsigned nElts)
528    : Decl(ObjCClass, L) {
529    if (nElts) {
530      ForwardDecls = new ObjCInterfaceDecl*[nElts];
531      memcpy(ForwardDecls, Elts, nElts*sizeof(ObjCInterfaceDecl*));
532    } else {
533      ForwardDecls = 0;
534    }
535    NumForwardDecls = nElts;
536  }
537public:
538  static ObjCClassDecl *Create(ASTContext &C, SourceLocation L,
539                               ObjCInterfaceDecl **Elts, unsigned nElts);
540
541  void setInterfaceDecl(unsigned idx, ObjCInterfaceDecl *OID) {
542    assert(idx < NumForwardDecls && "index out of range");
543    ForwardDecls[idx] = OID;
544  }
545  ObjCInterfaceDecl** getForwardDecls() const { return ForwardDecls; }
546  int getNumForwardDecls() const { return NumForwardDecls; }
547
548  static bool classof(const Decl *D) { return D->getKind() == ObjCClass; }
549  static bool classof(const ObjCClassDecl *D) { return true; }
550};
551
552/// ObjCForwardProtocolDecl - Specifies a list of forward protocol declarations.
553/// For example:
554///
555/// @protocol NSTextInput, NSChangeSpelling, NSDraggingInfo;
556///
557class ObjCForwardProtocolDecl : public Decl {
558  ObjCProtocolDecl **ReferencedProtocols;
559  unsigned NumReferencedProtocols;
560
561  ObjCForwardProtocolDecl(SourceLocation L,
562                          ObjCProtocolDecl **Elts, unsigned nElts)
563  : Decl(ObjCForwardProtocol, L) {
564    NumReferencedProtocols = nElts;
565    if (nElts) {
566      ReferencedProtocols = new ObjCProtocolDecl*[nElts];
567      memcpy(ReferencedProtocols, Elts, nElts*sizeof(ObjCProtocolDecl*));
568    } else {
569      ReferencedProtocols = 0;
570    }
571  }
572public:
573  static ObjCForwardProtocolDecl *Create(ASTContext &C, SourceLocation L,
574                                         ObjCProtocolDecl **Elts, unsigned Num);
575
576
577  void setForwardProtocolDecl(unsigned idx, ObjCProtocolDecl *OID) {
578    assert(idx < NumReferencedProtocols && "index out of range");
579    ReferencedProtocols[idx] = OID;
580  }
581
582  unsigned getNumForwardDecls() const { return NumReferencedProtocols; }
583
584  ObjCProtocolDecl *getForwardProtocolDecl(unsigned idx) {
585    assert(idx < NumReferencedProtocols && "index out of range");
586    return ReferencedProtocols[idx];
587  }
588  const ObjCProtocolDecl *getForwardProtocolDecl(unsigned idx) const {
589    assert(idx < NumReferencedProtocols && "index out of range");
590    return ReferencedProtocols[idx];
591  }
592
593  static bool classof(const Decl *D) {
594    return D->getKind() == ObjCForwardProtocol;
595  }
596  static bool classof(const ObjCForwardProtocolDecl *D) { return true; }
597};
598
599/// ObjCCategoryDecl - Represents a category declaration. A category allows
600/// you to add methods to an existing class (without subclassing or modifying
601/// the original class interface or implementation:-). Categories don't allow
602/// you to add instance data. The following example adds "myMethod" to all
603/// NSView's within a process:
604///
605/// @interface NSView (MyViewMethods)
606/// - myMethod;
607/// @end
608///
609/// Cateogries also allow you to split the implementation of a class across
610/// several files (a feature more naturally supported in C++).
611///
612/// Categories were originally inspired by dynamic languages such as Common
613/// Lisp and Smalltalk.  More traditional class-based languages (C++, Java)
614/// don't support this level of dynamism, which is both powerful and dangerous.
615///
616class ObjCCategoryDecl : public NamedDecl {
617  /// Interface belonging to this category
618  ObjCInterfaceDecl *ClassInterface;
619
620  /// referenced protocols in this category.
621  ObjCProtocolDecl **ReferencedProtocols;  // Null if none
622  unsigned NumReferencedProtocols;  // 0 if none
623
624  /// category instance methods
625  ObjCMethodDecl **InstanceMethods;  // Null if not defined
626  unsigned NumInstanceMethods;  // 0 if none
627
628  /// category class methods
629  ObjCMethodDecl **ClassMethods;  // Null if not defined
630  unsigned NumClassMethods;  // 0 if not defined
631
632  /// Next category belonging to this class
633  ObjCCategoryDecl *NextClassCategory;
634
635  SourceLocation EndLoc; // marks the '>' or identifier.
636  SourceLocation AtEndLoc; // marks the end of the entire interface.
637
638  ObjCCategoryDecl(SourceLocation L, IdentifierInfo *Id)
639    : NamedDecl(ObjCCategory, L, Id),
640      ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(0),
641      InstanceMethods(0), NumInstanceMethods(0),
642      ClassMethods(0), NumClassMethods(0),
643      NextClassCategory(0) {
644  }
645public:
646
647  static ObjCCategoryDecl *Create(ASTContext &C, SourceLocation L,
648                                  IdentifierInfo *Id);
649
650  ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
651  void setClassInterface(ObjCInterfaceDecl *IDecl) { ClassInterface = IDecl; }
652
653  void setReferencedProtocolList(ObjCProtocolDecl **List, unsigned NumRPs);
654
655  void setCatReferencedProtocols(unsigned idx, ObjCProtocolDecl *OID) {
656    assert((idx < NumReferencedProtocols) && "index out of range");
657    ReferencedProtocols[idx] = OID;
658  }
659
660  ObjCProtocolDecl **getReferencedProtocols() const {
661    return ReferencedProtocols;
662  }
663  unsigned getNumReferencedProtocols() const { return NumReferencedProtocols; }
664  unsigned getNumInstanceMethods() const { return NumInstanceMethods; }
665  unsigned getNumClassMethods() const { return NumClassMethods; }
666
667  typedef ObjCMethodDecl * const * instmeth_iterator;
668  instmeth_iterator instmeth_begin() const { return InstanceMethods; }
669  instmeth_iterator instmeth_end() const {
670    return InstanceMethods+NumInstanceMethods;
671  }
672
673  typedef ObjCMethodDecl * const * classmeth_iterator;
674  classmeth_iterator classmeth_begin() const { return ClassMethods; }
675  classmeth_iterator classmeth_end() const {
676    return ClassMethods+NumClassMethods;
677  }
678
679  // Get the local instance method declared in this interface.
680  ObjCMethodDecl *getInstanceMethod(Selector Sel) {
681    for (instmeth_iterator I = instmeth_begin(), E = instmeth_end();
682         I != E; ++I) {
683      if ((*I)->getSelector() == Sel)
684        return *I;
685    }
686    return 0;
687  }
688  // Get the local class method declared in this interface.
689  ObjCMethodDecl *getClassMethod(Selector Sel) {
690    for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
691         I != E; ++I) {
692      if ((*I)->getSelector() == Sel)
693        return *I;
694    }
695    return 0;
696  }
697
698  void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers,
699                  ObjCMethodDecl **clsMethods, unsigned numClsMembers,
700                  SourceLocation AtEndLoc);
701
702  ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
703  void insertNextClassCategory() {
704    NextClassCategory = ClassInterface->getCategoryList();
705    ClassInterface->setCategoryList(this);
706  }
707  // Location information, modeled after the Stmt API.
708  SourceLocation getLocStart() const { return getLocation(); } // '@'interface
709  SourceLocation getLocEnd() const { return EndLoc; }
710  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
711
712  // We also need to record the @end location.
713  SourceLocation getAtEndLoc() const { return AtEndLoc; }
714
715  static bool classof(const Decl *D) { return D->getKind() == ObjCCategory; }
716  static bool classof(const ObjCCategoryDecl *D) { return true; }
717};
718
719/// ObjCCategoryImplDecl - An object of this class encapsulates a category
720/// @implementation declaration.
721class ObjCCategoryImplDecl : public NamedDecl {
722  /// Class interface for this category implementation
723  ObjCInterfaceDecl *ClassInterface;
724
725  /// implemented instance methods
726  llvm::SmallVector<ObjCMethodDecl*, 32> InstanceMethods;
727
728  /// implemented class methods
729  llvm::SmallVector<ObjCMethodDecl*, 32> ClassMethods;
730
731  SourceLocation EndLoc;
732
733  ObjCCategoryImplDecl(SourceLocation L, IdentifierInfo *Id,
734                       ObjCInterfaceDecl *classInterface)
735    : NamedDecl(ObjCCategoryImpl, L, Id), ClassInterface(classInterface) {}
736public:
737  static ObjCCategoryImplDecl *Create(ASTContext &C, SourceLocation L,
738                                      IdentifierInfo *Id,
739                                      ObjCInterfaceDecl *classInterface);
740
741  ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
742
743  unsigned getNumInstanceMethods() const { return InstanceMethods.size(); }
744  unsigned getNumClassMethods() const { return ClassMethods.size(); }
745
746  void addInstanceMethod(ObjCMethodDecl *method) {
747    InstanceMethods.push_back(method);
748  }
749  void addClassMethod(ObjCMethodDecl *method) {
750    ClassMethods.push_back(method);
751  }
752  // Get the instance method definition for this implementation.
753  ObjCMethodDecl *getInstanceMethod(Selector Sel);
754
755  // Get the class method definition for this implementation.
756  ObjCMethodDecl *getClassMethod(Selector Sel);
757
758  typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator
759    instmeth_iterator;
760  instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); }
761  instmeth_iterator instmeth_end() const { return InstanceMethods.end(); }
762
763  typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator
764    classmeth_iterator;
765  classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); }
766  classmeth_iterator classmeth_end() const { return ClassMethods.end(); }
767
768
769  // Location information, modeled after the Stmt API.
770  SourceLocation getLocStart() const { return getLocation(); }
771  SourceLocation getLocEnd() const { return EndLoc; }
772  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
773
774  static bool classof(const Decl *D) { return D->getKind() == ObjCCategoryImpl;}
775  static bool classof(const ObjCCategoryImplDecl *D) { return true; }
776};
777
778/// ObjCImplementationDecl - Represents a class definition - this is where
779/// method definitions are specified. For example:
780///
781/// @implementation MyClass
782/// - (void)myMethod { /* do something */ }
783/// @end
784///
785/// Typically, instance variables are specified in the class interface,
786/// *not* in the implemenentation. Nevertheless (for legacy reasons), we
787/// allow instance variables to be specified in the implementation. When
788/// specified, they need to be *identical* to the interface. Now that we
789/// have support for non-fragile ivars in ObjC 2.0, we can consider removing
790/// the legacy semantics and allow developers to move private ivar declarations
791/// from the class interface to the class implementation (but I digress:-)
792///
793class ObjCImplementationDecl : public NamedDecl {
794  /// Class interface for this category implementation
795  ObjCInterfaceDecl *ClassInterface;
796
797  /// Implementation Class's super class.
798  ObjCInterfaceDecl *SuperClass;
799
800  /// Optional Ivars/NumIvars - This is a new[]'d array of pointers to Decls.
801  ObjCIvarDecl **Ivars;   // Null if not specified
802  int NumIvars;   // -1 if not defined.
803
804  /// implemented instance methods
805  llvm::SmallVector<ObjCMethodDecl*, 32> InstanceMethods;
806
807  /// implemented class methods
808  llvm::SmallVector<ObjCMethodDecl*, 32> ClassMethods;
809
810  SourceLocation EndLoc;
811
812  ObjCImplementationDecl(SourceLocation L, IdentifierInfo *Id,
813                         ObjCInterfaceDecl *classInterface,
814                         ObjCInterfaceDecl *superDecl)
815    : NamedDecl(ObjCImplementation, L, Id),
816      ClassInterface(classInterface), SuperClass(superDecl),
817      Ivars(0), NumIvars(-1) {}
818public:
819  static ObjCImplementationDecl *Create(ASTContext &C, SourceLocation L,
820                                        IdentifierInfo *Id,
821                                        ObjCInterfaceDecl *classInterface,
822                                        ObjCInterfaceDecl *superDecl);
823
824
825  void ObjCAddInstanceVariablesToClassImpl(ObjCIvarDecl **ivars,
826                                           unsigned numIvars);
827
828  void addInstanceMethod(ObjCMethodDecl *method) {
829    InstanceMethods.push_back(method);
830  }
831  void addClassMethod(ObjCMethodDecl *method) {
832    ClassMethods.push_back(method);
833  }
834  // Location information, modeled after the Stmt API.
835  SourceLocation getLocStart() const { return getLocation(); }
836  SourceLocation getLocEnd() const { return EndLoc; }
837  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
838
839  ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
840  ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
841
842  void setSuperClass(ObjCInterfaceDecl * superCls)
843         { SuperClass = superCls; }
844
845  int getNumInstanceMethods() const { return InstanceMethods.size(); }
846  unsigned getNumClassMethods() const { return ClassMethods.size(); }
847
848  int getImplDeclNumIvars() const { return NumIvars; }
849
850
851  typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator
852       instmeth_iterator;
853  instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); }
854  instmeth_iterator instmeth_end() const { return InstanceMethods.end(); }
855
856  typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator
857    classmeth_iterator;
858  classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); }
859  classmeth_iterator classmeth_end() const { return ClassMethods.end(); }
860
861  // Get the instance method definition for this implementation.
862  ObjCMethodDecl *getInstanceMethod(Selector Sel);
863
864  // Get the class method definition for this implementation.
865  ObjCMethodDecl *getClassMethod(Selector Sel);
866
867  typedef ObjCIvarDecl * const *ivar_iterator;
868  ivar_iterator ivar_begin() const { return Ivars; }
869  ivar_iterator ivar_end() const {return Ivars+(NumIvars == -1 ? 0 : NumIvars);}
870
871  static bool classof(const Decl *D) {
872    return D->getKind() == ObjCImplementation;
873  }
874  static bool classof(const ObjCImplementationDecl *D) { return true; }
875};
876
877/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
878/// declared as @compatibility_alias alias class.
879class ObjCCompatibleAliasDecl : public ScopedDecl {
880  /// Class that this is an alias of.
881  ObjCInterfaceDecl *AliasedClass;
882
883public:
884  ObjCCompatibleAliasDecl(SourceLocation L, IdentifierInfo *Id,
885                         ObjCInterfaceDecl* aliasedClass)
886  : ScopedDecl(CompatibleAlias, L, Id, 0),
887  AliasedClass(aliasedClass) {}
888
889  ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
890
891  static bool classof(const Decl *D) {
892    return D->getKind() == CompatibleAlias;
893  }
894  static bool classof(const ObjCCompatibleAliasDecl *D) { return true; }
895
896};
897
898class ObjCPropertyDecl : public Decl {
899public:
900  enum PropertyAttributeKind { OBJC_PR_noattr = 0x0,
901                       OBJC_PR_readonly = 0x01,
902                       OBJC_PR_getter = 0x02,
903                       OBJC_PR_assign = 0x04,
904                       OBJC_PR_readwrite = 0x08,
905                       OBJC_PR_retain = 0x10,
906                       OBJC_PR_copy = 0x20,
907                       OBJC_PR_nonatomic = 0x40,
908                       OBJC_PR_setter = 0x80 };
909private:
910  // List of property name declarations
911  // FIXME: Property is not an ivar.
912  ObjCIvarDecl **PropertyDecls;
913  int NumPropertyDecls;
914
915  // NOTE: VC++ treats enums as signed, avoid using PropertyAttributeKind enum
916  unsigned PropertyAttributes : 8;
917
918  IdentifierInfo *GetterName;    // getter name of NULL if no getter
919  IdentifierInfo *SetterName;    // setter name of NULL if no setter
920
921public:
922  ObjCPropertyDecl(SourceLocation L)
923  : Decl(PropertyDecl, L),
924  PropertyDecls(0), NumPropertyDecls(-1), PropertyAttributes(OBJC_PR_noattr),
925  GetterName(0), SetterName(0) {}
926
927  ObjCIvarDecl **const getPropertyDecls() const { return PropertyDecls; }
928  void setPropertyDecls(ObjCIvarDecl **property) { PropertyDecls = property; }
929
930  const int getNumPropertyDecls() const { return NumPropertyDecls; }
931  void setNumPropertyDecls(int num) { NumPropertyDecls = num; }
932
933  const PropertyAttributeKind getPropertyAttributes() const
934    { return PropertyAttributeKind(PropertyAttributes); }
935  void setPropertyAttributes(PropertyAttributeKind PRVal) {
936    PropertyAttributes =
937    (PropertyAttributeKind) (PropertyAttributes | PRVal);
938  }
939
940  const IdentifierInfo *getGetterName() const { return GetterName; }
941  IdentifierInfo *getGetterName() { return GetterName; }
942  void setGetterName(IdentifierInfo *Id) { GetterName = Id; }
943
944  const IdentifierInfo *getSetterName() const { return SetterName; }
945  IdentifierInfo *getSetterName() { return SetterName; }
946  void setSetterName(IdentifierInfo *Id) { SetterName = Id; }
947
948  static bool classof(const Decl *D) {
949    return D->getKind() == PropertyDecl;
950  }
951  static bool classof(const ObjCPropertyDecl *D) { return true; }
952};
953
954}  // end namespace clang
955#endif
956