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