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