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