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