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