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