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