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