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