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 ObjCListBase(const ObjCListBase &) LLVM_DELETED_FUNCTION; 37 void operator=(const ObjCListBase &) LLVM_DELETED_FUNCTION; 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: 44 ObjCListBase() : List(nullptr), NumElts(0) {} 45 unsigned size() const { return NumElts; } 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: 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. 76 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> { 77 SourceLocation *Locations; 78 79 using ObjCList<ObjCProtocolDecl>::set; 80 81 public: 82 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(nullptr) { } 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 /// 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 /// True if this method is the getter or setter for an explicit property. 127 unsigned IsPropertyAccessor : 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 /// \brief Indicates if the method was a definition but its body was skipped. 163 unsigned HasSkippedBody : 1; 164 165 // Return type of this method. 166 QualType MethodDeclType; 167 168 // Type source information for the return type. 169 TypeSourceInfo *ReturnTInfo; 170 171 /// \brief Array of ParmVarDecls for the formal parameters of this method 172 /// and optionally followed by selector locations. 173 void *ParamsAndSelLocs; 174 unsigned NumParams; 175 176 /// List of attributes for this method declaration. 177 SourceLocation DeclEndLoc; // the location of the ';' or '{'. 178 179 // The following are only used for method definitions, null otherwise. 180 LazyDeclStmtPtr Body; 181 182 /// SelfDecl - Decl for the implicit self parameter. This is lazily 183 /// constructed by createImplicitParams. 184 ImplicitParamDecl *SelfDecl; 185 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily 186 /// constructed by createImplicitParams. 187 ImplicitParamDecl *CmdDecl; 188 189 SelectorLocationsKind getSelLocsKind() const { 190 return (SelectorLocationsKind)SelLocsKind; 191 } 192 bool hasStandardSelLocs() const { 193 return getSelLocsKind() != SelLoc_NonStandard; 194 } 195 196 /// \brief Get a pointer to the stored selector identifiers locations array. 197 /// No locations will be stored if HasStandardSelLocs is true. 198 SourceLocation *getStoredSelLocs() { 199 return reinterpret_cast<SourceLocation*>(getParams() + NumParams); 200 } 201 const SourceLocation *getStoredSelLocs() const { 202 return reinterpret_cast<const SourceLocation*>(getParams() + NumParams); 203 } 204 205 /// \brief Get a pointer to the stored selector identifiers locations array. 206 /// No locations will be stored if HasStandardSelLocs is true. 207 ParmVarDecl **getParams() { 208 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs); 209 } 210 const ParmVarDecl *const *getParams() const { 211 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs); 212 } 213 214 /// \brief Get the number of stored selector identifiers locations. 215 /// No locations will be stored if HasStandardSelLocs is true. 216 unsigned getNumStoredSelLocs() const { 217 if (hasStandardSelLocs()) 218 return 0; 219 return getNumSelectorLocs(); 220 } 221 222 void setParamsAndSelLocs(ASTContext &C, 223 ArrayRef<ParmVarDecl*> Params, 224 ArrayRef<SourceLocation> SelLocs); 225 226 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc, 227 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, 228 DeclContext *contextDecl, bool isInstance = true, 229 bool isVariadic = false, bool isPropertyAccessor = false, 230 bool isImplicitlyDeclared = false, bool isDefined = false, 231 ImplementationControl impControl = None, 232 bool HasRelatedResultType = false) 233 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo), 234 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily), 235 IsInstance(isInstance), IsVariadic(isVariadic), 236 IsPropertyAccessor(isPropertyAccessor), IsDefined(isDefined), 237 IsRedeclaration(0), HasRedeclaration(0), DeclImplementation(impControl), 238 objcDeclQualifier(OBJC_TQ_None), 239 RelatedResultType(HasRelatedResultType), 240 SelLocsKind(SelLoc_StandardNoSpace), IsOverriding(0), HasSkippedBody(0), 241 MethodDeclType(T), ReturnTInfo(ReturnTInfo), ParamsAndSelLocs(nullptr), 242 NumParams(0), DeclEndLoc(endLoc), Body(), SelfDecl(nullptr), 243 CmdDecl(nullptr) { 244 setImplicit(isImplicitlyDeclared); 245 } 246 247 /// \brief A definition will return its interface declaration. 248 /// An interface declaration will return its definition. 249 /// Otherwise it will return itself. 250 ObjCMethodDecl *getNextRedeclarationImpl() override; 251 252 public: 253 static ObjCMethodDecl * 254 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, 255 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, 256 DeclContext *contextDecl, bool isInstance = true, 257 bool isVariadic = false, bool isPropertyAccessor = false, 258 bool isImplicitlyDeclared = false, bool isDefined = false, 259 ImplementationControl impControl = None, 260 bool HasRelatedResultType = false); 261 262 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID); 263 264 ObjCMethodDecl *getCanonicalDecl() override; 265 const ObjCMethodDecl *getCanonicalDecl() const { 266 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl(); 267 } 268 269 ObjCDeclQualifier getObjCDeclQualifier() const { 270 return ObjCDeclQualifier(objcDeclQualifier); 271 } 272 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; } 273 274 /// \brief Determine whether this method has a result type that is related 275 /// to the message receiver's type. 276 bool hasRelatedResultType() const { return RelatedResultType; } 277 278 /// \brief Note whether this method has a related result type. 279 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; } 280 281 /// \brief True if this is a method redeclaration in the same interface. 282 bool isRedeclaration() const { return IsRedeclaration; } 283 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod); 284 285 /// \brief Returns the location where the declarator ends. It will be 286 /// the location of ';' for a method declaration and the location of '{' 287 /// for a method definition. 288 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; } 289 290 // Location information, modeled after the Stmt API. 291 SourceLocation getLocStart() const LLVM_READONLY { return getLocation(); } 292 SourceLocation getLocEnd() const LLVM_READONLY; 293 SourceRange getSourceRange() const override LLVM_READONLY { 294 return SourceRange(getLocation(), getLocEnd()); 295 } 296 297 SourceLocation getSelectorStartLoc() const { 298 if (isImplicit()) 299 return getLocStart(); 300 return getSelectorLoc(0); 301 } 302 SourceLocation getSelectorLoc(unsigned Index) const { 303 assert(Index < getNumSelectorLocs() && "Index out of range!"); 304 if (hasStandardSelLocs()) 305 return getStandardSelectorLoc(Index, getSelector(), 306 getSelLocsKind() == SelLoc_StandardWithSpace, 307 parameters(), 308 DeclEndLoc); 309 return getStoredSelLocs()[Index]; 310 } 311 312 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const; 313 314 unsigned getNumSelectorLocs() const { 315 if (isImplicit()) 316 return 0; 317 Selector Sel = getSelector(); 318 if (Sel.isUnarySelector()) 319 return 1; 320 return Sel.getNumArgs(); 321 } 322 323 ObjCInterfaceDecl *getClassInterface(); 324 const ObjCInterfaceDecl *getClassInterface() const { 325 return const_cast<ObjCMethodDecl*>(this)->getClassInterface(); 326 } 327 328 Selector getSelector() const { return getDeclName().getObjCSelector(); } 329 330 QualType getReturnType() const { return MethodDeclType; } 331 void setReturnType(QualType T) { MethodDeclType = T; } 332 333 /// \brief Determine the type of an expression that sends a message to this 334 /// function. 335 QualType getSendResultType() const { 336 return getReturnType().getNonLValueExprType(getASTContext()); 337 } 338 339 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; } 340 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; } 341 342 // Iterator access to formal parameters. 343 unsigned param_size() const { return NumParams; } 344 typedef const ParmVarDecl *const *param_const_iterator; 345 typedef ParmVarDecl *const *param_iterator; 346 typedef llvm::iterator_range<param_iterator> param_range; 347 typedef llvm::iterator_range<param_const_iterator> param_const_range; 348 349 param_range params() { return param_range(param_begin(), param_end()); } 350 param_const_range params() const { 351 return param_const_range(param_begin(), param_end()); 352 } 353 354 param_const_iterator param_begin() const { 355 return param_const_iterator(getParams()); 356 } 357 param_const_iterator param_end() const { 358 return param_const_iterator(getParams() + NumParams); 359 } 360 param_iterator param_begin() { return param_iterator(getParams()); } 361 param_iterator param_end() { return param_iterator(getParams() + NumParams); } 362 363 // This method returns and of the parameters which are part of the selector 364 // name mangling requirements. 365 param_const_iterator sel_param_end() const { 366 return param_begin() + getSelector().getNumArgs(); 367 } 368 369 // ArrayRef access to formal parameters. This should eventually 370 // replace the iterator interface above. 371 ArrayRef<ParmVarDecl*> parameters() const { 372 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()), 373 NumParams); 374 } 375 376 /// \brief Sets the method's parameters and selector source locations. 377 /// If the method is implicit (not coming from source) \p SelLocs is 378 /// ignored. 379 void setMethodParams(ASTContext &C, 380 ArrayRef<ParmVarDecl*> Params, 381 ArrayRef<SourceLocation> SelLocs = 382 ArrayRef<SourceLocation>()); 383 384 // Iterator access to parameter types. 385 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun; 386 typedef llvm::mapped_iterator<param_const_iterator, deref_fun> 387 param_type_iterator; 388 389 param_type_iterator param_type_begin() const { 390 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType)); 391 } 392 param_type_iterator param_type_end() const { 393 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType)); 394 } 395 396 /// createImplicitParams - Used to lazily create the self and cmd 397 /// implict parameters. This must be called prior to using getSelfDecl() 398 /// or getCmdDecl(). The call is ignored if the implicit paramters 399 /// have already been created. 400 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID); 401 402 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; } 403 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; } 404 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; } 405 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; } 406 407 /// Determines the family of this method. 408 ObjCMethodFamily getMethodFamily() const; 409 410 bool isInstanceMethod() const { return IsInstance; } 411 void setInstanceMethod(bool isInst) { IsInstance = isInst; } 412 bool isVariadic() const { return IsVariadic; } 413 void setVariadic(bool isVar) { IsVariadic = isVar; } 414 415 bool isClassMethod() const { return !IsInstance; } 416 417 bool isPropertyAccessor() const { return IsPropertyAccessor; } 418 void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; } 419 420 bool isDefined() const { return IsDefined; } 421 void setDefined(bool isDefined) { IsDefined = isDefined; } 422 423 /// \brief Whether this method overrides any other in the class hierarchy. 424 /// 425 /// A method is said to override any method in the class's 426 /// base classes, its protocols, or its categories' protocols, that has 427 /// the same selector and is of the same kind (class or instance). 428 /// A method in an implementation is not considered as overriding the same 429 /// method in the interface or its categories. 430 bool isOverriding() const { return IsOverriding; } 431 void setOverriding(bool isOverriding) { IsOverriding = isOverriding; } 432 433 /// \brief Return overridden methods for the given \p Method. 434 /// 435 /// An ObjC method is considered to override any method in the class's 436 /// base classes (and base's categories), its protocols, or its categories' 437 /// protocols, that has 438 /// the same selector and is of the same kind (class or instance). 439 /// A method in an implementation is not considered as overriding the same 440 /// method in the interface or its categories. 441 void getOverriddenMethods( 442 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const; 443 444 /// \brief True if the method was a definition but its body was skipped. 445 bool hasSkippedBody() const { return HasSkippedBody; } 446 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; } 447 448 /// \brief Returns the property associated with this method's selector. 449 /// 450 /// Note that even if this particular method is not marked as a property 451 /// accessor, it is still possible for it to match a property declared in a 452 /// superclass. Pass \c false if you only want to check the current class. 453 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const; 454 455 // Related to protocols declared in \@protocol 456 void setDeclImplementation(ImplementationControl ic) { 457 DeclImplementation = ic; 458 } 459 ImplementationControl getImplementationControl() const { 460 return ImplementationControl(DeclImplementation); 461 } 462 463 /// Returns true if this specific method declaration is marked with the 464 /// designated initializer attribute. 465 bool isThisDeclarationADesignatedInitializer() const; 466 467 /// Returns true if the method selector resolves to a designated initializer 468 /// in the class's interface. 469 /// 470 /// \param InitMethod if non-null and the function returns true, it receives 471 /// the method declaration that was marked with the designated initializer 472 /// attribute. 473 bool isDesignatedInitializerForTheInterface( 474 const ObjCMethodDecl **InitMethod = nullptr) const; 475 476 /// \brief Determine whether this method has a body. 477 bool hasBody() const override { return Body.isValid(); } 478 479 /// \brief Retrieve the body of this method, if it has one. 480 Stmt *getBody() const override; 481 482 void setLazyBody(uint64_t Offset) { Body = Offset; } 483 484 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); } 485 void setBody(Stmt *B) { Body = B; } 486 487 /// \brief Returns whether this specific method is a definition. 488 bool isThisDeclarationADefinition() const { return hasBody(); } 489 490 // Implement isa/cast/dyncast/etc. 491 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 492 static bool classofKind(Kind K) { return K == ObjCMethod; } 493 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) { 494 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D)); 495 } 496 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) { 497 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC)); 498 } 499 500 friend class ASTDeclReader; 501 friend class ASTDeclWriter; 502 }; 503 504 /// ObjCContainerDecl - Represents a container for method declarations. 505 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl, 506 /// ObjCProtocolDecl, and ObjCImplDecl. 507 /// 508 class ObjCContainerDecl : public NamedDecl, public DeclContext { 509 void anchor() override; 510 511 SourceLocation AtStart; 512 513 // These two locations in the range mark the end of the method container. 514 // The first points to the '@' token, and the second to the 'end' token. 515 SourceRange AtEnd; 516 public: 517 518 ObjCContainerDecl(Kind DK, DeclContext *DC, 519 IdentifierInfo *Id, SourceLocation nameLoc, 520 SourceLocation atStartLoc) 521 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {} 522 523 // Iterator access to properties. 524 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator; 525 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>> 526 prop_range; 527 528 prop_range properties() const { return prop_range(prop_begin(), prop_end()); } 529 prop_iterator prop_begin() const { 530 return prop_iterator(decls_begin()); 531 } 532 prop_iterator prop_end() const { 533 return prop_iterator(decls_end()); 534 } 535 536 // Iterator access to instance/class methods. 537 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator; 538 typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>> 539 method_range; 540 541 method_range methods() const { 542 return method_range(meth_begin(), meth_end()); 543 } 544 method_iterator meth_begin() const { 545 return method_iterator(decls_begin()); 546 } 547 method_iterator meth_end() const { 548 return method_iterator(decls_end()); 549 } 550 551 typedef filtered_decl_iterator<ObjCMethodDecl, 552 &ObjCMethodDecl::isInstanceMethod> 553 instmeth_iterator; 554 typedef llvm::iterator_range<instmeth_iterator> instmeth_range; 555 556 instmeth_range instance_methods() const { 557 return instmeth_range(instmeth_begin(), instmeth_end()); 558 } 559 instmeth_iterator instmeth_begin() const { 560 return instmeth_iterator(decls_begin()); 561 } 562 instmeth_iterator instmeth_end() const { 563 return instmeth_iterator(decls_end()); 564 } 565 566 typedef filtered_decl_iterator<ObjCMethodDecl, 567 &ObjCMethodDecl::isClassMethod> 568 classmeth_iterator; 569 typedef llvm::iterator_range<classmeth_iterator> classmeth_range; 570 571 classmeth_range class_methods() const { 572 return classmeth_range(classmeth_begin(), classmeth_end()); 573 } 574 classmeth_iterator classmeth_begin() const { 575 return classmeth_iterator(decls_begin()); 576 } 577 classmeth_iterator classmeth_end() const { 578 return classmeth_iterator(decls_end()); 579 } 580 581 // Get the local instance/class method declared in this interface. 582 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance, 583 bool AllowHidden = false) const; 584 ObjCMethodDecl *getInstanceMethod(Selector Sel, 585 bool AllowHidden = false) const { 586 return getMethod(Sel, true/*isInstance*/, AllowHidden); 587 } 588 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const { 589 return getMethod(Sel, false/*isInstance*/, AllowHidden); 590 } 591 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const; 592 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const; 593 594 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const; 595 596 typedef llvm::DenseMap<IdentifierInfo*, ObjCPropertyDecl*> PropertyMap; 597 598 typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*> 599 ProtocolPropertyMap; 600 601 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder; 602 603 /// This routine collects list of properties to be implemented in the class. 604 /// This includes, class's and its conforming protocols' properties. 605 /// Note, the superclass's properties are not included in the list. 606 virtual void collectPropertiesToImplement(PropertyMap &PM, 607 PropertyDeclOrder &PO) const {} 608 609 SourceLocation getAtStartLoc() const { return AtStart; } 610 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; } 611 612 // Marks the end of the container. 613 SourceRange getAtEndRange() const { 614 return AtEnd; 615 } 616 void setAtEndRange(SourceRange atEnd) { 617 AtEnd = atEnd; 618 } 619 620 SourceRange getSourceRange() const override LLVM_READONLY { 621 return SourceRange(AtStart, getAtEndRange().getEnd()); 622 } 623 624 // Implement isa/cast/dyncast/etc. 625 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 626 static bool classofKind(Kind K) { 627 return K >= firstObjCContainer && 628 K <= lastObjCContainer; 629 } 630 631 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) { 632 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D)); 633 } 634 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) { 635 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC)); 636 } 637 }; 638 639 /// \brief Represents an ObjC class declaration. 640 /// 641 /// For example: 642 /// 643 /// \code 644 /// // MostPrimitive declares no super class (not particularly useful). 645 /// \@interface MostPrimitive 646 /// // no instance variables or methods. 647 /// \@end 648 /// 649 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol). 650 /// \@interface NSResponder : NSObject \<NSCoding> 651 /// { // instance variables are represented by ObjCIvarDecl. 652 /// id nextResponder; // nextResponder instance variable. 653 /// } 654 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder. 655 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer 656 /// \@end // to an NSEvent. 657 /// \endcode 658 /// 659 /// Unlike C/C++, forward class declarations are accomplished with \@class. 660 /// Unlike C/C++, \@class allows for a list of classes to be forward declared. 661 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes 662 /// typically inherit from NSObject (an exception is NSProxy). 663 /// 664 class ObjCInterfaceDecl : public ObjCContainerDecl 665 , public Redeclarable<ObjCInterfaceDecl> { 666 void anchor() override; 667 668 /// TypeForDecl - This indicates the Type object that represents this 669 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType 670 mutable const Type *TypeForDecl; 671 friend class ASTContext; 672 673 struct DefinitionData { 674 /// \brief The definition of this class, for quick access from any 675 /// declaration. 676 ObjCInterfaceDecl *Definition; 677 678 /// Class's super class. 679 ObjCInterfaceDecl *SuperClass; 680 681 /// Protocols referenced in the \@interface declaration 682 ObjCProtocolList ReferencedProtocols; 683 684 /// Protocols reference in both the \@interface and class extensions. 685 ObjCList<ObjCProtocolDecl> AllReferencedProtocols; 686 687 /// \brief List of categories and class extensions defined for this class. 688 /// 689 /// Categories are stored as a linked list in the AST, since the categories 690 /// and class extensions come long after the initial interface declaration, 691 /// and we avoid dynamically-resized arrays in the AST wherever possible. 692 ObjCCategoryDecl *CategoryList; 693 694 /// IvarList - List of all ivars defined by this class; including class 695 /// extensions and implementation. This list is built lazily. 696 ObjCIvarDecl *IvarList; 697 698 /// \brief Indicates that the contents of this Objective-C class will be 699 /// completed by the external AST source when required. 700 mutable bool ExternallyCompleted : 1; 701 702 /// \brief Indicates that the ivar cache does not yet include ivars 703 /// declared in the implementation. 704 mutable bool IvarListMissingImplementation : 1; 705 706 /// Indicates that this interface decl contains at least one initializer 707 /// marked with the 'objc_designated_initializer' attribute. 708 bool HasDesignatedInitializers : 1; 709 710 enum InheritedDesignatedInitializersState { 711 /// We didn't calculate whether the designated initializers should be 712 /// inherited or not. 713 IDI_Unknown = 0, 714 /// Designated initializers are inherited for the super class. 715 IDI_Inherited = 1, 716 /// The class does not inherit designated initializers. 717 IDI_NotInherited = 2 718 }; 719 /// One of the \c InheritedDesignatedInitializersState enumeratos. 720 mutable unsigned InheritedDesignatedInitializers : 2; 721 722 /// \brief The location of the superclass, if any. 723 SourceLocation SuperClassLoc; 724 725 /// \brief The location of the last location in this declaration, before 726 /// the properties/methods. For example, this will be the '>', '}', or 727 /// identifier, 728 SourceLocation EndLoc; 729 730 DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(), 731 ExternallyCompleted(), 732 IvarListMissingImplementation(true), 733 HasDesignatedInitializers(), 734 InheritedDesignatedInitializers(IDI_Unknown) { } 735 }; 736 737 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc, 738 IdentifierInfo *Id, SourceLocation CLoc, 739 ObjCInterfaceDecl *PrevDecl, bool IsInternal); 740 741 void LoadExternalDefinition() const; 742 743 /// \brief Contains a pointer to the data associated with this class, 744 /// which will be NULL if this class has not yet been defined. 745 /// 746 /// The bit indicates when we don't need to check for out-of-date 747 /// declarations. It will be set unless modules are enabled. 748 llvm::PointerIntPair<DefinitionData *, 1, bool> Data; 749 750 DefinitionData &data() const { 751 assert(Data.getPointer() && "Declaration has no definition!"); 752 return *Data.getPointer(); 753 } 754 755 /// \brief Allocate the definition data for this class. 756 void allocateDefinitionData(); 757 758 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base; 759 ObjCInterfaceDecl *getNextRedeclarationImpl() override { 760 return getNextRedeclaration(); 761 } 762 ObjCInterfaceDecl *getPreviousDeclImpl() override { 763 return getPreviousDecl(); 764 } 765 ObjCInterfaceDecl *getMostRecentDeclImpl() override { 766 return getMostRecentDecl(); 767 } 768 769 public: 770 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC, 771 SourceLocation atLoc, 772 IdentifierInfo *Id, 773 ObjCInterfaceDecl *PrevDecl, 774 SourceLocation ClassLoc = SourceLocation(), 775 bool isInternal = false); 776 777 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID); 778 779 SourceRange getSourceRange() const override LLVM_READONLY { 780 if (isThisDeclarationADefinition()) 781 return ObjCContainerDecl::getSourceRange(); 782 783 return SourceRange(getAtStartLoc(), getLocation()); 784 } 785 786 /// \brief Indicate that this Objective-C class is complete, but that 787 /// the external AST source will be responsible for filling in its contents 788 /// when a complete class is required. 789 void setExternallyCompleted(); 790 791 /// Indicate that this interface decl contains at least one initializer 792 /// marked with the 'objc_designated_initializer' attribute. 793 void setHasDesignatedInitializers(); 794 795 /// Returns true if this interface decl contains at least one initializer 796 /// marked with the 'objc_designated_initializer' attribute. 797 bool hasDesignatedInitializers() const; 798 799 /// Returns true if this interface decl declares a designated initializer 800 /// or it inherites one from its super class. 801 bool declaresOrInheritsDesignatedInitializers() const { 802 return hasDesignatedInitializers() || inheritsDesignatedInitializers(); 803 } 804 805 const ObjCProtocolList &getReferencedProtocols() const { 806 assert(hasDefinition() && "Caller did not check for forward reference!"); 807 if (data().ExternallyCompleted) 808 LoadExternalDefinition(); 809 810 return data().ReferencedProtocols; 811 } 812 813 ObjCImplementationDecl *getImplementation() const; 814 void setImplementation(ObjCImplementationDecl *ImplD); 815 816 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const; 817 818 // Get the local instance/class method declared in a category. 819 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const; 820 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const; 821 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const { 822 return isInstance ? getInstanceMethod(Sel) 823 : getClassMethod(Sel); 824 } 825 826 typedef ObjCProtocolList::iterator protocol_iterator; 827 typedef llvm::iterator_range<protocol_iterator> protocol_range; 828 829 protocol_range protocols() const { 830 return protocol_range(protocol_begin(), protocol_end()); 831 } 832 protocol_iterator protocol_begin() const { 833 // FIXME: Should make sure no callers ever do this. 834 if (!hasDefinition()) 835 return protocol_iterator(); 836 837 if (data().ExternallyCompleted) 838 LoadExternalDefinition(); 839 840 return data().ReferencedProtocols.begin(); 841 } 842 protocol_iterator protocol_end() const { 843 // FIXME: Should make sure no callers ever do this. 844 if (!hasDefinition()) 845 return protocol_iterator(); 846 847 if (data().ExternallyCompleted) 848 LoadExternalDefinition(); 849 850 return data().ReferencedProtocols.end(); 851 } 852 853 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 854 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 855 856 protocol_loc_range protocol_locs() const { 857 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 858 } 859 protocol_loc_iterator protocol_loc_begin() const { 860 // FIXME: Should make sure no callers ever do this. 861 if (!hasDefinition()) 862 return protocol_loc_iterator(); 863 864 if (data().ExternallyCompleted) 865 LoadExternalDefinition(); 866 867 return data().ReferencedProtocols.loc_begin(); 868 } 869 870 protocol_loc_iterator protocol_loc_end() const { 871 // FIXME: Should make sure no callers ever do this. 872 if (!hasDefinition()) 873 return protocol_loc_iterator(); 874 875 if (data().ExternallyCompleted) 876 LoadExternalDefinition(); 877 878 return data().ReferencedProtocols.loc_end(); 879 } 880 881 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator; 882 typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range; 883 884 all_protocol_range all_referenced_protocols() const { 885 return all_protocol_range(all_referenced_protocol_begin(), 886 all_referenced_protocol_end()); 887 } 888 all_protocol_iterator all_referenced_protocol_begin() const { 889 // FIXME: Should make sure no callers ever do this. 890 if (!hasDefinition()) 891 return all_protocol_iterator(); 892 893 if (data().ExternallyCompleted) 894 LoadExternalDefinition(); 895 896 return data().AllReferencedProtocols.empty() 897 ? protocol_begin() 898 : data().AllReferencedProtocols.begin(); 899 } 900 all_protocol_iterator all_referenced_protocol_end() const { 901 // FIXME: Should make sure no callers ever do this. 902 if (!hasDefinition()) 903 return all_protocol_iterator(); 904 905 if (data().ExternallyCompleted) 906 LoadExternalDefinition(); 907 908 return data().AllReferencedProtocols.empty() 909 ? protocol_end() 910 : data().AllReferencedProtocols.end(); 911 } 912 913 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 914 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 915 916 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 917 ivar_iterator ivar_begin() const { 918 if (const ObjCInterfaceDecl *Def = getDefinition()) 919 return ivar_iterator(Def->decls_begin()); 920 921 // FIXME: Should make sure no callers ever do this. 922 return ivar_iterator(); 923 } 924 ivar_iterator ivar_end() const { 925 if (const ObjCInterfaceDecl *Def = getDefinition()) 926 return ivar_iterator(Def->decls_end()); 927 928 // FIXME: Should make sure no callers ever do this. 929 return ivar_iterator(); 930 } 931 932 unsigned ivar_size() const { 933 return std::distance(ivar_begin(), ivar_end()); 934 } 935 936 bool ivar_empty() const { return ivar_begin() == ivar_end(); } 937 938 ObjCIvarDecl *all_declared_ivar_begin(); 939 const ObjCIvarDecl *all_declared_ivar_begin() const { 940 // Even though this modifies IvarList, it's conceptually const: 941 // the ivar chain is essentially a cached property of ObjCInterfaceDecl. 942 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin(); 943 } 944 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; } 945 946 /// setProtocolList - Set the list of protocols that this interface 947 /// implements. 948 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num, 949 const SourceLocation *Locs, ASTContext &C) { 950 data().ReferencedProtocols.set(List, Num, Locs, C); 951 } 952 953 /// mergeClassExtensionProtocolList - Merge class extension's protocol list 954 /// into the protocol list for this class. 955 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List, 956 unsigned Num, 957 ASTContext &C); 958 959 /// Returns the designated initializers for the interface. 960 /// 961 /// If this declaration does not have methods marked as designated 962 /// initializers then the interface inherits the designated initializers of 963 /// its super class. 964 void getDesignatedInitializers( 965 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const; 966 967 /// Returns true if the given selector is a designated initializer for the 968 /// interface. 969 /// 970 /// If this declaration does not have methods marked as designated 971 /// initializers then the interface inherits the designated initializers of 972 /// its super class. 973 /// 974 /// \param InitMethod if non-null and the function returns true, it receives 975 /// the method that was marked as a designated initializer. 976 bool 977 isDesignatedInitializer(Selector Sel, 978 const ObjCMethodDecl **InitMethod = nullptr) const; 979 980 /// \brief Determine whether this particular declaration of this class is 981 /// actually also a definition. 982 bool isThisDeclarationADefinition() const { 983 return getDefinition() == this; 984 } 985 986 /// \brief Determine whether this class has been defined. 987 bool hasDefinition() const { 988 // If the name of this class is out-of-date, bring it up-to-date, which 989 // might bring in a definition. 990 // Note: a null value indicates that we don't have a definition and that 991 // modules are enabled. 992 if (!Data.getOpaqueValue()) { 993 if (IdentifierInfo *II = getIdentifier()) { 994 if (II->isOutOfDate()) { 995 updateOutOfDate(*II); 996 } 997 } 998 } 999 1000 return Data.getPointer(); 1001 } 1002 1003 /// \brief Retrieve the definition of this class, or NULL if this class 1004 /// has been forward-declared (with \@class) but not yet defined (with 1005 /// \@interface). 1006 ObjCInterfaceDecl *getDefinition() { 1007 return hasDefinition()? Data.getPointer()->Definition : nullptr; 1008 } 1009 1010 /// \brief Retrieve the definition of this class, or NULL if this class 1011 /// has been forward-declared (with \@class) but not yet defined (with 1012 /// \@interface). 1013 const ObjCInterfaceDecl *getDefinition() const { 1014 return hasDefinition()? Data.getPointer()->Definition : nullptr; 1015 } 1016 1017 /// \brief Starts the definition of this Objective-C class, taking it from 1018 /// a forward declaration (\@class) to a definition (\@interface). 1019 void startDefinition(); 1020 1021 ObjCInterfaceDecl *getSuperClass() const { 1022 // FIXME: Should make sure no callers ever do this. 1023 if (!hasDefinition()) 1024 return nullptr; 1025 1026 if (data().ExternallyCompleted) 1027 LoadExternalDefinition(); 1028 1029 return data().SuperClass; 1030 } 1031 1032 void setSuperClass(ObjCInterfaceDecl * superCls) { 1033 data().SuperClass = 1034 (superCls && superCls->hasDefinition()) ? superCls->getDefinition() 1035 : superCls; 1036 } 1037 1038 /// \brief Iterator that walks over the list of categories, filtering out 1039 /// those that do not meet specific criteria. 1040 /// 1041 /// This class template is used for the various permutations of category 1042 /// and extension iterators. 1043 template<bool (*Filter)(ObjCCategoryDecl *)> 1044 class filtered_category_iterator { 1045 ObjCCategoryDecl *Current; 1046 1047 void findAcceptableCategory(); 1048 1049 public: 1050 typedef ObjCCategoryDecl * value_type; 1051 typedef value_type reference; 1052 typedef value_type pointer; 1053 typedef std::ptrdiff_t difference_type; 1054 typedef std::input_iterator_tag iterator_category; 1055 1056 filtered_category_iterator() : Current(nullptr) { } 1057 explicit filtered_category_iterator(ObjCCategoryDecl *Current) 1058 : Current(Current) 1059 { 1060 findAcceptableCategory(); 1061 } 1062 1063 reference operator*() const { return Current; } 1064 pointer operator->() const { return Current; } 1065 1066 filtered_category_iterator &operator++(); 1067 1068 filtered_category_iterator operator++(int) { 1069 filtered_category_iterator Tmp = *this; 1070 ++(*this); 1071 return Tmp; 1072 } 1073 1074 friend bool operator==(filtered_category_iterator X, 1075 filtered_category_iterator Y) { 1076 return X.Current == Y.Current; 1077 } 1078 1079 friend bool operator!=(filtered_category_iterator X, 1080 filtered_category_iterator Y) { 1081 return X.Current != Y.Current; 1082 } 1083 }; 1084 1085 private: 1086 /// \brief Test whether the given category is visible. 1087 /// 1088 /// Used in the \c visible_categories_iterator. 1089 static bool isVisibleCategory(ObjCCategoryDecl *Cat); 1090 1091 public: 1092 /// \brief Iterator that walks over the list of categories and extensions 1093 /// that are visible, i.e., not hidden in a non-imported submodule. 1094 typedef filtered_category_iterator<isVisibleCategory> 1095 visible_categories_iterator; 1096 1097 typedef llvm::iterator_range<visible_categories_iterator> 1098 visible_categories_range; 1099 1100 visible_categories_range visible_categories() const { 1101 return visible_categories_range(visible_categories_begin(), 1102 visible_categories_end()); 1103 } 1104 1105 /// \brief Retrieve an iterator to the beginning of the visible-categories 1106 /// list. 1107 visible_categories_iterator visible_categories_begin() const { 1108 return visible_categories_iterator(getCategoryListRaw()); 1109 } 1110 1111 /// \brief Retrieve an iterator to the end of the visible-categories list. 1112 visible_categories_iterator visible_categories_end() const { 1113 return visible_categories_iterator(); 1114 } 1115 1116 /// \brief Determine whether the visible-categories list is empty. 1117 bool visible_categories_empty() const { 1118 return visible_categories_begin() == visible_categories_end(); 1119 } 1120 1121 private: 1122 /// \brief Test whether the given category... is a category. 1123 /// 1124 /// Used in the \c known_categories_iterator. 1125 static bool isKnownCategory(ObjCCategoryDecl *) { return true; } 1126 1127 public: 1128 /// \brief Iterator that walks over all of the known categories and 1129 /// extensions, including those that are hidden. 1130 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator; 1131 typedef llvm::iterator_range<known_categories_iterator> 1132 known_categories_range; 1133 1134 known_categories_range known_categories() const { 1135 return known_categories_range(known_categories_begin(), 1136 known_categories_end()); 1137 } 1138 1139 /// \brief Retrieve an iterator to the beginning of the known-categories 1140 /// list. 1141 known_categories_iterator known_categories_begin() const { 1142 return known_categories_iterator(getCategoryListRaw()); 1143 } 1144 1145 /// \brief Retrieve an iterator to the end of the known-categories list. 1146 known_categories_iterator known_categories_end() const { 1147 return known_categories_iterator(); 1148 } 1149 1150 /// \brief Determine whether the known-categories list is empty. 1151 bool known_categories_empty() const { 1152 return known_categories_begin() == known_categories_end(); 1153 } 1154 1155 private: 1156 /// \brief Test whether the given category is a visible extension. 1157 /// 1158 /// Used in the \c visible_extensions_iterator. 1159 static bool isVisibleExtension(ObjCCategoryDecl *Cat); 1160 1161 public: 1162 /// \brief Iterator that walks over all of the visible extensions, skipping 1163 /// any that are known but hidden. 1164 typedef filtered_category_iterator<isVisibleExtension> 1165 visible_extensions_iterator; 1166 1167 typedef llvm::iterator_range<visible_extensions_iterator> 1168 visible_extensions_range; 1169 1170 visible_extensions_range visible_extensions() const { 1171 return visible_extensions_range(visible_extensions_begin(), 1172 visible_extensions_end()); 1173 } 1174 1175 /// \brief Retrieve an iterator to the beginning of the visible-extensions 1176 /// list. 1177 visible_extensions_iterator visible_extensions_begin() const { 1178 return visible_extensions_iterator(getCategoryListRaw()); 1179 } 1180 1181 /// \brief Retrieve an iterator to the end of the visible-extensions list. 1182 visible_extensions_iterator visible_extensions_end() const { 1183 return visible_extensions_iterator(); 1184 } 1185 1186 /// \brief Determine whether the visible-extensions list is empty. 1187 bool visible_extensions_empty() const { 1188 return visible_extensions_begin() == visible_extensions_end(); 1189 } 1190 1191 private: 1192 /// \brief Test whether the given category is an extension. 1193 /// 1194 /// Used in the \c known_extensions_iterator. 1195 static bool isKnownExtension(ObjCCategoryDecl *Cat); 1196 1197 public: 1198 /// \brief Iterator that walks over all of the known extensions. 1199 typedef filtered_category_iterator<isKnownExtension> 1200 known_extensions_iterator; 1201 typedef llvm::iterator_range<known_extensions_iterator> 1202 known_extensions_range; 1203 1204 known_extensions_range known_extensions() const { 1205 return known_extensions_range(known_extensions_begin(), 1206 known_extensions_end()); 1207 } 1208 1209 /// \brief Retrieve an iterator to the beginning of the known-extensions 1210 /// list. 1211 known_extensions_iterator known_extensions_begin() const { 1212 return known_extensions_iterator(getCategoryListRaw()); 1213 } 1214 1215 /// \brief Retrieve an iterator to the end of the known-extensions list. 1216 known_extensions_iterator known_extensions_end() const { 1217 return known_extensions_iterator(); 1218 } 1219 1220 /// \brief Determine whether the known-extensions list is empty. 1221 bool known_extensions_empty() const { 1222 return known_extensions_begin() == known_extensions_end(); 1223 } 1224 1225 /// \brief Retrieve the raw pointer to the start of the category/extension 1226 /// list. 1227 ObjCCategoryDecl* getCategoryListRaw() const { 1228 // FIXME: Should make sure no callers ever do this. 1229 if (!hasDefinition()) 1230 return nullptr; 1231 1232 if (data().ExternallyCompleted) 1233 LoadExternalDefinition(); 1234 1235 return data().CategoryList; 1236 } 1237 1238 /// \brief Set the raw pointer to the start of the category/extension 1239 /// list. 1240 void setCategoryListRaw(ObjCCategoryDecl *category) { 1241 data().CategoryList = category; 1242 } 1243 1244 ObjCPropertyDecl 1245 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const; 1246 1247 void collectPropertiesToImplement(PropertyMap &PM, 1248 PropertyDeclOrder &PO) const override; 1249 1250 /// isSuperClassOf - Return true if this class is the specified class or is a 1251 /// super class of the specified interface class. 1252 bool isSuperClassOf(const ObjCInterfaceDecl *I) const { 1253 // If RHS is derived from LHS it is OK; else it is not OK. 1254 while (I != nullptr) { 1255 if (declaresSameEntity(this, I)) 1256 return true; 1257 1258 I = I->getSuperClass(); 1259 } 1260 return false; 1261 } 1262 1263 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes 1264 /// to be incompatible with __weak references. Returns true if it is. 1265 bool isArcWeakrefUnavailable() const; 1266 1267 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super 1268 /// classes must not be auto-synthesized. Returns class decl. if it must not 1269 /// be; 0, otherwise. 1270 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const; 1271 1272 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName, 1273 ObjCInterfaceDecl *&ClassDeclared); 1274 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) { 1275 ObjCInterfaceDecl *ClassDeclared; 1276 return lookupInstanceVariable(IVarName, ClassDeclared); 1277 } 1278 1279 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name); 1280 1281 // Lookup a method. First, we search locally. If a method isn't 1282 // found, we search referenced protocols and class categories. 1283 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance, 1284 bool shallowCategoryLookup = false, 1285 bool followSuper = true, 1286 const ObjCCategoryDecl *C = nullptr) const; 1287 1288 /// Lookup an instance method for a given selector. 1289 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 1290 return lookupMethod(Sel, true/*isInstance*/); 1291 } 1292 1293 /// Lookup a class method for a given selector. 1294 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 1295 return lookupMethod(Sel, false/*isInstance*/); 1296 } 1297 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName); 1298 1299 /// \brief Lookup a method in the classes implementation hierarchy. 1300 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, 1301 bool Instance=true) const; 1302 1303 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) { 1304 return lookupPrivateMethod(Sel, false); 1305 } 1306 1307 /// \brief Lookup a setter or getter in the class hierarchy, 1308 /// including in all categories except for category passed 1309 /// as argument. 1310 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel, 1311 const ObjCCategoryDecl *Cat) const { 1312 return lookupMethod(Sel, true/*isInstance*/, 1313 false/*shallowCategoryLookup*/, 1314 true /* followsSuper */, 1315 Cat); 1316 } 1317 1318 SourceLocation getEndOfDefinitionLoc() const { 1319 if (!hasDefinition()) 1320 return getLocation(); 1321 1322 return data().EndLoc; 1323 } 1324 1325 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; } 1326 1327 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; } 1328 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; } 1329 1330 /// isImplicitInterfaceDecl - check that this is an implicitly declared 1331 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation 1332 /// declaration without an \@interface declaration. 1333 bool isImplicitInterfaceDecl() const { 1334 return hasDefinition() ? data().Definition->isImplicit() : isImplicit(); 1335 } 1336 1337 /// ClassImplementsProtocol - Checks that 'lProto' protocol 1338 /// has been implemented in IDecl class, its super class or categories (if 1339 /// lookupCategory is true). 1340 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, 1341 bool lookupCategory, 1342 bool RHSIsQualifiedID = false); 1343 1344 typedef redeclarable_base::redecl_range redecl_range; 1345 typedef redeclarable_base::redecl_iterator redecl_iterator; 1346 using redeclarable_base::redecls_begin; 1347 using redeclarable_base::redecls_end; 1348 using redeclarable_base::redecls; 1349 using redeclarable_base::getPreviousDecl; 1350 using redeclarable_base::getMostRecentDecl; 1351 using redeclarable_base::isFirstDecl; 1352 1353 /// Retrieves the canonical declaration of this Objective-C class. 1354 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); } 1355 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); } 1356 1357 // Low-level accessor 1358 const Type *getTypeForDecl() const { return TypeForDecl; } 1359 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; } 1360 1361 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1362 static bool classofKind(Kind K) { return K == ObjCInterface; } 1363 1364 friend class ASTReader; 1365 friend class ASTDeclReader; 1366 friend class ASTDeclWriter; 1367 1368 private: 1369 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const; 1370 bool inheritsDesignatedInitializers() const; 1371 }; 1372 1373 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC 1374 /// instance variables are identical to C. The only exception is Objective-C 1375 /// supports C++ style access control. For example: 1376 /// 1377 /// \@interface IvarExample : NSObject 1378 /// { 1379 /// id defaultToProtected; 1380 /// \@public: 1381 /// id canBePublic; // same as C++. 1382 /// \@protected: 1383 /// id canBeProtected; // same as C++. 1384 /// \@package: 1385 /// id canBePackage; // framework visibility (not available in C++). 1386 /// } 1387 /// 1388 class ObjCIvarDecl : public FieldDecl { 1389 void anchor() override; 1390 1391 public: 1392 enum AccessControl { 1393 None, Private, Protected, Public, Package 1394 }; 1395 1396 private: 1397 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc, 1398 SourceLocation IdLoc, IdentifierInfo *Id, 1399 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW, 1400 bool synthesized) 1401 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, 1402 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit), 1403 NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {} 1404 1405 public: 1406 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC, 1407 SourceLocation StartLoc, SourceLocation IdLoc, 1408 IdentifierInfo *Id, QualType T, 1409 TypeSourceInfo *TInfo, 1410 AccessControl ac, Expr *BW = nullptr, 1411 bool synthesized=false); 1412 1413 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1414 1415 /// \brief Return the class interface that this ivar is logically contained 1416 /// in; this is either the interface where the ivar was declared, or the 1417 /// interface the ivar is conceptually a part of in the case of synthesized 1418 /// ivars. 1419 const ObjCInterfaceDecl *getContainingInterface() const; 1420 1421 ObjCIvarDecl *getNextIvar() { return NextIvar; } 1422 const ObjCIvarDecl *getNextIvar() const { return NextIvar; } 1423 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; } 1424 1425 void setAccessControl(AccessControl ac) { DeclAccess = ac; } 1426 1427 AccessControl getAccessControl() const { return AccessControl(DeclAccess); } 1428 1429 AccessControl getCanonicalAccessControl() const { 1430 return DeclAccess == None ? Protected : AccessControl(DeclAccess); 1431 } 1432 1433 void setSynthesize(bool synth) { Synthesized = synth; } 1434 bool getSynthesize() const { return Synthesized; } 1435 1436 // Implement isa/cast/dyncast/etc. 1437 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1438 static bool classofKind(Kind K) { return K == ObjCIvar; } 1439 private: 1440 /// NextIvar - Next Ivar in the list of ivars declared in class; class's 1441 /// extensions and class's implementation 1442 ObjCIvarDecl *NextIvar; 1443 1444 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum 1445 unsigned DeclAccess : 3; 1446 unsigned Synthesized : 1; 1447 }; 1448 1449 1450 /// \brief Represents a field declaration created by an \@defs(...). 1451 class ObjCAtDefsFieldDecl : public FieldDecl { 1452 void anchor() override; 1453 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc, 1454 SourceLocation IdLoc, IdentifierInfo *Id, 1455 QualType T, Expr *BW) 1456 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T, 1457 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ? 1458 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {} 1459 1460 public: 1461 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC, 1462 SourceLocation StartLoc, 1463 SourceLocation IdLoc, IdentifierInfo *Id, 1464 QualType T, Expr *BW); 1465 1466 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1467 1468 // Implement isa/cast/dyncast/etc. 1469 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1470 static bool classofKind(Kind K) { return K == ObjCAtDefsField; } 1471 }; 1472 1473 /// \brief Represents an Objective-C protocol declaration. 1474 /// 1475 /// Objective-C protocols declare a pure abstract type (i.e., no instance 1476 /// variables are permitted). Protocols originally drew inspiration from 1477 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy 1478 /// syntax:-). Here is an example: 1479 /// 1480 /// \code 1481 /// \@protocol NSDraggingInfo <refproto1, refproto2> 1482 /// - (NSWindow *)draggingDestinationWindow; 1483 /// - (NSImage *)draggedImage; 1484 /// \@end 1485 /// \endcode 1486 /// 1487 /// This says that NSDraggingInfo requires two methods and requires everything 1488 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as 1489 /// well. 1490 /// 1491 /// \code 1492 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo> 1493 /// \@end 1494 /// \endcode 1495 /// 1496 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and 1497 /// protocols are in distinct namespaces. For example, Cocoa defines both 1498 /// an NSObject protocol and class (which isn't allowed in Java). As a result, 1499 /// protocols are referenced using angle brackets as follows: 1500 /// 1501 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo; 1502 /// 1503 class ObjCProtocolDecl : public ObjCContainerDecl, 1504 public Redeclarable<ObjCProtocolDecl> { 1505 void anchor() override; 1506 1507 struct DefinitionData { 1508 // \brief The declaration that defines this protocol. 1509 ObjCProtocolDecl *Definition; 1510 1511 /// \brief Referenced protocols 1512 ObjCProtocolList ReferencedProtocols; 1513 }; 1514 1515 /// \brief Contains a pointer to the data associated with this class, 1516 /// which will be NULL if this class has not yet been defined. 1517 /// 1518 /// The bit indicates when we don't need to check for out-of-date 1519 /// declarations. It will be set unless modules are enabled. 1520 llvm::PointerIntPair<DefinitionData *, 1, bool> Data; 1521 1522 DefinitionData &data() const { 1523 assert(Data.getPointer() && "Objective-C protocol has no definition!"); 1524 return *Data.getPointer(); 1525 } 1526 1527 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id, 1528 SourceLocation nameLoc, SourceLocation atStartLoc, 1529 ObjCProtocolDecl *PrevDecl); 1530 1531 void allocateDefinitionData(); 1532 1533 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base; 1534 ObjCProtocolDecl *getNextRedeclarationImpl() override { 1535 return getNextRedeclaration(); 1536 } 1537 ObjCProtocolDecl *getPreviousDeclImpl() override { 1538 return getPreviousDecl(); 1539 } 1540 ObjCProtocolDecl *getMostRecentDeclImpl() override { 1541 return getMostRecentDecl(); 1542 } 1543 1544 public: 1545 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC, 1546 IdentifierInfo *Id, 1547 SourceLocation nameLoc, 1548 SourceLocation atStartLoc, 1549 ObjCProtocolDecl *PrevDecl); 1550 1551 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1552 1553 const ObjCProtocolList &getReferencedProtocols() const { 1554 assert(hasDefinition() && "No definition available!"); 1555 return data().ReferencedProtocols; 1556 } 1557 typedef ObjCProtocolList::iterator protocol_iterator; 1558 typedef llvm::iterator_range<protocol_iterator> protocol_range; 1559 1560 protocol_range protocols() const { 1561 return protocol_range(protocol_begin(), protocol_end()); 1562 } 1563 protocol_iterator protocol_begin() const { 1564 if (!hasDefinition()) 1565 return protocol_iterator(); 1566 1567 return data().ReferencedProtocols.begin(); 1568 } 1569 protocol_iterator protocol_end() const { 1570 if (!hasDefinition()) 1571 return protocol_iterator(); 1572 1573 return data().ReferencedProtocols.end(); 1574 } 1575 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 1576 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 1577 1578 protocol_loc_range protocol_locs() const { 1579 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 1580 } 1581 protocol_loc_iterator protocol_loc_begin() const { 1582 if (!hasDefinition()) 1583 return protocol_loc_iterator(); 1584 1585 return data().ReferencedProtocols.loc_begin(); 1586 } 1587 protocol_loc_iterator protocol_loc_end() const { 1588 if (!hasDefinition()) 1589 return protocol_loc_iterator(); 1590 1591 return data().ReferencedProtocols.loc_end(); 1592 } 1593 unsigned protocol_size() const { 1594 if (!hasDefinition()) 1595 return 0; 1596 1597 return data().ReferencedProtocols.size(); 1598 } 1599 1600 /// setProtocolList - Set the list of protocols that this interface 1601 /// implements. 1602 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 1603 const SourceLocation *Locs, ASTContext &C) { 1604 assert(hasDefinition() && "Protocol is not defined"); 1605 data().ReferencedProtocols.set(List, Num, Locs, C); 1606 } 1607 1608 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName); 1609 1610 // Lookup a method. First, we search locally. If a method isn't 1611 // found, we search referenced protocols and class categories. 1612 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const; 1613 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 1614 return lookupMethod(Sel, true/*isInstance*/); 1615 } 1616 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 1617 return lookupMethod(Sel, false/*isInstance*/); 1618 } 1619 1620 /// \brief Determine whether this protocol has a definition. 1621 bool hasDefinition() const { 1622 // If the name of this protocol is out-of-date, bring it up-to-date, which 1623 // might bring in a definition. 1624 // Note: a null value indicates that we don't have a definition and that 1625 // modules are enabled. 1626 if (!Data.getOpaqueValue()) { 1627 if (IdentifierInfo *II = getIdentifier()) { 1628 if (II->isOutOfDate()) { 1629 updateOutOfDate(*II); 1630 } 1631 } 1632 } 1633 1634 return Data.getPointer(); 1635 } 1636 1637 /// \brief Retrieve the definition of this protocol, if any. 1638 ObjCProtocolDecl *getDefinition() { 1639 return hasDefinition()? Data.getPointer()->Definition : nullptr; 1640 } 1641 1642 /// \brief Retrieve the definition of this protocol, if any. 1643 const ObjCProtocolDecl *getDefinition() const { 1644 return hasDefinition()? Data.getPointer()->Definition : nullptr; 1645 } 1646 1647 /// \brief Determine whether this particular declaration is also the 1648 /// definition. 1649 bool isThisDeclarationADefinition() const { 1650 return getDefinition() == this; 1651 } 1652 1653 /// \brief Starts the definition of this Objective-C protocol. 1654 void startDefinition(); 1655 1656 SourceRange getSourceRange() const override LLVM_READONLY { 1657 if (isThisDeclarationADefinition()) 1658 return ObjCContainerDecl::getSourceRange(); 1659 1660 return SourceRange(getAtStartLoc(), getLocation()); 1661 } 1662 1663 typedef redeclarable_base::redecl_range redecl_range; 1664 typedef redeclarable_base::redecl_iterator redecl_iterator; 1665 using redeclarable_base::redecls_begin; 1666 using redeclarable_base::redecls_end; 1667 using redeclarable_base::redecls; 1668 using redeclarable_base::getPreviousDecl; 1669 using redeclarable_base::getMostRecentDecl; 1670 using redeclarable_base::isFirstDecl; 1671 1672 /// Retrieves the canonical declaration of this Objective-C protocol. 1673 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); } 1674 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); } 1675 1676 void collectPropertiesToImplement(PropertyMap &PM, 1677 PropertyDeclOrder &PO) const override; 1678 1679 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property, 1680 ProtocolPropertyMap &PM) const; 1681 1682 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1683 static bool classofKind(Kind K) { return K == ObjCProtocol; } 1684 1685 friend class ASTReader; 1686 friend class ASTDeclReader; 1687 friend class ASTDeclWriter; 1688 }; 1689 1690 /// ObjCCategoryDecl - Represents a category declaration. A category allows 1691 /// you to add methods to an existing class (without subclassing or modifying 1692 /// the original class interface or implementation:-). Categories don't allow 1693 /// you to add instance data. The following example adds "myMethod" to all 1694 /// NSView's within a process: 1695 /// 1696 /// \@interface NSView (MyViewMethods) 1697 /// - myMethod; 1698 /// \@end 1699 /// 1700 /// Categories also allow you to split the implementation of a class across 1701 /// several files (a feature more naturally supported in C++). 1702 /// 1703 /// Categories were originally inspired by dynamic languages such as Common 1704 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 1705 /// don't support this level of dynamism, which is both powerful and dangerous. 1706 /// 1707 class ObjCCategoryDecl : public ObjCContainerDecl { 1708 void anchor() override; 1709 1710 /// Interface belonging to this category 1711 ObjCInterfaceDecl *ClassInterface; 1712 1713 /// referenced protocols in this category. 1714 ObjCProtocolList ReferencedProtocols; 1715 1716 /// Next category belonging to this class. 1717 /// FIXME: this should not be a singly-linked list. Move storage elsewhere. 1718 ObjCCategoryDecl *NextClassCategory; 1719 1720 /// \brief The location of the category name in this declaration. 1721 SourceLocation CategoryNameLoc; 1722 1723 /// class extension may have private ivars. 1724 SourceLocation IvarLBraceLoc; 1725 SourceLocation IvarRBraceLoc; 1726 1727 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, 1728 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, 1729 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, 1730 SourceLocation IvarLBraceLoc=SourceLocation(), 1731 SourceLocation IvarRBraceLoc=SourceLocation()) 1732 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc), 1733 ClassInterface(IDecl), NextClassCategory(nullptr), 1734 CategoryNameLoc(CategoryNameLoc), 1735 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) { 1736 } 1737 1738 public: 1739 1740 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC, 1741 SourceLocation AtLoc, 1742 SourceLocation ClassNameLoc, 1743 SourceLocation CategoryNameLoc, 1744 IdentifierInfo *Id, 1745 ObjCInterfaceDecl *IDecl, 1746 SourceLocation IvarLBraceLoc=SourceLocation(), 1747 SourceLocation IvarRBraceLoc=SourceLocation()); 1748 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1749 1750 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 1751 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 1752 1753 ObjCCategoryImplDecl *getImplementation() const; 1754 void setImplementation(ObjCCategoryImplDecl *ImplD); 1755 1756 /// setProtocolList - Set the list of protocols that this interface 1757 /// implements. 1758 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 1759 const SourceLocation *Locs, ASTContext &C) { 1760 ReferencedProtocols.set(List, Num, Locs, C); 1761 } 1762 1763 const ObjCProtocolList &getReferencedProtocols() const { 1764 return ReferencedProtocols; 1765 } 1766 1767 typedef ObjCProtocolList::iterator protocol_iterator; 1768 typedef llvm::iterator_range<protocol_iterator> protocol_range; 1769 1770 protocol_range protocols() const { 1771 return protocol_range(protocol_begin(), protocol_end()); 1772 } 1773 protocol_iterator protocol_begin() const { 1774 return ReferencedProtocols.begin(); 1775 } 1776 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); } 1777 unsigned protocol_size() const { return ReferencedProtocols.size(); } 1778 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 1779 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 1780 1781 protocol_loc_range protocol_locs() const { 1782 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 1783 } 1784 protocol_loc_iterator protocol_loc_begin() const { 1785 return ReferencedProtocols.loc_begin(); 1786 } 1787 protocol_loc_iterator protocol_loc_end() const { 1788 return ReferencedProtocols.loc_end(); 1789 } 1790 1791 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } 1792 1793 /// \brief Retrieve the pointer to the next stored category (or extension), 1794 /// which may be hidden. 1795 ObjCCategoryDecl *getNextClassCategoryRaw() const { 1796 return NextClassCategory; 1797 } 1798 1799 bool IsClassExtension() const { return getIdentifier() == nullptr; } 1800 1801 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 1802 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 1803 1804 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 1805 ivar_iterator ivar_begin() const { 1806 return ivar_iterator(decls_begin()); 1807 } 1808 ivar_iterator ivar_end() const { 1809 return ivar_iterator(decls_end()); 1810 } 1811 unsigned ivar_size() const { 1812 return std::distance(ivar_begin(), ivar_end()); 1813 } 1814 bool ivar_empty() const { 1815 return ivar_begin() == ivar_end(); 1816 } 1817 1818 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 1819 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; } 1820 1821 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 1822 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 1823 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 1824 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 1825 1826 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1827 static bool classofKind(Kind K) { return K == ObjCCategory; } 1828 1829 friend class ASTDeclReader; 1830 friend class ASTDeclWriter; 1831 }; 1832 1833 class ObjCImplDecl : public ObjCContainerDecl { 1834 void anchor() override; 1835 1836 /// Class interface for this class/category implementation 1837 ObjCInterfaceDecl *ClassInterface; 1838 1839 protected: 1840 ObjCImplDecl(Kind DK, DeclContext *DC, 1841 ObjCInterfaceDecl *classInterface, 1842 SourceLocation nameLoc, SourceLocation atStartLoc) 1843 : ObjCContainerDecl(DK, DC, 1844 classInterface? classInterface->getIdentifier() 1845 : nullptr, 1846 nameLoc, atStartLoc), 1847 ClassInterface(classInterface) {} 1848 1849 public: 1850 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 1851 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 1852 void setClassInterface(ObjCInterfaceDecl *IFace); 1853 1854 void addInstanceMethod(ObjCMethodDecl *method) { 1855 // FIXME: Context should be set correctly before we get here. 1856 method->setLexicalDeclContext(this); 1857 addDecl(method); 1858 } 1859 void addClassMethod(ObjCMethodDecl *method) { 1860 // FIXME: Context should be set correctly before we get here. 1861 method->setLexicalDeclContext(this); 1862 addDecl(method); 1863 } 1864 1865 void addPropertyImplementation(ObjCPropertyImplDecl *property); 1866 1867 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const; 1868 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; 1869 1870 // Iterator access to properties. 1871 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator; 1872 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>> 1873 propimpl_range; 1874 1875 propimpl_range property_impls() const { 1876 return propimpl_range(propimpl_begin(), propimpl_end()); 1877 } 1878 propimpl_iterator propimpl_begin() const { 1879 return propimpl_iterator(decls_begin()); 1880 } 1881 propimpl_iterator propimpl_end() const { 1882 return propimpl_iterator(decls_end()); 1883 } 1884 1885 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1886 static bool classofKind(Kind K) { 1887 return K >= firstObjCImpl && K <= lastObjCImpl; 1888 } 1889 }; 1890 1891 /// ObjCCategoryImplDecl - An object of this class encapsulates a category 1892 /// \@implementation declaration. If a category class has declaration of a 1893 /// property, its implementation must be specified in the category's 1894 /// \@implementation declaration. Example: 1895 /// \@interface I \@end 1896 /// \@interface I(CATEGORY) 1897 /// \@property int p1, d1; 1898 /// \@end 1899 /// \@implementation I(CATEGORY) 1900 /// \@dynamic p1,d1; 1901 /// \@end 1902 /// 1903 /// ObjCCategoryImplDecl 1904 class ObjCCategoryImplDecl : public ObjCImplDecl { 1905 void anchor() override; 1906 1907 // Category name 1908 IdentifierInfo *Id; 1909 1910 // Category name location 1911 SourceLocation CategoryNameLoc; 1912 1913 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id, 1914 ObjCInterfaceDecl *classInterface, 1915 SourceLocation nameLoc, SourceLocation atStartLoc, 1916 SourceLocation CategoryNameLoc) 1917 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc), 1918 Id(Id), CategoryNameLoc(CategoryNameLoc) {} 1919 public: 1920 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, 1921 IdentifierInfo *Id, 1922 ObjCInterfaceDecl *classInterface, 1923 SourceLocation nameLoc, 1924 SourceLocation atStartLoc, 1925 SourceLocation CategoryNameLoc); 1926 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1927 1928 /// getIdentifier - Get the identifier that names the category 1929 /// interface associated with this implementation. 1930 /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier() 1931 /// with a different meaning. For example: 1932 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier() 1933 /// returns the class interface name, whereas 1934 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier() 1935 /// returns the category name. 1936 IdentifierInfo *getIdentifier() const { 1937 return Id; 1938 } 1939 void setIdentifier(IdentifierInfo *II) { Id = II; } 1940 1941 ObjCCategoryDecl *getCategoryDecl() const; 1942 1943 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 1944 1945 /// getName - Get the name of identifier for the class interface associated 1946 /// with this implementation as a StringRef. 1947 // 1948 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different 1949 // meaning. 1950 StringRef getName() const { return Id ? Id->getName() : StringRef(); } 1951 1952 /// @brief Get the name of the class associated with this interface. 1953 // 1954 // FIXME: Deprecated, move clients to getName(). 1955 std::string getNameAsString() const { 1956 return getName(); 1957 } 1958 1959 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1960 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;} 1961 1962 friend class ASTDeclReader; 1963 friend class ASTDeclWriter; 1964 }; 1965 1966 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID); 1967 1968 /// ObjCImplementationDecl - Represents a class definition - this is where 1969 /// method definitions are specified. For example: 1970 /// 1971 /// @code 1972 /// \@implementation MyClass 1973 /// - (void)myMethod { /* do something */ } 1974 /// \@end 1975 /// @endcode 1976 /// 1977 /// In a non-fragile runtime, instance variables can appear in the class 1978 /// interface, class extensions (nameless categories), and in the implementation 1979 /// itself, as well as being synthesized as backing storage for properties. 1980 /// 1981 /// In a fragile runtime, instance variables are specified in the class 1982 /// interface, \em not in the implementation. Nevertheless (for legacy reasons), 1983 /// we allow instance variables to be specified in the implementation. When 1984 /// specified, they need to be \em identical to the interface. 1985 class ObjCImplementationDecl : public ObjCImplDecl { 1986 void anchor() override; 1987 /// Implementation Class's super class. 1988 ObjCInterfaceDecl *SuperClass; 1989 SourceLocation SuperLoc; 1990 1991 /// \@implementation may have private ivars. 1992 SourceLocation IvarLBraceLoc; 1993 SourceLocation IvarRBraceLoc; 1994 1995 /// Support for ivar initialization. 1996 /// IvarInitializers - The arguments used to initialize the ivars 1997 CXXCtorInitializer **IvarInitializers; 1998 unsigned NumIvarInitializers; 1999 2000 /// Do the ivars of this class require initialization other than 2001 /// zero-initialization? 2002 bool HasNonZeroConstructors : 1; 2003 2004 /// Do the ivars of this class require non-trivial destruction? 2005 bool HasDestructors : 1; 2006 2007 ObjCImplementationDecl(DeclContext *DC, 2008 ObjCInterfaceDecl *classInterface, 2009 ObjCInterfaceDecl *superDecl, 2010 SourceLocation nameLoc, SourceLocation atStartLoc, 2011 SourceLocation superLoc = SourceLocation(), 2012 SourceLocation IvarLBraceLoc=SourceLocation(), 2013 SourceLocation IvarRBraceLoc=SourceLocation()) 2014 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc), 2015 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc), 2016 IvarRBraceLoc(IvarRBraceLoc), 2017 IvarInitializers(nullptr), NumIvarInitializers(0), 2018 HasNonZeroConstructors(false), HasDestructors(false) {} 2019 public: 2020 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, 2021 ObjCInterfaceDecl *classInterface, 2022 ObjCInterfaceDecl *superDecl, 2023 SourceLocation nameLoc, 2024 SourceLocation atStartLoc, 2025 SourceLocation superLoc = SourceLocation(), 2026 SourceLocation IvarLBraceLoc=SourceLocation(), 2027 SourceLocation IvarRBraceLoc=SourceLocation()); 2028 2029 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2030 2031 /// init_iterator - Iterates through the ivar initializer list. 2032 typedef CXXCtorInitializer **init_iterator; 2033 2034 /// init_const_iterator - Iterates through the ivar initializer list. 2035 typedef CXXCtorInitializer * const * init_const_iterator; 2036 2037 typedef llvm::iterator_range<init_iterator> init_range; 2038 typedef llvm::iterator_range<init_const_iterator> init_const_range; 2039 2040 init_range inits() { return init_range(init_begin(), init_end()); } 2041 init_const_range inits() const { 2042 return init_const_range(init_begin(), init_end()); 2043 } 2044 2045 /// init_begin() - Retrieve an iterator to the first initializer. 2046 init_iterator init_begin() { return IvarInitializers; } 2047 /// begin() - Retrieve an iterator to the first initializer. 2048 init_const_iterator init_begin() const { return IvarInitializers; } 2049 2050 /// init_end() - Retrieve an iterator past the last initializer. 2051 init_iterator init_end() { 2052 return IvarInitializers + NumIvarInitializers; 2053 } 2054 /// end() - Retrieve an iterator past the last initializer. 2055 init_const_iterator init_end() const { 2056 return IvarInitializers + NumIvarInitializers; 2057 } 2058 /// getNumArgs - Number of ivars which must be initialized. 2059 unsigned getNumIvarInitializers() const { 2060 return NumIvarInitializers; 2061 } 2062 2063 void setNumIvarInitializers(unsigned numNumIvarInitializers) { 2064 NumIvarInitializers = numNumIvarInitializers; 2065 } 2066 2067 void setIvarInitializers(ASTContext &C, 2068 CXXCtorInitializer ** initializers, 2069 unsigned numInitializers); 2070 2071 /// Do any of the ivars of this class (not counting its base classes) 2072 /// require construction other than zero-initialization? 2073 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; } 2074 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; } 2075 2076 /// Do any of the ivars of this class (not counting its base classes) 2077 /// require non-trivial destruction? 2078 bool hasDestructors() const { return HasDestructors; } 2079 void setHasDestructors(bool val) { HasDestructors = val; } 2080 2081 /// getIdentifier - Get the identifier that names the class 2082 /// interface associated with this implementation. 2083 IdentifierInfo *getIdentifier() const { 2084 return getClassInterface()->getIdentifier(); 2085 } 2086 2087 /// getName - Get the name of identifier for the class interface associated 2088 /// with this implementation as a StringRef. 2089 // 2090 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different 2091 // meaning. 2092 StringRef getName() const { 2093 assert(getIdentifier() && "Name is not a simple identifier"); 2094 return getIdentifier()->getName(); 2095 } 2096 2097 /// @brief Get the name of the class associated with this interface. 2098 // 2099 // FIXME: Move to StringRef API. 2100 std::string getNameAsString() const { 2101 return getName(); 2102 } 2103 2104 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } 2105 ObjCInterfaceDecl *getSuperClass() { return SuperClass; } 2106 SourceLocation getSuperClassLoc() const { return SuperLoc; } 2107 2108 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } 2109 2110 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 2111 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 2112 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 2113 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 2114 2115 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 2116 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 2117 2118 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 2119 ivar_iterator ivar_begin() const { 2120 return ivar_iterator(decls_begin()); 2121 } 2122 ivar_iterator ivar_end() const { 2123 return ivar_iterator(decls_end()); 2124 } 2125 unsigned ivar_size() const { 2126 return std::distance(ivar_begin(), ivar_end()); 2127 } 2128 bool ivar_empty() const { 2129 return ivar_begin() == ivar_end(); 2130 } 2131 2132 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2133 static bool classofKind(Kind K) { return K == ObjCImplementation; } 2134 2135 friend class ASTDeclReader; 2136 friend class ASTDeclWriter; 2137 }; 2138 2139 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID); 2140 2141 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is 2142 /// declared as \@compatibility_alias alias class. 2143 class ObjCCompatibleAliasDecl : public NamedDecl { 2144 void anchor() override; 2145 /// Class that this is an alias of. 2146 ObjCInterfaceDecl *AliasedClass; 2147 2148 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 2149 ObjCInterfaceDecl* aliasedClass) 2150 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {} 2151 public: 2152 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC, 2153 SourceLocation L, IdentifierInfo *Id, 2154 ObjCInterfaceDecl* aliasedClass); 2155 2156 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, 2157 unsigned ID); 2158 2159 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } 2160 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; } 2161 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; } 2162 2163 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2164 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; } 2165 2166 }; 2167 2168 /// \brief Represents one property declaration in an Objective-C interface. 2169 /// 2170 /// For example: 2171 /// \code{.mm} 2172 /// \@property (assign, readwrite) int MyProperty; 2173 /// \endcode 2174 class ObjCPropertyDecl : public NamedDecl { 2175 void anchor() override; 2176 public: 2177 enum PropertyAttributeKind { 2178 OBJC_PR_noattr = 0x00, 2179 OBJC_PR_readonly = 0x01, 2180 OBJC_PR_getter = 0x02, 2181 OBJC_PR_assign = 0x04, 2182 OBJC_PR_readwrite = 0x08, 2183 OBJC_PR_retain = 0x10, 2184 OBJC_PR_copy = 0x20, 2185 OBJC_PR_nonatomic = 0x40, 2186 OBJC_PR_setter = 0x80, 2187 OBJC_PR_atomic = 0x100, 2188 OBJC_PR_weak = 0x200, 2189 OBJC_PR_strong = 0x400, 2190 OBJC_PR_unsafe_unretained = 0x800 2191 // Adding a property should change NumPropertyAttrsBits 2192 }; 2193 2194 enum { 2195 /// \brief Number of bits fitting all the property attributes. 2196 NumPropertyAttrsBits = 12 2197 }; 2198 2199 enum SetterKind { Assign, Retain, Copy, Weak }; 2200 enum PropertyControl { None, Required, Optional }; 2201 private: 2202 SourceLocation AtLoc; // location of \@property 2203 SourceLocation LParenLoc; // location of '(' starting attribute list or null. 2204 TypeSourceInfo *DeclType; 2205 unsigned PropertyAttributes : NumPropertyAttrsBits; 2206 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits; 2207 // \@required/\@optional 2208 unsigned PropertyImplementation : 2; 2209 2210 Selector GetterName; // getter name of NULL if no getter 2211 Selector SetterName; // setter name of NULL if no setter 2212 2213 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method 2214 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method 2215 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property 2216 2217 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 2218 SourceLocation AtLocation, SourceLocation LParenLocation, 2219 TypeSourceInfo *T) 2220 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), 2221 LParenLoc(LParenLocation), DeclType(T), 2222 PropertyAttributes(OBJC_PR_noattr), 2223 PropertyAttributesAsWritten(OBJC_PR_noattr), 2224 PropertyImplementation(None), 2225 GetterName(Selector()), 2226 SetterName(Selector()), 2227 GetterMethodDecl(nullptr), SetterMethodDecl(nullptr), 2228 PropertyIvarDecl(nullptr) {} 2229 2230 public: 2231 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC, 2232 SourceLocation L, 2233 IdentifierInfo *Id, SourceLocation AtLocation, 2234 SourceLocation LParenLocation, 2235 TypeSourceInfo *T, 2236 PropertyControl propControl = None); 2237 2238 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2239 2240 SourceLocation getAtLoc() const { return AtLoc; } 2241 void setAtLoc(SourceLocation L) { AtLoc = L; } 2242 2243 SourceLocation getLParenLoc() const { return LParenLoc; } 2244 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2245 2246 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; } 2247 QualType getType() const { return DeclType->getType(); } 2248 void setType(TypeSourceInfo *T) { DeclType = T; } 2249 2250 PropertyAttributeKind getPropertyAttributes() const { 2251 return PropertyAttributeKind(PropertyAttributes); 2252 } 2253 void setPropertyAttributes(PropertyAttributeKind PRVal) { 2254 PropertyAttributes |= PRVal; 2255 } 2256 2257 PropertyAttributeKind getPropertyAttributesAsWritten() const { 2258 return PropertyAttributeKind(PropertyAttributesAsWritten); 2259 } 2260 2261 bool hasWrittenStorageAttribute() const { 2262 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy | 2263 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong | 2264 OBJC_PR_weak); 2265 } 2266 2267 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) { 2268 PropertyAttributesAsWritten = PRVal; 2269 } 2270 2271 void makeitReadWriteAttribute() { 2272 PropertyAttributes &= ~OBJC_PR_readonly; 2273 PropertyAttributes |= OBJC_PR_readwrite; 2274 } 2275 2276 // Helper methods for accessing attributes. 2277 2278 /// isReadOnly - Return true iff the property has a setter. 2279 bool isReadOnly() const { 2280 return (PropertyAttributes & OBJC_PR_readonly); 2281 } 2282 2283 /// isAtomic - Return true if the property is atomic. 2284 bool isAtomic() const { 2285 return (PropertyAttributes & OBJC_PR_atomic); 2286 } 2287 2288 /// isRetaining - Return true if the property retains its value. 2289 bool isRetaining() const { 2290 return (PropertyAttributes & 2291 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy)); 2292 } 2293 2294 /// getSetterKind - Return the method used for doing assignment in 2295 /// the property setter. This is only valid if the property has been 2296 /// defined to have a setter. 2297 SetterKind getSetterKind() const { 2298 if (PropertyAttributes & OBJC_PR_strong) 2299 return getType()->isBlockPointerType() ? Copy : Retain; 2300 if (PropertyAttributes & OBJC_PR_retain) 2301 return Retain; 2302 if (PropertyAttributes & OBJC_PR_copy) 2303 return Copy; 2304 if (PropertyAttributes & OBJC_PR_weak) 2305 return Weak; 2306 return Assign; 2307 } 2308 2309 Selector getGetterName() const { return GetterName; } 2310 void setGetterName(Selector Sel) { GetterName = Sel; } 2311 2312 Selector getSetterName() const { return SetterName; } 2313 void setSetterName(Selector Sel) { SetterName = Sel; } 2314 2315 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } 2316 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; } 2317 2318 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } 2319 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; } 2320 2321 // Related to \@optional/\@required declared in \@protocol 2322 void setPropertyImplementation(PropertyControl pc) { 2323 PropertyImplementation = pc; 2324 } 2325 PropertyControl getPropertyImplementation() const { 2326 return PropertyControl(PropertyImplementation); 2327 } 2328 2329 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) { 2330 PropertyIvarDecl = Ivar; 2331 } 2332 ObjCIvarDecl *getPropertyIvarDecl() const { 2333 return PropertyIvarDecl; 2334 } 2335 2336 SourceRange getSourceRange() const override LLVM_READONLY { 2337 return SourceRange(AtLoc, getLocation()); 2338 } 2339 2340 /// Get the default name of the synthesized ivar. 2341 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const; 2342 2343 /// Lookup a property by name in the specified DeclContext. 2344 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC, 2345 IdentifierInfo *propertyID); 2346 2347 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2348 static bool classofKind(Kind K) { return K == ObjCProperty; } 2349 }; 2350 2351 /// ObjCPropertyImplDecl - Represents implementation declaration of a property 2352 /// in a class or category implementation block. For example: 2353 /// \@synthesize prop1 = ivar1; 2354 /// 2355 class ObjCPropertyImplDecl : public Decl { 2356 public: 2357 enum Kind { 2358 Synthesize, 2359 Dynamic 2360 }; 2361 private: 2362 SourceLocation AtLoc; // location of \@synthesize or \@dynamic 2363 2364 /// \brief For \@synthesize, the location of the ivar, if it was written in 2365 /// the source code. 2366 /// 2367 /// \code 2368 /// \@synthesize int a = b 2369 /// \endcode 2370 SourceLocation IvarLoc; 2371 2372 /// Property declaration being implemented 2373 ObjCPropertyDecl *PropertyDecl; 2374 2375 /// Null for \@dynamic. Required for \@synthesize. 2376 ObjCIvarDecl *PropertyIvarDecl; 2377 2378 /// Null for \@dynamic. Non-null if property must be copy-constructed in 2379 /// getter. 2380 Expr *GetterCXXConstructor; 2381 2382 /// Null for \@dynamic. Non-null if property has assignment operator to call 2383 /// in Setter synthesis. 2384 Expr *SetterCXXAssignment; 2385 2386 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L, 2387 ObjCPropertyDecl *property, 2388 Kind PK, 2389 ObjCIvarDecl *ivarDecl, 2390 SourceLocation ivarLoc) 2391 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc), 2392 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl), 2393 GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) { 2394 assert (PK == Dynamic || PropertyIvarDecl); 2395 } 2396 2397 public: 2398 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC, 2399 SourceLocation atLoc, SourceLocation L, 2400 ObjCPropertyDecl *property, 2401 Kind PK, 2402 ObjCIvarDecl *ivarDecl, 2403 SourceLocation ivarLoc); 2404 2405 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2406 2407 SourceRange getSourceRange() const override LLVM_READONLY; 2408 2409 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; } 2410 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 2411 2412 ObjCPropertyDecl *getPropertyDecl() const { 2413 return PropertyDecl; 2414 } 2415 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; } 2416 2417 Kind getPropertyImplementation() const { 2418 return PropertyIvarDecl ? Synthesize : Dynamic; 2419 } 2420 2421 ObjCIvarDecl *getPropertyIvarDecl() const { 2422 return PropertyIvarDecl; 2423 } 2424 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; } 2425 2426 void setPropertyIvarDecl(ObjCIvarDecl *Ivar, 2427 SourceLocation IvarLoc) { 2428 PropertyIvarDecl = Ivar; 2429 this->IvarLoc = IvarLoc; 2430 } 2431 2432 /// \brief For \@synthesize, returns true if an ivar name was explicitly 2433 /// specified. 2434 /// 2435 /// \code 2436 /// \@synthesize int a = b; // true 2437 /// \@synthesize int a; // false 2438 /// \endcode 2439 bool isIvarNameSpecified() const { 2440 return IvarLoc.isValid() && IvarLoc != getLocation(); 2441 } 2442 2443 Expr *getGetterCXXConstructor() const { 2444 return GetterCXXConstructor; 2445 } 2446 void setGetterCXXConstructor(Expr *getterCXXConstructor) { 2447 GetterCXXConstructor = getterCXXConstructor; 2448 } 2449 2450 Expr *getSetterCXXAssignment() const { 2451 return SetterCXXAssignment; 2452 } 2453 void setSetterCXXAssignment(Expr *setterCXXAssignment) { 2454 SetterCXXAssignment = setterCXXAssignment; 2455 } 2456 2457 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2458 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; } 2459 2460 friend class ASTDeclReader; 2461 }; 2462 2463 template<bool (*Filter)(ObjCCategoryDecl *)> 2464 void 2465 ObjCInterfaceDecl::filtered_category_iterator<Filter>:: 2466 findAcceptableCategory() { 2467 while (Current && !Filter(Current)) 2468 Current = Current->getNextClassCategoryRaw(); 2469 } 2470 2471 template<bool (*Filter)(ObjCCategoryDecl *)> 2472 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> & 2473 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() { 2474 Current = Current->getNextClassCategoryRaw(); 2475 findAcceptableCategory(); 2476 return *this; 2477 } 2478 2479 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { 2480 return !Cat->isHidden(); 2481 } 2482 2483 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { 2484 return Cat->IsClassExtension() && !Cat->isHidden(); 2485 } 2486 2487 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) { 2488 return Cat->IsClassExtension(); 2489 } 2490 2491 } // end namespace clang 2492 #endif 2493