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