1 //===--- DeclObjC.h - Classes for representing declarations -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DeclObjC interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLOBJC_H
15 #define LLVM_CLANG_AST_DECLOBJC_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/SelectorLocationsKind.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Compiler.h"
29 class ObjCProtocolDecl;
30 class ObjCCategoryDecl;
31 class ObjCPropertyDecl;
32 class ObjCPropertyImplDecl;
33 class CXXCtorInitializer;
36 ObjCListBase(const ObjCListBase &) LLVM_DELETED_FUNCTION;
37 void operator=(const ObjCListBase &) LLVM_DELETED_FUNCTION;
39 /// List is an array of pointers to objects that are not owned by this object.
44 ObjCListBase() : List(nullptr), NumElts(0) {}
45 unsigned size() const { return NumElts; }
46 bool empty() const { return NumElts == 0; }
49 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
53 /// ObjCList - This is a simple template class used to hold various lists of
54 /// decls etc, which is heavily used by the ObjC front-end. This only use case
55 /// this supports is setting the list all at once and then reading elements out
58 class ObjCList : public ObjCListBase {
60 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
61 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
64 typedef T* const * iterator;
65 iterator begin() const { return (iterator)List; }
66 iterator end() const { return (iterator)List+NumElts; }
68 T* operator[](unsigned Idx) const {
69 assert(Idx < NumElts && "Invalid access");
74 /// \brief A list of Objective-C protocols, along with the source
75 /// locations at which they were referenced.
76 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
77 SourceLocation *Locations;
79 using ObjCList<ObjCProtocolDecl>::set;
82 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(nullptr) { }
84 typedef const SourceLocation *loc_iterator;
85 loc_iterator loc_begin() const { return Locations; }
86 loc_iterator loc_end() const { return Locations + size(); }
88 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
89 const SourceLocation *Locs, ASTContext &Ctx);
93 /// ObjCMethodDecl - Represents an instance or class method declaration.
94 /// ObjC methods can be declared within 4 contexts: class interfaces,
95 /// categories, protocols, and class implementations. While C++ member
96 /// functions leverage C syntax, Objective-C method syntax is modeled after
97 /// Smalltalk (using colons to specify argument types/expressions).
98 /// Here are some brief examples:
100 /// Setter/getter instance methods:
101 /// - (void)setMenu:(NSMenu *)menu;
102 /// - (NSMenu *)menu;
104 /// Instance method that takes 2 NSView arguments:
105 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
107 /// Getter class method:
108 /// + (NSMenu *)defaultMenu;
110 /// A selector represents a unique name for a method. The selector names for
111 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
113 class ObjCMethodDecl : public NamedDecl, public DeclContext {
115 enum ImplementationControl { None, Required, Optional };
117 // The conventional meaning of this method; an ObjCMethodFamily.
118 // This is not serialized; instead, it is computed on demand and
120 mutable unsigned Family : ObjCMethodFamilyBitWidth;
122 /// instance (true) or class (false) method.
123 unsigned IsInstance : 1;
124 unsigned IsVariadic : 1;
126 /// True if this method is the getter or setter for an explicit property.
127 unsigned IsPropertyAccessor : 1;
129 // Method has a definition.
130 unsigned IsDefined : 1;
132 /// \brief Method redeclaration in the same interface.
133 unsigned IsRedeclaration : 1;
135 /// \brief Is redeclared in the same interface.
136 mutable unsigned HasRedeclaration : 1;
138 // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
139 /// \@required/\@optional
140 unsigned DeclImplementation : 2;
142 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
144 unsigned objcDeclQualifier : 6;
146 /// \brief Indicates whether this method has a related result type.
147 unsigned RelatedResultType : 1;
149 /// \brief Whether the locations of the selector identifiers are in a
150 /// "standard" position, a enum SelectorLocationsKind.
151 unsigned SelLocsKind : 2;
153 /// \brief Whether this method overrides any other in the class hierarchy.
155 /// A method is said to override any method in the class's
156 /// base classes, its protocols, or its categories' protocols, that has
157 /// the same selector and is of the same kind (class or instance).
158 /// A method in an implementation is not considered as overriding the same
159 /// method in the interface or its categories.
160 unsigned IsOverriding : 1;
162 /// \brief Indicates if the method was a definition but its body was skipped.
163 unsigned HasSkippedBody : 1;
165 // Return type of this method.
166 QualType MethodDeclType;
168 // Type source information for the return type.
169 TypeSourceInfo *ReturnTInfo;
171 /// \brief Array of ParmVarDecls for the formal parameters of this method
172 /// and optionally followed by selector locations.
173 void *ParamsAndSelLocs;
176 /// List of attributes for this method declaration.
177 SourceLocation DeclEndLoc; // the location of the ';' or '{'.
179 // The following are only used for method definitions, null otherwise.
180 LazyDeclStmtPtr Body;
182 /// SelfDecl - Decl for the implicit self parameter. This is lazily
183 /// constructed by createImplicitParams.
184 ImplicitParamDecl *SelfDecl;
185 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
186 /// constructed by createImplicitParams.
187 ImplicitParamDecl *CmdDecl;
189 SelectorLocationsKind getSelLocsKind() const {
190 return (SelectorLocationsKind)SelLocsKind;
192 bool hasStandardSelLocs() const {
193 return getSelLocsKind() != SelLoc_NonStandard;
196 /// \brief Get a pointer to the stored selector identifiers locations array.
197 /// No locations will be stored if HasStandardSelLocs is true.
198 SourceLocation *getStoredSelLocs() {
199 return reinterpret_cast<SourceLocation*>(getParams() + NumParams);
201 const SourceLocation *getStoredSelLocs() const {
202 return reinterpret_cast<const SourceLocation*>(getParams() + NumParams);
205 /// \brief Get a pointer to the stored selector identifiers locations array.
206 /// No locations will be stored if HasStandardSelLocs is true.
207 ParmVarDecl **getParams() {
208 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
210 const ParmVarDecl *const *getParams() const {
211 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
214 /// \brief Get the number of stored selector identifiers locations.
215 /// No locations will be stored if HasStandardSelLocs is true.
216 unsigned getNumStoredSelLocs() const {
217 if (hasStandardSelLocs())
219 return getNumSelectorLocs();
222 void setParamsAndSelLocs(ASTContext &C,
223 ArrayRef<ParmVarDecl*> Params,
224 ArrayRef<SourceLocation> SelLocs);
226 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
227 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
228 DeclContext *contextDecl, bool isInstance = true,
229 bool isVariadic = false, bool isPropertyAccessor = false,
230 bool isImplicitlyDeclared = false, bool isDefined = false,
231 ImplementationControl impControl = None,
232 bool HasRelatedResultType = false)
233 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
234 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily),
235 IsInstance(isInstance), IsVariadic(isVariadic),
236 IsPropertyAccessor(isPropertyAccessor), IsDefined(isDefined),
237 IsRedeclaration(0), HasRedeclaration(0), DeclImplementation(impControl),
238 objcDeclQualifier(OBJC_TQ_None),
239 RelatedResultType(HasRelatedResultType),
240 SelLocsKind(SelLoc_StandardNoSpace), IsOverriding(0), HasSkippedBody(0),
241 MethodDeclType(T), ReturnTInfo(ReturnTInfo), ParamsAndSelLocs(nullptr),
242 NumParams(0), DeclEndLoc(endLoc), Body(), SelfDecl(nullptr),
244 setImplicit(isImplicitlyDeclared);
247 /// \brief A definition will return its interface declaration.
248 /// An interface declaration will return its definition.
249 /// Otherwise it will return itself.
250 ObjCMethodDecl *getNextRedeclarationImpl() override;
253 static ObjCMethodDecl *
254 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
255 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
256 DeclContext *contextDecl, bool isInstance = true,
257 bool isVariadic = false, bool isPropertyAccessor = false,
258 bool isImplicitlyDeclared = false, bool isDefined = false,
259 ImplementationControl impControl = None,
260 bool HasRelatedResultType = false);
262 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
264 ObjCMethodDecl *getCanonicalDecl() override;
265 const ObjCMethodDecl *getCanonicalDecl() const {
266 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
269 ObjCDeclQualifier getObjCDeclQualifier() const {
270 return ObjCDeclQualifier(objcDeclQualifier);
272 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
274 /// \brief Determine whether this method has a result type that is related
275 /// to the message receiver's type.
276 bool hasRelatedResultType() const { return RelatedResultType; }
278 /// \brief Note whether this method has a related result type.
279 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; }
281 /// \brief True if this is a method redeclaration in the same interface.
282 bool isRedeclaration() const { return IsRedeclaration; }
283 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
285 /// \brief Returns the location where the declarator ends. It will be
286 /// the location of ';' for a method declaration and the location of '{'
287 /// for a method definition.
288 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
290 // Location information, modeled after the Stmt API.
291 SourceLocation getLocStart() const LLVM_READONLY { return getLocation(); }
292 SourceLocation getLocEnd() const LLVM_READONLY;
293 SourceRange getSourceRange() const override LLVM_READONLY {
294 return SourceRange(getLocation(), getLocEnd());
297 SourceLocation getSelectorStartLoc() const {
299 return getLocStart();
300 return getSelectorLoc(0);
302 SourceLocation getSelectorLoc(unsigned Index) const {
303 assert(Index < getNumSelectorLocs() && "Index out of range!");
304 if (hasStandardSelLocs())
305 return getStandardSelectorLoc(Index, getSelector(),
306 getSelLocsKind() == SelLoc_StandardWithSpace,
309 return getStoredSelLocs()[Index];
312 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
314 unsigned getNumSelectorLocs() const {
317 Selector Sel = getSelector();
318 if (Sel.isUnarySelector())
320 return Sel.getNumArgs();
323 ObjCInterfaceDecl *getClassInterface();
324 const ObjCInterfaceDecl *getClassInterface() const {
325 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
328 Selector getSelector() const { return getDeclName().getObjCSelector(); }
330 QualType getReturnType() const { return MethodDeclType; }
331 void setReturnType(QualType T) { MethodDeclType = T; }
333 /// \brief Determine the type of an expression that sends a message to this
335 QualType getSendResultType() const {
336 return getReturnType().getNonLValueExprType(getASTContext());
339 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
340 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
342 // Iterator access to formal parameters.
343 unsigned param_size() const { return NumParams; }
344 typedef const ParmVarDecl *const *param_const_iterator;
345 typedef ParmVarDecl *const *param_iterator;
346 typedef llvm::iterator_range<param_iterator> param_range;
347 typedef llvm::iterator_range<param_const_iterator> param_const_range;
349 param_range params() { return param_range(param_begin(), param_end()); }
350 param_const_range params() const {
351 return param_const_range(param_begin(), param_end());
354 param_const_iterator param_begin() const {
355 return param_const_iterator(getParams());
357 param_const_iterator param_end() const {
358 return param_const_iterator(getParams() + NumParams);
360 param_iterator param_begin() { return param_iterator(getParams()); }
361 param_iterator param_end() { return param_iterator(getParams() + NumParams); }
363 // This method returns and of the parameters which are part of the selector
364 // name mangling requirements.
365 param_const_iterator sel_param_end() const {
366 return param_begin() + getSelector().getNumArgs();
369 // ArrayRef access to formal parameters. This should eventually
370 // replace the iterator interface above.
371 ArrayRef<ParmVarDecl*> parameters() const {
372 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
376 /// \brief Sets the method's parameters and selector source locations.
377 /// If the method is implicit (not coming from source) \p SelLocs is
379 void setMethodParams(ASTContext &C,
380 ArrayRef<ParmVarDecl*> Params,
381 ArrayRef<SourceLocation> SelLocs =
382 ArrayRef<SourceLocation>());
384 // Iterator access to parameter types.
385 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
386 typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
389 param_type_iterator param_type_begin() const {
390 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
392 param_type_iterator param_type_end() const {
393 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
396 /// createImplicitParams - Used to lazily create the self and cmd
397 /// implict parameters. This must be called prior to using getSelfDecl()
398 /// or getCmdDecl(). The call is ignored if the implicit paramters
399 /// have already been created.
400 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
402 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
403 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
404 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
405 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
407 /// Determines the family of this method.
408 ObjCMethodFamily getMethodFamily() const;
410 bool isInstanceMethod() const { return IsInstance; }
411 void setInstanceMethod(bool isInst) { IsInstance = isInst; }
412 bool isVariadic() const { return IsVariadic; }
413 void setVariadic(bool isVar) { IsVariadic = isVar; }
415 bool isClassMethod() const { return !IsInstance; }
417 bool isPropertyAccessor() const { return IsPropertyAccessor; }
418 void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; }
420 bool isDefined() const { return IsDefined; }
421 void setDefined(bool isDefined) { IsDefined = isDefined; }
423 /// \brief Whether this method overrides any other in the class hierarchy.
425 /// A method is said to override any method in the class's
426 /// base classes, its protocols, or its categories' protocols, that has
427 /// the same selector and is of the same kind (class or instance).
428 /// A method in an implementation is not considered as overriding the same
429 /// method in the interface or its categories.
430 bool isOverriding() const { return IsOverriding; }
431 void setOverriding(bool isOverriding) { IsOverriding = isOverriding; }
433 /// \brief Return overridden methods for the given \p Method.
435 /// An ObjC method is considered to override any method in the class's
436 /// base classes (and base's categories), its protocols, or its categories'
437 /// protocols, that has
438 /// the same selector and is of the same kind (class or instance).
439 /// A method in an implementation is not considered as overriding the same
440 /// method in the interface or its categories.
441 void getOverriddenMethods(
442 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
444 /// \brief True if the method was a definition but its body was skipped.
445 bool hasSkippedBody() const { return HasSkippedBody; }
446 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
448 /// \brief Returns the property associated with this method's selector.
450 /// Note that even if this particular method is not marked as a property
451 /// accessor, it is still possible for it to match a property declared in a
452 /// superclass. Pass \c false if you only want to check the current class.
453 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
455 // Related to protocols declared in \@protocol
456 void setDeclImplementation(ImplementationControl ic) {
457 DeclImplementation = ic;
459 ImplementationControl getImplementationControl() const {
460 return ImplementationControl(DeclImplementation);
463 /// Returns true if this specific method declaration is marked with the
464 /// designated initializer attribute.
465 bool isThisDeclarationADesignatedInitializer() const;
467 /// Returns true if the method selector resolves to a designated initializer
468 /// in the class's interface.
470 /// \param InitMethod if non-null and the function returns true, it receives
471 /// the method declaration that was marked with the designated initializer
473 bool isDesignatedInitializerForTheInterface(
474 const ObjCMethodDecl **InitMethod = nullptr) const;
476 /// \brief Determine whether this method has a body.
477 bool hasBody() const override { return Body.isValid(); }
479 /// \brief Retrieve the body of this method, if it has one.
480 Stmt *getBody() const override;
482 void setLazyBody(uint64_t Offset) { Body = Offset; }
484 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
485 void setBody(Stmt *B) { Body = B; }
487 /// \brief Returns whether this specific method is a definition.
488 bool isThisDeclarationADefinition() const { return hasBody(); }
490 // Implement isa/cast/dyncast/etc.
491 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
492 static bool classofKind(Kind K) { return K == ObjCMethod; }
493 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
494 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
496 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
497 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
500 friend class ASTDeclReader;
501 friend class ASTDeclWriter;
504 /// ObjCContainerDecl - Represents a container for method declarations.
505 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
506 /// ObjCProtocolDecl, and ObjCImplDecl.
508 class ObjCContainerDecl : public NamedDecl, public DeclContext {
509 void anchor() override;
511 SourceLocation AtStart;
513 // These two locations in the range mark the end of the method container.
514 // The first points to the '@' token, and the second to the 'end' token.
518 ObjCContainerDecl(Kind DK, DeclContext *DC,
519 IdentifierInfo *Id, SourceLocation nameLoc,
520 SourceLocation atStartLoc)
521 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
523 // Iterator access to properties.
524 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
525 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>
528 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
529 prop_iterator prop_begin() const {
530 return prop_iterator(decls_begin());
532 prop_iterator prop_end() const {
533 return prop_iterator(decls_end());
536 // Iterator access to instance/class methods.
537 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
538 typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>
541 method_range methods() const {
542 return method_range(meth_begin(), meth_end());
544 method_iterator meth_begin() const {
545 return method_iterator(decls_begin());
547 method_iterator meth_end() const {
548 return method_iterator(decls_end());
551 typedef filtered_decl_iterator<ObjCMethodDecl,
552 &ObjCMethodDecl::isInstanceMethod>
554 typedef llvm::iterator_range<instmeth_iterator> instmeth_range;
556 instmeth_range instance_methods() const {
557 return instmeth_range(instmeth_begin(), instmeth_end());
559 instmeth_iterator instmeth_begin() const {
560 return instmeth_iterator(decls_begin());
562 instmeth_iterator instmeth_end() const {
563 return instmeth_iterator(decls_end());
566 typedef filtered_decl_iterator<ObjCMethodDecl,
567 &ObjCMethodDecl::isClassMethod>
569 typedef llvm::iterator_range<classmeth_iterator> classmeth_range;
571 classmeth_range class_methods() const {
572 return classmeth_range(classmeth_begin(), classmeth_end());
574 classmeth_iterator classmeth_begin() const {
575 return classmeth_iterator(decls_begin());
577 classmeth_iterator classmeth_end() const {
578 return classmeth_iterator(decls_end());
581 // Get the local instance/class method declared in this interface.
582 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
583 bool AllowHidden = false) const;
584 ObjCMethodDecl *getInstanceMethod(Selector Sel,
585 bool AllowHidden = false) const {
586 return getMethod(Sel, true/*isInstance*/, AllowHidden);
588 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
589 return getMethod(Sel, false/*isInstance*/, AllowHidden);
591 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
592 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
594 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
596 typedef llvm::DenseMap<IdentifierInfo*, ObjCPropertyDecl*> PropertyMap;
598 typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*>
601 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
603 /// This routine collects list of properties to be implemented in the class.
604 /// This includes, class's and its conforming protocols' properties.
605 /// Note, the superclass's properties are not included in the list.
606 virtual void collectPropertiesToImplement(PropertyMap &PM,
607 PropertyDeclOrder &PO) const {}
609 SourceLocation getAtStartLoc() const { return AtStart; }
610 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
612 // Marks the end of the container.
613 SourceRange getAtEndRange() const {
616 void setAtEndRange(SourceRange atEnd) {
620 SourceRange getSourceRange() const override LLVM_READONLY {
621 return SourceRange(AtStart, getAtEndRange().getEnd());
624 // Implement isa/cast/dyncast/etc.
625 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
626 static bool classofKind(Kind K) {
627 return K >= firstObjCContainer &&
628 K <= lastObjCContainer;
631 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
632 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
634 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
635 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
639 /// \brief Represents an ObjC class declaration.
644 /// // MostPrimitive declares no super class (not particularly useful).
645 /// \@interface MostPrimitive
646 /// // no instance variables or methods.
649 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
650 /// \@interface NSResponder : NSObject \<NSCoding>
651 /// { // instance variables are represented by ObjCIvarDecl.
652 /// id nextResponder; // nextResponder instance variable.
654 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
655 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
656 /// \@end // to an NSEvent.
659 /// Unlike C/C++, forward class declarations are accomplished with \@class.
660 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
661 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
662 /// typically inherit from NSObject (an exception is NSProxy).
664 class ObjCInterfaceDecl : public ObjCContainerDecl
665 , public Redeclarable<ObjCInterfaceDecl> {
666 void anchor() override;
668 /// TypeForDecl - This indicates the Type object that represents this
669 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
670 mutable const Type *TypeForDecl;
671 friend class ASTContext;
673 struct DefinitionData {
674 /// \brief The definition of this class, for quick access from any
676 ObjCInterfaceDecl *Definition;
678 /// Class's super class.
679 ObjCInterfaceDecl *SuperClass;
681 /// Protocols referenced in the \@interface declaration
682 ObjCProtocolList ReferencedProtocols;
684 /// Protocols reference in both the \@interface and class extensions.
685 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
687 /// \brief List of categories and class extensions defined for this class.
689 /// Categories are stored as a linked list in the AST, since the categories
690 /// and class extensions come long after the initial interface declaration,
691 /// and we avoid dynamically-resized arrays in the AST wherever possible.
692 ObjCCategoryDecl *CategoryList;
694 /// IvarList - List of all ivars defined by this class; including class
695 /// extensions and implementation. This list is built lazily.
696 ObjCIvarDecl *IvarList;
698 /// \brief Indicates that the contents of this Objective-C class will be
699 /// completed by the external AST source when required.
700 mutable bool ExternallyCompleted : 1;
702 /// \brief Indicates that the ivar cache does not yet include ivars
703 /// declared in the implementation.
704 mutable bool IvarListMissingImplementation : 1;
706 /// Indicates that this interface decl contains at least one initializer
707 /// marked with the 'objc_designated_initializer' attribute.
708 bool HasDesignatedInitializers : 1;
710 enum InheritedDesignatedInitializersState {
711 /// We didn't calculate whether the designated initializers should be
712 /// inherited or not.
714 /// Designated initializers are inherited for the super class.
716 /// The class does not inherit designated initializers.
719 /// One of the \c InheritedDesignatedInitializersState enumeratos.
720 mutable unsigned InheritedDesignatedInitializers : 2;
722 /// \brief The location of the superclass, if any.
723 SourceLocation SuperClassLoc;
725 /// \brief The location of the last location in this declaration, before
726 /// the properties/methods. For example, this will be the '>', '}', or
728 SourceLocation EndLoc;
730 DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(),
731 ExternallyCompleted(),
732 IvarListMissingImplementation(true),
733 HasDesignatedInitializers(),
734 InheritedDesignatedInitializers(IDI_Unknown) { }
737 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
738 IdentifierInfo *Id, SourceLocation CLoc,
739 ObjCInterfaceDecl *PrevDecl, bool IsInternal);
741 void LoadExternalDefinition() const;
743 /// \brief Contains a pointer to the data associated with this class,
744 /// which will be NULL if this class has not yet been defined.
746 /// The bit indicates when we don't need to check for out-of-date
747 /// declarations. It will be set unless modules are enabled.
748 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
750 DefinitionData &data() const {
751 assert(Data.getPointer() && "Declaration has no definition!");
752 return *Data.getPointer();
755 /// \brief Allocate the definition data for this class.
756 void allocateDefinitionData();
758 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
759 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
760 return getNextRedeclaration();
762 ObjCInterfaceDecl *getPreviousDeclImpl() override {
763 return getPreviousDecl();
765 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
766 return getMostRecentDecl();
770 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
771 SourceLocation atLoc,
773 ObjCInterfaceDecl *PrevDecl,
774 SourceLocation ClassLoc = SourceLocation(),
775 bool isInternal = false);
777 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
779 SourceRange getSourceRange() const override LLVM_READONLY {
780 if (isThisDeclarationADefinition())
781 return ObjCContainerDecl::getSourceRange();
783 return SourceRange(getAtStartLoc(), getLocation());
786 /// \brief Indicate that this Objective-C class is complete, but that
787 /// the external AST source will be responsible for filling in its contents
788 /// when a complete class is required.
789 void setExternallyCompleted();
791 /// Indicate that this interface decl contains at least one initializer
792 /// marked with the 'objc_designated_initializer' attribute.
793 void setHasDesignatedInitializers();
795 /// Returns true if this interface decl contains at least one initializer
796 /// marked with the 'objc_designated_initializer' attribute.
797 bool hasDesignatedInitializers() const;
799 /// Returns true if this interface decl declares a designated initializer
800 /// or it inherites one from its super class.
801 bool declaresOrInheritsDesignatedInitializers() const {
802 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
805 const ObjCProtocolList &getReferencedProtocols() const {
806 assert(hasDefinition() && "Caller did not check for forward reference!");
807 if (data().ExternallyCompleted)
808 LoadExternalDefinition();
810 return data().ReferencedProtocols;
813 ObjCImplementationDecl *getImplementation() const;
814 void setImplementation(ObjCImplementationDecl *ImplD);
816 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
818 // Get the local instance/class method declared in a category.
819 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
820 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
821 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
822 return isInstance ? getInstanceMethod(Sel)
823 : getClassMethod(Sel);
826 typedef ObjCProtocolList::iterator protocol_iterator;
827 typedef llvm::iterator_range<protocol_iterator> protocol_range;
829 protocol_range protocols() const {
830 return protocol_range(protocol_begin(), protocol_end());
832 protocol_iterator protocol_begin() const {
833 // FIXME: Should make sure no callers ever do this.
834 if (!hasDefinition())
835 return protocol_iterator();
837 if (data().ExternallyCompleted)
838 LoadExternalDefinition();
840 return data().ReferencedProtocols.begin();
842 protocol_iterator protocol_end() const {
843 // FIXME: Should make sure no callers ever do this.
844 if (!hasDefinition())
845 return protocol_iterator();
847 if (data().ExternallyCompleted)
848 LoadExternalDefinition();
850 return data().ReferencedProtocols.end();
853 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
854 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
856 protocol_loc_range protocol_locs() const {
857 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
859 protocol_loc_iterator protocol_loc_begin() const {
860 // FIXME: Should make sure no callers ever do this.
861 if (!hasDefinition())
862 return protocol_loc_iterator();
864 if (data().ExternallyCompleted)
865 LoadExternalDefinition();
867 return data().ReferencedProtocols.loc_begin();
870 protocol_loc_iterator protocol_loc_end() const {
871 // FIXME: Should make sure no callers ever do this.
872 if (!hasDefinition())
873 return protocol_loc_iterator();
875 if (data().ExternallyCompleted)
876 LoadExternalDefinition();
878 return data().ReferencedProtocols.loc_end();
881 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
882 typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range;
884 all_protocol_range all_referenced_protocols() const {
885 return all_protocol_range(all_referenced_protocol_begin(),
886 all_referenced_protocol_end());
888 all_protocol_iterator all_referenced_protocol_begin() const {
889 // FIXME: Should make sure no callers ever do this.
890 if (!hasDefinition())
891 return all_protocol_iterator();
893 if (data().ExternallyCompleted)
894 LoadExternalDefinition();
896 return data().AllReferencedProtocols.empty()
898 : data().AllReferencedProtocols.begin();
900 all_protocol_iterator all_referenced_protocol_end() const {
901 // FIXME: Should make sure no callers ever do this.
902 if (!hasDefinition())
903 return all_protocol_iterator();
905 if (data().ExternallyCompleted)
906 LoadExternalDefinition();
908 return data().AllReferencedProtocols.empty()
910 : data().AllReferencedProtocols.end();
913 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
914 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
916 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
917 ivar_iterator ivar_begin() const {
918 if (const ObjCInterfaceDecl *Def = getDefinition())
919 return ivar_iterator(Def->decls_begin());
921 // FIXME: Should make sure no callers ever do this.
922 return ivar_iterator();
924 ivar_iterator ivar_end() const {
925 if (const ObjCInterfaceDecl *Def = getDefinition())
926 return ivar_iterator(Def->decls_end());
928 // FIXME: Should make sure no callers ever do this.
929 return ivar_iterator();
932 unsigned ivar_size() const {
933 return std::distance(ivar_begin(), ivar_end());
936 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
938 ObjCIvarDecl *all_declared_ivar_begin();
939 const ObjCIvarDecl *all_declared_ivar_begin() const {
940 // Even though this modifies IvarList, it's conceptually const:
941 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
942 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
944 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
946 /// setProtocolList - Set the list of protocols that this interface
948 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
949 const SourceLocation *Locs, ASTContext &C) {
950 data().ReferencedProtocols.set(List, Num, Locs, C);
953 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
954 /// into the protocol list for this class.
955 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
959 /// Produce a name to be used for class's metadata. It comes either via
960 /// objc_runtime_name attribute or class name.
961 StringRef getObjCRuntimeNameAsString() const;
963 /// Returns the designated initializers for the interface.
965 /// If this declaration does not have methods marked as designated
966 /// initializers then the interface inherits the designated initializers of
968 void getDesignatedInitializers(
969 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
971 /// Returns true if the given selector is a designated initializer for the
974 /// If this declaration does not have methods marked as designated
975 /// initializers then the interface inherits the designated initializers of
978 /// \param InitMethod if non-null and the function returns true, it receives
979 /// the method that was marked as a designated initializer.
981 isDesignatedInitializer(Selector Sel,
982 const ObjCMethodDecl **InitMethod = nullptr) const;
984 /// \brief Determine whether this particular declaration of this class is
985 /// actually also a definition.
986 bool isThisDeclarationADefinition() const {
987 return getDefinition() == this;
990 /// \brief Determine whether this class has been defined.
991 bool hasDefinition() const {
992 // If the name of this class is out-of-date, bring it up-to-date, which
993 // might bring in a definition.
994 // Note: a null value indicates that we don't have a definition and that
995 // modules are enabled.
996 if (!Data.getOpaqueValue()) {
997 if (IdentifierInfo *II = getIdentifier()) {
998 if (II->isOutOfDate()) {
999 updateOutOfDate(*II);
1004 return Data.getPointer();
1007 /// \brief Retrieve the definition of this class, or NULL if this class
1008 /// has been forward-declared (with \@class) but not yet defined (with
1010 ObjCInterfaceDecl *getDefinition() {
1011 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1014 /// \brief Retrieve the definition of this class, or NULL if this class
1015 /// has been forward-declared (with \@class) but not yet defined (with
1017 const ObjCInterfaceDecl *getDefinition() const {
1018 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1021 /// \brief Starts the definition of this Objective-C class, taking it from
1022 /// a forward declaration (\@class) to a definition (\@interface).
1023 void startDefinition();
1025 ObjCInterfaceDecl *getSuperClass() const {
1026 // FIXME: Should make sure no callers ever do this.
1027 if (!hasDefinition())
1030 if (data().ExternallyCompleted)
1031 LoadExternalDefinition();
1033 return data().SuperClass;
1036 void setSuperClass(ObjCInterfaceDecl * superCls) {
1038 (superCls && superCls->hasDefinition()) ? superCls->getDefinition()
1042 /// \brief Iterator that walks over the list of categories, filtering out
1043 /// those that do not meet specific criteria.
1045 /// This class template is used for the various permutations of category
1046 /// and extension iterators.
1047 template<bool (*Filter)(ObjCCategoryDecl *)>
1048 class filtered_category_iterator {
1049 ObjCCategoryDecl *Current;
1051 void findAcceptableCategory();
1054 typedef ObjCCategoryDecl * value_type;
1055 typedef value_type reference;
1056 typedef value_type pointer;
1057 typedef std::ptrdiff_t difference_type;
1058 typedef std::input_iterator_tag iterator_category;
1060 filtered_category_iterator() : Current(nullptr) { }
1061 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1064 findAcceptableCategory();
1067 reference operator*() const { return Current; }
1068 pointer operator->() const { return Current; }
1070 filtered_category_iterator &operator++();
1072 filtered_category_iterator operator++(int) {
1073 filtered_category_iterator Tmp = *this;
1078 friend bool operator==(filtered_category_iterator X,
1079 filtered_category_iterator Y) {
1080 return X.Current == Y.Current;
1083 friend bool operator!=(filtered_category_iterator X,
1084 filtered_category_iterator Y) {
1085 return X.Current != Y.Current;
1090 /// \brief Test whether the given category is visible.
1092 /// Used in the \c visible_categories_iterator.
1093 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1096 /// \brief Iterator that walks over the list of categories and extensions
1097 /// that are visible, i.e., not hidden in a non-imported submodule.
1098 typedef filtered_category_iterator<isVisibleCategory>
1099 visible_categories_iterator;
1101 typedef llvm::iterator_range<visible_categories_iterator>
1102 visible_categories_range;
1104 visible_categories_range visible_categories() const {
1105 return visible_categories_range(visible_categories_begin(),
1106 visible_categories_end());
1109 /// \brief Retrieve an iterator to the beginning of the visible-categories
1111 visible_categories_iterator visible_categories_begin() const {
1112 return visible_categories_iterator(getCategoryListRaw());
1115 /// \brief Retrieve an iterator to the end of the visible-categories list.
1116 visible_categories_iterator visible_categories_end() const {
1117 return visible_categories_iterator();
1120 /// \brief Determine whether the visible-categories list is empty.
1121 bool visible_categories_empty() const {
1122 return visible_categories_begin() == visible_categories_end();
1126 /// \brief Test whether the given category... is a category.
1128 /// Used in the \c known_categories_iterator.
1129 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1132 /// \brief Iterator that walks over all of the known categories and
1133 /// extensions, including those that are hidden.
1134 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1135 typedef llvm::iterator_range<known_categories_iterator>
1136 known_categories_range;
1138 known_categories_range known_categories() const {
1139 return known_categories_range(known_categories_begin(),
1140 known_categories_end());
1143 /// \brief Retrieve an iterator to the beginning of the known-categories
1145 known_categories_iterator known_categories_begin() const {
1146 return known_categories_iterator(getCategoryListRaw());
1149 /// \brief Retrieve an iterator to the end of the known-categories list.
1150 known_categories_iterator known_categories_end() const {
1151 return known_categories_iterator();
1154 /// \brief Determine whether the known-categories list is empty.
1155 bool known_categories_empty() const {
1156 return known_categories_begin() == known_categories_end();
1160 /// \brief Test whether the given category is a visible extension.
1162 /// Used in the \c visible_extensions_iterator.
1163 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1166 /// \brief Iterator that walks over all of the visible extensions, skipping
1167 /// any that are known but hidden.
1168 typedef filtered_category_iterator<isVisibleExtension>
1169 visible_extensions_iterator;
1171 typedef llvm::iterator_range<visible_extensions_iterator>
1172 visible_extensions_range;
1174 visible_extensions_range visible_extensions() const {
1175 return visible_extensions_range(visible_extensions_begin(),
1176 visible_extensions_end());
1179 /// \brief Retrieve an iterator to the beginning of the visible-extensions
1181 visible_extensions_iterator visible_extensions_begin() const {
1182 return visible_extensions_iterator(getCategoryListRaw());
1185 /// \brief Retrieve an iterator to the end of the visible-extensions list.
1186 visible_extensions_iterator visible_extensions_end() const {
1187 return visible_extensions_iterator();
1190 /// \brief Determine whether the visible-extensions list is empty.
1191 bool visible_extensions_empty() const {
1192 return visible_extensions_begin() == visible_extensions_end();
1196 /// \brief Test whether the given category is an extension.
1198 /// Used in the \c known_extensions_iterator.
1199 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1202 /// \brief Iterator that walks over all of the known extensions.
1203 typedef filtered_category_iterator<isKnownExtension>
1204 known_extensions_iterator;
1205 typedef llvm::iterator_range<known_extensions_iterator>
1206 known_extensions_range;
1208 known_extensions_range known_extensions() const {
1209 return known_extensions_range(known_extensions_begin(),
1210 known_extensions_end());
1213 /// \brief Retrieve an iterator to the beginning of the known-extensions
1215 known_extensions_iterator known_extensions_begin() const {
1216 return known_extensions_iterator(getCategoryListRaw());
1219 /// \brief Retrieve an iterator to the end of the known-extensions list.
1220 known_extensions_iterator known_extensions_end() const {
1221 return known_extensions_iterator();
1224 /// \brief Determine whether the known-extensions list is empty.
1225 bool known_extensions_empty() const {
1226 return known_extensions_begin() == known_extensions_end();
1229 /// \brief Retrieve the raw pointer to the start of the category/extension
1231 ObjCCategoryDecl* getCategoryListRaw() const {
1232 // FIXME: Should make sure no callers ever do this.
1233 if (!hasDefinition())
1236 if (data().ExternallyCompleted)
1237 LoadExternalDefinition();
1239 return data().CategoryList;
1242 /// \brief Set the raw pointer to the start of the category/extension
1244 void setCategoryListRaw(ObjCCategoryDecl *category) {
1245 data().CategoryList = category;
1249 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
1251 void collectPropertiesToImplement(PropertyMap &PM,
1252 PropertyDeclOrder &PO) const override;
1254 /// isSuperClassOf - Return true if this class is the specified class or is a
1255 /// super class of the specified interface class.
1256 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1257 // If RHS is derived from LHS it is OK; else it is not OK.
1258 while (I != nullptr) {
1259 if (declaresSameEntity(this, I))
1262 I = I->getSuperClass();
1267 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1268 /// to be incompatible with __weak references. Returns true if it is.
1269 bool isArcWeakrefUnavailable() const;
1271 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1272 /// classes must not be auto-synthesized. Returns class decl. if it must not
1273 /// be; 0, otherwise.
1274 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1276 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1277 ObjCInterfaceDecl *&ClassDeclared);
1278 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1279 ObjCInterfaceDecl *ClassDeclared;
1280 return lookupInstanceVariable(IVarName, ClassDeclared);
1283 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1285 // Lookup a method. First, we search locally. If a method isn't
1286 // found, we search referenced protocols and class categories.
1287 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1288 bool shallowCategoryLookup = false,
1289 bool followSuper = true,
1290 const ObjCCategoryDecl *C = nullptr) const;
1292 /// Lookup an instance method for a given selector.
1293 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1294 return lookupMethod(Sel, true/*isInstance*/);
1297 /// Lookup a class method for a given selector.
1298 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1299 return lookupMethod(Sel, false/*isInstance*/);
1301 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1303 /// \brief Lookup a method in the classes implementation hierarchy.
1304 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1305 bool Instance=true) const;
1307 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1308 return lookupPrivateMethod(Sel, false);
1311 /// \brief Lookup a setter or getter in the class hierarchy,
1312 /// including in all categories except for category passed
1314 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1315 const ObjCCategoryDecl *Cat) const {
1316 return lookupMethod(Sel, true/*isInstance*/,
1317 false/*shallowCategoryLookup*/,
1318 true /* followsSuper */,
1322 SourceLocation getEndOfDefinitionLoc() const {
1323 if (!hasDefinition())
1324 return getLocation();
1326 return data().EndLoc;
1329 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1331 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
1332 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
1334 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1335 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1336 /// declaration without an \@interface declaration.
1337 bool isImplicitInterfaceDecl() const {
1338 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1341 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1342 /// has been implemented in IDecl class, its super class or categories (if
1343 /// lookupCategory is true).
1344 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1345 bool lookupCategory,
1346 bool RHSIsQualifiedID = false);
1348 typedef redeclarable_base::redecl_range redecl_range;
1349 typedef redeclarable_base::redecl_iterator redecl_iterator;
1350 using redeclarable_base::redecls_begin;
1351 using redeclarable_base::redecls_end;
1352 using redeclarable_base::redecls;
1353 using redeclarable_base::getPreviousDecl;
1354 using redeclarable_base::getMostRecentDecl;
1355 using redeclarable_base::isFirstDecl;
1357 /// Retrieves the canonical declaration of this Objective-C class.
1358 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1359 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1361 // Low-level accessor
1362 const Type *getTypeForDecl() const { return TypeForDecl; }
1363 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1365 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1366 static bool classofKind(Kind K) { return K == ObjCInterface; }
1368 friend class ASTReader;
1369 friend class ASTDeclReader;
1370 friend class ASTDeclWriter;
1373 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1374 bool inheritsDesignatedInitializers() const;
1377 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1378 /// instance variables are identical to C. The only exception is Objective-C
1379 /// supports C++ style access control. For example:
1381 /// \@interface IvarExample : NSObject
1383 /// id defaultToProtected;
1385 /// id canBePublic; // same as C++.
1387 /// id canBeProtected; // same as C++.
1389 /// id canBePackage; // framework visibility (not available in C++).
1392 class ObjCIvarDecl : public FieldDecl {
1393 void anchor() override;
1396 enum AccessControl {
1397 None, Private, Protected, Public, Package
1401 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1402 SourceLocation IdLoc, IdentifierInfo *Id,
1403 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1405 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1406 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1407 NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {}
1410 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1411 SourceLocation StartLoc, SourceLocation IdLoc,
1412 IdentifierInfo *Id, QualType T,
1413 TypeSourceInfo *TInfo,
1414 AccessControl ac, Expr *BW = nullptr,
1415 bool synthesized=false);
1417 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1419 /// \brief Return the class interface that this ivar is logically contained
1420 /// in; this is either the interface where the ivar was declared, or the
1421 /// interface the ivar is conceptually a part of in the case of synthesized
1423 const ObjCInterfaceDecl *getContainingInterface() const;
1425 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1426 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1427 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1429 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1431 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1433 AccessControl getCanonicalAccessControl() const {
1434 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1437 void setSynthesize(bool synth) { Synthesized = synth; }
1438 bool getSynthesize() const { return Synthesized; }
1440 // Implement isa/cast/dyncast/etc.
1441 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1442 static bool classofKind(Kind K) { return K == ObjCIvar; }
1444 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1445 /// extensions and class's implementation
1446 ObjCIvarDecl *NextIvar;
1448 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1449 unsigned DeclAccess : 3;
1450 unsigned Synthesized : 1;
1454 /// \brief Represents a field declaration created by an \@defs(...).
1455 class ObjCAtDefsFieldDecl : public FieldDecl {
1456 void anchor() override;
1457 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1458 SourceLocation IdLoc, IdentifierInfo *Id,
1459 QualType T, Expr *BW)
1460 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1461 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
1462 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1465 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1466 SourceLocation StartLoc,
1467 SourceLocation IdLoc, IdentifierInfo *Id,
1468 QualType T, Expr *BW);
1470 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1472 // Implement isa/cast/dyncast/etc.
1473 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1474 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1477 /// \brief Represents an Objective-C protocol declaration.
1479 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1480 /// variables are permitted). Protocols originally drew inspiration from
1481 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1482 /// syntax:-). Here is an example:
1485 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1486 /// - (NSWindow *)draggingDestinationWindow;
1487 /// - (NSImage *)draggedImage;
1491 /// This says that NSDraggingInfo requires two methods and requires everything
1492 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1496 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1500 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1501 /// protocols are in distinct namespaces. For example, Cocoa defines both
1502 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1503 /// protocols are referenced using angle brackets as follows:
1505 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1507 class ObjCProtocolDecl : public ObjCContainerDecl,
1508 public Redeclarable<ObjCProtocolDecl> {
1509 void anchor() override;
1511 struct DefinitionData {
1512 // \brief The declaration that defines this protocol.
1513 ObjCProtocolDecl *Definition;
1515 /// \brief Referenced protocols
1516 ObjCProtocolList ReferencedProtocols;
1519 /// \brief Contains a pointer to the data associated with this class,
1520 /// which will be NULL if this class has not yet been defined.
1522 /// The bit indicates when we don't need to check for out-of-date
1523 /// declarations. It will be set unless modules are enabled.
1524 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1526 DefinitionData &data() const {
1527 assert(Data.getPointer() && "Objective-C protocol has no definition!");
1528 return *Data.getPointer();
1531 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
1532 SourceLocation nameLoc, SourceLocation atStartLoc,
1533 ObjCProtocolDecl *PrevDecl);
1535 void allocateDefinitionData();
1537 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1538 ObjCProtocolDecl *getNextRedeclarationImpl() override {
1539 return getNextRedeclaration();
1541 ObjCProtocolDecl *getPreviousDeclImpl() override {
1542 return getPreviousDecl();
1544 ObjCProtocolDecl *getMostRecentDeclImpl() override {
1545 return getMostRecentDecl();
1549 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1551 SourceLocation nameLoc,
1552 SourceLocation atStartLoc,
1553 ObjCProtocolDecl *PrevDecl);
1555 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1557 const ObjCProtocolList &getReferencedProtocols() const {
1558 assert(hasDefinition() && "No definition available!");
1559 return data().ReferencedProtocols;
1561 typedef ObjCProtocolList::iterator protocol_iterator;
1562 typedef llvm::iterator_range<protocol_iterator> protocol_range;
1564 protocol_range protocols() const {
1565 return protocol_range(protocol_begin(), protocol_end());
1567 protocol_iterator protocol_begin() const {
1568 if (!hasDefinition())
1569 return protocol_iterator();
1571 return data().ReferencedProtocols.begin();
1573 protocol_iterator protocol_end() const {
1574 if (!hasDefinition())
1575 return protocol_iterator();
1577 return data().ReferencedProtocols.end();
1579 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1580 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1582 protocol_loc_range protocol_locs() const {
1583 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1585 protocol_loc_iterator protocol_loc_begin() const {
1586 if (!hasDefinition())
1587 return protocol_loc_iterator();
1589 return data().ReferencedProtocols.loc_begin();
1591 protocol_loc_iterator protocol_loc_end() const {
1592 if (!hasDefinition())
1593 return protocol_loc_iterator();
1595 return data().ReferencedProtocols.loc_end();
1597 unsigned protocol_size() const {
1598 if (!hasDefinition())
1601 return data().ReferencedProtocols.size();
1604 /// setProtocolList - Set the list of protocols that this interface
1606 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1607 const SourceLocation *Locs, ASTContext &C) {
1608 assert(hasDefinition() && "Protocol is not defined");
1609 data().ReferencedProtocols.set(List, Num, Locs, C);
1612 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1614 // Lookup a method. First, we search locally. If a method isn't
1615 // found, we search referenced protocols and class categories.
1616 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1617 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1618 return lookupMethod(Sel, true/*isInstance*/);
1620 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1621 return lookupMethod(Sel, false/*isInstance*/);
1624 /// \brief Determine whether this protocol has a definition.
1625 bool hasDefinition() const {
1626 // If the name of this protocol is out-of-date, bring it up-to-date, which
1627 // might bring in a definition.
1628 // Note: a null value indicates that we don't have a definition and that
1629 // modules are enabled.
1630 if (!Data.getOpaqueValue()) {
1631 if (IdentifierInfo *II = getIdentifier()) {
1632 if (II->isOutOfDate()) {
1633 updateOutOfDate(*II);
1638 return Data.getPointer();
1641 /// \brief Retrieve the definition of this protocol, if any.
1642 ObjCProtocolDecl *getDefinition() {
1643 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1646 /// \brief Retrieve the definition of this protocol, if any.
1647 const ObjCProtocolDecl *getDefinition() const {
1648 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1651 /// \brief Determine whether this particular declaration is also the
1653 bool isThisDeclarationADefinition() const {
1654 return getDefinition() == this;
1657 /// \brief Starts the definition of this Objective-C protocol.
1658 void startDefinition();
1660 /// Produce a name to be used for protocol's metadata. It comes either via
1661 /// objc_runtime_name attribute or protocol name.
1662 StringRef getObjCRuntimeNameAsString() const;
1664 SourceRange getSourceRange() const override LLVM_READONLY {
1665 if (isThisDeclarationADefinition())
1666 return ObjCContainerDecl::getSourceRange();
1668 return SourceRange(getAtStartLoc(), getLocation());
1671 typedef redeclarable_base::redecl_range redecl_range;
1672 typedef redeclarable_base::redecl_iterator redecl_iterator;
1673 using redeclarable_base::redecls_begin;
1674 using redeclarable_base::redecls_end;
1675 using redeclarable_base::redecls;
1676 using redeclarable_base::getPreviousDecl;
1677 using redeclarable_base::getMostRecentDecl;
1678 using redeclarable_base::isFirstDecl;
1680 /// Retrieves the canonical declaration of this Objective-C protocol.
1681 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
1682 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
1684 void collectPropertiesToImplement(PropertyMap &PM,
1685 PropertyDeclOrder &PO) const override;
1687 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
1688 ProtocolPropertyMap &PM) const;
1690 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1691 static bool classofKind(Kind K) { return K == ObjCProtocol; }
1693 friend class ASTReader;
1694 friend class ASTDeclReader;
1695 friend class ASTDeclWriter;
1698 /// ObjCCategoryDecl - Represents a category declaration. A category allows
1699 /// you to add methods to an existing class (without subclassing or modifying
1700 /// the original class interface or implementation:-). Categories don't allow
1701 /// you to add instance data. The following example adds "myMethod" to all
1702 /// NSView's within a process:
1704 /// \@interface NSView (MyViewMethods)
1708 /// Categories also allow you to split the implementation of a class across
1709 /// several files (a feature more naturally supported in C++).
1711 /// Categories were originally inspired by dynamic languages such as Common
1712 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
1713 /// don't support this level of dynamism, which is both powerful and dangerous.
1715 class ObjCCategoryDecl : public ObjCContainerDecl {
1716 void anchor() override;
1718 /// Interface belonging to this category
1719 ObjCInterfaceDecl *ClassInterface;
1721 /// referenced protocols in this category.
1722 ObjCProtocolList ReferencedProtocols;
1724 /// Next category belonging to this class.
1725 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
1726 ObjCCategoryDecl *NextClassCategory;
1728 /// \brief The location of the category name in this declaration.
1729 SourceLocation CategoryNameLoc;
1731 /// class extension may have private ivars.
1732 SourceLocation IvarLBraceLoc;
1733 SourceLocation IvarRBraceLoc;
1735 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1736 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1737 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1738 SourceLocation IvarLBraceLoc=SourceLocation(),
1739 SourceLocation IvarRBraceLoc=SourceLocation())
1740 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1741 ClassInterface(IDecl), NextClassCategory(nullptr),
1742 CategoryNameLoc(CategoryNameLoc),
1743 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
1748 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1749 SourceLocation AtLoc,
1750 SourceLocation ClassNameLoc,
1751 SourceLocation CategoryNameLoc,
1753 ObjCInterfaceDecl *IDecl,
1754 SourceLocation IvarLBraceLoc=SourceLocation(),
1755 SourceLocation IvarRBraceLoc=SourceLocation());
1756 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1758 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1759 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1761 ObjCCategoryImplDecl *getImplementation() const;
1762 void setImplementation(ObjCCategoryImplDecl *ImplD);
1764 /// setProtocolList - Set the list of protocols that this interface
1766 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1767 const SourceLocation *Locs, ASTContext &C) {
1768 ReferencedProtocols.set(List, Num, Locs, C);
1771 const ObjCProtocolList &getReferencedProtocols() const {
1772 return ReferencedProtocols;
1775 typedef ObjCProtocolList::iterator protocol_iterator;
1776 typedef llvm::iterator_range<protocol_iterator> protocol_range;
1778 protocol_range protocols() const {
1779 return protocol_range(protocol_begin(), protocol_end());
1781 protocol_iterator protocol_begin() const {
1782 return ReferencedProtocols.begin();
1784 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1785 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1786 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1787 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1789 protocol_loc_range protocol_locs() const {
1790 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1792 protocol_loc_iterator protocol_loc_begin() const {
1793 return ReferencedProtocols.loc_begin();
1795 protocol_loc_iterator protocol_loc_end() const {
1796 return ReferencedProtocols.loc_end();
1799 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1801 /// \brief Retrieve the pointer to the next stored category (or extension),
1802 /// which may be hidden.
1803 ObjCCategoryDecl *getNextClassCategoryRaw() const {
1804 return NextClassCategory;
1807 bool IsClassExtension() const { return getIdentifier() == nullptr; }
1809 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1810 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
1812 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1813 ivar_iterator ivar_begin() const {
1814 return ivar_iterator(decls_begin());
1816 ivar_iterator ivar_end() const {
1817 return ivar_iterator(decls_end());
1819 unsigned ivar_size() const {
1820 return std::distance(ivar_begin(), ivar_end());
1822 bool ivar_empty() const {
1823 return ivar_begin() == ivar_end();
1826 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1827 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1829 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1830 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1831 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1832 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1834 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1835 static bool classofKind(Kind K) { return K == ObjCCategory; }
1837 friend class ASTDeclReader;
1838 friend class ASTDeclWriter;
1841 class ObjCImplDecl : public ObjCContainerDecl {
1842 void anchor() override;
1844 /// Class interface for this class/category implementation
1845 ObjCInterfaceDecl *ClassInterface;
1848 ObjCImplDecl(Kind DK, DeclContext *DC,
1849 ObjCInterfaceDecl *classInterface,
1850 SourceLocation nameLoc, SourceLocation atStartLoc)
1851 : ObjCContainerDecl(DK, DC,
1852 classInterface? classInterface->getIdentifier()
1854 nameLoc, atStartLoc),
1855 ClassInterface(classInterface) {}
1858 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1859 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1860 void setClassInterface(ObjCInterfaceDecl *IFace);
1862 void addInstanceMethod(ObjCMethodDecl *method) {
1863 // FIXME: Context should be set correctly before we get here.
1864 method->setLexicalDeclContext(this);
1867 void addClassMethod(ObjCMethodDecl *method) {
1868 // FIXME: Context should be set correctly before we get here.
1869 method->setLexicalDeclContext(this);
1873 void addPropertyImplementation(ObjCPropertyImplDecl *property);
1875 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1876 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1878 // Iterator access to properties.
1879 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1880 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>
1883 propimpl_range property_impls() const {
1884 return propimpl_range(propimpl_begin(), propimpl_end());
1886 propimpl_iterator propimpl_begin() const {
1887 return propimpl_iterator(decls_begin());
1889 propimpl_iterator propimpl_end() const {
1890 return propimpl_iterator(decls_end());
1893 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1894 static bool classofKind(Kind K) {
1895 return K >= firstObjCImpl && K <= lastObjCImpl;
1899 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1900 /// \@implementation declaration. If a category class has declaration of a
1901 /// property, its implementation must be specified in the category's
1902 /// \@implementation declaration. Example:
1903 /// \@interface I \@end
1904 /// \@interface I(CATEGORY)
1905 /// \@property int p1, d1;
1907 /// \@implementation I(CATEGORY)
1908 /// \@dynamic p1,d1;
1911 /// ObjCCategoryImplDecl
1912 class ObjCCategoryImplDecl : public ObjCImplDecl {
1913 void anchor() override;
1918 // Category name location
1919 SourceLocation CategoryNameLoc;
1921 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1922 ObjCInterfaceDecl *classInterface,
1923 SourceLocation nameLoc, SourceLocation atStartLoc,
1924 SourceLocation CategoryNameLoc)
1925 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1926 Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1928 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1930 ObjCInterfaceDecl *classInterface,
1931 SourceLocation nameLoc,
1932 SourceLocation atStartLoc,
1933 SourceLocation CategoryNameLoc);
1934 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1936 /// getIdentifier - Get the identifier that names the category
1937 /// interface associated with this implementation.
1938 /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier()
1939 /// with a different meaning. For example:
1940 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1941 /// returns the class interface name, whereas
1942 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1943 /// returns the category name.
1944 IdentifierInfo *getIdentifier() const {
1947 void setIdentifier(IdentifierInfo *II) { Id = II; }
1949 ObjCCategoryDecl *getCategoryDecl() const;
1951 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1953 /// getName - Get the name of identifier for the class interface associated
1954 /// with this implementation as a StringRef.
1956 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
1958 StringRef getName() const { return Id ? Id->getName() : StringRef(); }
1960 /// @brief Get the name of the class associated with this interface.
1962 // FIXME: Deprecated, move clients to getName().
1963 std::string getNameAsString() const {
1967 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1968 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1970 friend class ASTDeclReader;
1971 friend class ASTDeclWriter;
1974 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
1976 /// ObjCImplementationDecl - Represents a class definition - this is where
1977 /// method definitions are specified. For example:
1980 /// \@implementation MyClass
1981 /// - (void)myMethod { /* do something */ }
1985 /// In a non-fragile runtime, instance variables can appear in the class
1986 /// interface, class extensions (nameless categories), and in the implementation
1987 /// itself, as well as being synthesized as backing storage for properties.
1989 /// In a fragile runtime, instance variables are specified in the class
1990 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
1991 /// we allow instance variables to be specified in the implementation. When
1992 /// specified, they need to be \em identical to the interface.
1993 class ObjCImplementationDecl : public ObjCImplDecl {
1994 void anchor() override;
1995 /// Implementation Class's super class.
1996 ObjCInterfaceDecl *SuperClass;
1997 SourceLocation SuperLoc;
1999 /// \@implementation may have private ivars.
2000 SourceLocation IvarLBraceLoc;
2001 SourceLocation IvarRBraceLoc;
2003 /// Support for ivar initialization.
2004 /// IvarInitializers - The arguments used to initialize the ivars
2005 CXXCtorInitializer **IvarInitializers;
2006 unsigned NumIvarInitializers;
2008 /// Do the ivars of this class require initialization other than
2009 /// zero-initialization?
2010 bool HasNonZeroConstructors : 1;
2012 /// Do the ivars of this class require non-trivial destruction?
2013 bool HasDestructors : 1;
2015 ObjCImplementationDecl(DeclContext *DC,
2016 ObjCInterfaceDecl *classInterface,
2017 ObjCInterfaceDecl *superDecl,
2018 SourceLocation nameLoc, SourceLocation atStartLoc,
2019 SourceLocation superLoc = SourceLocation(),
2020 SourceLocation IvarLBraceLoc=SourceLocation(),
2021 SourceLocation IvarRBraceLoc=SourceLocation())
2022 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
2023 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
2024 IvarRBraceLoc(IvarRBraceLoc),
2025 IvarInitializers(nullptr), NumIvarInitializers(0),
2026 HasNonZeroConstructors(false), HasDestructors(false) {}
2028 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2029 ObjCInterfaceDecl *classInterface,
2030 ObjCInterfaceDecl *superDecl,
2031 SourceLocation nameLoc,
2032 SourceLocation atStartLoc,
2033 SourceLocation superLoc = SourceLocation(),
2034 SourceLocation IvarLBraceLoc=SourceLocation(),
2035 SourceLocation IvarRBraceLoc=SourceLocation());
2037 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2039 /// init_iterator - Iterates through the ivar initializer list.
2040 typedef CXXCtorInitializer **init_iterator;
2042 /// init_const_iterator - Iterates through the ivar initializer list.
2043 typedef CXXCtorInitializer * const * init_const_iterator;
2045 typedef llvm::iterator_range<init_iterator> init_range;
2046 typedef llvm::iterator_range<init_const_iterator> init_const_range;
2048 init_range inits() { return init_range(init_begin(), init_end()); }
2049 init_const_range inits() const {
2050 return init_const_range(init_begin(), init_end());
2053 /// init_begin() - Retrieve an iterator to the first initializer.
2054 init_iterator init_begin() { return IvarInitializers; }
2055 /// begin() - Retrieve an iterator to the first initializer.
2056 init_const_iterator init_begin() const { return IvarInitializers; }
2058 /// init_end() - Retrieve an iterator past the last initializer.
2059 init_iterator init_end() {
2060 return IvarInitializers + NumIvarInitializers;
2062 /// end() - Retrieve an iterator past the last initializer.
2063 init_const_iterator init_end() const {
2064 return IvarInitializers + NumIvarInitializers;
2066 /// getNumArgs - Number of ivars which must be initialized.
2067 unsigned getNumIvarInitializers() const {
2068 return NumIvarInitializers;
2071 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2072 NumIvarInitializers = numNumIvarInitializers;
2075 void setIvarInitializers(ASTContext &C,
2076 CXXCtorInitializer ** initializers,
2077 unsigned numInitializers);
2079 /// Do any of the ivars of this class (not counting its base classes)
2080 /// require construction other than zero-initialization?
2081 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2082 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2084 /// Do any of the ivars of this class (not counting its base classes)
2085 /// require non-trivial destruction?
2086 bool hasDestructors() const { return HasDestructors; }
2087 void setHasDestructors(bool val) { HasDestructors = val; }
2089 /// getIdentifier - Get the identifier that names the class
2090 /// interface associated with this implementation.
2091 IdentifierInfo *getIdentifier() const {
2092 return getClassInterface()->getIdentifier();
2095 /// getName - Get the name of identifier for the class interface associated
2096 /// with this implementation as a StringRef.
2098 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2100 StringRef getName() const {
2101 assert(getIdentifier() && "Name is not a simple identifier");
2102 return getIdentifier()->getName();
2105 /// @brief Get the name of the class associated with this interface.
2107 // FIXME: Move to StringRef API.
2108 std::string getNameAsString() const {
2112 /// Produce a name to be used for class's metadata. It comes either via
2113 /// class's objc_runtime_name attribute or class name.
2114 StringRef getObjCRuntimeNameAsString() const;
2116 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2117 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2118 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2120 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2122 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2123 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2124 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2125 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2127 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2128 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2130 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2131 ivar_iterator ivar_begin() const {
2132 return ivar_iterator(decls_begin());
2134 ivar_iterator ivar_end() const {
2135 return ivar_iterator(decls_end());
2137 unsigned ivar_size() const {
2138 return std::distance(ivar_begin(), ivar_end());
2140 bool ivar_empty() const {
2141 return ivar_begin() == ivar_end();
2144 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2145 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2147 friend class ASTDeclReader;
2148 friend class ASTDeclWriter;
2151 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2153 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2154 /// declared as \@compatibility_alias alias class.
2155 class ObjCCompatibleAliasDecl : public NamedDecl {
2156 void anchor() override;
2157 /// Class that this is an alias of.
2158 ObjCInterfaceDecl *AliasedClass;
2160 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2161 ObjCInterfaceDecl* aliasedClass)
2162 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2164 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2165 SourceLocation L, IdentifierInfo *Id,
2166 ObjCInterfaceDecl* aliasedClass);
2168 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2171 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2172 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2173 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2175 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2176 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2180 /// \brief Represents one property declaration in an Objective-C interface.
2184 /// \@property (assign, readwrite) int MyProperty;
2186 class ObjCPropertyDecl : public NamedDecl {
2187 void anchor() override;
2189 enum PropertyAttributeKind {
2190 OBJC_PR_noattr = 0x00,
2191 OBJC_PR_readonly = 0x01,
2192 OBJC_PR_getter = 0x02,
2193 OBJC_PR_assign = 0x04,
2194 OBJC_PR_readwrite = 0x08,
2195 OBJC_PR_retain = 0x10,
2196 OBJC_PR_copy = 0x20,
2197 OBJC_PR_nonatomic = 0x40,
2198 OBJC_PR_setter = 0x80,
2199 OBJC_PR_atomic = 0x100,
2200 OBJC_PR_weak = 0x200,
2201 OBJC_PR_strong = 0x400,
2202 OBJC_PR_unsafe_unretained = 0x800
2203 // Adding a property should change NumPropertyAttrsBits
2207 /// \brief Number of bits fitting all the property attributes.
2208 NumPropertyAttrsBits = 12
2211 enum SetterKind { Assign, Retain, Copy, Weak };
2212 enum PropertyControl { None, Required, Optional };
2214 SourceLocation AtLoc; // location of \@property
2215 SourceLocation LParenLoc; // location of '(' starting attribute list or null.
2216 TypeSourceInfo *DeclType;
2217 unsigned PropertyAttributes : NumPropertyAttrsBits;
2218 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
2219 // \@required/\@optional
2220 unsigned PropertyImplementation : 2;
2222 Selector GetterName; // getter name of NULL if no getter
2223 Selector SetterName; // setter name of NULL if no setter
2225 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
2226 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
2227 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
2229 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2230 SourceLocation AtLocation, SourceLocation LParenLocation,
2232 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
2233 LParenLoc(LParenLocation), DeclType(T),
2234 PropertyAttributes(OBJC_PR_noattr),
2235 PropertyAttributesAsWritten(OBJC_PR_noattr),
2236 PropertyImplementation(None),
2237 GetterName(Selector()),
2238 SetterName(Selector()),
2239 GetterMethodDecl(nullptr), SetterMethodDecl(nullptr),
2240 PropertyIvarDecl(nullptr) {}
2243 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
2245 IdentifierInfo *Id, SourceLocation AtLocation,
2246 SourceLocation LParenLocation,
2248 PropertyControl propControl = None);
2250 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2252 SourceLocation getAtLoc() const { return AtLoc; }
2253 void setAtLoc(SourceLocation L) { AtLoc = L; }
2255 SourceLocation getLParenLoc() const { return LParenLoc; }
2256 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2258 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
2259 QualType getType() const { return DeclType->getType(); }
2260 void setType(TypeSourceInfo *T) { DeclType = T; }
2262 PropertyAttributeKind getPropertyAttributes() const {
2263 return PropertyAttributeKind(PropertyAttributes);
2265 void setPropertyAttributes(PropertyAttributeKind PRVal) {
2266 PropertyAttributes |= PRVal;
2269 PropertyAttributeKind getPropertyAttributesAsWritten() const {
2270 return PropertyAttributeKind(PropertyAttributesAsWritten);
2273 bool hasWrittenStorageAttribute() const {
2274 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
2275 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
2279 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
2280 PropertyAttributesAsWritten = PRVal;
2283 void makeitReadWriteAttribute() {
2284 PropertyAttributes &= ~OBJC_PR_readonly;
2285 PropertyAttributes |= OBJC_PR_readwrite;
2288 // Helper methods for accessing attributes.
2290 /// isReadOnly - Return true iff the property has a setter.
2291 bool isReadOnly() const {
2292 return (PropertyAttributes & OBJC_PR_readonly);
2295 /// isAtomic - Return true if the property is atomic.
2296 bool isAtomic() const {
2297 return (PropertyAttributes & OBJC_PR_atomic);
2300 /// isRetaining - Return true if the property retains its value.
2301 bool isRetaining() const {
2302 return (PropertyAttributes &
2303 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
2306 /// getSetterKind - Return the method used for doing assignment in
2307 /// the property setter. This is only valid if the property has been
2308 /// defined to have a setter.
2309 SetterKind getSetterKind() const {
2310 if (PropertyAttributes & OBJC_PR_strong)
2311 return getType()->isBlockPointerType() ? Copy : Retain;
2312 if (PropertyAttributes & OBJC_PR_retain)
2314 if (PropertyAttributes & OBJC_PR_copy)
2316 if (PropertyAttributes & OBJC_PR_weak)
2321 Selector getGetterName() const { return GetterName; }
2322 void setGetterName(Selector Sel) { GetterName = Sel; }
2324 Selector getSetterName() const { return SetterName; }
2325 void setSetterName(Selector Sel) { SetterName = Sel; }
2327 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2328 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
2330 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2331 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
2333 // Related to \@optional/\@required declared in \@protocol
2334 void setPropertyImplementation(PropertyControl pc) {
2335 PropertyImplementation = pc;
2337 PropertyControl getPropertyImplementation() const {
2338 return PropertyControl(PropertyImplementation);
2341 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
2342 PropertyIvarDecl = Ivar;
2344 ObjCIvarDecl *getPropertyIvarDecl() const {
2345 return PropertyIvarDecl;
2348 SourceRange getSourceRange() const override LLVM_READONLY {
2349 return SourceRange(AtLoc, getLocation());
2352 /// Get the default name of the synthesized ivar.
2353 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
2355 /// Lookup a property by name in the specified DeclContext.
2356 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
2357 IdentifierInfo *propertyID);
2359 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2360 static bool classofKind(Kind K) { return K == ObjCProperty; }
2363 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2364 /// in a class or category implementation block. For example:
2365 /// \@synthesize prop1 = ivar1;
2367 class ObjCPropertyImplDecl : public Decl {
2374 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2376 /// \brief For \@synthesize, the location of the ivar, if it was written in
2377 /// the source code.
2380 /// \@synthesize int a = b
2382 SourceLocation IvarLoc;
2384 /// Property declaration being implemented
2385 ObjCPropertyDecl *PropertyDecl;
2387 /// Null for \@dynamic. Required for \@synthesize.
2388 ObjCIvarDecl *PropertyIvarDecl;
2390 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2392 Expr *GetterCXXConstructor;
2394 /// Null for \@dynamic. Non-null if property has assignment operator to call
2395 /// in Setter synthesis.
2396 Expr *SetterCXXAssignment;
2398 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2399 ObjCPropertyDecl *property,
2401 ObjCIvarDecl *ivarDecl,
2402 SourceLocation ivarLoc)
2403 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2404 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2405 GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) {
2406 assert (PK == Dynamic || PropertyIvarDecl);
2410 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2411 SourceLocation atLoc, SourceLocation L,
2412 ObjCPropertyDecl *property,
2414 ObjCIvarDecl *ivarDecl,
2415 SourceLocation ivarLoc);
2417 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2419 SourceRange getSourceRange() const override LLVM_READONLY;
2421 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2422 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2424 ObjCPropertyDecl *getPropertyDecl() const {
2425 return PropertyDecl;
2427 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2429 Kind getPropertyImplementation() const {
2430 return PropertyIvarDecl ? Synthesize : Dynamic;
2433 ObjCIvarDecl *getPropertyIvarDecl() const {
2434 return PropertyIvarDecl;
2436 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2438 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2439 SourceLocation IvarLoc) {
2440 PropertyIvarDecl = Ivar;
2441 this->IvarLoc = IvarLoc;
2444 /// \brief For \@synthesize, returns true if an ivar name was explicitly
2448 /// \@synthesize int a = b; // true
2449 /// \@synthesize int a; // false
2451 bool isIvarNameSpecified() const {
2452 return IvarLoc.isValid() && IvarLoc != getLocation();
2455 Expr *getGetterCXXConstructor() const {
2456 return GetterCXXConstructor;
2458 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2459 GetterCXXConstructor = getterCXXConstructor;
2462 Expr *getSetterCXXAssignment() const {
2463 return SetterCXXAssignment;
2465 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2466 SetterCXXAssignment = setterCXXAssignment;
2469 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2470 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2472 friend class ASTDeclReader;
2475 template<bool (*Filter)(ObjCCategoryDecl *)>
2477 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2478 findAcceptableCategory() {
2479 while (Current && !Filter(Current))
2480 Current = Current->getNextClassCategoryRaw();
2483 template<bool (*Filter)(ObjCCategoryDecl *)>
2484 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2485 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2486 Current = Current->getNextClassCategoryRaw();
2487 findAcceptableCategory();
2491 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2492 return !Cat->isHidden();
2495 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2496 return Cat->IsClassExtension() && !Cat->isHidden();
2499 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2500 return Cat->IsClassExtension();
2503 } // end namespace clang