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