DeclObjC.h revision e61354b274ec5aa6acf3d15271896ce7596bb123
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 /// \brief Lookup a method in the classes implementation hierarchy. 943 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true); 944 945 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) { 946 return lookupPrivateMethod(Sel, false); 947 } 948 949 SourceLocation getEndOfDefinitionLoc() const { 950 if (!hasDefinition()) 951 return getLocation(); 952 953 return data().EndLoc; 954 } 955 956 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; } 957 958 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; } 959 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; } 960 961 /// isImplicitInterfaceDecl - check that this is an implicitly declared 962 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation 963 /// declaration without an \@interface declaration. 964 bool isImplicitInterfaceDecl() const { 965 return hasDefinition() ? Data->Definition->isImplicit() : isImplicit(); 966 } 967 968 /// ClassImplementsProtocol - Checks that 'lProto' protocol 969 /// has been implemented in IDecl class, its super class or categories (if 970 /// lookupCategory is true). 971 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, 972 bool lookupCategory, 973 bool RHSIsQualifiedID = false); 974 975 typedef redeclarable_base::redecl_iterator redecl_iterator; 976 using redeclarable_base::redecls_begin; 977 using redeclarable_base::redecls_end; 978 using redeclarable_base::getPreviousDecl; 979 using redeclarable_base::getMostRecentDecl; 980 981 /// Retrieves the canonical declaration of this Objective-C class. 982 ObjCInterfaceDecl *getCanonicalDecl() { 983 return getFirstDeclaration(); 984 } 985 const ObjCInterfaceDecl *getCanonicalDecl() const { 986 return getFirstDeclaration(); 987 } 988 989 // Low-level accessor 990 const Type *getTypeForDecl() const { return TypeForDecl; } 991 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; } 992 993 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 994 static bool classof(const ObjCInterfaceDecl *D) { return true; } 995 static bool classofKind(Kind K) { return K == ObjCInterface; } 996 997 friend class ASTReader; 998 friend class ASTDeclReader; 999 friend class ASTDeclWriter; 1000}; 1001 1002/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC 1003/// instance variables are identical to C. The only exception is Objective-C 1004/// supports C++ style access control. For example: 1005/// 1006/// \@interface IvarExample : NSObject 1007/// { 1008/// id defaultToProtected; 1009/// \@public: 1010/// id canBePublic; // same as C++. 1011/// \@protected: 1012/// id canBeProtected; // same as C++. 1013/// \@package: 1014/// id canBePackage; // framework visibility (not available in C++). 1015/// } 1016/// 1017class ObjCIvarDecl : public FieldDecl { 1018 virtual void anchor(); 1019 1020public: 1021 enum AccessControl { 1022 None, Private, Protected, Public, Package 1023 }; 1024 1025private: 1026 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc, 1027 SourceLocation IdLoc, IdentifierInfo *Id, 1028 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW, 1029 bool synthesized) 1030 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, 1031 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit), 1032 NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {} 1033 1034public: 1035 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC, 1036 SourceLocation StartLoc, SourceLocation IdLoc, 1037 IdentifierInfo *Id, QualType T, 1038 TypeSourceInfo *TInfo, 1039 AccessControl ac, Expr *BW = NULL, 1040 bool synthesized=false); 1041 1042 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1043 1044 /// \brief Return the class interface that this ivar is logically contained 1045 /// in; this is either the interface where the ivar was declared, or the 1046 /// interface the ivar is conceptually a part of in the case of synthesized 1047 /// ivars. 1048 const ObjCInterfaceDecl *getContainingInterface() const; 1049 1050 ObjCIvarDecl *getNextIvar() { return NextIvar; } 1051 const ObjCIvarDecl *getNextIvar() const { return NextIvar; } 1052 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; } 1053 1054 void setAccessControl(AccessControl ac) { DeclAccess = ac; } 1055 1056 AccessControl getAccessControl() const { return AccessControl(DeclAccess); } 1057 1058 AccessControl getCanonicalAccessControl() const { 1059 return DeclAccess == None ? Protected : AccessControl(DeclAccess); 1060 } 1061 1062 void setSynthesize(bool synth) { Synthesized = synth; } 1063 bool getSynthesize() const { return Synthesized; } 1064 1065 // Implement isa/cast/dyncast/etc. 1066 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1067 static bool classof(const ObjCIvarDecl *D) { return true; } 1068 static bool classofKind(Kind K) { return K == ObjCIvar; } 1069private: 1070 /// NextIvar - Next Ivar in the list of ivars declared in class; class's 1071 /// extensions and class's implementation 1072 ObjCIvarDecl *NextIvar; 1073 1074 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum 1075 unsigned DeclAccess : 3; 1076 unsigned Synthesized : 1; 1077}; 1078 1079 1080/// \brief Represents a field declaration created by an \@defs(...). 1081class ObjCAtDefsFieldDecl : public FieldDecl { 1082 virtual void anchor(); 1083 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc, 1084 SourceLocation IdLoc, IdentifierInfo *Id, 1085 QualType T, Expr *BW) 1086 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T, 1087 /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ? 1088 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {} 1089 1090public: 1091 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC, 1092 SourceLocation StartLoc, 1093 SourceLocation IdLoc, IdentifierInfo *Id, 1094 QualType T, Expr *BW); 1095 1096 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1097 1098 // Implement isa/cast/dyncast/etc. 1099 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1100 static bool classof(const ObjCAtDefsFieldDecl *D) { return true; } 1101 static bool classofKind(Kind K) { return K == ObjCAtDefsField; } 1102}; 1103 1104/// \brief Represents an Objective-C protocol declaration. 1105/// 1106/// Objective-C protocols declare a pure abstract type (i.e., no instance 1107/// variables are permitted). Protocols originally drew inspiration from 1108/// C++ pure virtual functions (a C++ feature with nice semantics and lousy 1109/// syntax:-). Here is an example: 1110/// 1111/// \code 1112/// \@protocol NSDraggingInfo <refproto1, refproto2> 1113/// - (NSWindow *)draggingDestinationWindow; 1114/// - (NSImage *)draggedImage; 1115/// \@end 1116/// \endcode 1117/// 1118/// This says that NSDraggingInfo requires two methods and requires everything 1119/// that the two "referenced protocols" 'refproto1' and 'refproto2' require as 1120/// well. 1121/// 1122/// \code 1123/// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo> 1124/// \@end 1125/// \endcode 1126/// 1127/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and 1128/// protocols are in distinct namespaces. For example, Cocoa defines both 1129/// an NSObject protocol and class (which isn't allowed in Java). As a result, 1130/// protocols are referenced using angle brackets as follows: 1131/// 1132/// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo; 1133/// 1134class ObjCProtocolDecl : public ObjCContainerDecl, 1135 public Redeclarable<ObjCProtocolDecl> { 1136 virtual void anchor(); 1137 1138 struct DefinitionData { 1139 // \brief The declaration that defines this protocol. 1140 ObjCProtocolDecl *Definition; 1141 1142 /// \brief Referenced protocols 1143 ObjCProtocolList ReferencedProtocols; 1144 }; 1145 1146 DefinitionData *Data; 1147 1148 DefinitionData &data() const { 1149 assert(Data && "Objective-C protocol has no definition!"); 1150 return *Data; 1151 } 1152 1153 ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id, 1154 SourceLocation nameLoc, SourceLocation atStartLoc, 1155 ObjCProtocolDecl *PrevDecl); 1156 1157 void allocateDefinitionData(); 1158 1159 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base; 1160 virtual ObjCProtocolDecl *getNextRedeclaration() { 1161 return RedeclLink.getNext(); 1162 } 1163 virtual ObjCProtocolDecl *getPreviousDeclImpl() { 1164 return getPreviousDecl(); 1165 } 1166 virtual ObjCProtocolDecl *getMostRecentDeclImpl() { 1167 return getMostRecentDecl(); 1168 } 1169 1170public: 1171 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC, 1172 IdentifierInfo *Id, 1173 SourceLocation nameLoc, 1174 SourceLocation atStartLoc, 1175 ObjCProtocolDecl *PrevDecl); 1176 1177 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1178 1179 const ObjCProtocolList &getReferencedProtocols() const { 1180 assert(hasDefinition() && "No definition available!"); 1181 return data().ReferencedProtocols; 1182 } 1183 typedef ObjCProtocolList::iterator protocol_iterator; 1184 protocol_iterator protocol_begin() const { 1185 if (!hasDefinition()) 1186 return protocol_iterator(); 1187 1188 return data().ReferencedProtocols.begin(); 1189 } 1190 protocol_iterator protocol_end() const { 1191 if (!hasDefinition()) 1192 return protocol_iterator(); 1193 1194 return data().ReferencedProtocols.end(); 1195 } 1196 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 1197 protocol_loc_iterator protocol_loc_begin() const { 1198 if (!hasDefinition()) 1199 return protocol_loc_iterator(); 1200 1201 return data().ReferencedProtocols.loc_begin(); 1202 } 1203 protocol_loc_iterator protocol_loc_end() const { 1204 if (!hasDefinition()) 1205 return protocol_loc_iterator(); 1206 1207 return data().ReferencedProtocols.loc_end(); 1208 } 1209 unsigned protocol_size() const { 1210 if (!hasDefinition()) 1211 return 0; 1212 1213 return data().ReferencedProtocols.size(); 1214 } 1215 1216 /// setProtocolList - Set the list of protocols that this interface 1217 /// implements. 1218 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 1219 const SourceLocation *Locs, ASTContext &C) { 1220 assert(Data && "Protocol is not defined"); 1221 data().ReferencedProtocols.set(List, Num, Locs, C); 1222 } 1223 1224 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName); 1225 1226 // Lookup a method. First, we search locally. If a method isn't 1227 // found, we search referenced protocols and class categories. 1228 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const; 1229 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 1230 return lookupMethod(Sel, true/*isInstance*/); 1231 } 1232 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 1233 return lookupMethod(Sel, false/*isInstance*/); 1234 } 1235 1236 /// \brief Determine whether this protocol has a definition. 1237 bool hasDefinition() const { return Data != 0; } 1238 1239 /// \brief Retrieve the definition of this protocol, if any. 1240 ObjCProtocolDecl *getDefinition() { 1241 return Data? Data->Definition : 0; 1242 } 1243 1244 /// \brief Retrieve the definition of this protocol, if any. 1245 const ObjCProtocolDecl *getDefinition() const { 1246 return Data? Data->Definition : 0; 1247 } 1248 1249 /// \brief Determine whether this particular declaration is also the 1250 /// definition. 1251 bool isThisDeclarationADefinition() const { 1252 return getDefinition() == this; 1253 } 1254 1255 /// \brief Starts the definition of this Objective-C protocol. 1256 void startDefinition(); 1257 1258 virtual SourceRange getSourceRange() const LLVM_READONLY { 1259 if (isThisDeclarationADefinition()) 1260 return ObjCContainerDecl::getSourceRange(); 1261 1262 return SourceRange(getAtStartLoc(), getLocation()); 1263 } 1264 1265 typedef redeclarable_base::redecl_iterator redecl_iterator; 1266 using redeclarable_base::redecls_begin; 1267 using redeclarable_base::redecls_end; 1268 using redeclarable_base::getPreviousDecl; 1269 using redeclarable_base::getMostRecentDecl; 1270 1271 /// Retrieves the canonical declaration of this Objective-C protocol. 1272 ObjCProtocolDecl *getCanonicalDecl() { 1273 return getFirstDeclaration(); 1274 } 1275 const ObjCProtocolDecl *getCanonicalDecl() const { 1276 return getFirstDeclaration(); 1277 } 1278 1279 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1280 static bool classof(const ObjCProtocolDecl *D) { return true; } 1281 static bool classofKind(Kind K) { return K == ObjCProtocol; } 1282 1283 friend class ASTReader; 1284 friend class ASTDeclReader; 1285 friend class ASTDeclWriter; 1286}; 1287 1288/// ObjCCategoryDecl - Represents a category declaration. A category allows 1289/// you to add methods to an existing class (without subclassing or modifying 1290/// the original class interface or implementation:-). Categories don't allow 1291/// you to add instance data. The following example adds "myMethod" to all 1292/// NSView's within a process: 1293/// 1294/// \@interface NSView (MyViewMethods) 1295/// - myMethod; 1296/// \@end 1297/// 1298/// Categories also allow you to split the implementation of a class across 1299/// several files (a feature more naturally supported in C++). 1300/// 1301/// Categories were originally inspired by dynamic languages such as Common 1302/// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 1303/// don't support this level of dynamism, which is both powerful and dangerous. 1304/// 1305class ObjCCategoryDecl : public ObjCContainerDecl { 1306 virtual void anchor(); 1307 1308 /// Interface belonging to this category 1309 ObjCInterfaceDecl *ClassInterface; 1310 1311 /// referenced protocols in this category. 1312 ObjCProtocolList ReferencedProtocols; 1313 1314 /// Next category belonging to this class. 1315 /// FIXME: this should not be a singly-linked list. Move storage elsewhere. 1316 ObjCCategoryDecl *NextClassCategory; 1317 1318 /// \brief The location of the category name in this declaration. 1319 SourceLocation CategoryNameLoc; 1320 1321 /// class extension may have private ivars. 1322 SourceLocation IvarLBraceLoc; 1323 SourceLocation IvarRBraceLoc; 1324 1325 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, 1326 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, 1327 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, 1328 SourceLocation IvarLBraceLoc=SourceLocation(), 1329 SourceLocation IvarRBraceLoc=SourceLocation()) 1330 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc), 1331 ClassInterface(IDecl), NextClassCategory(0), 1332 CategoryNameLoc(CategoryNameLoc), 1333 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) { 1334 } 1335public: 1336 1337 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC, 1338 SourceLocation AtLoc, 1339 SourceLocation ClassNameLoc, 1340 SourceLocation CategoryNameLoc, 1341 IdentifierInfo *Id, 1342 ObjCInterfaceDecl *IDecl, 1343 SourceLocation IvarLBraceLoc=SourceLocation(), 1344 SourceLocation IvarRBraceLoc=SourceLocation()); 1345 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1346 1347 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 1348 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 1349 1350 ObjCCategoryImplDecl *getImplementation() const; 1351 void setImplementation(ObjCCategoryImplDecl *ImplD); 1352 1353 /// setProtocolList - Set the list of protocols that this interface 1354 /// implements. 1355 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 1356 const SourceLocation *Locs, ASTContext &C) { 1357 ReferencedProtocols.set(List, Num, Locs, C); 1358 } 1359 1360 const ObjCProtocolList &getReferencedProtocols() const { 1361 return ReferencedProtocols; 1362 } 1363 1364 typedef ObjCProtocolList::iterator protocol_iterator; 1365 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();} 1366 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); } 1367 unsigned protocol_size() const { return ReferencedProtocols.size(); } 1368 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 1369 protocol_loc_iterator protocol_loc_begin() const { 1370 return ReferencedProtocols.loc_begin(); 1371 } 1372 protocol_loc_iterator protocol_loc_end() const { 1373 return ReferencedProtocols.loc_end(); 1374 } 1375 1376 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } 1377 1378 bool IsClassExtension() const { return getIdentifier() == 0; } 1379 const ObjCCategoryDecl *getNextClassExtension() const; 1380 1381 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 1382 ivar_iterator ivar_begin() const { 1383 return ivar_iterator(decls_begin()); 1384 } 1385 ivar_iterator ivar_end() const { 1386 return ivar_iterator(decls_end()); 1387 } 1388 unsigned ivar_size() const { 1389 return std::distance(ivar_begin(), ivar_end()); 1390 } 1391 bool ivar_empty() const { 1392 return ivar_begin() == ivar_end(); 1393 } 1394 1395 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 1396 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; } 1397 1398 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 1399 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 1400 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 1401 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 1402 1403 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1404 static bool classof(const ObjCCategoryDecl *D) { return true; } 1405 static bool classofKind(Kind K) { return K == ObjCCategory; } 1406 1407 friend class ASTDeclReader; 1408 friend class ASTDeclWriter; 1409}; 1410 1411class ObjCImplDecl : public ObjCContainerDecl { 1412 virtual void anchor(); 1413 1414 /// Class interface for this class/category implementation 1415 ObjCInterfaceDecl *ClassInterface; 1416 1417protected: 1418 ObjCImplDecl(Kind DK, DeclContext *DC, 1419 ObjCInterfaceDecl *classInterface, 1420 SourceLocation nameLoc, SourceLocation atStartLoc) 1421 : ObjCContainerDecl(DK, DC, 1422 classInterface? classInterface->getIdentifier() : 0, 1423 nameLoc, atStartLoc), 1424 ClassInterface(classInterface) {} 1425 1426public: 1427 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 1428 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 1429 void setClassInterface(ObjCInterfaceDecl *IFace); 1430 1431 void addInstanceMethod(ObjCMethodDecl *method) { 1432 // FIXME: Context should be set correctly before we get here. 1433 method->setLexicalDeclContext(this); 1434 addDecl(method); 1435 } 1436 void addClassMethod(ObjCMethodDecl *method) { 1437 // FIXME: Context should be set correctly before we get here. 1438 method->setLexicalDeclContext(this); 1439 addDecl(method); 1440 } 1441 1442 void addPropertyImplementation(ObjCPropertyImplDecl *property); 1443 1444 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const; 1445 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; 1446 1447 // Iterator access to properties. 1448 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator; 1449 propimpl_iterator propimpl_begin() const { 1450 return propimpl_iterator(decls_begin()); 1451 } 1452 propimpl_iterator propimpl_end() const { 1453 return propimpl_iterator(decls_end()); 1454 } 1455 1456 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1457 static bool classof(const ObjCImplDecl *D) { return true; } 1458 static bool classofKind(Kind K) { 1459 return K >= firstObjCImpl && K <= lastObjCImpl; 1460 } 1461}; 1462 1463/// ObjCCategoryImplDecl - An object of this class encapsulates a category 1464/// \@implementation declaration. If a category class has declaration of a 1465/// property, its implementation must be specified in the category's 1466/// \@implementation declaration. Example: 1467/// \@interface I \@end 1468/// \@interface I(CATEGORY) 1469/// \@property int p1, d1; 1470/// \@end 1471/// \@implementation I(CATEGORY) 1472/// \@dynamic p1,d1; 1473/// \@end 1474/// 1475/// ObjCCategoryImplDecl 1476class ObjCCategoryImplDecl : public ObjCImplDecl { 1477 virtual void anchor(); 1478 1479 // Category name 1480 IdentifierInfo *Id; 1481 1482 // Category name location 1483 SourceLocation CategoryNameLoc; 1484 1485 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id, 1486 ObjCInterfaceDecl *classInterface, 1487 SourceLocation nameLoc, SourceLocation atStartLoc, 1488 SourceLocation CategoryNameLoc) 1489 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc), 1490 Id(Id), CategoryNameLoc(CategoryNameLoc) {} 1491public: 1492 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, 1493 IdentifierInfo *Id, 1494 ObjCInterfaceDecl *classInterface, 1495 SourceLocation nameLoc, 1496 SourceLocation atStartLoc, 1497 SourceLocation CategoryNameLoc); 1498 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1499 1500 /// getIdentifier - Get the identifier that names the category 1501 /// interface associated with this implementation. 1502 /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier() 1503 /// to mean something different. For example: 1504 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier() 1505 /// returns the class interface name, whereas 1506 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier() 1507 /// returns the category name. 1508 IdentifierInfo *getIdentifier() const { 1509 return Id; 1510 } 1511 void setIdentifier(IdentifierInfo *II) { Id = II; } 1512 1513 ObjCCategoryDecl *getCategoryDecl() const; 1514 1515 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 1516 1517 /// getName - Get the name of identifier for the class interface associated 1518 /// with this implementation as a StringRef. 1519 // 1520 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean 1521 // something different. 1522 StringRef getName() const { 1523 return Id ? Id->getNameStart() : ""; 1524 } 1525 1526 /// getNameAsCString - Get the name of identifier for the class 1527 /// interface associated with this implementation as a C string 1528 /// (const char*). 1529 // 1530 // FIXME: Deprecated, move clients to getName(). 1531 const char *getNameAsCString() const { 1532 return Id ? Id->getNameStart() : ""; 1533 } 1534 1535 /// @brief Get the name of the class associated with this interface. 1536 // 1537 // FIXME: Deprecated, move clients to getName(). 1538 std::string getNameAsString() const { 1539 return getName(); 1540 } 1541 1542 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1543 static bool classof(const ObjCCategoryImplDecl *D) { return true; } 1544 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;} 1545 1546 friend class ASTDeclReader; 1547 friend class ASTDeclWriter; 1548}; 1549 1550raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID); 1551 1552/// ObjCImplementationDecl - Represents a class definition - this is where 1553/// method definitions are specified. For example: 1554/// 1555/// @code 1556/// \@implementation MyClass 1557/// - (void)myMethod { /* do something */ } 1558/// \@end 1559/// @endcode 1560/// 1561/// Typically, instance variables are specified in the class interface, 1562/// *not* in the implementation. Nevertheless (for legacy reasons), we 1563/// allow instance variables to be specified in the implementation. When 1564/// specified, they need to be *identical* to the interface. 1565/// 1566class ObjCImplementationDecl : public ObjCImplDecl { 1567 virtual void anchor(); 1568 /// Implementation Class's super class. 1569 ObjCInterfaceDecl *SuperClass; 1570 /// \@implementation may have private ivars. 1571 SourceLocation IvarLBraceLoc; 1572 SourceLocation IvarRBraceLoc; 1573 1574 /// Support for ivar initialization. 1575 /// IvarInitializers - The arguments used to initialize the ivars 1576 CXXCtorInitializer **IvarInitializers; 1577 unsigned NumIvarInitializers; 1578 1579 /// true if class has a .cxx_[construct,destruct] method. 1580 bool HasCXXStructors : 1; 1581 1582 ObjCImplementationDecl(DeclContext *DC, 1583 ObjCInterfaceDecl *classInterface, 1584 ObjCInterfaceDecl *superDecl, 1585 SourceLocation nameLoc, SourceLocation atStartLoc, 1586 SourceLocation IvarLBraceLoc=SourceLocation(), 1587 SourceLocation IvarRBraceLoc=SourceLocation()) 1588 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc), 1589 SuperClass(superDecl), IvarLBraceLoc(IvarLBraceLoc), 1590 IvarRBraceLoc(IvarRBraceLoc), 1591 IvarInitializers(0), NumIvarInitializers(0), 1592 HasCXXStructors(false) {} 1593public: 1594 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, 1595 ObjCInterfaceDecl *classInterface, 1596 ObjCInterfaceDecl *superDecl, 1597 SourceLocation nameLoc, 1598 SourceLocation atStartLoc, 1599 SourceLocation IvarLBraceLoc=SourceLocation(), 1600 SourceLocation IvarRBraceLoc=SourceLocation()); 1601 1602 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1603 1604 /// init_iterator - Iterates through the ivar initializer list. 1605 typedef CXXCtorInitializer **init_iterator; 1606 1607 /// init_const_iterator - Iterates through the ivar initializer list. 1608 typedef CXXCtorInitializer * const * init_const_iterator; 1609 1610 /// init_begin() - Retrieve an iterator to the first initializer. 1611 init_iterator init_begin() { return IvarInitializers; } 1612 /// begin() - Retrieve an iterator to the first initializer. 1613 init_const_iterator init_begin() const { return IvarInitializers; } 1614 1615 /// init_end() - Retrieve an iterator past the last initializer. 1616 init_iterator init_end() { 1617 return IvarInitializers + NumIvarInitializers; 1618 } 1619 /// end() - Retrieve an iterator past the last initializer. 1620 init_const_iterator init_end() const { 1621 return IvarInitializers + NumIvarInitializers; 1622 } 1623 /// getNumArgs - Number of ivars which must be initialized. 1624 unsigned getNumIvarInitializers() const { 1625 return NumIvarInitializers; 1626 } 1627 1628 void setNumIvarInitializers(unsigned numNumIvarInitializers) { 1629 NumIvarInitializers = numNumIvarInitializers; 1630 } 1631 1632 void setIvarInitializers(ASTContext &C, 1633 CXXCtorInitializer ** initializers, 1634 unsigned numInitializers); 1635 1636 bool hasCXXStructors() const { return HasCXXStructors; } 1637 void setHasCXXStructors(bool val) { HasCXXStructors = val; } 1638 1639 /// getIdentifier - Get the identifier that names the class 1640 /// interface associated with this implementation. 1641 IdentifierInfo *getIdentifier() const { 1642 return getClassInterface()->getIdentifier(); 1643 } 1644 1645 /// getName - Get the name of identifier for the class interface associated 1646 /// with this implementation as a StringRef. 1647 // 1648 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean 1649 // something different. 1650 StringRef getName() const { 1651 assert(getIdentifier() && "Name is not a simple identifier"); 1652 return getIdentifier()->getName(); 1653 } 1654 1655 /// getNameAsCString - Get the name of identifier for the class 1656 /// interface associated with this implementation as a C string 1657 /// (const char*). 1658 // 1659 // FIXME: Move to StringRef API. 1660 const char *getNameAsCString() const { 1661 return getName().data(); 1662 } 1663 1664 /// @brief Get the name of the class associated with this interface. 1665 // 1666 // FIXME: Move to StringRef API. 1667 std::string getNameAsString() const { 1668 return getName(); 1669 } 1670 1671 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } 1672 ObjCInterfaceDecl *getSuperClass() { return SuperClass; } 1673 1674 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } 1675 1676 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 1677 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 1678 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 1679 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 1680 1681 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 1682 ivar_iterator ivar_begin() const { 1683 return ivar_iterator(decls_begin()); 1684 } 1685 ivar_iterator ivar_end() const { 1686 return ivar_iterator(decls_end()); 1687 } 1688 unsigned ivar_size() const { 1689 return std::distance(ivar_begin(), ivar_end()); 1690 } 1691 bool ivar_empty() const { 1692 return ivar_begin() == ivar_end(); 1693 } 1694 1695 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1696 static bool classof(const ObjCImplementationDecl *D) { return true; } 1697 static bool classofKind(Kind K) { return K == ObjCImplementation; } 1698 1699 friend class ASTDeclReader; 1700 friend class ASTDeclWriter; 1701}; 1702 1703raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID); 1704 1705/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is 1706/// declared as \@compatibility_alias alias class. 1707class ObjCCompatibleAliasDecl : public NamedDecl { 1708 virtual void anchor(); 1709 /// Class that this is an alias of. 1710 ObjCInterfaceDecl *AliasedClass; 1711 1712 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 1713 ObjCInterfaceDecl* aliasedClass) 1714 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {} 1715public: 1716 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC, 1717 SourceLocation L, IdentifierInfo *Id, 1718 ObjCInterfaceDecl* aliasedClass); 1719 1720 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, 1721 unsigned ID); 1722 1723 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } 1724 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; } 1725 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; } 1726 1727 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1728 static bool classof(const ObjCCompatibleAliasDecl *D) { return true; } 1729 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; } 1730 1731}; 1732 1733/// \brief Represents one property declaration in an Objective-C interface. 1734/// 1735/// For example: 1736/// \code{.mm} 1737/// \@property (assign, readwrite) int MyProperty; 1738/// \endcode 1739class ObjCPropertyDecl : public NamedDecl { 1740 virtual void anchor(); 1741public: 1742 enum PropertyAttributeKind { 1743 OBJC_PR_noattr = 0x00, 1744 OBJC_PR_readonly = 0x01, 1745 OBJC_PR_getter = 0x02, 1746 OBJC_PR_assign = 0x04, 1747 OBJC_PR_readwrite = 0x08, 1748 OBJC_PR_retain = 0x10, 1749 OBJC_PR_copy = 0x20, 1750 OBJC_PR_nonatomic = 0x40, 1751 OBJC_PR_setter = 0x80, 1752 OBJC_PR_atomic = 0x100, 1753 OBJC_PR_weak = 0x200, 1754 OBJC_PR_strong = 0x400, 1755 OBJC_PR_unsafe_unretained = 0x800 1756 // Adding a property should change NumPropertyAttrsBits 1757 }; 1758 1759 enum { 1760 /// \brief Number of bits fitting all the property attributes. 1761 NumPropertyAttrsBits = 12 1762 }; 1763 1764 enum SetterKind { Assign, Retain, Copy, Weak }; 1765 enum PropertyControl { None, Required, Optional }; 1766private: 1767 SourceLocation AtLoc; // location of \@property 1768 SourceLocation LParenLoc; // location of '(' starting attribute list or null. 1769 TypeSourceInfo *DeclType; 1770 unsigned PropertyAttributes : NumPropertyAttrsBits; 1771 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits; 1772 // \@required/\@optional 1773 unsigned PropertyImplementation : 2; 1774 1775 Selector GetterName; // getter name of NULL if no getter 1776 Selector SetterName; // setter name of NULL if no setter 1777 1778 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method 1779 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method 1780 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property 1781 1782 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 1783 SourceLocation AtLocation, SourceLocation LParenLocation, 1784 TypeSourceInfo *T) 1785 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), 1786 LParenLoc(LParenLocation), DeclType(T), 1787 PropertyAttributes(OBJC_PR_noattr), 1788 PropertyAttributesAsWritten(OBJC_PR_noattr), 1789 PropertyImplementation(None), 1790 GetterName(Selector()), 1791 SetterName(Selector()), 1792 GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {} 1793public: 1794 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC, 1795 SourceLocation L, 1796 IdentifierInfo *Id, SourceLocation AtLocation, 1797 SourceLocation LParenLocation, 1798 TypeSourceInfo *T, 1799 PropertyControl propControl = None); 1800 1801 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1802 1803 SourceLocation getAtLoc() const { return AtLoc; } 1804 void setAtLoc(SourceLocation L) { AtLoc = L; } 1805 1806 SourceLocation getLParenLoc() const { return LParenLoc; } 1807 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1808 1809 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; } 1810 QualType getType() const { return DeclType->getType(); } 1811 void setType(TypeSourceInfo *T) { DeclType = T; } 1812 1813 PropertyAttributeKind getPropertyAttributes() const { 1814 return PropertyAttributeKind(PropertyAttributes); 1815 } 1816 void setPropertyAttributes(PropertyAttributeKind PRVal) { 1817 PropertyAttributes |= PRVal; 1818 } 1819 1820 PropertyAttributeKind getPropertyAttributesAsWritten() const { 1821 return PropertyAttributeKind(PropertyAttributesAsWritten); 1822 } 1823 1824 bool hasWrittenStorageAttribute() const { 1825 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy | 1826 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong | 1827 OBJC_PR_weak); 1828 } 1829 1830 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) { 1831 PropertyAttributesAsWritten = PRVal; 1832 } 1833 1834 void makeitReadWriteAttribute(void) { 1835 PropertyAttributes &= ~OBJC_PR_readonly; 1836 PropertyAttributes |= OBJC_PR_readwrite; 1837 } 1838 1839 // Helper methods for accessing attributes. 1840 1841 /// isReadOnly - Return true iff the property has a setter. 1842 bool isReadOnly() const { 1843 return (PropertyAttributes & OBJC_PR_readonly); 1844 } 1845 1846 /// isAtomic - Return true if the property is atomic. 1847 bool isAtomic() const { 1848 return (PropertyAttributes & OBJC_PR_atomic); 1849 } 1850 1851 /// isRetaining - Return true if the property retains its value. 1852 bool isRetaining() const { 1853 return (PropertyAttributes & 1854 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy)); 1855 } 1856 1857 /// getSetterKind - Return the method used for doing assignment in 1858 /// the property setter. This is only valid if the property has been 1859 /// defined to have a setter. 1860 SetterKind getSetterKind() const { 1861 if (PropertyAttributes & OBJC_PR_strong) 1862 return getType()->isBlockPointerType() ? Copy : Retain; 1863 if (PropertyAttributes & OBJC_PR_retain) 1864 return Retain; 1865 if (PropertyAttributes & OBJC_PR_copy) 1866 return Copy; 1867 if (PropertyAttributes & OBJC_PR_weak) 1868 return Weak; 1869 return Assign; 1870 } 1871 1872 Selector getGetterName() const { return GetterName; } 1873 void setGetterName(Selector Sel) { GetterName = Sel; } 1874 1875 Selector getSetterName() const { return SetterName; } 1876 void setSetterName(Selector Sel) { SetterName = Sel; } 1877 1878 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } 1879 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; } 1880 1881 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } 1882 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; } 1883 1884 // Related to \@optional/\@required declared in \@protocol 1885 void setPropertyImplementation(PropertyControl pc) { 1886 PropertyImplementation = pc; 1887 } 1888 PropertyControl getPropertyImplementation() const { 1889 return PropertyControl(PropertyImplementation); 1890 } 1891 1892 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) { 1893 PropertyIvarDecl = Ivar; 1894 } 1895 ObjCIvarDecl *getPropertyIvarDecl() const { 1896 return PropertyIvarDecl; 1897 } 1898 1899 virtual SourceRange getSourceRange() const LLVM_READONLY { 1900 return SourceRange(AtLoc, getLocation()); 1901 } 1902 1903 /// Lookup a property by name in the specified DeclContext. 1904 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC, 1905 IdentifierInfo *propertyID); 1906 1907 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1908 static bool classof(const ObjCPropertyDecl *D) { return true; } 1909 static bool classofKind(Kind K) { return K == ObjCProperty; } 1910}; 1911 1912/// ObjCPropertyImplDecl - Represents implementation declaration of a property 1913/// in a class or category implementation block. For example: 1914/// \@synthesize prop1 = ivar1; 1915/// 1916class ObjCPropertyImplDecl : public Decl { 1917public: 1918 enum Kind { 1919 Synthesize, 1920 Dynamic 1921 }; 1922private: 1923 SourceLocation AtLoc; // location of \@synthesize or \@dynamic 1924 1925 /// \brief For \@synthesize, the location of the ivar, if it was written in 1926 /// the source code. 1927 /// 1928 /// \code 1929 /// \@synthesize int a = b 1930 /// \endcode 1931 SourceLocation IvarLoc; 1932 1933 /// Property declaration being implemented 1934 ObjCPropertyDecl *PropertyDecl; 1935 1936 /// Null for \@dynamic. Required for \@synthesize. 1937 ObjCIvarDecl *PropertyIvarDecl; 1938 1939 /// Null for \@dynamic. Non-null if property must be copy-constructed in 1940 /// getter. 1941 Expr *GetterCXXConstructor; 1942 1943 /// Null for \@dynamic. Non-null if property has assignment operator to call 1944 /// in Setter synthesis. 1945 Expr *SetterCXXAssignment; 1946 1947 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L, 1948 ObjCPropertyDecl *property, 1949 Kind PK, 1950 ObjCIvarDecl *ivarDecl, 1951 SourceLocation ivarLoc) 1952 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc), 1953 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl), 1954 GetterCXXConstructor(0), SetterCXXAssignment(0) { 1955 assert (PK == Dynamic || PropertyIvarDecl); 1956 } 1957 1958public: 1959 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC, 1960 SourceLocation atLoc, SourceLocation L, 1961 ObjCPropertyDecl *property, 1962 Kind PK, 1963 ObjCIvarDecl *ivarDecl, 1964 SourceLocation ivarLoc); 1965 1966 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1967 1968 virtual SourceRange getSourceRange() const LLVM_READONLY; 1969 1970 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; } 1971 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 1972 1973 ObjCPropertyDecl *getPropertyDecl() const { 1974 return PropertyDecl; 1975 } 1976 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; } 1977 1978 Kind getPropertyImplementation() const { 1979 return PropertyIvarDecl ? Synthesize : Dynamic; 1980 } 1981 1982 ObjCIvarDecl *getPropertyIvarDecl() const { 1983 return PropertyIvarDecl; 1984 } 1985 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; } 1986 1987 void setPropertyIvarDecl(ObjCIvarDecl *Ivar, 1988 SourceLocation IvarLoc) { 1989 PropertyIvarDecl = Ivar; 1990 this->IvarLoc = IvarLoc; 1991 } 1992 1993 /// \brief For \@synthesize, returns true if an ivar name was explicitly 1994 /// specified. 1995 /// 1996 /// \code 1997 /// \@synthesize int a = b; // true 1998 /// \@synthesize int a; // false 1999 /// \endcode 2000 bool isIvarNameSpecified() const { 2001 return IvarLoc.isValid() && IvarLoc != getLocation(); 2002 } 2003 2004 Expr *getGetterCXXConstructor() const { 2005 return GetterCXXConstructor; 2006 } 2007 void setGetterCXXConstructor(Expr *getterCXXConstructor) { 2008 GetterCXXConstructor = getterCXXConstructor; 2009 } 2010 2011 Expr *getSetterCXXAssignment() const { 2012 return SetterCXXAssignment; 2013 } 2014 void setSetterCXXAssignment(Expr *setterCXXAssignment) { 2015 SetterCXXAssignment = setterCXXAssignment; 2016 } 2017 2018 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2019 static bool classof(const ObjCPropertyImplDecl *D) { return true; } 2020 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; } 2021 2022 friend class ASTDeclReader; 2023}; 2024 2025} // end namespace clang 2026#endif 2027