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