clang API Documentation
00001 //===--- DeclObjC.h - Classes for representing declarations -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the DeclObjC interface and subclasses. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_DECLOBJC_H 00015 #define LLVM_CLANG_AST_DECLOBJC_H 00016 00017 #include "clang/AST/Decl.h" 00018 #include "clang/AST/SelectorLocationsKind.h" 00019 #include "llvm/ADT/STLExtras.h" 00020 #include "llvm/Support/Compiler.h" 00021 00022 namespace clang { 00023 class Expr; 00024 class Stmt; 00025 class FunctionDecl; 00026 class RecordDecl; 00027 class ObjCIvarDecl; 00028 class ObjCMethodDecl; 00029 class ObjCProtocolDecl; 00030 class ObjCCategoryDecl; 00031 class ObjCPropertyDecl; 00032 class ObjCPropertyImplDecl; 00033 class CXXCtorInitializer; 00034 00035 class ObjCListBase { 00036 ObjCListBase(const ObjCListBase &) LLVM_DELETED_FUNCTION; 00037 void operator=(const ObjCListBase &) LLVM_DELETED_FUNCTION; 00038 protected: 00039 /// List is an array of pointers to objects that are not owned by this object. 00040 void **List; 00041 unsigned NumElts; 00042 00043 public: 00044 ObjCListBase() : List(nullptr), NumElts(0) {} 00045 unsigned size() const { return NumElts; } 00046 bool empty() const { return NumElts == 0; } 00047 00048 protected: 00049 void set(void *const* InList, unsigned Elts, ASTContext &Ctx); 00050 }; 00051 00052 00053 /// ObjCList - This is a simple template class used to hold various lists of 00054 /// decls etc, which is heavily used by the ObjC front-end. This only use case 00055 /// this supports is setting the list all at once and then reading elements out 00056 /// of it. 00057 template <typename T> 00058 class ObjCList : public ObjCListBase { 00059 public: 00060 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) { 00061 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx); 00062 } 00063 00064 typedef T* const * iterator; 00065 iterator begin() const { return (iterator)List; } 00066 iterator end() const { return (iterator)List+NumElts; } 00067 00068 T* operator[](unsigned Idx) const { 00069 assert(Idx < NumElts && "Invalid access"); 00070 return (T*)List[Idx]; 00071 } 00072 }; 00073 00074 /// \brief A list of Objective-C protocols, along with the source 00075 /// locations at which they were referenced. 00076 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> { 00077 SourceLocation *Locations; 00078 00079 using ObjCList<ObjCProtocolDecl>::set; 00080 00081 public: 00082 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(nullptr) { } 00083 00084 typedef const SourceLocation *loc_iterator; 00085 loc_iterator loc_begin() const { return Locations; } 00086 loc_iterator loc_end() const { return Locations + size(); } 00087 00088 void set(ObjCProtocolDecl* const* InList, unsigned Elts, 00089 const SourceLocation *Locs, ASTContext &Ctx); 00090 }; 00091 00092 00093 /// ObjCMethodDecl - Represents an instance or class method declaration. 00094 /// ObjC methods can be declared within 4 contexts: class interfaces, 00095 /// categories, protocols, and class implementations. While C++ member 00096 /// functions leverage C syntax, Objective-C method syntax is modeled after 00097 /// Smalltalk (using colons to specify argument types/expressions). 00098 /// Here are some brief examples: 00099 /// 00100 /// Setter/getter instance methods: 00101 /// - (void)setMenu:(NSMenu *)menu; 00102 /// - (NSMenu *)menu; 00103 /// 00104 /// Instance method that takes 2 NSView arguments: 00105 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView; 00106 /// 00107 /// Getter class method: 00108 /// + (NSMenu *)defaultMenu; 00109 /// 00110 /// A selector represents a unique name for a method. The selector names for 00111 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu. 00112 /// 00113 class ObjCMethodDecl : public NamedDecl, public DeclContext { 00114 public: 00115 enum ImplementationControl { None, Required, Optional }; 00116 private: 00117 // The conventional meaning of this method; an ObjCMethodFamily. 00118 // This is not serialized; instead, it is computed on demand and 00119 // cached. 00120 mutable unsigned Family : ObjCMethodFamilyBitWidth; 00121 00122 /// instance (true) or class (false) method. 00123 unsigned IsInstance : 1; 00124 unsigned IsVariadic : 1; 00125 00126 /// True if this method is the getter or setter for an explicit property. 00127 unsigned IsPropertyAccessor : 1; 00128 00129 // Method has a definition. 00130 unsigned IsDefined : 1; 00131 00132 /// \brief Method redeclaration in the same interface. 00133 unsigned IsRedeclaration : 1; 00134 00135 /// \brief Is redeclared in the same interface. 00136 mutable unsigned HasRedeclaration : 1; 00137 00138 // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum 00139 /// \@required/\@optional 00140 unsigned DeclImplementation : 2; 00141 00142 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum 00143 /// in, inout, etc. 00144 unsigned objcDeclQualifier : 6; 00145 00146 /// \brief Indicates whether this method has a related result type. 00147 unsigned RelatedResultType : 1; 00148 00149 /// \brief Whether the locations of the selector identifiers are in a 00150 /// "standard" position, a enum SelectorLocationsKind. 00151 unsigned SelLocsKind : 2; 00152 00153 /// \brief Whether this method overrides any other in the class hierarchy. 00154 /// 00155 /// A method is said to override any method in the class's 00156 /// base classes, its protocols, or its categories' protocols, that has 00157 /// the same selector and is of the same kind (class or instance). 00158 /// A method in an implementation is not considered as overriding the same 00159 /// method in the interface or its categories. 00160 unsigned IsOverriding : 1; 00161 00162 /// \brief Indicates if the method was a definition but its body was skipped. 00163 unsigned HasSkippedBody : 1; 00164 00165 // Return type of this method. 00166 QualType MethodDeclType; 00167 00168 // Type source information for the return type. 00169 TypeSourceInfo *ReturnTInfo; 00170 00171 /// \brief Array of ParmVarDecls for the formal parameters of this method 00172 /// and optionally followed by selector locations. 00173 void *ParamsAndSelLocs; 00174 unsigned NumParams; 00175 00176 /// List of attributes for this method declaration. 00177 SourceLocation DeclEndLoc; // the location of the ';' or '{'. 00178 00179 // The following are only used for method definitions, null otherwise. 00180 LazyDeclStmtPtr Body; 00181 00182 /// SelfDecl - Decl for the implicit self parameter. This is lazily 00183 /// constructed by createImplicitParams. 00184 ImplicitParamDecl *SelfDecl; 00185 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily 00186 /// constructed by createImplicitParams. 00187 ImplicitParamDecl *CmdDecl; 00188 00189 SelectorLocationsKind getSelLocsKind() const { 00190 return (SelectorLocationsKind)SelLocsKind; 00191 } 00192 bool hasStandardSelLocs() const { 00193 return getSelLocsKind() != SelLoc_NonStandard; 00194 } 00195 00196 /// \brief Get a pointer to the stored selector identifiers locations array. 00197 /// No locations will be stored if HasStandardSelLocs is true. 00198 SourceLocation *getStoredSelLocs() { 00199 return reinterpret_cast<SourceLocation*>(getParams() + NumParams); 00200 } 00201 const SourceLocation *getStoredSelLocs() const { 00202 return reinterpret_cast<const SourceLocation*>(getParams() + NumParams); 00203 } 00204 00205 /// \brief Get a pointer to the stored selector identifiers locations array. 00206 /// No locations will be stored if HasStandardSelLocs is true. 00207 ParmVarDecl **getParams() { 00208 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs); 00209 } 00210 const ParmVarDecl *const *getParams() const { 00211 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs); 00212 } 00213 00214 /// \brief Get the number of stored selector identifiers locations. 00215 /// No locations will be stored if HasStandardSelLocs is true. 00216 unsigned getNumStoredSelLocs() const { 00217 if (hasStandardSelLocs()) 00218 return 0; 00219 return getNumSelectorLocs(); 00220 } 00221 00222 void setParamsAndSelLocs(ASTContext &C, 00223 ArrayRef<ParmVarDecl*> Params, 00224 ArrayRef<SourceLocation> SelLocs); 00225 00226 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc, 00227 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, 00228 DeclContext *contextDecl, bool isInstance = true, 00229 bool isVariadic = false, bool isPropertyAccessor = false, 00230 bool isImplicitlyDeclared = false, bool isDefined = false, 00231 ImplementationControl impControl = None, 00232 bool HasRelatedResultType = false) 00233 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo), 00234 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily), 00235 IsInstance(isInstance), IsVariadic(isVariadic), 00236 IsPropertyAccessor(isPropertyAccessor), IsDefined(isDefined), 00237 IsRedeclaration(0), HasRedeclaration(0), DeclImplementation(impControl), 00238 objcDeclQualifier(OBJC_TQ_None), 00239 RelatedResultType(HasRelatedResultType), 00240 SelLocsKind(SelLoc_StandardNoSpace), IsOverriding(0), HasSkippedBody(0), 00241 MethodDeclType(T), ReturnTInfo(ReturnTInfo), ParamsAndSelLocs(nullptr), 00242 NumParams(0), DeclEndLoc(endLoc), Body(), SelfDecl(nullptr), 00243 CmdDecl(nullptr) { 00244 setImplicit(isImplicitlyDeclared); 00245 } 00246 00247 /// \brief A definition will return its interface declaration. 00248 /// An interface declaration will return its definition. 00249 /// Otherwise it will return itself. 00250 ObjCMethodDecl *getNextRedeclarationImpl() override; 00251 00252 public: 00253 static ObjCMethodDecl * 00254 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, 00255 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, 00256 DeclContext *contextDecl, bool isInstance = true, 00257 bool isVariadic = false, bool isPropertyAccessor = false, 00258 bool isImplicitlyDeclared = false, bool isDefined = false, 00259 ImplementationControl impControl = None, 00260 bool HasRelatedResultType = false); 00261 00262 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID); 00263 00264 ObjCMethodDecl *getCanonicalDecl() override; 00265 const ObjCMethodDecl *getCanonicalDecl() const { 00266 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl(); 00267 } 00268 00269 ObjCDeclQualifier getObjCDeclQualifier() const { 00270 return ObjCDeclQualifier(objcDeclQualifier); 00271 } 00272 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; } 00273 00274 /// \brief Determine whether this method has a result type that is related 00275 /// to the message receiver's type. 00276 bool hasRelatedResultType() const { return RelatedResultType; } 00277 00278 /// \brief Note whether this method has a related result type. 00279 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; } 00280 00281 /// \brief True if this is a method redeclaration in the same interface. 00282 bool isRedeclaration() const { return IsRedeclaration; } 00283 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod); 00284 00285 /// \brief Returns the location where the declarator ends. It will be 00286 /// the location of ';' for a method declaration and the location of '{' 00287 /// for a method definition. 00288 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; } 00289 00290 // Location information, modeled after the Stmt API. 00291 SourceLocation getLocStart() const LLVM_READONLY { return getLocation(); } 00292 SourceLocation getLocEnd() const LLVM_READONLY; 00293 SourceRange getSourceRange() const override LLVM_READONLY { 00294 return SourceRange(getLocation(), getLocEnd()); 00295 } 00296 00297 SourceLocation getSelectorStartLoc() const { 00298 if (isImplicit()) 00299 return getLocStart(); 00300 return getSelectorLoc(0); 00301 } 00302 SourceLocation getSelectorLoc(unsigned Index) const { 00303 assert(Index < getNumSelectorLocs() && "Index out of range!"); 00304 if (hasStandardSelLocs()) 00305 return getStandardSelectorLoc(Index, getSelector(), 00306 getSelLocsKind() == SelLoc_StandardWithSpace, 00307 parameters(), 00308 DeclEndLoc); 00309 return getStoredSelLocs()[Index]; 00310 } 00311 00312 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const; 00313 00314 unsigned getNumSelectorLocs() const { 00315 if (isImplicit()) 00316 return 0; 00317 Selector Sel = getSelector(); 00318 if (Sel.isUnarySelector()) 00319 return 1; 00320 return Sel.getNumArgs(); 00321 } 00322 00323 ObjCInterfaceDecl *getClassInterface(); 00324 const ObjCInterfaceDecl *getClassInterface() const { 00325 return const_cast<ObjCMethodDecl*>(this)->getClassInterface(); 00326 } 00327 00328 Selector getSelector() const { return getDeclName().getObjCSelector(); } 00329 00330 QualType getReturnType() const { return MethodDeclType; } 00331 void setReturnType(QualType T) { MethodDeclType = T; } 00332 SourceRange getReturnTypeSourceRange() const; 00333 00334 /// \brief Determine the type of an expression that sends a message to this 00335 /// function. 00336 QualType getSendResultType() const { 00337 return getReturnType().getNonLValueExprType(getASTContext()); 00338 } 00339 00340 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; } 00341 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; } 00342 00343 // Iterator access to formal parameters. 00344 unsigned param_size() const { return NumParams; } 00345 typedef const ParmVarDecl *const *param_const_iterator; 00346 typedef ParmVarDecl *const *param_iterator; 00347 typedef llvm::iterator_range<param_iterator> param_range; 00348 typedef llvm::iterator_range<param_const_iterator> param_const_range; 00349 00350 param_range params() { return param_range(param_begin(), param_end()); } 00351 param_const_range params() const { 00352 return param_const_range(param_begin(), param_end()); 00353 } 00354 00355 param_const_iterator param_begin() const { 00356 return param_const_iterator(getParams()); 00357 } 00358 param_const_iterator param_end() const { 00359 return param_const_iterator(getParams() + NumParams); 00360 } 00361 param_iterator param_begin() { return param_iterator(getParams()); } 00362 param_iterator param_end() { return param_iterator(getParams() + NumParams); } 00363 00364 // This method returns and of the parameters which are part of the selector 00365 // name mangling requirements. 00366 param_const_iterator sel_param_end() const { 00367 return param_begin() + getSelector().getNumArgs(); 00368 } 00369 00370 // ArrayRef access to formal parameters. This should eventually 00371 // replace the iterator interface above. 00372 ArrayRef<ParmVarDecl*> parameters() const { 00373 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()), 00374 NumParams); 00375 } 00376 00377 /// \brief Sets the method's parameters and selector source locations. 00378 /// If the method is implicit (not coming from source) \p SelLocs is 00379 /// ignored. 00380 void setMethodParams(ASTContext &C, 00381 ArrayRef<ParmVarDecl*> Params, 00382 ArrayRef<SourceLocation> SelLocs = llvm::None); 00383 00384 // Iterator access to parameter types. 00385 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun; 00386 typedef llvm::mapped_iterator<param_const_iterator, deref_fun> 00387 param_type_iterator; 00388 00389 param_type_iterator param_type_begin() const { 00390 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType)); 00391 } 00392 param_type_iterator param_type_end() const { 00393 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType)); 00394 } 00395 00396 /// createImplicitParams - Used to lazily create the self and cmd 00397 /// implict parameters. This must be called prior to using getSelfDecl() 00398 /// or getCmdDecl(). The call is ignored if the implicit paramters 00399 /// have already been created. 00400 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID); 00401 00402 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; } 00403 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; } 00404 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; } 00405 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; } 00406 00407 /// Determines the family of this method. 00408 ObjCMethodFamily getMethodFamily() const; 00409 00410 bool isInstanceMethod() const { return IsInstance; } 00411 void setInstanceMethod(bool isInst) { IsInstance = isInst; } 00412 bool isVariadic() const { return IsVariadic; } 00413 void setVariadic(bool isVar) { IsVariadic = isVar; } 00414 00415 bool isClassMethod() const { return !IsInstance; } 00416 00417 bool isPropertyAccessor() const { return IsPropertyAccessor; } 00418 void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; } 00419 00420 bool isDefined() const { return IsDefined; } 00421 void setDefined(bool isDefined) { IsDefined = isDefined; } 00422 00423 /// \brief Whether this method overrides any other in the class hierarchy. 00424 /// 00425 /// A method is said to override any method in the class's 00426 /// base classes, its protocols, or its categories' protocols, that has 00427 /// the same selector and is of the same kind (class or instance). 00428 /// A method in an implementation is not considered as overriding the same 00429 /// method in the interface or its categories. 00430 bool isOverriding() const { return IsOverriding; } 00431 void setOverriding(bool isOverriding) { IsOverriding = isOverriding; } 00432 00433 /// \brief Return overridden methods for the given \p Method. 00434 /// 00435 /// An ObjC method is considered to override any method in the class's 00436 /// base classes (and base's categories), its protocols, or its categories' 00437 /// protocols, that has 00438 /// the same selector and is of the same kind (class or instance). 00439 /// A method in an implementation is not considered as overriding the same 00440 /// method in the interface or its categories. 00441 void getOverriddenMethods( 00442 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const; 00443 00444 /// \brief True if the method was a definition but its body was skipped. 00445 bool hasSkippedBody() const { return HasSkippedBody; } 00446 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; } 00447 00448 /// \brief Returns the property associated with this method's selector. 00449 /// 00450 /// Note that even if this particular method is not marked as a property 00451 /// accessor, it is still possible for it to match a property declared in a 00452 /// superclass. Pass \c false if you only want to check the current class. 00453 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const; 00454 00455 // Related to protocols declared in \@protocol 00456 void setDeclImplementation(ImplementationControl ic) { 00457 DeclImplementation = ic; 00458 } 00459 ImplementationControl getImplementationControl() const { 00460 return ImplementationControl(DeclImplementation); 00461 } 00462 00463 /// Returns true if this specific method declaration is marked with the 00464 /// designated initializer attribute. 00465 bool isThisDeclarationADesignatedInitializer() const; 00466 00467 /// Returns true if the method selector resolves to a designated initializer 00468 /// in the class's interface. 00469 /// 00470 /// \param InitMethod if non-null and the function returns true, it receives 00471 /// the method declaration that was marked with the designated initializer 00472 /// attribute. 00473 bool isDesignatedInitializerForTheInterface( 00474 const ObjCMethodDecl **InitMethod = nullptr) const; 00475 00476 /// \brief Determine whether this method has a body. 00477 bool hasBody() const override { return Body.isValid(); } 00478 00479 /// \brief Retrieve the body of this method, if it has one. 00480 Stmt *getBody() const override; 00481 00482 void setLazyBody(uint64_t Offset) { Body = Offset; } 00483 00484 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); } 00485 void setBody(Stmt *B) { Body = B; } 00486 00487 /// \brief Returns whether this specific method is a definition. 00488 bool isThisDeclarationADefinition() const { return hasBody(); } 00489 00490 // Implement isa/cast/dyncast/etc. 00491 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 00492 static bool classofKind(Kind K) { return K == ObjCMethod; } 00493 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) { 00494 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D)); 00495 } 00496 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) { 00497 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC)); 00498 } 00499 00500 friend class ASTDeclReader; 00501 friend class ASTDeclWriter; 00502 }; 00503 00504 /// ObjCContainerDecl - Represents a container for method declarations. 00505 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl, 00506 /// ObjCProtocolDecl, and ObjCImplDecl. 00507 /// 00508 class ObjCContainerDecl : public NamedDecl, public DeclContext { 00509 void anchor() override; 00510 00511 SourceLocation AtStart; 00512 00513 // These two locations in the range mark the end of the method container. 00514 // The first points to the '@' token, and the second to the 'end' token. 00515 SourceRange AtEnd; 00516 public: 00517 00518 ObjCContainerDecl(Kind DK, DeclContext *DC, 00519 IdentifierInfo *Id, SourceLocation nameLoc, 00520 SourceLocation atStartLoc) 00521 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {} 00522 00523 // Iterator access to properties. 00524 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator; 00525 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>> 00526 prop_range; 00527 00528 prop_range properties() const { return prop_range(prop_begin(), prop_end()); } 00529 prop_iterator prop_begin() const { 00530 return prop_iterator(decls_begin()); 00531 } 00532 prop_iterator prop_end() const { 00533 return prop_iterator(decls_end()); 00534 } 00535 00536 // Iterator access to instance/class methods. 00537 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator; 00538 typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>> 00539 method_range; 00540 00541 method_range methods() const { 00542 return method_range(meth_begin(), meth_end()); 00543 } 00544 method_iterator meth_begin() const { 00545 return method_iterator(decls_begin()); 00546 } 00547 method_iterator meth_end() const { 00548 return method_iterator(decls_end()); 00549 } 00550 00551 typedef filtered_decl_iterator<ObjCMethodDecl, 00552 &ObjCMethodDecl::isInstanceMethod> 00553 instmeth_iterator; 00554 typedef llvm::iterator_range<instmeth_iterator> instmeth_range; 00555 00556 instmeth_range instance_methods() const { 00557 return instmeth_range(instmeth_begin(), instmeth_end()); 00558 } 00559 instmeth_iterator instmeth_begin() const { 00560 return instmeth_iterator(decls_begin()); 00561 } 00562 instmeth_iterator instmeth_end() const { 00563 return instmeth_iterator(decls_end()); 00564 } 00565 00566 typedef filtered_decl_iterator<ObjCMethodDecl, 00567 &ObjCMethodDecl::isClassMethod> 00568 classmeth_iterator; 00569 typedef llvm::iterator_range<classmeth_iterator> classmeth_range; 00570 00571 classmeth_range class_methods() const { 00572 return classmeth_range(classmeth_begin(), classmeth_end()); 00573 } 00574 classmeth_iterator classmeth_begin() const { 00575 return classmeth_iterator(decls_begin()); 00576 } 00577 classmeth_iterator classmeth_end() const { 00578 return classmeth_iterator(decls_end()); 00579 } 00580 00581 // Get the local instance/class method declared in this interface. 00582 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance, 00583 bool AllowHidden = false) const; 00584 ObjCMethodDecl *getInstanceMethod(Selector Sel, 00585 bool AllowHidden = false) const { 00586 return getMethod(Sel, true/*isInstance*/, AllowHidden); 00587 } 00588 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const { 00589 return getMethod(Sel, false/*isInstance*/, AllowHidden); 00590 } 00591 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const; 00592 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const; 00593 00594 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const; 00595 00596 typedef llvm::DenseMap<IdentifierInfo*, ObjCPropertyDecl*> PropertyMap; 00597 00598 typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*> 00599 ProtocolPropertyMap; 00600 00601 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder; 00602 00603 /// This routine collects list of properties to be implemented in the class. 00604 /// This includes, class's and its conforming protocols' properties. 00605 /// Note, the superclass's properties are not included in the list. 00606 virtual void collectPropertiesToImplement(PropertyMap &PM, 00607 PropertyDeclOrder &PO) const {} 00608 00609 SourceLocation getAtStartLoc() const { return AtStart; } 00610 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; } 00611 00612 // Marks the end of the container. 00613 SourceRange getAtEndRange() const { 00614 return AtEnd; 00615 } 00616 void setAtEndRange(SourceRange atEnd) { 00617 AtEnd = atEnd; 00618 } 00619 00620 SourceRange getSourceRange() const override LLVM_READONLY { 00621 return SourceRange(AtStart, getAtEndRange().getEnd()); 00622 } 00623 00624 // Implement isa/cast/dyncast/etc. 00625 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 00626 static bool classofKind(Kind K) { 00627 return K >= firstObjCContainer && 00628 K <= lastObjCContainer; 00629 } 00630 00631 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) { 00632 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D)); 00633 } 00634 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) { 00635 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC)); 00636 } 00637 }; 00638 00639 /// \brief Represents an ObjC class declaration. 00640 /// 00641 /// For example: 00642 /// 00643 /// \code 00644 /// // MostPrimitive declares no super class (not particularly useful). 00645 /// \@interface MostPrimitive 00646 /// // no instance variables or methods. 00647 /// \@end 00648 /// 00649 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol). 00650 /// \@interface NSResponder : NSObject <NSCoding> 00651 /// { // instance variables are represented by ObjCIvarDecl. 00652 /// id nextResponder; // nextResponder instance variable. 00653 /// } 00654 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder. 00655 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer 00656 /// \@end // to an NSEvent. 00657 /// \endcode 00658 /// 00659 /// Unlike C/C++, forward class declarations are accomplished with \@class. 00660 /// Unlike C/C++, \@class allows for a list of classes to be forward declared. 00661 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes 00662 /// typically inherit from NSObject (an exception is NSProxy). 00663 /// 00664 class ObjCInterfaceDecl : public ObjCContainerDecl 00665 , public Redeclarable<ObjCInterfaceDecl> { 00666 void anchor() override; 00667 00668 /// TypeForDecl - This indicates the Type object that represents this 00669 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType 00670 mutable const Type *TypeForDecl; 00671 friend class ASTContext; 00672 00673 struct DefinitionData { 00674 /// \brief The definition of this class, for quick access from any 00675 /// declaration. 00676 ObjCInterfaceDecl *Definition; 00677 00678 /// Class's super class. 00679 ObjCInterfaceDecl *SuperClass; 00680 00681 /// Protocols referenced in the \@interface declaration 00682 ObjCProtocolList ReferencedProtocols; 00683 00684 /// Protocols reference in both the \@interface and class extensions. 00685 ObjCList<ObjCProtocolDecl> AllReferencedProtocols; 00686 00687 /// \brief List of categories and class extensions defined for this class. 00688 /// 00689 /// Categories are stored as a linked list in the AST, since the categories 00690 /// and class extensions come long after the initial interface declaration, 00691 /// and we avoid dynamically-resized arrays in the AST wherever possible. 00692 ObjCCategoryDecl *CategoryList; 00693 00694 /// IvarList - List of all ivars defined by this class; including class 00695 /// extensions and implementation. This list is built lazily. 00696 ObjCIvarDecl *IvarList; 00697 00698 /// \brief Indicates that the contents of this Objective-C class will be 00699 /// completed by the external AST source when required. 00700 mutable bool ExternallyCompleted : 1; 00701 00702 /// \brief Indicates that the ivar cache does not yet include ivars 00703 /// declared in the implementation. 00704 mutable bool IvarListMissingImplementation : 1; 00705 00706 /// Indicates that this interface decl contains at least one initializer 00707 /// marked with the 'objc_designated_initializer' attribute. 00708 bool HasDesignatedInitializers : 1; 00709 00710 enum InheritedDesignatedInitializersState { 00711 /// We didn't calculate whether the designated initializers should be 00712 /// inherited or not. 00713 IDI_Unknown = 0, 00714 /// Designated initializers are inherited for the super class. 00715 IDI_Inherited = 1, 00716 /// The class does not inherit designated initializers. 00717 IDI_NotInherited = 2 00718 }; 00719 /// One of the \c InheritedDesignatedInitializersState enumeratos. 00720 mutable unsigned InheritedDesignatedInitializers : 2; 00721 00722 /// \brief The location of the superclass, if any. 00723 SourceLocation SuperClassLoc; 00724 00725 /// \brief The location of the last location in this declaration, before 00726 /// the properties/methods. For example, this will be the '>', '}', or 00727 /// identifier, 00728 SourceLocation EndLoc; 00729 00730 DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(), 00731 ExternallyCompleted(), 00732 IvarListMissingImplementation(true), 00733 HasDesignatedInitializers(), 00734 InheritedDesignatedInitializers(IDI_Unknown) { } 00735 }; 00736 00737 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc, 00738 IdentifierInfo *Id, SourceLocation CLoc, 00739 ObjCInterfaceDecl *PrevDecl, bool IsInternal); 00740 00741 void LoadExternalDefinition() const; 00742 00743 /// \brief Contains a pointer to the data associated with this class, 00744 /// which will be NULL if this class has not yet been defined. 00745 /// 00746 /// The bit indicates when we don't need to check for out-of-date 00747 /// declarations. It will be set unless modules are enabled. 00748 llvm::PointerIntPair<DefinitionData *, 1, bool> Data; 00749 00750 DefinitionData &data() const { 00751 assert(Data.getPointer() && "Declaration has no definition!"); 00752 return *Data.getPointer(); 00753 } 00754 00755 /// \brief Allocate the definition data for this class. 00756 void allocateDefinitionData(); 00757 00758 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base; 00759 ObjCInterfaceDecl *getNextRedeclarationImpl() override { 00760 return getNextRedeclaration(); 00761 } 00762 ObjCInterfaceDecl *getPreviousDeclImpl() override { 00763 return getPreviousDecl(); 00764 } 00765 ObjCInterfaceDecl *getMostRecentDeclImpl() override { 00766 return getMostRecentDecl(); 00767 } 00768 00769 public: 00770 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC, 00771 SourceLocation atLoc, 00772 IdentifierInfo *Id, 00773 ObjCInterfaceDecl *PrevDecl, 00774 SourceLocation ClassLoc = SourceLocation(), 00775 bool isInternal = false); 00776 00777 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID); 00778 00779 SourceRange getSourceRange() const override LLVM_READONLY { 00780 if (isThisDeclarationADefinition()) 00781 return ObjCContainerDecl::getSourceRange(); 00782 00783 return SourceRange(getAtStartLoc(), getLocation()); 00784 } 00785 00786 /// \brief Indicate that this Objective-C class is complete, but that 00787 /// the external AST source will be responsible for filling in its contents 00788 /// when a complete class is required. 00789 void setExternallyCompleted(); 00790 00791 /// Indicate that this interface decl contains at least one initializer 00792 /// marked with the 'objc_designated_initializer' attribute. 00793 void setHasDesignatedInitializers(); 00794 00795 /// Returns true if this interface decl contains at least one initializer 00796 /// marked with the 'objc_designated_initializer' attribute. 00797 bool hasDesignatedInitializers() const; 00798 00799 /// Returns true if this interface decl declares a designated initializer 00800 /// or it inherites one from its super class. 00801 bool declaresOrInheritsDesignatedInitializers() const { 00802 return hasDesignatedInitializers() || inheritsDesignatedInitializers(); 00803 } 00804 00805 const ObjCProtocolList &getReferencedProtocols() const { 00806 assert(hasDefinition() && "Caller did not check for forward reference!"); 00807 if (data().ExternallyCompleted) 00808 LoadExternalDefinition(); 00809 00810 return data().ReferencedProtocols; 00811 } 00812 00813 ObjCImplementationDecl *getImplementation() const; 00814 void setImplementation(ObjCImplementationDecl *ImplD); 00815 00816 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const; 00817 00818 // Get the local instance/class method declared in a category. 00819 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const; 00820 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const; 00821 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const { 00822 return isInstance ? getInstanceMethod(Sel) 00823 : getClassMethod(Sel); 00824 } 00825 00826 typedef ObjCProtocolList::iterator protocol_iterator; 00827 typedef llvm::iterator_range<protocol_iterator> protocol_range; 00828 00829 protocol_range protocols() const { 00830 return protocol_range(protocol_begin(), protocol_end()); 00831 } 00832 protocol_iterator protocol_begin() const { 00833 // FIXME: Should make sure no callers ever do this. 00834 if (!hasDefinition()) 00835 return protocol_iterator(); 00836 00837 if (data().ExternallyCompleted) 00838 LoadExternalDefinition(); 00839 00840 return data().ReferencedProtocols.begin(); 00841 } 00842 protocol_iterator protocol_end() const { 00843 // FIXME: Should make sure no callers ever do this. 00844 if (!hasDefinition()) 00845 return protocol_iterator(); 00846 00847 if (data().ExternallyCompleted) 00848 LoadExternalDefinition(); 00849 00850 return data().ReferencedProtocols.end(); 00851 } 00852 00853 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 00854 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 00855 00856 protocol_loc_range protocol_locs() const { 00857 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 00858 } 00859 protocol_loc_iterator protocol_loc_begin() const { 00860 // FIXME: Should make sure no callers ever do this. 00861 if (!hasDefinition()) 00862 return protocol_loc_iterator(); 00863 00864 if (data().ExternallyCompleted) 00865 LoadExternalDefinition(); 00866 00867 return data().ReferencedProtocols.loc_begin(); 00868 } 00869 00870 protocol_loc_iterator protocol_loc_end() const { 00871 // FIXME: Should make sure no callers ever do this. 00872 if (!hasDefinition()) 00873 return protocol_loc_iterator(); 00874 00875 if (data().ExternallyCompleted) 00876 LoadExternalDefinition(); 00877 00878 return data().ReferencedProtocols.loc_end(); 00879 } 00880 00881 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator; 00882 typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range; 00883 00884 all_protocol_range all_referenced_protocols() const { 00885 return all_protocol_range(all_referenced_protocol_begin(), 00886 all_referenced_protocol_end()); 00887 } 00888 all_protocol_iterator all_referenced_protocol_begin() const { 00889 // FIXME: Should make sure no callers ever do this. 00890 if (!hasDefinition()) 00891 return all_protocol_iterator(); 00892 00893 if (data().ExternallyCompleted) 00894 LoadExternalDefinition(); 00895 00896 return data().AllReferencedProtocols.empty() 00897 ? protocol_begin() 00898 : data().AllReferencedProtocols.begin(); 00899 } 00900 all_protocol_iterator all_referenced_protocol_end() const { 00901 // FIXME: Should make sure no callers ever do this. 00902 if (!hasDefinition()) 00903 return all_protocol_iterator(); 00904 00905 if (data().ExternallyCompleted) 00906 LoadExternalDefinition(); 00907 00908 return data().AllReferencedProtocols.empty() 00909 ? protocol_end() 00910 : data().AllReferencedProtocols.end(); 00911 } 00912 00913 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 00914 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 00915 00916 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 00917 ivar_iterator ivar_begin() const { 00918 if (const ObjCInterfaceDecl *Def = getDefinition()) 00919 return ivar_iterator(Def->decls_begin()); 00920 00921 // FIXME: Should make sure no callers ever do this. 00922 return ivar_iterator(); 00923 } 00924 ivar_iterator ivar_end() const { 00925 if (const ObjCInterfaceDecl *Def = getDefinition()) 00926 return ivar_iterator(Def->decls_end()); 00927 00928 // FIXME: Should make sure no callers ever do this. 00929 return ivar_iterator(); 00930 } 00931 00932 unsigned ivar_size() const { 00933 return std::distance(ivar_begin(), ivar_end()); 00934 } 00935 00936 bool ivar_empty() const { return ivar_begin() == ivar_end(); } 00937 00938 ObjCIvarDecl *all_declared_ivar_begin(); 00939 const ObjCIvarDecl *all_declared_ivar_begin() const { 00940 // Even though this modifies IvarList, it's conceptually const: 00941 // the ivar chain is essentially a cached property of ObjCInterfaceDecl. 00942 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin(); 00943 } 00944 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; } 00945 00946 /// setProtocolList - Set the list of protocols that this interface 00947 /// implements. 00948 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num, 00949 const SourceLocation *Locs, ASTContext &C) { 00950 data().ReferencedProtocols.set(List, Num, Locs, C); 00951 } 00952 00953 /// mergeClassExtensionProtocolList - Merge class extension's protocol list 00954 /// into the protocol list for this class. 00955 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List, 00956 unsigned Num, 00957 ASTContext &C); 00958 00959 /// Produce a name to be used for class's metadata. It comes either via 00960 /// objc_runtime_name attribute or class name. 00961 StringRef getObjCRuntimeNameAsString() const; 00962 00963 /// Returns the designated initializers for the interface. 00964 /// 00965 /// If this declaration does not have methods marked as designated 00966 /// initializers then the interface inherits the designated initializers of 00967 /// its super class. 00968 void getDesignatedInitializers( 00969 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const; 00970 00971 /// Returns true if the given selector is a designated initializer for the 00972 /// interface. 00973 /// 00974 /// If this declaration does not have methods marked as designated 00975 /// initializers then the interface inherits the designated initializers of 00976 /// its super class. 00977 /// 00978 /// \param InitMethod if non-null and the function returns true, it receives 00979 /// the method that was marked as a designated initializer. 00980 bool 00981 isDesignatedInitializer(Selector Sel, 00982 const ObjCMethodDecl **InitMethod = nullptr) const; 00983 00984 /// \brief Determine whether this particular declaration of this class is 00985 /// actually also a definition. 00986 bool isThisDeclarationADefinition() const { 00987 return getDefinition() == this; 00988 } 00989 00990 /// \brief Determine whether this class has been defined. 00991 bool hasDefinition() const { 00992 // If the name of this class is out-of-date, bring it up-to-date, which 00993 // might bring in a definition. 00994 // Note: a null value indicates that we don't have a definition and that 00995 // modules are enabled. 00996 if (!Data.getOpaqueValue()) { 00997 if (IdentifierInfo *II = getIdentifier()) { 00998 if (II->isOutOfDate()) { 00999 updateOutOfDate(*II); 01000 } 01001 } 01002 } 01003 01004 return Data.getPointer(); 01005 } 01006 01007 /// \brief Retrieve the definition of this class, or NULL if this class 01008 /// has been forward-declared (with \@class) but not yet defined (with 01009 /// \@interface). 01010 ObjCInterfaceDecl *getDefinition() { 01011 return hasDefinition()? Data.getPointer()->Definition : nullptr; 01012 } 01013 01014 /// \brief Retrieve the definition of this class, or NULL if this class 01015 /// has been forward-declared (with \@class) but not yet defined (with 01016 /// \@interface). 01017 const ObjCInterfaceDecl *getDefinition() const { 01018 return hasDefinition()? Data.getPointer()->Definition : nullptr; 01019 } 01020 01021 /// \brief Starts the definition of this Objective-C class, taking it from 01022 /// a forward declaration (\@class) to a definition (\@interface). 01023 void startDefinition(); 01024 01025 ObjCInterfaceDecl *getSuperClass() const { 01026 // FIXME: Should make sure no callers ever do this. 01027 if (!hasDefinition()) 01028 return nullptr; 01029 01030 if (data().ExternallyCompleted) 01031 LoadExternalDefinition(); 01032 01033 return data().SuperClass; 01034 } 01035 01036 void setSuperClass(ObjCInterfaceDecl * superCls) { 01037 data().SuperClass = 01038 (superCls && superCls->hasDefinition()) ? superCls->getDefinition() 01039 : superCls; 01040 } 01041 01042 /// \brief Iterator that walks over the list of categories, filtering out 01043 /// those that do not meet specific criteria. 01044 /// 01045 /// This class template is used for the various permutations of category 01046 /// and extension iterators. 01047 template<bool (*Filter)(ObjCCategoryDecl *)> 01048 class filtered_category_iterator { 01049 ObjCCategoryDecl *Current; 01050 01051 void findAcceptableCategory(); 01052 01053 public: 01054 typedef ObjCCategoryDecl * value_type; 01055 typedef value_type reference; 01056 typedef value_type pointer; 01057 typedef std::ptrdiff_t difference_type; 01058 typedef std::input_iterator_tag iterator_category; 01059 01060 filtered_category_iterator() : Current(nullptr) { } 01061 explicit filtered_category_iterator(ObjCCategoryDecl *Current) 01062 : Current(Current) 01063 { 01064 findAcceptableCategory(); 01065 } 01066 01067 reference operator*() const { return Current; } 01068 pointer operator->() const { return Current; } 01069 01070 filtered_category_iterator &operator++(); 01071 01072 filtered_category_iterator operator++(int) { 01073 filtered_category_iterator Tmp = *this; 01074 ++(*this); 01075 return Tmp; 01076 } 01077 01078 friend bool operator==(filtered_category_iterator X, 01079 filtered_category_iterator Y) { 01080 return X.Current == Y.Current; 01081 } 01082 01083 friend bool operator!=(filtered_category_iterator X, 01084 filtered_category_iterator Y) { 01085 return X.Current != Y.Current; 01086 } 01087 }; 01088 01089 private: 01090 /// \brief Test whether the given category is visible. 01091 /// 01092 /// Used in the \c visible_categories_iterator. 01093 static bool isVisibleCategory(ObjCCategoryDecl *Cat); 01094 01095 public: 01096 /// \brief Iterator that walks over the list of categories and extensions 01097 /// that are visible, i.e., not hidden in a non-imported submodule. 01098 typedef filtered_category_iterator<isVisibleCategory> 01099 visible_categories_iterator; 01100 01101 typedef llvm::iterator_range<visible_categories_iterator> 01102 visible_categories_range; 01103 01104 visible_categories_range visible_categories() const { 01105 return visible_categories_range(visible_categories_begin(), 01106 visible_categories_end()); 01107 } 01108 01109 /// \brief Retrieve an iterator to the beginning of the visible-categories 01110 /// list. 01111 visible_categories_iterator visible_categories_begin() const { 01112 return visible_categories_iterator(getCategoryListRaw()); 01113 } 01114 01115 /// \brief Retrieve an iterator to the end of the visible-categories list. 01116 visible_categories_iterator visible_categories_end() const { 01117 return visible_categories_iterator(); 01118 } 01119 01120 /// \brief Determine whether the visible-categories list is empty. 01121 bool visible_categories_empty() const { 01122 return visible_categories_begin() == visible_categories_end(); 01123 } 01124 01125 private: 01126 /// \brief Test whether the given category... is a category. 01127 /// 01128 /// Used in the \c known_categories_iterator. 01129 static bool isKnownCategory(ObjCCategoryDecl *) { return true; } 01130 01131 public: 01132 /// \brief Iterator that walks over all of the known categories and 01133 /// extensions, including those that are hidden. 01134 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator; 01135 typedef llvm::iterator_range<known_categories_iterator> 01136 known_categories_range; 01137 01138 known_categories_range known_categories() const { 01139 return known_categories_range(known_categories_begin(), 01140 known_categories_end()); 01141 } 01142 01143 /// \brief Retrieve an iterator to the beginning of the known-categories 01144 /// list. 01145 known_categories_iterator known_categories_begin() const { 01146 return known_categories_iterator(getCategoryListRaw()); 01147 } 01148 01149 /// \brief Retrieve an iterator to the end of the known-categories list. 01150 known_categories_iterator known_categories_end() const { 01151 return known_categories_iterator(); 01152 } 01153 01154 /// \brief Determine whether the known-categories list is empty. 01155 bool known_categories_empty() const { 01156 return known_categories_begin() == known_categories_end(); 01157 } 01158 01159 private: 01160 /// \brief Test whether the given category is a visible extension. 01161 /// 01162 /// Used in the \c visible_extensions_iterator. 01163 static bool isVisibleExtension(ObjCCategoryDecl *Cat); 01164 01165 public: 01166 /// \brief Iterator that walks over all of the visible extensions, skipping 01167 /// any that are known but hidden. 01168 typedef filtered_category_iterator<isVisibleExtension> 01169 visible_extensions_iterator; 01170 01171 typedef llvm::iterator_range<visible_extensions_iterator> 01172 visible_extensions_range; 01173 01174 visible_extensions_range visible_extensions() const { 01175 return visible_extensions_range(visible_extensions_begin(), 01176 visible_extensions_end()); 01177 } 01178 01179 /// \brief Retrieve an iterator to the beginning of the visible-extensions 01180 /// list. 01181 visible_extensions_iterator visible_extensions_begin() const { 01182 return visible_extensions_iterator(getCategoryListRaw()); 01183 } 01184 01185 /// \brief Retrieve an iterator to the end of the visible-extensions list. 01186 visible_extensions_iterator visible_extensions_end() const { 01187 return visible_extensions_iterator(); 01188 } 01189 01190 /// \brief Determine whether the visible-extensions list is empty. 01191 bool visible_extensions_empty() const { 01192 return visible_extensions_begin() == visible_extensions_end(); 01193 } 01194 01195 private: 01196 /// \brief Test whether the given category is an extension. 01197 /// 01198 /// Used in the \c known_extensions_iterator. 01199 static bool isKnownExtension(ObjCCategoryDecl *Cat); 01200 01201 public: 01202 /// \brief Iterator that walks over all of the known extensions. 01203 typedef filtered_category_iterator<isKnownExtension> 01204 known_extensions_iterator; 01205 typedef llvm::iterator_range<known_extensions_iterator> 01206 known_extensions_range; 01207 01208 known_extensions_range known_extensions() const { 01209 return known_extensions_range(known_extensions_begin(), 01210 known_extensions_end()); 01211 } 01212 01213 /// \brief Retrieve an iterator to the beginning of the known-extensions 01214 /// list. 01215 known_extensions_iterator known_extensions_begin() const { 01216 return known_extensions_iterator(getCategoryListRaw()); 01217 } 01218 01219 /// \brief Retrieve an iterator to the end of the known-extensions list. 01220 known_extensions_iterator known_extensions_end() const { 01221 return known_extensions_iterator(); 01222 } 01223 01224 /// \brief Determine whether the known-extensions list is empty. 01225 bool known_extensions_empty() const { 01226 return known_extensions_begin() == known_extensions_end(); 01227 } 01228 01229 /// \brief Retrieve the raw pointer to the start of the category/extension 01230 /// list. 01231 ObjCCategoryDecl* getCategoryListRaw() const { 01232 // FIXME: Should make sure no callers ever do this. 01233 if (!hasDefinition()) 01234 return nullptr; 01235 01236 if (data().ExternallyCompleted) 01237 LoadExternalDefinition(); 01238 01239 return data().CategoryList; 01240 } 01241 01242 /// \brief Set the raw pointer to the start of the category/extension 01243 /// list. 01244 void setCategoryListRaw(ObjCCategoryDecl *category) { 01245 data().CategoryList = category; 01246 } 01247 01248 ObjCPropertyDecl 01249 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const; 01250 01251 void collectPropertiesToImplement(PropertyMap &PM, 01252 PropertyDeclOrder &PO) const override; 01253 01254 /// isSuperClassOf - Return true if this class is the specified class or is a 01255 /// super class of the specified interface class. 01256 bool isSuperClassOf(const ObjCInterfaceDecl *I) const { 01257 // If RHS is derived from LHS it is OK; else it is not OK. 01258 while (I != nullptr) { 01259 if (declaresSameEntity(this, I)) 01260 return true; 01261 01262 I = I->getSuperClass(); 01263 } 01264 return false; 01265 } 01266 01267 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes 01268 /// to be incompatible with __weak references. Returns true if it is. 01269 bool isArcWeakrefUnavailable() const; 01270 01271 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super 01272 /// classes must not be auto-synthesized. Returns class decl. if it must not 01273 /// be; 0, otherwise. 01274 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const; 01275 01276 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName, 01277 ObjCInterfaceDecl *&ClassDeclared); 01278 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) { 01279 ObjCInterfaceDecl *ClassDeclared; 01280 return lookupInstanceVariable(IVarName, ClassDeclared); 01281 } 01282 01283 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name); 01284 01285 // Lookup a method. First, we search locally. If a method isn't 01286 // found, we search referenced protocols and class categories. 01287 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance, 01288 bool shallowCategoryLookup = false, 01289 bool followSuper = true, 01290 const ObjCCategoryDecl *C = nullptr) const; 01291 01292 /// Lookup an instance method for a given selector. 01293 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 01294 return lookupMethod(Sel, true/*isInstance*/); 01295 } 01296 01297 /// Lookup a class method for a given selector. 01298 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 01299 return lookupMethod(Sel, false/*isInstance*/); 01300 } 01301 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName); 01302 01303 /// \brief Lookup a method in the classes implementation hierarchy. 01304 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, 01305 bool Instance=true) const; 01306 01307 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) { 01308 return lookupPrivateMethod(Sel, false); 01309 } 01310 01311 /// \brief Lookup a setter or getter in the class hierarchy, 01312 /// including in all categories except for category passed 01313 /// as argument. 01314 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel, 01315 const ObjCCategoryDecl *Cat) const { 01316 return lookupMethod(Sel, true/*isInstance*/, 01317 false/*shallowCategoryLookup*/, 01318 true /* followsSuper */, 01319 Cat); 01320 } 01321 01322 SourceLocation getEndOfDefinitionLoc() const { 01323 if (!hasDefinition()) 01324 return getLocation(); 01325 01326 return data().EndLoc; 01327 } 01328 01329 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; } 01330 01331 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; } 01332 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; } 01333 01334 /// isImplicitInterfaceDecl - check that this is an implicitly declared 01335 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation 01336 /// declaration without an \@interface declaration. 01337 bool isImplicitInterfaceDecl() const { 01338 return hasDefinition() ? data().Definition->isImplicit() : isImplicit(); 01339 } 01340 01341 /// ClassImplementsProtocol - Checks that 'lProto' protocol 01342 /// has been implemented in IDecl class, its super class or categories (if 01343 /// lookupCategory is true). 01344 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, 01345 bool lookupCategory, 01346 bool RHSIsQualifiedID = false); 01347 01348 typedef redeclarable_base::redecl_range redecl_range; 01349 typedef redeclarable_base::redecl_iterator redecl_iterator; 01350 using redeclarable_base::redecls_begin; 01351 using redeclarable_base::redecls_end; 01352 using redeclarable_base::redecls; 01353 using redeclarable_base::getPreviousDecl; 01354 using redeclarable_base::getMostRecentDecl; 01355 using redeclarable_base::isFirstDecl; 01356 01357 /// Retrieves the canonical declaration of this Objective-C class. 01358 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); } 01359 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); } 01360 01361 // Low-level accessor 01362 const Type *getTypeForDecl() const { return TypeForDecl; } 01363 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; } 01364 01365 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01366 static bool classofKind(Kind K) { return K == ObjCInterface; } 01367 01368 friend class ASTReader; 01369 friend class ASTDeclReader; 01370 friend class ASTDeclWriter; 01371 01372 private: 01373 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const; 01374 bool inheritsDesignatedInitializers() const; 01375 }; 01376 01377 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC 01378 /// instance variables are identical to C. The only exception is Objective-C 01379 /// supports C++ style access control. For example: 01380 /// 01381 /// \@interface IvarExample : NSObject 01382 /// { 01383 /// id defaultToProtected; 01384 /// \@public: 01385 /// id canBePublic; // same as C++. 01386 /// \@protected: 01387 /// id canBeProtected; // same as C++. 01388 /// \@package: 01389 /// id canBePackage; // framework visibility (not available in C++). 01390 /// } 01391 /// 01392 class ObjCIvarDecl : public FieldDecl { 01393 void anchor() override; 01394 01395 public: 01396 enum AccessControl { 01397 None, Private, Protected, Public, Package 01398 }; 01399 01400 private: 01401 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc, 01402 SourceLocation IdLoc, IdentifierInfo *Id, 01403 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW, 01404 bool synthesized) 01405 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, 01406 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit), 01407 NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {} 01408 01409 public: 01410 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC, 01411 SourceLocation StartLoc, SourceLocation IdLoc, 01412 IdentifierInfo *Id, QualType T, 01413 TypeSourceInfo *TInfo, 01414 AccessControl ac, Expr *BW = nullptr, 01415 bool synthesized=false); 01416 01417 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID); 01418 01419 /// \brief Return the class interface that this ivar is logically contained 01420 /// in; this is either the interface where the ivar was declared, or the 01421 /// interface the ivar is conceptually a part of in the case of synthesized 01422 /// ivars. 01423 const ObjCInterfaceDecl *getContainingInterface() const; 01424 01425 ObjCIvarDecl *getNextIvar() { return NextIvar; } 01426 const ObjCIvarDecl *getNextIvar() const { return NextIvar; } 01427 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; } 01428 01429 void setAccessControl(AccessControl ac) { DeclAccess = ac; } 01430 01431 AccessControl getAccessControl() const { return AccessControl(DeclAccess); } 01432 01433 AccessControl getCanonicalAccessControl() const { 01434 return DeclAccess == None ? Protected : AccessControl(DeclAccess); 01435 } 01436 01437 void setSynthesize(bool synth) { Synthesized = synth; } 01438 bool getSynthesize() const { return Synthesized; } 01439 01440 // Implement isa/cast/dyncast/etc. 01441 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01442 static bool classofKind(Kind K) { return K == ObjCIvar; } 01443 private: 01444 /// NextIvar - Next Ivar in the list of ivars declared in class; class's 01445 /// extensions and class's implementation 01446 ObjCIvarDecl *NextIvar; 01447 01448 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum 01449 unsigned DeclAccess : 3; 01450 unsigned Synthesized : 1; 01451 }; 01452 01453 01454 /// \brief Represents a field declaration created by an \@defs(...). 01455 class ObjCAtDefsFieldDecl : public FieldDecl { 01456 void anchor() override; 01457 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc, 01458 SourceLocation IdLoc, IdentifierInfo *Id, 01459 QualType T, Expr *BW) 01460 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T, 01461 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ? 01462 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {} 01463 01464 public: 01465 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC, 01466 SourceLocation StartLoc, 01467 SourceLocation IdLoc, IdentifierInfo *Id, 01468 QualType T, Expr *BW); 01469 01470 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); 01471 01472 // Implement isa/cast/dyncast/etc. 01473 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01474 static bool classofKind(Kind K) { return K == ObjCAtDefsField; } 01475 }; 01476 01477 /// \brief Represents an Objective-C protocol declaration. 01478 /// 01479 /// Objective-C protocols declare a pure abstract type (i.e., no instance 01480 /// variables are permitted). Protocols originally drew inspiration from 01481 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy 01482 /// syntax:-). Here is an example: 01483 /// 01484 /// \code 01485 /// \@protocol NSDraggingInfo <refproto1, refproto2> 01486 /// - (NSWindow *)draggingDestinationWindow; 01487 /// - (NSImage *)draggedImage; 01488 /// \@end 01489 /// \endcode 01490 /// 01491 /// This says that NSDraggingInfo requires two methods and requires everything 01492 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as 01493 /// well. 01494 /// 01495 /// \code 01496 /// \@interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo> 01497 /// \@end 01498 /// \endcode 01499 /// 01500 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and 01501 /// protocols are in distinct namespaces. For example, Cocoa defines both 01502 /// an NSObject protocol and class (which isn't allowed in Java). As a result, 01503 /// protocols are referenced using angle brackets as follows: 01504 /// 01505 /// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo; 01506 /// 01507 class ObjCProtocolDecl : public ObjCContainerDecl, 01508 public Redeclarable<ObjCProtocolDecl> { 01509 void anchor() override; 01510 01511 struct DefinitionData { 01512 // \brief The declaration that defines this protocol. 01513 ObjCProtocolDecl *Definition; 01514 01515 /// \brief Referenced protocols 01516 ObjCProtocolList ReferencedProtocols; 01517 }; 01518 01519 /// \brief Contains a pointer to the data associated with this class, 01520 /// which will be NULL if this class has not yet been defined. 01521 /// 01522 /// The bit indicates when we don't need to check for out-of-date 01523 /// declarations. It will be set unless modules are enabled. 01524 llvm::PointerIntPair<DefinitionData *, 1, bool> Data; 01525 01526 DefinitionData &data() const { 01527 assert(Data.getPointer() && "Objective-C protocol has no definition!"); 01528 return *Data.getPointer(); 01529 } 01530 01531 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id, 01532 SourceLocation nameLoc, SourceLocation atStartLoc, 01533 ObjCProtocolDecl *PrevDecl); 01534 01535 void allocateDefinitionData(); 01536 01537 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base; 01538 ObjCProtocolDecl *getNextRedeclarationImpl() override { 01539 return getNextRedeclaration(); 01540 } 01541 ObjCProtocolDecl *getPreviousDeclImpl() override { 01542 return getPreviousDecl(); 01543 } 01544 ObjCProtocolDecl *getMostRecentDeclImpl() override { 01545 return getMostRecentDecl(); 01546 } 01547 01548 public: 01549 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC, 01550 IdentifierInfo *Id, 01551 SourceLocation nameLoc, 01552 SourceLocation atStartLoc, 01553 ObjCProtocolDecl *PrevDecl); 01554 01555 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID); 01556 01557 const ObjCProtocolList &getReferencedProtocols() const { 01558 assert(hasDefinition() && "No definition available!"); 01559 return data().ReferencedProtocols; 01560 } 01561 typedef ObjCProtocolList::iterator protocol_iterator; 01562 typedef llvm::iterator_range<protocol_iterator> protocol_range; 01563 01564 protocol_range protocols() const { 01565 return protocol_range(protocol_begin(), protocol_end()); 01566 } 01567 protocol_iterator protocol_begin() const { 01568 if (!hasDefinition()) 01569 return protocol_iterator(); 01570 01571 return data().ReferencedProtocols.begin(); 01572 } 01573 protocol_iterator protocol_end() const { 01574 if (!hasDefinition()) 01575 return protocol_iterator(); 01576 01577 return data().ReferencedProtocols.end(); 01578 } 01579 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 01580 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 01581 01582 protocol_loc_range protocol_locs() const { 01583 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 01584 } 01585 protocol_loc_iterator protocol_loc_begin() const { 01586 if (!hasDefinition()) 01587 return protocol_loc_iterator(); 01588 01589 return data().ReferencedProtocols.loc_begin(); 01590 } 01591 protocol_loc_iterator protocol_loc_end() const { 01592 if (!hasDefinition()) 01593 return protocol_loc_iterator(); 01594 01595 return data().ReferencedProtocols.loc_end(); 01596 } 01597 unsigned protocol_size() const { 01598 if (!hasDefinition()) 01599 return 0; 01600 01601 return data().ReferencedProtocols.size(); 01602 } 01603 01604 /// setProtocolList - Set the list of protocols that this interface 01605 /// implements. 01606 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 01607 const SourceLocation *Locs, ASTContext &C) { 01608 assert(hasDefinition() && "Protocol is not defined"); 01609 data().ReferencedProtocols.set(List, Num, Locs, C); 01610 } 01611 01612 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName); 01613 01614 // Lookup a method. First, we search locally. If a method isn't 01615 // found, we search referenced protocols and class categories. 01616 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const; 01617 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { 01618 return lookupMethod(Sel, true/*isInstance*/); 01619 } 01620 ObjCMethodDecl *lookupClassMethod(Selector Sel) const { 01621 return lookupMethod(Sel, false/*isInstance*/); 01622 } 01623 01624 /// \brief Determine whether this protocol has a definition. 01625 bool hasDefinition() const { 01626 // If the name of this protocol is out-of-date, bring it up-to-date, which 01627 // might bring in a definition. 01628 // Note: a null value indicates that we don't have a definition and that 01629 // modules are enabled. 01630 if (!Data.getOpaqueValue()) { 01631 if (IdentifierInfo *II = getIdentifier()) { 01632 if (II->isOutOfDate()) { 01633 updateOutOfDate(*II); 01634 } 01635 } 01636 } 01637 01638 return Data.getPointer(); 01639 } 01640 01641 /// \brief Retrieve the definition of this protocol, if any. 01642 ObjCProtocolDecl *getDefinition() { 01643 return hasDefinition()? Data.getPointer()->Definition : nullptr; 01644 } 01645 01646 /// \brief Retrieve the definition of this protocol, if any. 01647 const ObjCProtocolDecl *getDefinition() const { 01648 return hasDefinition()? Data.getPointer()->Definition : nullptr; 01649 } 01650 01651 /// \brief Determine whether this particular declaration is also the 01652 /// definition. 01653 bool isThisDeclarationADefinition() const { 01654 return getDefinition() == this; 01655 } 01656 01657 /// \brief Starts the definition of this Objective-C protocol. 01658 void startDefinition(); 01659 01660 /// Produce a name to be used for protocol's metadata. It comes either via 01661 /// objc_runtime_name attribute or protocol name. 01662 StringRef getObjCRuntimeNameAsString() const; 01663 01664 SourceRange getSourceRange() const override LLVM_READONLY { 01665 if (isThisDeclarationADefinition()) 01666 return ObjCContainerDecl::getSourceRange(); 01667 01668 return SourceRange(getAtStartLoc(), getLocation()); 01669 } 01670 01671 typedef redeclarable_base::redecl_range redecl_range; 01672 typedef redeclarable_base::redecl_iterator redecl_iterator; 01673 using redeclarable_base::redecls_begin; 01674 using redeclarable_base::redecls_end; 01675 using redeclarable_base::redecls; 01676 using redeclarable_base::getPreviousDecl; 01677 using redeclarable_base::getMostRecentDecl; 01678 using redeclarable_base::isFirstDecl; 01679 01680 /// Retrieves the canonical declaration of this Objective-C protocol. 01681 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); } 01682 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); } 01683 01684 void collectPropertiesToImplement(PropertyMap &PM, 01685 PropertyDeclOrder &PO) const override; 01686 01687 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property, 01688 ProtocolPropertyMap &PM) const; 01689 01690 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01691 static bool classofKind(Kind K) { return K == ObjCProtocol; } 01692 01693 friend class ASTReader; 01694 friend class ASTDeclReader; 01695 friend class ASTDeclWriter; 01696 }; 01697 01698 /// ObjCCategoryDecl - Represents a category declaration. A category allows 01699 /// you to add methods to an existing class (without subclassing or modifying 01700 /// the original class interface or implementation:-). Categories don't allow 01701 /// you to add instance data. The following example adds "myMethod" to all 01702 /// NSView's within a process: 01703 /// 01704 /// \@interface NSView (MyViewMethods) 01705 /// - myMethod; 01706 /// \@end 01707 /// 01708 /// Categories also allow you to split the implementation of a class across 01709 /// several files (a feature more naturally supported in C++). 01710 /// 01711 /// Categories were originally inspired by dynamic languages such as Common 01712 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java) 01713 /// don't support this level of dynamism, which is both powerful and dangerous. 01714 /// 01715 class ObjCCategoryDecl : public ObjCContainerDecl { 01716 void anchor() override; 01717 01718 /// Interface belonging to this category 01719 ObjCInterfaceDecl *ClassInterface; 01720 01721 /// referenced protocols in this category. 01722 ObjCProtocolList ReferencedProtocols; 01723 01724 /// Next category belonging to this class. 01725 /// FIXME: this should not be a singly-linked list. Move storage elsewhere. 01726 ObjCCategoryDecl *NextClassCategory; 01727 01728 /// \brief The location of the category name in this declaration. 01729 SourceLocation CategoryNameLoc; 01730 01731 /// class extension may have private ivars. 01732 SourceLocation IvarLBraceLoc; 01733 SourceLocation IvarRBraceLoc; 01734 01735 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, 01736 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, 01737 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, 01738 SourceLocation IvarLBraceLoc=SourceLocation(), 01739 SourceLocation IvarRBraceLoc=SourceLocation()) 01740 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc), 01741 ClassInterface(IDecl), NextClassCategory(nullptr), 01742 CategoryNameLoc(CategoryNameLoc), 01743 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) { 01744 } 01745 01746 public: 01747 01748 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC, 01749 SourceLocation AtLoc, 01750 SourceLocation ClassNameLoc, 01751 SourceLocation CategoryNameLoc, 01752 IdentifierInfo *Id, 01753 ObjCInterfaceDecl *IDecl, 01754 SourceLocation IvarLBraceLoc=SourceLocation(), 01755 SourceLocation IvarRBraceLoc=SourceLocation()); 01756 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID); 01757 01758 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 01759 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 01760 01761 ObjCCategoryImplDecl *getImplementation() const; 01762 void setImplementation(ObjCCategoryImplDecl *ImplD); 01763 01764 /// setProtocolList - Set the list of protocols that this interface 01765 /// implements. 01766 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, 01767 const SourceLocation *Locs, ASTContext &C) { 01768 ReferencedProtocols.set(List, Num, Locs, C); 01769 } 01770 01771 const ObjCProtocolList &getReferencedProtocols() const { 01772 return ReferencedProtocols; 01773 } 01774 01775 typedef ObjCProtocolList::iterator protocol_iterator; 01776 typedef llvm::iterator_range<protocol_iterator> protocol_range; 01777 01778 protocol_range protocols() const { 01779 return protocol_range(protocol_begin(), protocol_end()); 01780 } 01781 protocol_iterator protocol_begin() const { 01782 return ReferencedProtocols.begin(); 01783 } 01784 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); } 01785 unsigned protocol_size() const { return ReferencedProtocols.size(); } 01786 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator; 01787 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range; 01788 01789 protocol_loc_range protocol_locs() const { 01790 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); 01791 } 01792 protocol_loc_iterator protocol_loc_begin() const { 01793 return ReferencedProtocols.loc_begin(); 01794 } 01795 protocol_loc_iterator protocol_loc_end() const { 01796 return ReferencedProtocols.loc_end(); 01797 } 01798 01799 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } 01800 01801 /// \brief Retrieve the pointer to the next stored category (or extension), 01802 /// which may be hidden. 01803 ObjCCategoryDecl *getNextClassCategoryRaw() const { 01804 return NextClassCategory; 01805 } 01806 01807 bool IsClassExtension() const { return getIdentifier() == nullptr; } 01808 01809 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 01810 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 01811 01812 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 01813 ivar_iterator ivar_begin() const { 01814 return ivar_iterator(decls_begin()); 01815 } 01816 ivar_iterator ivar_end() const { 01817 return ivar_iterator(decls_end()); 01818 } 01819 unsigned ivar_size() const { 01820 return std::distance(ivar_begin(), ivar_end()); 01821 } 01822 bool ivar_empty() const { 01823 return ivar_begin() == ivar_end(); 01824 } 01825 01826 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 01827 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; } 01828 01829 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 01830 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 01831 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 01832 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 01833 01834 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01835 static bool classofKind(Kind K) { return K == ObjCCategory; } 01836 01837 friend class ASTDeclReader; 01838 friend class ASTDeclWriter; 01839 }; 01840 01841 class ObjCImplDecl : public ObjCContainerDecl { 01842 void anchor() override; 01843 01844 /// Class interface for this class/category implementation 01845 ObjCInterfaceDecl *ClassInterface; 01846 01847 protected: 01848 ObjCImplDecl(Kind DK, DeclContext *DC, 01849 ObjCInterfaceDecl *classInterface, 01850 SourceLocation nameLoc, SourceLocation atStartLoc) 01851 : ObjCContainerDecl(DK, DC, 01852 classInterface? classInterface->getIdentifier() 01853 : nullptr, 01854 nameLoc, atStartLoc), 01855 ClassInterface(classInterface) {} 01856 01857 public: 01858 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } 01859 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } 01860 void setClassInterface(ObjCInterfaceDecl *IFace); 01861 01862 void addInstanceMethod(ObjCMethodDecl *method) { 01863 // FIXME: Context should be set correctly before we get here. 01864 method->setLexicalDeclContext(this); 01865 addDecl(method); 01866 } 01867 void addClassMethod(ObjCMethodDecl *method) { 01868 // FIXME: Context should be set correctly before we get here. 01869 method->setLexicalDeclContext(this); 01870 addDecl(method); 01871 } 01872 01873 void addPropertyImplementation(ObjCPropertyImplDecl *property); 01874 01875 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const; 01876 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; 01877 01878 // Iterator access to properties. 01879 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator; 01880 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>> 01881 propimpl_range; 01882 01883 propimpl_range property_impls() const { 01884 return propimpl_range(propimpl_begin(), propimpl_end()); 01885 } 01886 propimpl_iterator propimpl_begin() const { 01887 return propimpl_iterator(decls_begin()); 01888 } 01889 propimpl_iterator propimpl_end() const { 01890 return propimpl_iterator(decls_end()); 01891 } 01892 01893 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01894 static bool classofKind(Kind K) { 01895 return K >= firstObjCImpl && K <= lastObjCImpl; 01896 } 01897 }; 01898 01899 /// ObjCCategoryImplDecl - An object of this class encapsulates a category 01900 /// \@implementation declaration. If a category class has declaration of a 01901 /// property, its implementation must be specified in the category's 01902 /// \@implementation declaration. Example: 01903 /// \@interface I \@end 01904 /// \@interface I(CATEGORY) 01905 /// \@property int p1, d1; 01906 /// \@end 01907 /// \@implementation I(CATEGORY) 01908 /// \@dynamic p1,d1; 01909 /// \@end 01910 /// 01911 /// ObjCCategoryImplDecl 01912 class ObjCCategoryImplDecl : public ObjCImplDecl { 01913 void anchor() override; 01914 01915 // Category name 01916 IdentifierInfo *Id; 01917 01918 // Category name location 01919 SourceLocation CategoryNameLoc; 01920 01921 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id, 01922 ObjCInterfaceDecl *classInterface, 01923 SourceLocation nameLoc, SourceLocation atStartLoc, 01924 SourceLocation CategoryNameLoc) 01925 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc), 01926 Id(Id), CategoryNameLoc(CategoryNameLoc) {} 01927 public: 01928 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, 01929 IdentifierInfo *Id, 01930 ObjCInterfaceDecl *classInterface, 01931 SourceLocation nameLoc, 01932 SourceLocation atStartLoc, 01933 SourceLocation CategoryNameLoc); 01934 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 01935 01936 /// getIdentifier - Get the identifier that names the category 01937 /// interface associated with this implementation. 01938 /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier() 01939 /// with a different meaning. For example: 01940 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier() 01941 /// returns the class interface name, whereas 01942 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier() 01943 /// returns the category name. 01944 IdentifierInfo *getIdentifier() const { 01945 return Id; 01946 } 01947 void setIdentifier(IdentifierInfo *II) { Id = II; } 01948 01949 ObjCCategoryDecl *getCategoryDecl() const; 01950 01951 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } 01952 01953 /// getName - Get the name of identifier for the class interface associated 01954 /// with this implementation as a StringRef. 01955 // 01956 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different 01957 // meaning. 01958 StringRef getName() const { return Id ? Id->getName() : StringRef(); } 01959 01960 /// @brief Get the name of the class associated with this interface. 01961 // 01962 // FIXME: Deprecated, move clients to getName(). 01963 std::string getNameAsString() const { 01964 return getName(); 01965 } 01966 01967 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 01968 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;} 01969 01970 friend class ASTDeclReader; 01971 friend class ASTDeclWriter; 01972 }; 01973 01974 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID); 01975 01976 /// ObjCImplementationDecl - Represents a class definition - this is where 01977 /// method definitions are specified. For example: 01978 /// 01979 /// @code 01980 /// \@implementation MyClass 01981 /// - (void)myMethod { /* do something */ } 01982 /// \@end 01983 /// @endcode 01984 /// 01985 /// In a non-fragile runtime, instance variables can appear in the class 01986 /// interface, class extensions (nameless categories), and in the implementation 01987 /// itself, as well as being synthesized as backing storage for properties. 01988 /// 01989 /// In a fragile runtime, instance variables are specified in the class 01990 /// interface, \em not in the implementation. Nevertheless (for legacy reasons), 01991 /// we allow instance variables to be specified in the implementation. When 01992 /// specified, they need to be \em identical to the interface. 01993 class ObjCImplementationDecl : public ObjCImplDecl { 01994 void anchor() override; 01995 /// Implementation Class's super class. 01996 ObjCInterfaceDecl *SuperClass; 01997 SourceLocation SuperLoc; 01998 01999 /// \@implementation may have private ivars. 02000 SourceLocation IvarLBraceLoc; 02001 SourceLocation IvarRBraceLoc; 02002 02003 /// Support for ivar initialization. 02004 /// IvarInitializers - The arguments used to initialize the ivars 02005 CXXCtorInitializer **IvarInitializers; 02006 unsigned NumIvarInitializers; 02007 02008 /// Do the ivars of this class require initialization other than 02009 /// zero-initialization? 02010 bool HasNonZeroConstructors : 1; 02011 02012 /// Do the ivars of this class require non-trivial destruction? 02013 bool HasDestructors : 1; 02014 02015 ObjCImplementationDecl(DeclContext *DC, 02016 ObjCInterfaceDecl *classInterface, 02017 ObjCInterfaceDecl *superDecl, 02018 SourceLocation nameLoc, SourceLocation atStartLoc, 02019 SourceLocation superLoc = SourceLocation(), 02020 SourceLocation IvarLBraceLoc=SourceLocation(), 02021 SourceLocation IvarRBraceLoc=SourceLocation()) 02022 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc), 02023 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc), 02024 IvarRBraceLoc(IvarRBraceLoc), 02025 IvarInitializers(nullptr), NumIvarInitializers(0), 02026 HasNonZeroConstructors(false), HasDestructors(false) {} 02027 public: 02028 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, 02029 ObjCInterfaceDecl *classInterface, 02030 ObjCInterfaceDecl *superDecl, 02031 SourceLocation nameLoc, 02032 SourceLocation atStartLoc, 02033 SourceLocation superLoc = SourceLocation(), 02034 SourceLocation IvarLBraceLoc=SourceLocation(), 02035 SourceLocation IvarRBraceLoc=SourceLocation()); 02036 02037 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID); 02038 02039 /// init_iterator - Iterates through the ivar initializer list. 02040 typedef CXXCtorInitializer **init_iterator; 02041 02042 /// init_const_iterator - Iterates through the ivar initializer list. 02043 typedef CXXCtorInitializer * const * init_const_iterator; 02044 02045 typedef llvm::iterator_range<init_iterator> init_range; 02046 typedef llvm::iterator_range<init_const_iterator> init_const_range; 02047 02048 init_range inits() { return init_range(init_begin(), init_end()); } 02049 init_const_range inits() const { 02050 return init_const_range(init_begin(), init_end()); 02051 } 02052 02053 /// init_begin() - Retrieve an iterator to the first initializer. 02054 init_iterator init_begin() { return IvarInitializers; } 02055 /// begin() - Retrieve an iterator to the first initializer. 02056 init_const_iterator init_begin() const { return IvarInitializers; } 02057 02058 /// init_end() - Retrieve an iterator past the last initializer. 02059 init_iterator init_end() { 02060 return IvarInitializers + NumIvarInitializers; 02061 } 02062 /// end() - Retrieve an iterator past the last initializer. 02063 init_const_iterator init_end() const { 02064 return IvarInitializers + NumIvarInitializers; 02065 } 02066 /// getNumArgs - Number of ivars which must be initialized. 02067 unsigned getNumIvarInitializers() const { 02068 return NumIvarInitializers; 02069 } 02070 02071 void setNumIvarInitializers(unsigned numNumIvarInitializers) { 02072 NumIvarInitializers = numNumIvarInitializers; 02073 } 02074 02075 void setIvarInitializers(ASTContext &C, 02076 CXXCtorInitializer ** initializers, 02077 unsigned numInitializers); 02078 02079 /// Do any of the ivars of this class (not counting its base classes) 02080 /// require construction other than zero-initialization? 02081 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; } 02082 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; } 02083 02084 /// Do any of the ivars of this class (not counting its base classes) 02085 /// require non-trivial destruction? 02086 bool hasDestructors() const { return HasDestructors; } 02087 void setHasDestructors(bool val) { HasDestructors = val; } 02088 02089 /// getIdentifier - Get the identifier that names the class 02090 /// interface associated with this implementation. 02091 IdentifierInfo *getIdentifier() const { 02092 return getClassInterface()->getIdentifier(); 02093 } 02094 02095 /// getName - Get the name of identifier for the class interface associated 02096 /// with this implementation as a StringRef. 02097 // 02098 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different 02099 // meaning. 02100 StringRef getName() const { 02101 assert(getIdentifier() && "Name is not a simple identifier"); 02102 return getIdentifier()->getName(); 02103 } 02104 02105 /// @brief Get the name of the class associated with this interface. 02106 // 02107 // FIXME: Move to StringRef API. 02108 std::string getNameAsString() const { 02109 return getName(); 02110 } 02111 02112 /// Produce a name to be used for class's metadata. It comes either via 02113 /// class's objc_runtime_name attribute or class name. 02114 StringRef getObjCRuntimeNameAsString() const; 02115 02116 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } 02117 ObjCInterfaceDecl *getSuperClass() { return SuperClass; } 02118 SourceLocation getSuperClassLoc() const { return SuperLoc; } 02119 02120 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } 02121 02122 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } 02123 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } 02124 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } 02125 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } 02126 02127 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator; 02128 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range; 02129 02130 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } 02131 ivar_iterator ivar_begin() const { 02132 return ivar_iterator(decls_begin()); 02133 } 02134 ivar_iterator ivar_end() const { 02135 return ivar_iterator(decls_end()); 02136 } 02137 unsigned ivar_size() const { 02138 return std::distance(ivar_begin(), ivar_end()); 02139 } 02140 bool ivar_empty() const { 02141 return ivar_begin() == ivar_end(); 02142 } 02143 02144 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 02145 static bool classofKind(Kind K) { return K == ObjCImplementation; } 02146 02147 friend class ASTDeclReader; 02148 friend class ASTDeclWriter; 02149 }; 02150 02151 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID); 02152 02153 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is 02154 /// declared as \@compatibility_alias alias class. 02155 class ObjCCompatibleAliasDecl : public NamedDecl { 02156 void anchor() override; 02157 /// Class that this is an alias of. 02158 ObjCInterfaceDecl *AliasedClass; 02159 02160 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 02161 ObjCInterfaceDecl* aliasedClass) 02162 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {} 02163 public: 02164 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC, 02165 SourceLocation L, IdentifierInfo *Id, 02166 ObjCInterfaceDecl* aliasedClass); 02167 02168 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, 02169 unsigned ID); 02170 02171 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } 02172 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; } 02173 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; } 02174 02175 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 02176 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; } 02177 02178 }; 02179 02180 /// \brief Represents one property declaration in an Objective-C interface. 02181 /// 02182 /// For example: 02183 /// \code{.mm} 02184 /// \@property (assign, readwrite) int MyProperty; 02185 /// \endcode 02186 class ObjCPropertyDecl : public NamedDecl { 02187 void anchor() override; 02188 public: 02189 enum PropertyAttributeKind { 02190 OBJC_PR_noattr = 0x00, 02191 OBJC_PR_readonly = 0x01, 02192 OBJC_PR_getter = 0x02, 02193 OBJC_PR_assign = 0x04, 02194 OBJC_PR_readwrite = 0x08, 02195 OBJC_PR_retain = 0x10, 02196 OBJC_PR_copy = 0x20, 02197 OBJC_PR_nonatomic = 0x40, 02198 OBJC_PR_setter = 0x80, 02199 OBJC_PR_atomic = 0x100, 02200 OBJC_PR_weak = 0x200, 02201 OBJC_PR_strong = 0x400, 02202 OBJC_PR_unsafe_unretained = 0x800 02203 // Adding a property should change NumPropertyAttrsBits 02204 }; 02205 02206 enum { 02207 /// \brief Number of bits fitting all the property attributes. 02208 NumPropertyAttrsBits = 12 02209 }; 02210 02211 enum SetterKind { Assign, Retain, Copy, Weak }; 02212 enum PropertyControl { None, Required, Optional }; 02213 private: 02214 SourceLocation AtLoc; // location of \@property 02215 SourceLocation LParenLoc; // location of '(' starting attribute list or null. 02216 TypeSourceInfo *DeclType; 02217 unsigned PropertyAttributes : NumPropertyAttrsBits; 02218 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits; 02219 // \@required/\@optional 02220 unsigned PropertyImplementation : 2; 02221 02222 Selector GetterName; // getter name of NULL if no getter 02223 Selector SetterName; // setter name of NULL if no setter 02224 02225 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method 02226 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method 02227 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property 02228 02229 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 02230 SourceLocation AtLocation, SourceLocation LParenLocation, 02231 TypeSourceInfo *T) 02232 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), 02233 LParenLoc(LParenLocation), DeclType(T), 02234 PropertyAttributes(OBJC_PR_noattr), 02235 PropertyAttributesAsWritten(OBJC_PR_noattr), 02236 PropertyImplementation(None), 02237 GetterName(Selector()), 02238 SetterName(Selector()), 02239 GetterMethodDecl(nullptr), SetterMethodDecl(nullptr), 02240 PropertyIvarDecl(nullptr) {} 02241 02242 public: 02243 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC, 02244 SourceLocation L, 02245 IdentifierInfo *Id, SourceLocation AtLocation, 02246 SourceLocation LParenLocation, 02247 TypeSourceInfo *T, 02248 PropertyControl propControl = None); 02249 02250 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID); 02251 02252 SourceLocation getAtLoc() const { return AtLoc; } 02253 void setAtLoc(SourceLocation L) { AtLoc = L; } 02254 02255 SourceLocation getLParenLoc() const { return LParenLoc; } 02256 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 02257 02258 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; } 02259 QualType getType() const { return DeclType->getType(); } 02260 void setType(TypeSourceInfo *T) { DeclType = T; } 02261 02262 PropertyAttributeKind getPropertyAttributes() const { 02263 return PropertyAttributeKind(PropertyAttributes); 02264 } 02265 void setPropertyAttributes(PropertyAttributeKind PRVal) { 02266 PropertyAttributes |= PRVal; 02267 } 02268 02269 PropertyAttributeKind getPropertyAttributesAsWritten() const { 02270 return PropertyAttributeKind(PropertyAttributesAsWritten); 02271 } 02272 02273 bool hasWrittenStorageAttribute() const { 02274 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy | 02275 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong | 02276 OBJC_PR_weak); 02277 } 02278 02279 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) { 02280 PropertyAttributesAsWritten = PRVal; 02281 } 02282 02283 void makeitReadWriteAttribute() { 02284 PropertyAttributes &= ~OBJC_PR_readonly; 02285 PropertyAttributes |= OBJC_PR_readwrite; 02286 } 02287 02288 // Helper methods for accessing attributes. 02289 02290 /// isReadOnly - Return true iff the property has a setter. 02291 bool isReadOnly() const { 02292 return (PropertyAttributes & OBJC_PR_readonly); 02293 } 02294 02295 /// isAtomic - Return true if the property is atomic. 02296 bool isAtomic() const { 02297 return (PropertyAttributes & OBJC_PR_atomic); 02298 } 02299 02300 /// isRetaining - Return true if the property retains its value. 02301 bool isRetaining() const { 02302 return (PropertyAttributes & 02303 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy)); 02304 } 02305 02306 /// getSetterKind - Return the method used for doing assignment in 02307 /// the property setter. This is only valid if the property has been 02308 /// defined to have a setter. 02309 SetterKind getSetterKind() const { 02310 if (PropertyAttributes & OBJC_PR_strong) 02311 return getType()->isBlockPointerType() ? Copy : Retain; 02312 if (PropertyAttributes & OBJC_PR_retain) 02313 return Retain; 02314 if (PropertyAttributes & OBJC_PR_copy) 02315 return Copy; 02316 if (PropertyAttributes & OBJC_PR_weak) 02317 return Weak; 02318 return Assign; 02319 } 02320 02321 Selector getGetterName() const { return GetterName; } 02322 void setGetterName(Selector Sel) { GetterName = Sel; } 02323 02324 Selector getSetterName() const { return SetterName; } 02325 void setSetterName(Selector Sel) { SetterName = Sel; } 02326 02327 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } 02328 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; } 02329 02330 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } 02331 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; } 02332 02333 // Related to \@optional/\@required declared in \@protocol 02334 void setPropertyImplementation(PropertyControl pc) { 02335 PropertyImplementation = pc; 02336 } 02337 PropertyControl getPropertyImplementation() const { 02338 return PropertyControl(PropertyImplementation); 02339 } 02340 02341 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) { 02342 PropertyIvarDecl = Ivar; 02343 } 02344 ObjCIvarDecl *getPropertyIvarDecl() const { 02345 return PropertyIvarDecl; 02346 } 02347 02348 SourceRange getSourceRange() const override LLVM_READONLY { 02349 return SourceRange(AtLoc, getLocation()); 02350 } 02351 02352 /// Get the default name of the synthesized ivar. 02353 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const; 02354 02355 /// Lookup a property by name in the specified DeclContext. 02356 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC, 02357 IdentifierInfo *propertyID); 02358 02359 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 02360 static bool classofKind(Kind K) { return K == ObjCProperty; } 02361 }; 02362 02363 /// ObjCPropertyImplDecl - Represents implementation declaration of a property 02364 /// in a class or category implementation block. For example: 02365 /// \@synthesize prop1 = ivar1; 02366 /// 02367 class ObjCPropertyImplDecl : public Decl { 02368 public: 02369 enum Kind { 02370 Synthesize, 02371 Dynamic 02372 }; 02373 private: 02374 SourceLocation AtLoc; // location of \@synthesize or \@dynamic 02375 02376 /// \brief For \@synthesize, the location of the ivar, if it was written in 02377 /// the source code. 02378 /// 02379 /// \code 02380 /// \@synthesize int a = b 02381 /// \endcode 02382 SourceLocation IvarLoc; 02383 02384 /// Property declaration being implemented 02385 ObjCPropertyDecl *PropertyDecl; 02386 02387 /// Null for \@dynamic. Required for \@synthesize. 02388 ObjCIvarDecl *PropertyIvarDecl; 02389 02390 /// Null for \@dynamic. Non-null if property must be copy-constructed in 02391 /// getter. 02392 Expr *GetterCXXConstructor; 02393 02394 /// Null for \@dynamic. Non-null if property has assignment operator to call 02395 /// in Setter synthesis. 02396 Expr *SetterCXXAssignment; 02397 02398 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L, 02399 ObjCPropertyDecl *property, 02400 Kind PK, 02401 ObjCIvarDecl *ivarDecl, 02402 SourceLocation ivarLoc) 02403 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc), 02404 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl), 02405 GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) { 02406 assert (PK == Dynamic || PropertyIvarDecl); 02407 } 02408 02409 public: 02410 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC, 02411 SourceLocation atLoc, SourceLocation L, 02412 ObjCPropertyDecl *property, 02413 Kind PK, 02414 ObjCIvarDecl *ivarDecl, 02415 SourceLocation ivarLoc); 02416 02417 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); 02418 02419 SourceRange getSourceRange() const override LLVM_READONLY; 02420 02421 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; } 02422 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 02423 02424 ObjCPropertyDecl *getPropertyDecl() const { 02425 return PropertyDecl; 02426 } 02427 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; } 02428 02429 Kind getPropertyImplementation() const { 02430 return PropertyIvarDecl ? Synthesize : Dynamic; 02431 } 02432 02433 ObjCIvarDecl *getPropertyIvarDecl() const { 02434 return PropertyIvarDecl; 02435 } 02436 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; } 02437 02438 void setPropertyIvarDecl(ObjCIvarDecl *Ivar, 02439 SourceLocation IvarLoc) { 02440 PropertyIvarDecl = Ivar; 02441 this->IvarLoc = IvarLoc; 02442 } 02443 02444 /// \brief For \@synthesize, returns true if an ivar name was explicitly 02445 /// specified. 02446 /// 02447 /// \code 02448 /// \@synthesize int a = b; // true 02449 /// \@synthesize int a; // false 02450 /// \endcode 02451 bool isIvarNameSpecified() const { 02452 return IvarLoc.isValid() && IvarLoc != getLocation(); 02453 } 02454 02455 Expr *getGetterCXXConstructor() const { 02456 return GetterCXXConstructor; 02457 } 02458 void setGetterCXXConstructor(Expr *getterCXXConstructor) { 02459 GetterCXXConstructor = getterCXXConstructor; 02460 } 02461 02462 Expr *getSetterCXXAssignment() const { 02463 return SetterCXXAssignment; 02464 } 02465 void setSetterCXXAssignment(Expr *setterCXXAssignment) { 02466 SetterCXXAssignment = setterCXXAssignment; 02467 } 02468 02469 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 02470 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; } 02471 02472 friend class ASTDeclReader; 02473 }; 02474 02475 template<bool (*Filter)(ObjCCategoryDecl *)> 02476 void 02477 ObjCInterfaceDecl::filtered_category_iterator<Filter>:: 02478 findAcceptableCategory() { 02479 while (Current && !Filter(Current)) 02480 Current = Current->getNextClassCategoryRaw(); 02481 } 02482 02483 template<bool (*Filter)(ObjCCategoryDecl *)> 02484 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> & 02485 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() { 02486 Current = Current->getNextClassCategoryRaw(); 02487 findAcceptableCategory(); 02488 return *this; 02489 } 02490 02491 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { 02492 return !Cat->isHidden(); 02493 } 02494 02495 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { 02496 return Cat->IsClassExtension() && !Cat->isHidden(); 02497 } 02498 02499 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) { 02500 return Cat->IsClassExtension(); 02501 } 02502 02503 } // end namespace clang 02504 #endif