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