DeclObjC.h revision a906135721c350435319347d2672bbb3bf494f91
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 int NumClassMethods; // -1 if not defined 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(-1), 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 int 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 == -1 ? 0 : 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 int NumInstanceMethods; // -1 if not defined 422 423 /// protocol class methods 424 ObjCMethodDecl **ClassMethods; // Null if not defined 425 int NumClassMethods; // -1 if not defined 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(-1), 436 ClassMethods(0), NumClassMethods(-1), 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 int 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 == -1 ? 0 : 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 == -1 ? 0 : 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; 526public: 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 } 537 void setInterfaceDecl(unsigned idx, ObjCInterfaceDecl *OID) { 538 assert(idx < NumForwardDecls && "index out of range"); 539 ForwardDecls[idx] = OID; 540 } 541 ObjCInterfaceDecl** getForwardDecls() const { return ForwardDecls; } 542 int getNumForwardDecls() const { return NumForwardDecls; } 543 544 static bool classof(const Decl *D) { return D->getKind() == ObjCClass; } 545 static bool classof(const ObjCClassDecl *D) { return true; } 546}; 547 548/// ObjCForwardProtocolDecl - Specifies a list of forward protocol declarations. 549/// For example: 550/// 551/// @protocol NSTextInput, NSChangeSpelling, NSDraggingInfo; 552/// 553class ObjCForwardProtocolDecl : public Decl { 554 ObjCProtocolDecl **ReferencedProtocols; 555 unsigned NumReferencedProtocols; 556public: 557 ObjCForwardProtocolDecl(SourceLocation L, 558 ObjCProtocolDecl **Elts, unsigned nElts) 559 : Decl(ObjCForwardProtocol, L) { 560 NumReferencedProtocols = nElts; 561 if (nElts) { 562 ReferencedProtocols = new ObjCProtocolDecl*[nElts]; 563 memcpy(ReferencedProtocols, Elts, nElts*sizeof(ObjCProtocolDecl*)); 564 } else { 565 ReferencedProtocols = 0; 566 } 567 } 568 void setForwardProtocolDecl(unsigned idx, ObjCProtocolDecl *OID) { 569 assert(idx < NumReferencedProtocols && "index out of range"); 570 ReferencedProtocols[idx] = OID; 571 } 572 573 unsigned getNumForwardDecls() const { return NumReferencedProtocols; } 574 575 ObjCProtocolDecl *getForwardProtocolDecl(unsigned idx) { 576 assert(idx < NumReferencedProtocols && "index out of range"); 577 return ReferencedProtocols[idx]; 578 } 579 const ObjCProtocolDecl *getForwardProtocolDecl(unsigned idx) const { 580 assert(idx < NumReferencedProtocols && "index out of range"); 581 return ReferencedProtocols[idx]; 582 } 583 584 static bool classof(const Decl *D) { 585 return D->getKind() == ObjCForwardProtocol; 586 } 587 static bool classof(const ObjCForwardProtocolDecl *D) { return true; } 588}; 589 590/// ObjCCategoryDecl - Represents a category declaration. A category allows 591/// you to add methods to an existing class (without subclassing or modifying 592/// the original class interface or implementation:-). Categories don't allow 593/// you to add instance data. The following example adds "myMethod" to all 594/// NSView's within a process: 595/// 596/// @interface NSView (MyViewMethods) 597/// - myMethod; 598/// @end 599/// 600/// Cateogries also allow you to split the implementation of a class across 601/// several files (a feature more naturally supported in C++). 602/// 603/// Categories were originally inspired by dynamic languages such as Common 604/// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 605/// don't support this level of dynamism, which is both powerful and dangerous. 606/// 607class ObjCCategoryDecl : public NamedDecl { 608 /// Interface belonging to this category 609 ObjCInterfaceDecl *ClassInterface; 610 611 /// referenced protocols in this category 612 ObjCProtocolDecl **ReferencedProtocols; // Null if none 613 unsigned NumReferencedProtocols; // 0 if none 614 615 /// category instance methods 616 ObjCMethodDecl **InstanceMethods; // Null if not defined 617 unsigned NumInstanceMethods; // 0 if none 618 619 /// category class methods 620 ObjCMethodDecl **ClassMethods; // Null if not defined 621 unsigned NumClassMethods; // 0 if not defined 622 623 /// Next category belonging to this class 624 ObjCCategoryDecl *NextClassCategory; 625 626 SourceLocation EndLoc; // marks the '>' or identifier. 627 SourceLocation AtEndLoc; // marks the end of the entire interface. 628public: 629 ObjCCategoryDecl(SourceLocation L, unsigned numRefProtocol,IdentifierInfo *Id) 630 : NamedDecl(ObjCCategory, L, Id), 631 ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(0), 632 InstanceMethods(0), NumInstanceMethods(0), 633 ClassMethods(0), NumClassMethods(0), 634 NextClassCategory(0) { 635 if (numRefProtocol) { 636 ReferencedProtocols = new ObjCProtocolDecl*[numRefProtocol]; 637 memset(ReferencedProtocols, '\0', 638 numRefProtocol*sizeof(ObjCProtocolDecl*)); 639 NumReferencedProtocols = numRefProtocol; 640 } 641 } 642 643 ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 644 void setClassInterface(ObjCInterfaceDecl *IDecl) { ClassInterface = IDecl; } 645 646 void setCatReferencedProtocols(unsigned idx, ObjCProtocolDecl *OID) { 647 assert((idx < NumReferencedProtocols) && "index out of range"); 648 ReferencedProtocols[idx] = OID; 649 } 650 651 ObjCProtocolDecl **getReferencedProtocols() const { 652 return ReferencedProtocols; 653 } 654 unsigned getNumReferencedProtocols() const { return NumReferencedProtocols; } 655 unsigned getNumInstanceMethods() const { return NumInstanceMethods; } 656 unsigned getNumClassMethods() const { return NumClassMethods; } 657 658 typedef ObjCMethodDecl * const * instmeth_iterator; 659 instmeth_iterator instmeth_begin() const { return InstanceMethods; } 660 instmeth_iterator instmeth_end() const { 661 return InstanceMethods+NumInstanceMethods; 662 } 663 664 typedef ObjCMethodDecl * const * classmeth_iterator; 665 classmeth_iterator classmeth_begin() const { return ClassMethods; } 666 classmeth_iterator classmeth_end() const { 667 return ClassMethods+NumClassMethods; 668 } 669 670 // Get the local instance method declared in this interface. 671 ObjCMethodDecl *getInstanceMethod(const Selector &Sel) { 672 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); 673 I != E; ++I) { 674 if ((*I)->getSelector() == Sel) 675 return *I; 676 } 677 return 0; 678 } 679 // Get the local class method declared in this interface. 680 ObjCMethodDecl *getClassMethod(const Selector &Sel) { 681 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); 682 I != E; ++I) { 683 if ((*I)->getSelector() == Sel) 684 return *I; 685 } 686 return 0; 687 } 688 689 void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers, 690 ObjCMethodDecl **clsMethods, unsigned numClsMembers, 691 SourceLocation AtEndLoc); 692 693 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } 694 void insertNextClassCategory() { 695 NextClassCategory = ClassInterface->getCategoryList(); 696 ClassInterface->setCategoryList(this); 697 } 698 // Location information, modeled after the Stmt API. 699 SourceLocation getLocStart() const { return getLocation(); } // '@'interface 700 SourceLocation getLocEnd() const { return EndLoc; } 701 void setLocEnd(SourceLocation LE) { EndLoc = LE; }; 702 703 // We also need to record the @end location. 704 SourceLocation getAtEndLoc() const { return AtEndLoc; } 705 706 static bool classof(const Decl *D) { return D->getKind() == ObjCCategory; } 707 static bool classof(const ObjCCategoryDecl *D) { return true; } 708}; 709 710/// ObjCCategoryImplDecl - An object of this class encapsulates a category 711/// @implementation declaration. 712class ObjCCategoryImplDecl : public NamedDecl { 713 /// Class interface for this category implementation 714 ObjCInterfaceDecl *ClassInterface; 715 716 /// implemented instance methods 717 llvm::SmallVector<ObjCMethodDecl*, 32> InstanceMethods; 718 719 /// implemented class methods 720 llvm::SmallVector<ObjCMethodDecl*, 32> ClassMethods; 721 722 SourceLocation EndLoc; 723public: 724 ObjCCategoryImplDecl(SourceLocation L, IdentifierInfo *Id, 725 ObjCInterfaceDecl *classInterface) 726 : NamedDecl(ObjCCategoryImpl, L, Id), ClassInterface(classInterface) {} 727 728 ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 729 730 unsigned getNumInstanceMethods() const { return InstanceMethods.size(); } 731 unsigned getNumClassMethods() const { return ClassMethods.size(); } 732 733 void addInstanceMethod(ObjCMethodDecl *method) { 734 InstanceMethods.push_back(method); 735 } 736 void addClassMethod(ObjCMethodDecl *method) { 737 ClassMethods.push_back(method); 738 } 739 // Get the instance method definition for this implementation. 740 ObjCMethodDecl *getInstanceMethod(Selector Sel); 741 742 // Get the class method definition for this implementation. 743 ObjCMethodDecl *getClassMethod(Selector Sel); 744 745 typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator 746 instmeth_iterator; 747 instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); } 748 instmeth_iterator instmeth_end() const { return InstanceMethods.end(); } 749 750 typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator 751 classmeth_iterator; 752 classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); } 753 classmeth_iterator classmeth_end() const { return ClassMethods.end(); } 754 755 756 // Location information, modeled after the Stmt API. 757 SourceLocation getLocStart() const { return getLocation(); } 758 SourceLocation getLocEnd() const { return EndLoc; } 759 void setLocEnd(SourceLocation LE) { EndLoc = LE; }; 760 761 static bool classof(const Decl *D) { return D->getKind() == ObjCCategoryImpl;} 762 static bool classof(const ObjCCategoryImplDecl *D) { return true; } 763}; 764 765/// ObjCImplementationDecl - Represents a class definition - this is where 766/// method definitions are specified. For example: 767/// 768/// @implementation MyClass 769/// - (void)myMethod { /* do something */ } 770/// @end 771/// 772/// Typically, instance variables are specified in the class interface, 773/// *not* in the implemenentation. Nevertheless (for legacy reasons), we 774/// allow instance variables to be specified in the implementation. When 775/// specified, they need to be *identical* to the interface. Now that we 776/// have support for non-fragile ivars in ObjC 2.0, we can consider removing 777/// the legacy semantics and allow developers to move private ivar declarations 778/// from the class interface to the class implementation (but I digress:-) 779/// 780class ObjCImplementationDecl : public NamedDecl { 781 /// Class interface for this category implementation 782 ObjCInterfaceDecl *ClassInterface; 783 784 /// Implementation Class's super class. 785 ObjCInterfaceDecl *SuperClass; 786 787 /// Optional Ivars/NumIvars - This is a new[]'d array of pointers to Decls. 788 ObjCIvarDecl **Ivars; // Null if not specified 789 int NumIvars; // -1 if not defined. 790 791 /// implemented instance methods 792 llvm::SmallVector<ObjCMethodDecl*, 32> InstanceMethods; 793 794 /// implemented class methods 795 llvm::SmallVector<ObjCMethodDecl*, 32> ClassMethods; 796 797 SourceLocation EndLoc; 798public: 799 ObjCImplementationDecl(SourceLocation L, IdentifierInfo *Id, 800 ObjCInterfaceDecl *classInterface, 801 ObjCInterfaceDecl *superDecl) 802 : NamedDecl(ObjCImplementation, L, Id), 803 ClassInterface(classInterface), SuperClass(superDecl), 804 Ivars(0), NumIvars(-1) {} 805 806 void ObjCAddInstanceVariablesToClassImpl(ObjCIvarDecl **ivars, 807 unsigned numIvars); 808 809 void addInstanceMethod(ObjCMethodDecl *method) { 810 InstanceMethods.push_back(method); 811 } 812 void addClassMethod(ObjCMethodDecl *method) { 813 ClassMethods.push_back(method); 814 } 815 // Location information, modeled after the Stmt API. 816 SourceLocation getLocStart() const { return getLocation(); } 817 SourceLocation getLocEnd() const { return EndLoc; } 818 void setLocEnd(SourceLocation LE) { EndLoc = LE; }; 819 820 ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 821 ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } 822 823 void setSuperClass(ObjCInterfaceDecl * superCls) 824 { SuperClass = superCls; } 825 826 int getNumInstanceMethods() const { return InstanceMethods.size(); } 827 int getNumClassMethods() const { return ClassMethods.size(); } 828 829 int getImplDeclNumIvars() const { return NumIvars; } 830 831 832 typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator 833 instmeth_iterator; 834 instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); } 835 instmeth_iterator instmeth_end() const { return InstanceMethods.end(); } 836 837 typedef llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator 838 classmeth_iterator; 839 classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); } 840 classmeth_iterator classmeth_end() const { return ClassMethods.end(); } 841 842 // Get the instance method definition for this implementation. 843 ObjCMethodDecl *getInstanceMethod(Selector Sel); 844 845 // Get the class method definition for this implementation. 846 ObjCMethodDecl *getClassMethod(Selector Sel); 847 848 typedef ObjCIvarDecl * const *ivar_iterator; 849 ivar_iterator ivar_begin() const { return Ivars; } 850 ivar_iterator ivar_end() const {return Ivars+(NumIvars == -1 ? 0 : NumIvars);} 851 852 static bool classof(const Decl *D) { 853 return D->getKind() == ObjCImplementation; 854 } 855 static bool classof(const ObjCImplementationDecl *D) { return true; } 856}; 857 858/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is 859/// declared as @compatibility_alias alias class. 860class ObjCCompatibleAliasDecl : public ScopedDecl { 861 /// Class that this is an alias of. 862 ObjCInterfaceDecl *AliasedClass; 863 864public: 865 ObjCCompatibleAliasDecl(SourceLocation L, IdentifierInfo *Id, 866 ObjCInterfaceDecl* aliasedClass) 867 : ScopedDecl(CompatibleAlias, L, Id, 0), 868 AliasedClass(aliasedClass) {} 869 870 ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } 871 872 static bool classof(const Decl *D) { 873 return D->getKind() == CompatibleAlias; 874 } 875 static bool classof(const ObjCCompatibleAliasDecl *D) { return true; } 876 877}; 878 879class ObjCPropertyDecl : public Decl { 880public: 881 enum PropertyAttributeKind { OBJC_PR_noattr = 0x0, 882 OBJC_PR_readonly = 0x01, 883 OBJC_PR_getter = 0x02, 884 OBJC_PR_assign = 0x04, 885 OBJC_PR_readwrite = 0x08, 886 OBJC_PR_retain = 0x10, 887 OBJC_PR_copy = 0x20, 888 OBJC_PR_nonatomic = 0x40, 889 OBJC_PR_setter = 0x80 }; 890private: 891 // List of property name declarations 892 // FIXME: Property is not an ivar. 893 ObjCIvarDecl **PropertyDecls; 894 int NumPropertyDecls; 895 896 // NOTE: VC++ treats enums as signed, avoid using PropertyAttributeKind enum 897 unsigned PropertyAttributes : 8; 898 899 IdentifierInfo *GetterName; // getter name of NULL if no getter 900 IdentifierInfo *SetterName; // setter name of NULL if no setter 901 902public: 903 ObjCPropertyDecl(SourceLocation L) 904 : Decl(PropertyDecl, L), 905 PropertyDecls(0), NumPropertyDecls(-1), PropertyAttributes(OBJC_PR_noattr), 906 GetterName(0), SetterName(0) {} 907 908 ObjCIvarDecl **const getPropertyDecls() const { return PropertyDecls; } 909 void setPropertyDecls(ObjCIvarDecl **property) { PropertyDecls = property; } 910 911 const int getNumPropertyDecls() const { return NumPropertyDecls; } 912 void setNumPropertyDecls(int num) { NumPropertyDecls = num; } 913 914 const PropertyAttributeKind getPropertyAttributes() const 915 { return PropertyAttributeKind(PropertyAttributes); } 916 void setPropertyAttributes(PropertyAttributeKind PRVal) { 917 PropertyAttributes = 918 (PropertyAttributeKind) (PropertyAttributes | PRVal); 919 } 920 921 const IdentifierInfo *getGetterName() const { return GetterName; } 922 IdentifierInfo *getGetterName() { return GetterName; } 923 void setGetterName(IdentifierInfo *Id) { GetterName = Id; } 924 925 const IdentifierInfo *getSetterName() const { return SetterName; } 926 IdentifierInfo *getSetterName() { return SetterName; } 927 void setSetterName(IdentifierInfo *Id) { SetterName = Id; } 928 929 static bool classof(const Decl *D) { 930 return D->getKind() == PropertyDecl; 931 } 932 static bool classof(const ObjCPropertyDecl *D) { return true; } 933}; 934 935} // end namespace clang 936#endif 937