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