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 = {}); 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 ASTDeclMerger; 1751 friend class ASTDeclReader; 1752 friend class ASTDeclWriter; 1753 friend class ASTReader; 1754 1755 /// Iterator that walks over all of the known extensions. 1756 using known_extensions_iterator = 1757 filtered_category_iterator<isKnownExtension>; 1758 using known_extensions_range = 1759 llvm::iterator_range<known_extensions_iterator>; 1760 1761 known_extensions_range known_extensions() const { 1762 return known_extensions_range(known_extensions_begin(), 1763 known_extensions_end()); 1764 } 1765 1766 /// Retrieve an iterator to the beginning of the known-extensions 1767 /// list. 1768 known_extensions_iterator known_extensions_begin() const { 1769 return known_extensions_iterator(getCategoryListRaw()); 1770 } 1771 1772 /// Retrieve an iterator to the end of the known-extensions list. 1773 known_extensions_iterator known_extensions_end() const { 1774 return known_extensions_iterator(); 1775 } 1776 1777 /// Determine whether the known-extensions list is empty. 1778 bool known_extensions_empty() const { 1779 return known_extensions_begin() == known_extensions_end(); 1780 } 1781 1782 /// Retrieve the raw pointer to the start of the category/extension 1783 /// list. 1784 ObjCCategoryDecl* getCategoryListRaw() const { 1785 // FIXME: Should make sure no callers ever do this. 1786 if (!hasDefinition()) 1787 return nullptr; 1788 1789 if (data().ExternallyCompleted) 1790 LoadExternalDefinition(); 1791 1792 return data().CategoryList; 1793 } 1794 1795 /// Set the raw pointer to the start of the category/extension 1796 /// list. 1797 void setCategoryListRaw(ObjCCategoryDecl *category) { 1798 data().CategoryList = category; 1799 } 1800 1801 ObjCPropertyDecl * 1802 FindPropertyVisibleInPrimaryClass(const IdentifierInfo *PropertyId, 1803 ObjCPropertyQueryKind QueryKind) const; 1804 1805 void collectPropertiesToImplement(PropertyMap &PM) const override; 1806 1807 /// isSuperClassOf - Return true if this class is the specified class or is a 1808 /// super class of the specified interface class. 1809 bool isSuperClassOf(const ObjCInterfaceDecl *I) const { 1810 // If RHS is derived from LHS it is OK; else it is not OK. 1811 while (I != nullptr) { 1812 if (declaresSameEntity(this, I)) 1813 return true; 1814 1815 I = I->getSuperClass(); 1816 } 1817 return false; 1818 } 1819 1820 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes 1821 /// to be incompatible with __weak references. Returns true if it is. 1822 bool isArcWeakrefUnavailable() const; 1823 1824 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super 1825 /// classes must not be auto-synthesized. Returns class decl. if it must not 1826 /// be; 0, otherwise. 1827 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const; 1828 1829 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName, 1830 ObjCInterfaceDecl *&ClassDeclared); 1831 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) { 1832 ObjCInterfaceDecl *ClassDeclared; 1833 return lookupInstanceVariable(IVarName, ClassDeclared); 1834 } 1835 1836 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name); 1837 1838 // Lookup a method. First, we search locally. If a method isn't 1839 // found, we search referenced protocols and class categories. 1840 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance, 1841 bool shallowCategoryLookup = false, 1842 bool followSuper = true, 1843 const ObjCCategoryDecl *C = nullptr) const; 1844 1845 /// Lookup an instance method for a given selector. 1846 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 1847 return lookupMethod(Sel, true/*isInstance*/); 1848 } 1849 1850 /// Lookup a class method for a given selector. 1851 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 1852 return lookupMethod(Sel, false/*isInstance*/); 1853 } 1854 1855 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName); 1856 1857 /// Lookup a method in the classes implementation hierarchy. 1858 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, 1859 bool Instance=true) const; 1860 1861 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) { 1862 return lookupPrivateMethod(Sel, false); 1863 } 1864 1865 /// Lookup a setter or getter in the class hierarchy, 1866 /// including in all categories except for category passed 1867 /// as argument. 1868 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel, 1869 const ObjCCategoryDecl *Cat, 1870 bool IsClassProperty) const { 1871 return lookupMethod(Sel, !IsClassProperty/*isInstance*/, 1872 false/*shallowCategoryLookup*/, 1873 true /* followsSuper */, 1874 Cat); 1875 } 1876 1877 SourceLocation getEndOfDefinitionLoc() const { 1878 if (!hasDefinition()) 1879 return getLocation(); 1880 1881 return data().EndLoc; 1882 } 1883 1884 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; } 1885 1886 /// Retrieve the starting location of the superclass. 1887 SourceLocation getSuperClassLoc() const; 1888 1889 /// isImplicitInterfaceDecl - check that this is an implicitly declared 1890 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation 1891 /// declaration without an \@interface declaration. 1892 bool isImplicitInterfaceDecl() const { 1893 return hasDefinition() ? data().Definition->isImplicit() : isImplicit(); 1894 } 1895 1896 /// ClassImplementsProtocol - Checks that 'lProto' protocol 1897 /// has been implemented in IDecl class, its super class or categories (if 1898 /// lookupCategory is true). 1899 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, 1900 bool lookupCategory, 1901 bool RHSIsQualifiedID = false); 1902 1903 using redecl_range = redeclarable_base::redecl_range; 1904 using redecl_iterator = redeclarable_base::redecl_iterator; 1905 1906 using redeclarable_base::redecls_begin; 1907 using redeclarable_base::redecls_end; 1908 using redeclarable_base::redecls; 1909 using redeclarable_base::getPreviousDecl; 1910 using redeclarable_base::getMostRecentDecl; 1911 using redeclarable_base::isFirstDecl; 1912 1913 /// Retrieves the canonical declaration of this Objective-C class. 1914 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); } 1915 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); } 1916 1917 // Low-level accessor 1918 const Type *getTypeForDecl() const { return TypeForDecl; } 1919 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; } 1920 1921 /// Get precomputed ODRHash or add a new one. 1922 unsigned getODRHash(); 1923 1924 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1925 static bool classofKind(Kind K) { return K == ObjCInterface; } 1926 1927 private: 1928 /// True if a valid hash is stored in ODRHash. 1929 bool hasODRHash() const; 1930 void setHasODRHash(bool HasHash); 1931 1932 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const; 1933 bool inheritsDesignatedInitializers() const; 1934 }; 1935 1936 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC 1937 /// instance variables are identical to C. The only exception is Objective-C 1938 /// supports C++ style access control. For example: 1939 /// 1940 /// \@interface IvarExample : NSObject 1941 /// { 1942 /// id defaultToProtected; 1943 /// \@public: 1944 /// id canBePublic; // same as C++. 1945 /// \@protected: 1946 /// id canBeProtected; // same as C++. 1947 /// \@package: 1948 /// id canBePackage; // framework visibility (not available in C++). 1949 /// } 1950 /// 1951 class ObjCIvarDecl : public FieldDecl { 1952 void anchor() override; 1953 1954 public: 1955 enum AccessControl { 1956 None, Private, Protected, Public, Package 1957 }; 1958 1959 private: 1960 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc, 1961 SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, 1962 TypeSourceInfo *TInfo, AccessControl ac, Expr *BW, 1963 bool synthesized) 1964 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, 1965 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit), 1966 DeclAccess(ac), Synthesized(synthesized) {} 1967 1968 public: 1969 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC, 1970 SourceLocation StartLoc, SourceLocation IdLoc, 1971 const IdentifierInfo *Id, QualType T, 1972 TypeSourceInfo *TInfo, AccessControl ac, 1973 Expr *BW = nullptr, bool synthesized = false); 1974 1975 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); 1976 1977 /// Return the class interface that this ivar is logically contained 1978 /// in; this is either the interface where the ivar was declared, or the 1979 /// interface the ivar is conceptually a part of in the case of synthesized 1980 /// ivars. 1981 ObjCInterfaceDecl *getContainingInterface(); 1982 const ObjCInterfaceDecl *getContainingInterface() const { 1983 return const_cast<ObjCIvarDecl *>(this)->getContainingInterface(); 1984 } 1985 1986 ObjCIvarDecl *getNextIvar() { return NextIvar; } 1987 const ObjCIvarDecl *getNextIvar() const { return NextIvar; } 1988 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; } 1989 1990 ObjCIvarDecl *getCanonicalDecl() override { 1991 return cast<ObjCIvarDecl>(FieldDecl::getCanonicalDecl()); 1992 } 1993 const ObjCIvarDecl *getCanonicalDecl() const { 1994 return const_cast<ObjCIvarDecl *>(this)->getCanonicalDecl(); 1995 } 1996 1997 void setAccessControl(AccessControl ac) { DeclAccess = ac; } 1998 1999 AccessControl getAccessControl() const { return AccessControl(DeclAccess); } 2000 2001 AccessControl getCanonicalAccessControl() const { 2002 return DeclAccess == None ? Protected : AccessControl(DeclAccess); 2003 } 2004 2005 void setSynthesize(bool synth) { Synthesized = synth; } 2006 bool getSynthesize() const { return Synthesized; } 2007 2008 /// Retrieve the type of this instance variable when viewed as a member of a 2009 /// specific object type. 2010 QualType getUsageType(QualType objectType) const; 2011 2012 // Implement isa/cast/dyncast/etc. 2013 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2014 static bool classofKind(Kind K) { return K == ObjCIvar; } 2015 2016 private: 2017 /// NextIvar - Next Ivar in the list of ivars declared in class; class's 2018 /// extensions and class's implementation 2019 ObjCIvarDecl *NextIvar = nullptr; 2020 2021 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum 2022 LLVM_PREFERRED_TYPE(AccessControl) 2023 unsigned DeclAccess : 3; 2024 LLVM_PREFERRED_TYPE(bool) 2025 unsigned Synthesized : 1; 2026 }; 2027 2028 /// Represents a field declaration created by an \@defs(...). 2029 class ObjCAtDefsFieldDecl : public FieldDecl { 2030 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc, 2031 SourceLocation IdLoc, IdentifierInfo *Id, 2032 QualType T, Expr *BW) 2033 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T, 2034 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ? 2035 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {} 2036 2037 void anchor() override; 2038 2039 public: 2040 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC, 2041 SourceLocation StartLoc, 2042 SourceLocation IdLoc, IdentifierInfo *Id, 2043 QualType T, Expr *BW); 2044 2045 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, 2046 GlobalDeclID ID); 2047 2048 // Implement isa/cast/dyncast/etc. 2049 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2050 static bool classofKind(Kind K) { return K == ObjCAtDefsField; } 2051 }; 2052 2053 /// Represents an Objective-C protocol declaration. 2054 /// 2055 /// Objective-C protocols declare a pure abstract type (i.e., no instance 2056 /// variables are permitted). Protocols originally drew inspiration from 2057 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy 2058 /// syntax:-). Here is an example: 2059 /// 2060 /// \code 2061 /// \@protocol NSDraggingInfo <refproto1, refproto2> 2062 /// - (NSWindow *)draggingDestinationWindow; 2063 /// - (NSImage *)draggedImage; 2064 /// \@end 2065 /// \endcode 2066 /// 2067 /// This says that NSDraggingInfo requires two methods and requires everything 2068 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as 2069 /// well. 2070 /// 2071 /// \code 2072 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo> 2073 /// \@end 2074 /// \endcode 2075 /// 2076 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and 2077 /// protocols are in distinct namespaces. For example, Cocoa defines both 2078 /// an NSObject protocol and class (which isn't allowed in Java). As a result, 2079 /// protocols are referenced using angle brackets as follows: 2080 /// 2081 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo; 2082 class ObjCProtocolDecl : public ObjCContainerDecl, 2083 public Redeclarable<ObjCProtocolDecl> { 2084 struct DefinitionData { 2085 // The declaration that defines this protocol. 2086 ObjCProtocolDecl *Definition; 2087 2088 /// Referenced protocols 2089 ObjCProtocolList ReferencedProtocols; 2090 2091 /// Tracks whether a ODR hash has been computed for this protocol. 2092 LLVM_PREFERRED_TYPE(bool) 2093 unsigned HasODRHash : 1; 2094 2095 /// A hash of parts of the class to help in ODR checking. 2096 unsigned ODRHash = 0; 2097 }; 2098 2099 /// Contains a pointer to the data associated with this class, 2100 /// which will be NULL if this class has not yet been defined. 2101 /// 2102 /// The bit indicates when we don't need to check for out-of-date 2103 /// declarations. It will be set unless modules are enabled. 2104 llvm::PointerIntPair<DefinitionData *, 1, bool> Data; 2105 2106 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id, 2107 SourceLocation nameLoc, SourceLocation atStartLoc, 2108 ObjCProtocolDecl *PrevDecl); 2109 2110 void anchor() override; 2111 2112 DefinitionData &data() const { 2113 assert(Data.getPointer() && "Objective-C protocol has no definition!"); 2114 return *Data.getPointer(); 2115 } 2116 2117 void allocateDefinitionData(); 2118 2119 using redeclarable_base = Redeclarable<ObjCProtocolDecl>; 2120 2121 ObjCProtocolDecl *getNextRedeclarationImpl() override { 2122 return getNextRedeclaration(); 2123 } 2124 2125 ObjCProtocolDecl *getPreviousDeclImpl() override { 2126 return getPreviousDecl(); 2127 } 2128 2129 ObjCProtocolDecl *getMostRecentDeclImpl() override { 2130 return getMostRecentDecl(); 2131 } 2132 2133 /// True if a valid hash is stored in ODRHash. 2134 bool hasODRHash() const; 2135 void setHasODRHash(bool HasHash); 2136 2137 public: 2138 friend class ASTDeclMerger; 2139 friend class ASTDeclReader; 2140 friend class ASTDeclWriter; 2141 friend class ASTReader; 2142 friend class ODRDiagsEmitter; 2143 2144 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC, 2145 IdentifierInfo *Id, 2146 SourceLocation nameLoc, 2147 SourceLocation atStartLoc, 2148 ObjCProtocolDecl *PrevDecl); 2149 2150 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); 2151 2152 const ObjCProtocolList &getReferencedProtocols() const { 2153 assert(hasDefinition() && "No definition available!"); 2154 return data().ReferencedProtocols; 2155 } 2156 2157 using protocol_iterator = ObjCProtocolList::iterator; 2158 using protocol_range = llvm::iterator_range<protocol_iterator>; 2159 2160 protocol_range protocols() const { 2161 return protocol_range(protocol_begin(), protocol_end()); 2162 } 2163 2164 protocol_iterator protocol_begin() const { 2165 if (!hasDefinition()) 2166 return protocol_iterator(); 2167 2168 return data().ReferencedProtocols.begin(); 2169 } 2170 2171 protocol_iterator protocol_end() const { 2172 if (!hasDefinition()) 2173 return protocol_iterator(); 2174 2175 return data().ReferencedProtocols.end(); 2176 } 2177 2178 using protocol_loc_iterator = ObjCProtocolList::loc_iterator; 2179 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>; 2180 2181 protocol_loc_range protocol_locs() const { 2182 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 2183 } 2184 2185 protocol_loc_iterator protocol_loc_begin() const { 2186 if (!hasDefinition()) 2187 return protocol_loc_iterator(); 2188 2189 return data().ReferencedProtocols.loc_begin(); 2190 } 2191 2192 protocol_loc_iterator protocol_loc_end() const { 2193 if (!hasDefinition()) 2194 return protocol_loc_iterator(); 2195 2196 return data().ReferencedProtocols.loc_end(); 2197 } 2198 2199 unsigned protocol_size() const { 2200 if (!hasDefinition()) 2201 return 0; 2202 2203 return data().ReferencedProtocols.size(); 2204 } 2205 2206 /// setProtocolList - Set the list of protocols that this interface 2207 /// implements. 2208 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 2209 const SourceLocation *Locs, ASTContext &C) { 2210 assert(hasDefinition() && "Protocol is not defined"); 2211 data().ReferencedProtocols.set(List, Num, Locs, C); 2212 } 2213 2214 /// This is true iff the protocol is tagged with the 2215 /// `objc_non_runtime_protocol` attribute. 2216 bool isNonRuntimeProtocol() const; 2217 2218 /// Get the set of all protocols implied by this protocols inheritance 2219 /// hierarchy. 2220 void getImpliedProtocols(llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const; 2221 2222 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName); 2223 2224 // Lookup a method. First, we search locally. If a method isn't 2225 // found, we search referenced protocols and class categories. 2226 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const; 2227 2228 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 2229 return lookupMethod(Sel, true/*isInstance*/); 2230 } 2231 2232 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 2233 return lookupMethod(Sel, false/*isInstance*/); 2234 } 2235 2236 /// Determine whether this protocol has a definition. 2237 bool hasDefinition() const { 2238 // If the name of this protocol is out-of-date, bring it up-to-date, which 2239 // might bring in a definition. 2240 // Note: a null value indicates that we don't have a definition and that 2241 // modules are enabled. 2242 if (!Data.getOpaqueValue()) 2243 getMostRecentDecl(); 2244 2245 return Data.getPointer(); 2246 } 2247 2248 /// Retrieve the definition of this protocol, if any. 2249 ObjCProtocolDecl *getDefinition() { 2250 return hasDefinition()? Data.getPointer()->Definition : nullptr; 2251 } 2252 2253 /// Retrieve the definition of this protocol, if any. 2254 const ObjCProtocolDecl *getDefinition() const { 2255 return hasDefinition()? Data.getPointer()->Definition : nullptr; 2256 } 2257 2258 /// Determine whether this particular declaration is also the 2259 /// definition. 2260 bool isThisDeclarationADefinition() const { 2261 return getDefinition() == this; 2262 } 2263 2264 /// Starts the definition of this Objective-C protocol. 2265 void startDefinition(); 2266 2267 /// Starts the definition without sharing it with other redeclarations. 2268 /// Such definition shouldn't be used for anything but only to compare if 2269 /// a duplicate is compatible with previous definition or if it is 2270 /// a distinct duplicate. 2271 void startDuplicateDefinitionForComparison(); 2272 void mergeDuplicateDefinitionWithCommon(const ObjCProtocolDecl *Definition); 2273 2274 /// Produce a name to be used for protocol's metadata. It comes either via 2275 /// objc_runtime_name attribute or protocol name. 2276 StringRef getObjCRuntimeNameAsString() const; 2277 2278 SourceRange getSourceRange() const override LLVM_READONLY { 2279 if (isThisDeclarationADefinition()) 2280 return ObjCContainerDecl::getSourceRange(); 2281 2282 return SourceRange(getAtStartLoc(), getLocation()); 2283 } 2284 2285 using redecl_range = redeclarable_base::redecl_range; 2286 using redecl_iterator = redeclarable_base::redecl_iterator; 2287 2288 using redeclarable_base::redecls_begin; 2289 using redeclarable_base::redecls_end; 2290 using redeclarable_base::redecls; 2291 using redeclarable_base::getPreviousDecl; 2292 using redeclarable_base::getMostRecentDecl; 2293 using redeclarable_base::isFirstDecl; 2294 2295 /// Retrieves the canonical declaration of this Objective-C protocol. 2296 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); } 2297 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); } 2298 2299 void collectPropertiesToImplement(PropertyMap &PM) const override; 2300 2301 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property, 2302 ProtocolPropertySet &PS, 2303 PropertyDeclOrder &PO) const; 2304 2305 /// Get precomputed ODRHash or add a new one. 2306 unsigned getODRHash(); 2307 2308 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2309 static bool classofKind(Kind K) { return K == ObjCProtocol; } 2310 }; 2311 2312 /// ObjCCategoryDecl - Represents a category declaration. A category allows 2313 /// you to add methods to an existing class (without subclassing or modifying 2314 /// the original class interface or implementation:-). Categories don't allow 2315 /// you to add instance data. The following example adds "myMethod" to all 2316 /// NSView's within a process: 2317 /// 2318 /// \@interface NSView (MyViewMethods) 2319 /// - myMethod; 2320 /// \@end 2321 /// 2322 /// Categories also allow you to split the implementation of a class across 2323 /// several files (a feature more naturally supported in C++). 2324 /// 2325 /// Categories were originally inspired by dynamic languages such as Common 2326 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 2327 /// don't support this level of dynamism, which is both powerful and dangerous. 2328 class ObjCCategoryDecl : public ObjCContainerDecl { 2329 /// Interface belonging to this category 2330 ObjCInterfaceDecl *ClassInterface; 2331 2332 /// The type parameters associated with this category, if any. 2333 ObjCTypeParamList *TypeParamList = nullptr; 2334 2335 /// referenced protocols in this category. 2336 ObjCProtocolList ReferencedProtocols; 2337 2338 /// Next category belonging to this class. 2339 /// FIXME: this should not be a singly-linked list. Move storage elsewhere. 2340 ObjCCategoryDecl *NextClassCategory = nullptr; 2341 2342 /// The location of the category name in this declaration. 2343 SourceLocation CategoryNameLoc; 2344 2345 /// class extension may have private ivars. 2346 SourceLocation IvarLBraceLoc; 2347 SourceLocation IvarRBraceLoc; 2348 2349 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, 2350 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, 2351 const IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, 2352 ObjCTypeParamList *typeParamList, 2353 SourceLocation IvarLBraceLoc = SourceLocation(), 2354 SourceLocation IvarRBraceLoc = SourceLocation()); 2355 2356 void anchor() override; 2357 2358 public: 2359 friend class ASTDeclReader; 2360 friend class ASTDeclWriter; 2361 2362 static ObjCCategoryDecl * 2363 Create(ASTContext &C, DeclContext *DC, SourceLocation AtLoc, 2364 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, 2365 const IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, 2366 ObjCTypeParamList *typeParamList, 2367 SourceLocation IvarLBraceLoc = SourceLocation(), 2368 SourceLocation IvarRBraceLoc = SourceLocation()); 2369 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); 2370 2371 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 2372 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 2373 2374 /// Retrieve the type parameter list associated with this category or 2375 /// extension. 2376 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; } 2377 2378 /// Set the type parameters of this category. 2379 /// 2380 /// This function is used by the AST importer, which must import the type 2381 /// parameters after creating their DeclContext to avoid loops. 2382 void setTypeParamList(ObjCTypeParamList *TPL); 2383 2384 2385 ObjCCategoryImplDecl *getImplementation() const; 2386 void setImplementation(ObjCCategoryImplDecl *ImplD); 2387 2388 /// setProtocolList - Set the list of protocols that this interface 2389 /// implements. 2390 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 2391 const SourceLocation *Locs, ASTContext &C) { 2392 ReferencedProtocols.set(List, Num, Locs, C); 2393 } 2394 2395 const ObjCProtocolList &getReferencedProtocols() const { 2396 return ReferencedProtocols; 2397 } 2398 2399 using protocol_iterator = ObjCProtocolList::iterator; 2400 using protocol_range = llvm::iterator_range<protocol_iterator>; 2401 2402 protocol_range protocols() const { 2403 return protocol_range(protocol_begin(), protocol_end()); 2404 } 2405 2406 protocol_iterator protocol_begin() const { 2407 return ReferencedProtocols.begin(); 2408 } 2409 2410 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); } 2411 unsigned protocol_size() const { return ReferencedProtocols.size(); } 2412 2413 using protocol_loc_iterator = ObjCProtocolList::loc_iterator; 2414 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>; 2415 2416 protocol_loc_range protocol_locs() const { 2417 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 2418 } 2419 2420 protocol_loc_iterator protocol_loc_begin() const { 2421 return ReferencedProtocols.loc_begin(); 2422 } 2423 2424 protocol_loc_iterator protocol_loc_end() const { 2425 return ReferencedProtocols.loc_end(); 2426 } 2427 2428 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } 2429 2430 /// Retrieve the pointer to the next stored category (or extension), 2431 /// which may be hidden. 2432 ObjCCategoryDecl *getNextClassCategoryRaw() const { 2433 return NextClassCategory; 2434 } 2435 2436 bool IsClassExtension() const { return getIdentifier() == nullptr; } 2437 2438 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>; 2439 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>; 2440 2441 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 2442 2443 ivar_iterator ivar_begin() const { 2444 return ivar_iterator(decls_begin()); 2445 } 2446 2447 ivar_iterator ivar_end() const { 2448 return ivar_iterator(decls_end()); 2449 } 2450 2451 unsigned ivar_size() const { 2452 return std::distance(ivar_begin(), ivar_end()); 2453 } 2454 2455 bool ivar_empty() const { 2456 return ivar_begin() == ivar_end(); 2457 } 2458 2459 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 2460 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; } 2461 2462 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 2463 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 2464 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 2465 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 2466 2467 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2468 static bool classofKind(Kind K) { return K == ObjCCategory; } 2469 }; 2470 2471 class ObjCImplDecl : public ObjCContainerDecl { 2472 /// Class interface for this class/category implementation 2473 ObjCInterfaceDecl *ClassInterface; 2474 2475 void anchor() override; 2476 2477 protected: 2478 ObjCImplDecl(Kind DK, DeclContext *DC, ObjCInterfaceDecl *classInterface, 2479 const IdentifierInfo *Id, SourceLocation nameLoc, 2480 SourceLocation atStartLoc) 2481 : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc), 2482 ClassInterface(classInterface) {} 2483 2484 public: 2485 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 2486 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 2487 void setClassInterface(ObjCInterfaceDecl *IFace); 2488 2489 void addInstanceMethod(ObjCMethodDecl *method) { 2490 // FIXME: Context should be set correctly before we get here. 2491 method->setLexicalDeclContext(this); 2492 addDecl(method); 2493 } 2494 2495 void addClassMethod(ObjCMethodDecl *method) { 2496 // FIXME: Context should be set correctly before we get here. 2497 method->setLexicalDeclContext(this); 2498 addDecl(method); 2499 } 2500 2501 void addPropertyImplementation(ObjCPropertyImplDecl *property); 2502 2503 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId, 2504 ObjCPropertyQueryKind queryKind) const; 2505 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; 2506 2507 // Iterator access to properties. 2508 using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>; 2509 using propimpl_range = 2510 llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>; 2511 2512 propimpl_range property_impls() const { 2513 return propimpl_range(propimpl_begin(), propimpl_end()); 2514 } 2515 2516 propimpl_iterator propimpl_begin() const { 2517 return propimpl_iterator(decls_begin()); 2518 } 2519 2520 propimpl_iterator propimpl_end() const { 2521 return propimpl_iterator(decls_end()); 2522 } 2523 2524 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2525 2526 static bool classofKind(Kind K) { 2527 return K >= firstObjCImpl && K <= lastObjCImpl; 2528 } 2529 }; 2530 2531 /// ObjCCategoryImplDecl - An object of this class encapsulates a category 2532 /// \@implementation declaration. If a category class has declaration of a 2533 /// property, its implementation must be specified in the category's 2534 /// \@implementation declaration. Example: 2535 /// \@interface I \@end 2536 /// \@interface I(CATEGORY) 2537 /// \@property int p1, d1; 2538 /// \@end 2539 /// \@implementation I(CATEGORY) 2540 /// \@dynamic p1,d1; 2541 /// \@end 2542 /// 2543 /// ObjCCategoryImplDecl 2544 class ObjCCategoryImplDecl : public ObjCImplDecl { 2545 // Category name location 2546 SourceLocation CategoryNameLoc; 2547 2548 ObjCCategoryImplDecl(DeclContext *DC, const IdentifierInfo *Id, 2549 ObjCInterfaceDecl *classInterface, 2550 SourceLocation nameLoc, SourceLocation atStartLoc, 2551 SourceLocation CategoryNameLoc) 2552 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id, nameLoc, 2553 atStartLoc), 2554 CategoryNameLoc(CategoryNameLoc) {} 2555 2556 void anchor() override; 2557 2558 public: 2559 friend class ASTDeclReader; 2560 friend class ASTDeclWriter; 2561 2562 static ObjCCategoryImplDecl * 2563 Create(ASTContext &C, DeclContext *DC, const IdentifierInfo *Id, 2564 ObjCInterfaceDecl *classInterface, SourceLocation nameLoc, 2565 SourceLocation atStartLoc, SourceLocation CategoryNameLoc); 2566 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, 2567 GlobalDeclID ID); 2568 2569 ObjCCategoryDecl *getCategoryDecl() const; 2570 2571 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 2572 2573 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2574 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;} 2575 }; 2576 2577 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID); 2578 2579 /// ObjCImplementationDecl - Represents a class definition - this is where 2580 /// method definitions are specified. For example: 2581 /// 2582 /// @code 2583 /// \@implementation MyClass 2584 /// - (void)myMethod { /* do something */ } 2585 /// \@end 2586 /// @endcode 2587 /// 2588 /// In a non-fragile runtime, instance variables can appear in the class 2589 /// interface, class extensions (nameless categories), and in the implementation 2590 /// itself, as well as being synthesized as backing storage for properties. 2591 /// 2592 /// In a fragile runtime, instance variables are specified in the class 2593 /// interface, \em not in the implementation. Nevertheless (for legacy reasons), 2594 /// we allow instance variables to be specified in the implementation. When 2595 /// specified, they need to be \em identical to the interface. 2596 class ObjCImplementationDecl : public ObjCImplDecl { 2597 /// Implementation Class's super class. 2598 ObjCInterfaceDecl *SuperClass; 2599 SourceLocation SuperLoc; 2600 2601 /// \@implementation may have private ivars. 2602 SourceLocation IvarLBraceLoc; 2603 SourceLocation IvarRBraceLoc; 2604 2605 /// Support for ivar initialization. 2606 /// The arguments used to initialize the ivars 2607 LazyCXXCtorInitializersPtr IvarInitializers; 2608 unsigned NumIvarInitializers = 0; 2609 2610 /// Do the ivars of this class require initialization other than 2611 /// zero-initialization? 2612 LLVM_PREFERRED_TYPE(bool) 2613 bool HasNonZeroConstructors : 1; 2614 2615 /// Do the ivars of this class require non-trivial destruction? 2616 LLVM_PREFERRED_TYPE(bool) 2617 bool HasDestructors : 1; 2618 2619 ObjCImplementationDecl(DeclContext *DC, 2620 ObjCInterfaceDecl *classInterface, 2621 ObjCInterfaceDecl *superDecl, 2622 SourceLocation nameLoc, SourceLocation atStartLoc, 2623 SourceLocation superLoc = SourceLocation(), 2624 SourceLocation IvarLBraceLoc=SourceLocation(), 2625 SourceLocation IvarRBraceLoc=SourceLocation()) 2626 : ObjCImplDecl(ObjCImplementation, DC, classInterface, 2627 classInterface ? classInterface->getIdentifier() 2628 : nullptr, 2629 nameLoc, atStartLoc), 2630 SuperClass(superDecl), SuperLoc(superLoc), 2631 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc), 2632 HasNonZeroConstructors(false), HasDestructors(false) {} 2633 2634 void anchor() override; 2635 2636 public: 2637 friend class ASTDeclReader; 2638 friend class ASTDeclWriter; 2639 2640 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, 2641 ObjCInterfaceDecl *classInterface, 2642 ObjCInterfaceDecl *superDecl, 2643 SourceLocation nameLoc, 2644 SourceLocation atStartLoc, 2645 SourceLocation superLoc = SourceLocation(), 2646 SourceLocation IvarLBraceLoc=SourceLocation(), 2647 SourceLocation IvarRBraceLoc=SourceLocation()); 2648 2649 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, 2650 GlobalDeclID ID); 2651 2652 /// init_iterator - Iterates through the ivar initializer list. 2653 using init_iterator = CXXCtorInitializer **; 2654 2655 /// init_const_iterator - Iterates through the ivar initializer list. 2656 using init_const_iterator = CXXCtorInitializer * const *; 2657 2658 using init_range = llvm::iterator_range<init_iterator>; 2659 using init_const_range = llvm::iterator_range<init_const_iterator>; 2660 2661 init_range inits() { return init_range(init_begin(), init_end()); } 2662 2663 init_const_range inits() const { 2664 return init_const_range(init_begin(), init_end()); 2665 } 2666 2667 /// init_begin() - Retrieve an iterator to the first initializer. 2668 init_iterator init_begin() { 2669 const auto *ConstThis = this; 2670 return const_cast<init_iterator>(ConstThis->init_begin()); 2671 } 2672 2673 /// begin() - Retrieve an iterator to the first initializer. 2674 init_const_iterator init_begin() const; 2675 2676 /// init_end() - Retrieve an iterator past the last initializer. 2677 init_iterator init_end() { 2678 return init_begin() + NumIvarInitializers; 2679 } 2680 2681 /// end() - Retrieve an iterator past the last initializer. 2682 init_const_iterator init_end() const { 2683 return init_begin() + NumIvarInitializers; 2684 } 2685 2686 /// getNumArgs - Number of ivars which must be initialized. 2687 unsigned getNumIvarInitializers() const { 2688 return NumIvarInitializers; 2689 } 2690 2691 void setNumIvarInitializers(unsigned numNumIvarInitializers) { 2692 NumIvarInitializers = numNumIvarInitializers; 2693 } 2694 2695 void setIvarInitializers(ASTContext &C, 2696 CXXCtorInitializer ** initializers, 2697 unsigned numInitializers); 2698 2699 /// Do any of the ivars of this class (not counting its base classes) 2700 /// require construction other than zero-initialization? 2701 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; } 2702 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; } 2703 2704 /// Do any of the ivars of this class (not counting its base classes) 2705 /// require non-trivial destruction? 2706 bool hasDestructors() const { return HasDestructors; } 2707 void setHasDestructors(bool val) { HasDestructors = val; } 2708 2709 /// getIdentifier - Get the identifier that names the class 2710 /// interface associated with this implementation. 2711 IdentifierInfo *getIdentifier() const { 2712 return getClassInterface()->getIdentifier(); 2713 } 2714 2715 /// getName - Get the name of identifier for the class interface associated 2716 /// with this implementation as a StringRef. 2717 // 2718 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different 2719 // meaning. 2720 StringRef getName() const { 2721 assert(getIdentifier() && "Name is not a simple identifier"); 2722 return getIdentifier()->getName(); 2723 } 2724 2725 /// Get the name of the class associated with this interface. 2726 // 2727 // FIXME: Move to StringRef API. 2728 std::string getNameAsString() const { return std::string(getName()); } 2729 2730 /// Produce a name to be used for class's metadata. It comes either via 2731 /// class's objc_runtime_name attribute or class name. 2732 StringRef getObjCRuntimeNameAsString() const; 2733 2734 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } 2735 ObjCInterfaceDecl *getSuperClass() { return SuperClass; } 2736 SourceLocation getSuperClassLoc() const { return SuperLoc; } 2737 2738 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } 2739 2740 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 2741 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 2742 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 2743 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 2744 2745 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>; 2746 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>; 2747 2748 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 2749 2750 ivar_iterator ivar_begin() const { 2751 return ivar_iterator(decls_begin()); 2752 } 2753 2754 ivar_iterator ivar_end() const { 2755 return ivar_iterator(decls_end()); 2756 } 2757 2758 unsigned ivar_size() const { 2759 return std::distance(ivar_begin(), ivar_end()); 2760 } 2761 2762 bool ivar_empty() const { 2763 return ivar_begin() == ivar_end(); 2764 } 2765 2766 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2767 static bool classofKind(Kind K) { return K == ObjCImplementation; } 2768 }; 2769 2770 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID); 2771 2772 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is 2773 /// declared as \@compatibility_alias alias class. 2774 class ObjCCompatibleAliasDecl : public NamedDecl { 2775 /// Class that this is an alias of. 2776 ObjCInterfaceDecl *AliasedClass; 2777 2778 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 2779 ObjCInterfaceDecl* aliasedClass) 2780 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {} 2781 2782 void anchor() override; 2783 2784 public: 2785 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC, 2786 SourceLocation L, IdentifierInfo *Id, 2787 ObjCInterfaceDecl* aliasedClass); 2788 2789 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, 2790 GlobalDeclID ID); 2791 2792 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } 2793 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; } 2794 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; } 2795 2796 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2797 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; } 2798 }; 2799 2800 /// ObjCPropertyImplDecl - Represents implementation declaration of a property 2801 /// in a class or category implementation block. For example: 2802 /// \@synthesize prop1 = ivar1; 2803 /// 2804 class ObjCPropertyImplDecl : public Decl { 2805 public: 2806 enum Kind { 2807 Synthesize, 2808 Dynamic 2809 }; 2810 2811 private: 2812 SourceLocation AtLoc; // location of \@synthesize or \@dynamic 2813 2814 /// For \@synthesize, the location of the ivar, if it was written in 2815 /// the source code. 2816 /// 2817 /// \code 2818 /// \@synthesize int a = b 2819 /// \endcode 2820 SourceLocation IvarLoc; 2821 2822 /// Property declaration being implemented 2823 ObjCPropertyDecl *PropertyDecl; 2824 2825 /// Null for \@dynamic. Required for \@synthesize. 2826 ObjCIvarDecl *PropertyIvarDecl; 2827 2828 /// The getter's definition, which has an empty body if synthesized. 2829 ObjCMethodDecl *GetterMethodDecl = nullptr; 2830 /// The getter's definition, which has an empty body if synthesized. 2831 ObjCMethodDecl *SetterMethodDecl = nullptr; 2832 2833 /// Null for \@dynamic. Non-null if property must be copy-constructed in 2834 /// getter. 2835 Expr *GetterCXXConstructor = nullptr; 2836 2837 /// Null for \@dynamic. Non-null if property has assignment operator to call 2838 /// in Setter synthesis. 2839 Expr *SetterCXXAssignment = nullptr; 2840 2841 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L, 2842 ObjCPropertyDecl *property, 2843 Kind PK, 2844 ObjCIvarDecl *ivarDecl, 2845 SourceLocation ivarLoc) 2846 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc), 2847 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) { 2848 assert(PK == Dynamic || PropertyIvarDecl); 2849 } 2850 2851 public: 2852 friend class ASTDeclReader; 2853 2854 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC, 2855 SourceLocation atLoc, SourceLocation L, 2856 ObjCPropertyDecl *property, 2857 Kind PK, 2858 ObjCIvarDecl *ivarDecl, 2859 SourceLocation ivarLoc); 2860 2861 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, 2862 GlobalDeclID ID); 2863 2864 SourceRange getSourceRange() const override LLVM_READONLY; 2865 2866 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; } 2867 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 2868 2869 ObjCPropertyDecl *getPropertyDecl() const { 2870 return PropertyDecl; 2871 } 2872 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; } 2873 2874 Kind getPropertyImplementation() const { 2875 return PropertyIvarDecl ? Synthesize : Dynamic; 2876 } 2877 2878 ObjCIvarDecl *getPropertyIvarDecl() const { 2879 return PropertyIvarDecl; 2880 } 2881 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; } 2882 2883 void setPropertyIvarDecl(ObjCIvarDecl *Ivar, 2884 SourceLocation IvarLoc) { 2885 PropertyIvarDecl = Ivar; 2886 this->IvarLoc = IvarLoc; 2887 } 2888 2889 /// For \@synthesize, returns true if an ivar name was explicitly 2890 /// specified. 2891 /// 2892 /// \code 2893 /// \@synthesize int a = b; // true 2894 /// \@synthesize int a; // false 2895 /// \endcode 2896 bool isIvarNameSpecified() const { 2897 return IvarLoc.isValid() && IvarLoc != getLocation(); 2898 } 2899 2900 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } 2901 void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; } 2902 2903 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } 2904 void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; } 2905 2906 Expr *getGetterCXXConstructor() const { 2907 return GetterCXXConstructor; 2908 } 2909 2910 void setGetterCXXConstructor(Expr *getterCXXConstructor) { 2911 GetterCXXConstructor = getterCXXConstructor; 2912 } 2913 2914 Expr *getSetterCXXAssignment() const { 2915 return SetterCXXAssignment; 2916 } 2917 2918 void setSetterCXXAssignment(Expr *setterCXXAssignment) { 2919 SetterCXXAssignment = setterCXXAssignment; 2920 } 2921 2922 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2923 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; } 2924 }; 2925 2926 template<bool (*Filter)(ObjCCategoryDecl *)> 2927 void 2928 ObjCInterfaceDecl::filtered_category_iterator<Filter>:: 2929 findAcceptableCategory() { 2930 while (Current && !Filter(Current)) 2931 Current = Current->getNextClassCategoryRaw(); 2932 } 2933 2934 template<bool (*Filter)(ObjCCategoryDecl *)> 2935 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> & 2936 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() { 2937 Current = Current->getNextClassCategoryRaw(); 2938 findAcceptableCategory(); 2939 return *this; 2940 } 2941 2942 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { 2943 return !Cat->isInvalidDecl() && Cat->isUnconditionallyVisible(); 2944 } 2945 2946 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { 2947 return !Cat->isInvalidDecl() && Cat->IsClassExtension() && 2948 Cat->isUnconditionallyVisible(); 2949 } 2950 2951 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) { 2952 return !Cat->isInvalidDecl() && Cat->IsClassExtension(); 2953 } 2954 2955 } // namespace clang 2956 2957 #endif // LLVM_CLANG_AST_DECLOBJC_H 2958