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