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 &) = delete;
37 void operator=(const ObjCListBase &) = delete;
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 : 7;
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; }
332 SourceRange getReturnTypeSourceRange() const;
334 /// \brief Determine the type of an expression that sends a message to this
335 /// function. This replaces the type parameters with the types they would
336 /// get if the receiver was parameterless (e.g. it may replace the type
337 /// parameter with 'id').
338 QualType getSendResultType() const;
340 /// Determine the type of an expression that sends a message to this
341 /// function with the given receiver type.
342 QualType getSendResultType(QualType receiverType) const;
344 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
345 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
347 // Iterator access to formal parameters.
348 unsigned param_size() const { return NumParams; }
349 typedef const ParmVarDecl *const *param_const_iterator;
350 typedef ParmVarDecl *const *param_iterator;
351 typedef llvm::iterator_range<param_iterator> param_range;
352 typedef llvm::iterator_range<param_const_iterator> param_const_range;
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 = llvm::None);
383 // Iterator access to parameter types.
384 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
385 typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
388 param_type_iterator param_type_begin() const {
389 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
391 param_type_iterator param_type_end() const {
392 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
395 /// createImplicitParams - Used to lazily create the self and cmd
396 /// implict parameters. This must be called prior to using getSelfDecl()
397 /// or getCmdDecl(). The call is ignored if the implicit paramters
398 /// have already been created.
399 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
401 /// \return the type for \c self and set \arg selfIsPseudoStrong and
402 /// \arg selfIsConsumed accordingly.
403 QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
404 bool &selfIsPseudoStrong, bool &selfIsConsumed);
406 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
407 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
408 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
409 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
411 /// Determines the family of this method.
412 ObjCMethodFamily getMethodFamily() const;
414 bool isInstanceMethod() const { return IsInstance; }
415 void setInstanceMethod(bool isInst) { IsInstance = isInst; }
416 bool isVariadic() const { return IsVariadic; }
417 void setVariadic(bool isVar) { IsVariadic = isVar; }
419 bool isClassMethod() const { return !IsInstance; }
421 bool isPropertyAccessor() const { return IsPropertyAccessor; }
422 void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; }
424 bool isDefined() const { return IsDefined; }
425 void setDefined(bool isDefined) { IsDefined = isDefined; }
427 /// \brief Whether this method overrides any other in the class hierarchy.
429 /// A method is said to override any method in the class's
430 /// base classes, its protocols, or its categories' protocols, that has
431 /// the same selector and is of the same kind (class or instance).
432 /// A method in an implementation is not considered as overriding the same
433 /// method in the interface or its categories.
434 bool isOverriding() const { return IsOverriding; }
435 void setOverriding(bool isOverriding) { IsOverriding = isOverriding; }
437 /// \brief Return overridden methods for the given \p Method.
439 /// An ObjC method is considered to override any method in the class's
440 /// base classes (and base's categories), its protocols, or its categories'
441 /// protocols, that has
442 /// the same selector and is of the same kind (class or instance).
443 /// A method in an implementation is not considered as overriding the same
444 /// method in the interface or its categories.
445 void getOverriddenMethods(
446 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
448 /// \brief True if the method was a definition but its body was skipped.
449 bool hasSkippedBody() const { return HasSkippedBody; }
450 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
452 /// \brief Returns the property associated with this method's selector.
454 /// Note that even if this particular method is not marked as a property
455 /// accessor, it is still possible for it to match a property declared in a
456 /// superclass. Pass \c false if you only want to check the current class.
457 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
459 // Related to protocols declared in \@protocol
460 void setDeclImplementation(ImplementationControl ic) {
461 DeclImplementation = ic;
463 ImplementationControl getImplementationControl() const {
464 return ImplementationControl(DeclImplementation);
467 /// Returns true if this specific method declaration is marked with the
468 /// designated initializer attribute.
469 bool isThisDeclarationADesignatedInitializer() const;
471 /// Returns true if the method selector resolves to a designated initializer
472 /// in the class's interface.
474 /// \param InitMethod if non-null and the function returns true, it receives
475 /// the method declaration that was marked with the designated initializer
477 bool isDesignatedInitializerForTheInterface(
478 const ObjCMethodDecl **InitMethod = nullptr) const;
480 /// \brief Determine whether this method has a body.
481 bool hasBody() const override { return Body.isValid(); }
483 /// \brief Retrieve the body of this method, if it has one.
484 Stmt *getBody() const override;
486 void setLazyBody(uint64_t Offset) { Body = Offset; }
488 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
489 void setBody(Stmt *B) { Body = B; }
491 /// \brief Returns whether this specific method is a definition.
492 bool isThisDeclarationADefinition() const { return hasBody(); }
494 // Implement isa/cast/dyncast/etc.
495 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
496 static bool classofKind(Kind K) { return K == ObjCMethod; }
497 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
498 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
500 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
501 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
504 friend class ASTDeclReader;
505 friend class ASTDeclWriter;
508 /// Describes the variance of a given generic parameter.
509 enum class ObjCTypeParamVariance : uint8_t {
510 /// The parameter is invariant: must match exactly.
512 /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
513 /// the type parameter is covariant and T is a subtype of U.
515 /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
516 /// when the type parameter is covariant and U is a subtype of T.
520 /// Represents the declaration of an Objective-C type parameter.
523 /// @interface NSDictionary<Key : id<NSCopying>, Value>
527 /// In the example above, both \c Key and \c Value are represented by
528 /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
529 /// while \c Value gets an implicit bound of \c id.
531 /// Objective-C type parameters are typedef-names in the grammar,
532 class ObjCTypeParamDecl : public TypedefNameDecl {
533 void anchor() override;
535 /// Index of this type parameter in the type parameter list.
538 /// The variance of the type parameter.
539 unsigned Variance : 2;
541 /// The location of the variance, if any.
542 SourceLocation VarianceLoc;
544 /// The location of the ':', which will be valid when the bound was
545 /// explicitly specified.
546 SourceLocation ColonLoc;
548 ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
549 ObjCTypeParamVariance variance, SourceLocation varianceLoc,
551 SourceLocation nameLoc, IdentifierInfo *name,
552 SourceLocation colonLoc, TypeSourceInfo *boundInfo)
553 : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
555 Index(index), Variance(static_cast<unsigned>(variance)),
556 VarianceLoc(varianceLoc), ColonLoc(colonLoc) { }
559 static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
560 ObjCTypeParamVariance variance,
561 SourceLocation varianceLoc,
563 SourceLocation nameLoc,
564 IdentifierInfo *name,
565 SourceLocation colonLoc,
566 TypeSourceInfo *boundInfo);
567 static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
569 SourceRange getSourceRange() const override LLVM_READONLY;
571 /// Determine the variance of this type parameter.
572 ObjCTypeParamVariance getVariance() const {
573 return static_cast<ObjCTypeParamVariance>(Variance);
576 /// Set the variance of this type parameter.
577 void setVariance(ObjCTypeParamVariance variance) {
578 Variance = static_cast<unsigned>(variance);
581 /// Retrieve the location of the variance keyword.
582 SourceLocation getVarianceLoc() const { return VarianceLoc; }
584 /// Retrieve the index into its type parameter list.
585 unsigned getIndex() const { return Index; }
587 /// Whether this type parameter has an explicitly-written type bound, e.g.,
589 bool hasExplicitBound() const { return ColonLoc.isValid(); }
591 /// Retrieve the location of the ':' separating the type parameter name
592 /// from the explicitly-specified bound.
593 SourceLocation getColonLoc() const { return ColonLoc; }
595 // Implement isa/cast/dyncast/etc.
596 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
597 static bool classofKind(Kind K) { return K == ObjCTypeParam; }
599 friend class ASTDeclReader;
600 friend class ASTDeclWriter;
603 /// Stores a list of Objective-C type parameters for a parameterized class
604 /// or a category/extension thereof.
607 /// @interface NSArray<T> // stores the <T>
610 class ObjCTypeParamList final
611 : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
612 /// Stores the components of a SourceRange as a POD.
613 struct PODSourceRange {
619 /// Location of the left and right angle brackets.
620 PODSourceRange Brackets;
622 // Used only for alignment.
623 ObjCTypeParamDecl *AlignmentHack;
626 /// The number of parameters in the list, which are tail-allocated.
629 ObjCTypeParamList(SourceLocation lAngleLoc,
630 ArrayRef<ObjCTypeParamDecl *> typeParams,
631 SourceLocation rAngleLoc);
634 /// Create a new Objective-C type parameter list.
635 static ObjCTypeParamList *create(ASTContext &ctx,
636 SourceLocation lAngleLoc,
637 ArrayRef<ObjCTypeParamDecl *> typeParams,
638 SourceLocation rAngleLoc);
640 /// Iterate through the type parameters in the list.
641 typedef ObjCTypeParamDecl **iterator;
643 iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
645 iterator end() { return begin() + size(); }
647 /// Determine the number of type parameters in this list.
648 unsigned size() const { return NumParams; }
650 // Iterate through the type parameters in the list.
651 typedef ObjCTypeParamDecl * const *const_iterator;
653 const_iterator begin() const {
654 return getTrailingObjects<ObjCTypeParamDecl *>();
657 const_iterator end() const {
658 return begin() + size();
661 ObjCTypeParamDecl *front() const {
662 assert(size() > 0 && "empty Objective-C type parameter list");
666 ObjCTypeParamDecl *back() const {
667 assert(size() > 0 && "empty Objective-C type parameter list");
671 SourceLocation getLAngleLoc() const {
672 return SourceLocation::getFromRawEncoding(Brackets.Begin);
674 SourceLocation getRAngleLoc() const {
675 return SourceLocation::getFromRawEncoding(Brackets.End);
677 SourceRange getSourceRange() const {
678 return SourceRange(getLAngleLoc(), getRAngleLoc());
681 /// Gather the default set of type arguments to be substituted for
682 /// these type parameters when dealing with an unspecialized type.
683 void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
684 friend TrailingObjects;
687 enum class ObjCPropertyQueryKind : uint8_t {
688 OBJC_PR_query_unknown = 0x00,
689 OBJC_PR_query_instance,
693 /// \brief Represents one property declaration in an Objective-C interface.
697 /// \@property (assign, readwrite) int MyProperty;
699 class ObjCPropertyDecl : public NamedDecl {
700 void anchor() override;
702 enum PropertyAttributeKind {
703 OBJC_PR_noattr = 0x00,
704 OBJC_PR_readonly = 0x01,
705 OBJC_PR_getter = 0x02,
706 OBJC_PR_assign = 0x04,
707 OBJC_PR_readwrite = 0x08,
708 OBJC_PR_retain = 0x10,
710 OBJC_PR_nonatomic = 0x40,
711 OBJC_PR_setter = 0x80,
712 OBJC_PR_atomic = 0x100,
713 OBJC_PR_weak = 0x200,
714 OBJC_PR_strong = 0x400,
715 OBJC_PR_unsafe_unretained = 0x800,
716 /// Indicates that the nullability of the type was spelled with a
717 /// property attribute rather than a type qualifier.
718 OBJC_PR_nullability = 0x1000,
719 OBJC_PR_null_resettable = 0x2000,
720 OBJC_PR_class = 0x4000
721 // Adding a property should change NumPropertyAttrsBits
725 /// \brief Number of bits fitting all the property attributes.
726 NumPropertyAttrsBits = 15
729 enum SetterKind { Assign, Retain, Copy, Weak };
730 enum PropertyControl { None, Required, Optional };
732 SourceLocation AtLoc; // location of \@property
733 SourceLocation LParenLoc; // location of '(' starting attribute list or null.
735 TypeSourceInfo *DeclTypeSourceInfo;
736 unsigned PropertyAttributes : NumPropertyAttrsBits;
737 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
738 // \@required/\@optional
739 unsigned PropertyImplementation : 2;
741 Selector GetterName; // getter name of NULL if no getter
742 Selector SetterName; // setter name of NULL if no setter
744 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
745 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
746 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
748 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
749 SourceLocation AtLocation, SourceLocation LParenLocation,
750 QualType T, TypeSourceInfo *TSI,
751 PropertyControl propControl)
752 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
753 LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
754 PropertyAttributes(OBJC_PR_noattr),
755 PropertyAttributesAsWritten(OBJC_PR_noattr),
756 PropertyImplementation(propControl),
757 GetterName(Selector()),
758 SetterName(Selector()),
759 GetterMethodDecl(nullptr), SetterMethodDecl(nullptr),
760 PropertyIvarDecl(nullptr) {}
763 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
765 IdentifierInfo *Id, SourceLocation AtLocation,
766 SourceLocation LParenLocation,
769 PropertyControl propControl = None);
771 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
773 SourceLocation getAtLoc() const { return AtLoc; }
774 void setAtLoc(SourceLocation L) { AtLoc = L; }
776 SourceLocation getLParenLoc() const { return LParenLoc; }
777 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
779 TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
781 QualType getType() const { return DeclType; }
783 void setType(QualType T, TypeSourceInfo *TSI) {
785 DeclTypeSourceInfo = TSI;
788 /// Retrieve the type when this property is used with a specific base object
790 QualType getUsageType(QualType objectType) const;
792 PropertyAttributeKind getPropertyAttributes() const {
793 return PropertyAttributeKind(PropertyAttributes);
795 void setPropertyAttributes(PropertyAttributeKind PRVal) {
796 PropertyAttributes |= PRVal;
798 void overwritePropertyAttributes(unsigned PRVal) {
799 PropertyAttributes = PRVal;
802 PropertyAttributeKind getPropertyAttributesAsWritten() const {
803 return PropertyAttributeKind(PropertyAttributesAsWritten);
806 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
807 PropertyAttributesAsWritten = PRVal;
810 // Helper methods for accessing attributes.
812 /// isReadOnly - Return true iff the property has a setter.
813 bool isReadOnly() const {
814 return (PropertyAttributes & OBJC_PR_readonly);
817 /// isAtomic - Return true if the property is atomic.
818 bool isAtomic() const {
819 return (PropertyAttributes & OBJC_PR_atomic);
822 /// isRetaining - Return true if the property retains its value.
823 bool isRetaining() const {
824 return (PropertyAttributes &
825 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
828 bool isInstanceProperty() const { return !isClassProperty(); }
829 bool isClassProperty() const { return PropertyAttributes & OBJC_PR_class; }
830 ObjCPropertyQueryKind getQueryKind() const {
831 return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
832 ObjCPropertyQueryKind::OBJC_PR_query_instance;
834 static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
835 return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
836 ObjCPropertyQueryKind::OBJC_PR_query_instance;
839 /// getSetterKind - Return the method used for doing assignment in
840 /// the property setter. This is only valid if the property has been
841 /// defined to have a setter.
842 SetterKind getSetterKind() const {
843 if (PropertyAttributes & OBJC_PR_strong)
844 return getType()->isBlockPointerType() ? Copy : Retain;
845 if (PropertyAttributes & OBJC_PR_retain)
847 if (PropertyAttributes & OBJC_PR_copy)
849 if (PropertyAttributes & OBJC_PR_weak)
854 Selector getGetterName() const { return GetterName; }
855 void setGetterName(Selector Sel) { GetterName = Sel; }
857 Selector getSetterName() const { return SetterName; }
858 void setSetterName(Selector Sel) { SetterName = Sel; }
860 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
861 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
863 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
864 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
866 // Related to \@optional/\@required declared in \@protocol
867 void setPropertyImplementation(PropertyControl pc) {
868 PropertyImplementation = pc;
870 PropertyControl getPropertyImplementation() const {
871 return PropertyControl(PropertyImplementation);
874 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
875 PropertyIvarDecl = Ivar;
877 ObjCIvarDecl *getPropertyIvarDecl() const {
878 return PropertyIvarDecl;
881 SourceRange getSourceRange() const override LLVM_READONLY {
882 return SourceRange(AtLoc, getLocation());
885 /// Get the default name of the synthesized ivar.
886 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
888 /// Lookup a property by name in the specified DeclContext.
889 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
890 const IdentifierInfo *propertyID,
891 ObjCPropertyQueryKind queryKind);
893 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
894 static bool classofKind(Kind K) { return K == ObjCProperty; }
897 /// ObjCContainerDecl - Represents a container for method declarations.
898 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
899 /// ObjCProtocolDecl, and ObjCImplDecl.
901 class ObjCContainerDecl : public NamedDecl, public DeclContext {
902 void anchor() override;
904 SourceLocation AtStart;
906 // These two locations in the range mark the end of the method container.
907 // The first points to the '@' token, and the second to the 'end' token.
911 ObjCContainerDecl(Kind DK, DeclContext *DC,
912 IdentifierInfo *Id, SourceLocation nameLoc,
913 SourceLocation atStartLoc)
914 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
916 // Iterator access to instance/class properties.
917 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
918 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>
921 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
922 prop_iterator prop_begin() const {
923 return prop_iterator(decls_begin());
925 prop_iterator prop_end() const {
926 return prop_iterator(decls_end());
929 typedef filtered_decl_iterator<ObjCPropertyDecl,
930 &ObjCPropertyDecl::isInstanceProperty>
932 typedef llvm::iterator_range<instprop_iterator> instprop_range;
934 instprop_range instance_properties() const {
935 return instprop_range(instprop_begin(), instprop_end());
937 instprop_iterator instprop_begin() const {
938 return instprop_iterator(decls_begin());
940 instprop_iterator instprop_end() const {
941 return instprop_iterator(decls_end());
944 typedef filtered_decl_iterator<ObjCPropertyDecl,
945 &ObjCPropertyDecl::isClassProperty>
947 typedef llvm::iterator_range<classprop_iterator> classprop_range;
949 classprop_range class_properties() const {
950 return classprop_range(classprop_begin(), classprop_end());
952 classprop_iterator classprop_begin() const {
953 return classprop_iterator(decls_begin());
955 classprop_iterator classprop_end() const {
956 return classprop_iterator(decls_end());
959 // Iterator access to instance/class methods.
960 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
961 typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>
964 method_range methods() const {
965 return method_range(meth_begin(), meth_end());
967 method_iterator meth_begin() const {
968 return method_iterator(decls_begin());
970 method_iterator meth_end() const {
971 return method_iterator(decls_end());
974 typedef filtered_decl_iterator<ObjCMethodDecl,
975 &ObjCMethodDecl::isInstanceMethod>
977 typedef llvm::iterator_range<instmeth_iterator> instmeth_range;
979 instmeth_range instance_methods() const {
980 return instmeth_range(instmeth_begin(), instmeth_end());
982 instmeth_iterator instmeth_begin() const {
983 return instmeth_iterator(decls_begin());
985 instmeth_iterator instmeth_end() const {
986 return instmeth_iterator(decls_end());
989 typedef filtered_decl_iterator<ObjCMethodDecl,
990 &ObjCMethodDecl::isClassMethod>
992 typedef llvm::iterator_range<classmeth_iterator> classmeth_range;
994 classmeth_range class_methods() const {
995 return classmeth_range(classmeth_begin(), classmeth_end());
997 classmeth_iterator classmeth_begin() const {
998 return classmeth_iterator(decls_begin());
1000 classmeth_iterator classmeth_end() const {
1001 return classmeth_iterator(decls_end());
1004 // Get the local instance/class method declared in this interface.
1005 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1006 bool AllowHidden = false) const;
1007 ObjCMethodDecl *getInstanceMethod(Selector Sel,
1008 bool AllowHidden = false) const {
1009 return getMethod(Sel, true/*isInstance*/, AllowHidden);
1011 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1012 return getMethod(Sel, false/*isInstance*/, AllowHidden);
1014 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1015 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1018 FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1019 ObjCPropertyQueryKind QueryKind) const;
1021 typedef llvm::DenseMap<std::pair<IdentifierInfo*,
1022 unsigned/*isClassProperty*/>,
1023 ObjCPropertyDecl*> PropertyMap;
1025 typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*>
1026 ProtocolPropertyMap;
1028 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
1030 /// This routine collects list of properties to be implemented in the class.
1031 /// This includes, class's and its conforming protocols' properties.
1032 /// Note, the superclass's properties are not included in the list.
1033 virtual void collectPropertiesToImplement(PropertyMap &PM,
1034 PropertyDeclOrder &PO) const {}
1036 SourceLocation getAtStartLoc() const { return AtStart; }
1037 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
1039 // Marks the end of the container.
1040 SourceRange getAtEndRange() const {
1043 void setAtEndRange(SourceRange atEnd) {
1047 SourceRange getSourceRange() const override LLVM_READONLY {
1048 return SourceRange(AtStart, getAtEndRange().getEnd());
1051 // Implement isa/cast/dyncast/etc.
1052 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1053 static bool classofKind(Kind K) {
1054 return K >= firstObjCContainer &&
1055 K <= lastObjCContainer;
1058 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1059 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1061 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1062 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1066 /// \brief Represents an ObjC class declaration.
1071 /// // MostPrimitive declares no super class (not particularly useful).
1072 /// \@interface MostPrimitive
1073 /// // no instance variables or methods.
1076 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1077 /// \@interface NSResponder : NSObject \<NSCoding>
1078 /// { // instance variables are represented by ObjCIvarDecl.
1079 /// id nextResponder; // nextResponder instance variable.
1081 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1082 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1083 /// \@end // to an NSEvent.
1086 /// Unlike C/C++, forward class declarations are accomplished with \@class.
1087 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
1088 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1089 /// typically inherit from NSObject (an exception is NSProxy).
1091 class ObjCInterfaceDecl : public ObjCContainerDecl
1092 , public Redeclarable<ObjCInterfaceDecl> {
1093 void anchor() override;
1095 /// TypeForDecl - This indicates the Type object that represents this
1096 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
1097 mutable const Type *TypeForDecl;
1098 friend class ASTContext;
1100 struct DefinitionData {
1101 /// \brief The definition of this class, for quick access from any
1103 ObjCInterfaceDecl *Definition;
1105 /// When non-null, this is always an ObjCObjectType.
1106 TypeSourceInfo *SuperClassTInfo;
1108 /// Protocols referenced in the \@interface declaration
1109 ObjCProtocolList ReferencedProtocols;
1111 /// Protocols reference in both the \@interface and class extensions.
1112 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1114 /// \brief List of categories and class extensions defined for this class.
1116 /// Categories are stored as a linked list in the AST, since the categories
1117 /// and class extensions come long after the initial interface declaration,
1118 /// and we avoid dynamically-resized arrays in the AST wherever possible.
1119 ObjCCategoryDecl *CategoryList;
1121 /// IvarList - List of all ivars defined by this class; including class
1122 /// extensions and implementation. This list is built lazily.
1123 ObjCIvarDecl *IvarList;
1125 /// \brief Indicates that the contents of this Objective-C class will be
1126 /// completed by the external AST source when required.
1127 mutable unsigned ExternallyCompleted : 1;
1129 /// \brief Indicates that the ivar cache does not yet include ivars
1130 /// declared in the implementation.
1131 mutable unsigned IvarListMissingImplementation : 1;
1133 /// Indicates that this interface decl contains at least one initializer
1134 /// marked with the 'objc_designated_initializer' attribute.
1135 unsigned HasDesignatedInitializers : 1;
1137 enum InheritedDesignatedInitializersState {
1138 /// We didn't calculate whether the designated initializers should be
1139 /// inherited or not.
1141 /// Designated initializers are inherited for the super class.
1143 /// The class does not inherit designated initializers.
1144 IDI_NotInherited = 2
1146 /// One of the \c InheritedDesignatedInitializersState enumeratos.
1147 mutable unsigned InheritedDesignatedInitializers : 2;
1149 /// \brief The location of the last location in this declaration, before
1150 /// the properties/methods. For example, this will be the '>', '}', or
1152 SourceLocation EndLoc;
1154 DefinitionData() : Definition(), SuperClassTInfo(), CategoryList(), IvarList(),
1155 ExternallyCompleted(),
1156 IvarListMissingImplementation(true),
1157 HasDesignatedInitializers(),
1158 InheritedDesignatedInitializers(IDI_Unknown) { }
1161 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1162 IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1163 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1166 void LoadExternalDefinition() const;
1168 /// The type parameters associated with this class, if any.
1169 ObjCTypeParamList *TypeParamList;
1171 /// \brief Contains a pointer to the data associated with this class,
1172 /// which will be NULL if this class has not yet been defined.
1174 /// The bit indicates when we don't need to check for out-of-date
1175 /// declarations. It will be set unless modules are enabled.
1176 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1178 DefinitionData &data() const {
1179 assert(Data.getPointer() && "Declaration has no definition!");
1180 return *Data.getPointer();
1183 /// \brief Allocate the definition data for this class.
1184 void allocateDefinitionData();
1186 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
1187 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1188 return getNextRedeclaration();
1190 ObjCInterfaceDecl *getPreviousDeclImpl() override {
1191 return getPreviousDecl();
1193 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1194 return getMostRecentDecl();
1198 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1199 SourceLocation atLoc,
1201 ObjCTypeParamList *typeParamList,
1202 ObjCInterfaceDecl *PrevDecl,
1203 SourceLocation ClassLoc = SourceLocation(),
1204 bool isInternal = false);
1206 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1208 /// Retrieve the type parameters of this class.
1210 /// This function looks for a type parameter list for the given
1211 /// class; if the class has been declared (with \c \@class) but not
1212 /// defined (with \c \@interface), it will search for a declaration that
1213 /// has type parameters, skipping any declarations that do not.
1214 ObjCTypeParamList *getTypeParamList() const;
1216 /// Set the type parameters of this class.
1218 /// This function is used by the AST importer, which must import the type
1219 /// parameters after creating their DeclContext to avoid loops.
1220 void setTypeParamList(ObjCTypeParamList *TPL);
1222 /// Retrieve the type parameters written on this particular declaration of
1224 ObjCTypeParamList *getTypeParamListAsWritten() const {
1225 return TypeParamList;
1228 SourceRange getSourceRange() const override LLVM_READONLY {
1229 if (isThisDeclarationADefinition())
1230 return ObjCContainerDecl::getSourceRange();
1232 return SourceRange(getAtStartLoc(), getLocation());
1235 /// \brief Indicate that this Objective-C class is complete, but that
1236 /// the external AST source will be responsible for filling in its contents
1237 /// when a complete class is required.
1238 void setExternallyCompleted();
1240 /// Indicate that this interface decl contains at least one initializer
1241 /// marked with the 'objc_designated_initializer' attribute.
1242 void setHasDesignatedInitializers();
1244 /// Returns true if this interface decl contains at least one initializer
1245 /// marked with the 'objc_designated_initializer' attribute.
1246 bool hasDesignatedInitializers() const;
1248 /// Returns true if this interface decl declares a designated initializer
1249 /// or it inherites one from its super class.
1250 bool declaresOrInheritsDesignatedInitializers() const {
1251 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1254 const ObjCProtocolList &getReferencedProtocols() const {
1255 assert(hasDefinition() && "Caller did not check for forward reference!");
1256 if (data().ExternallyCompleted)
1257 LoadExternalDefinition();
1259 return data().ReferencedProtocols;
1262 ObjCImplementationDecl *getImplementation() const;
1263 void setImplementation(ObjCImplementationDecl *ImplD);
1265 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1267 // Get the local instance/class method declared in a category.
1268 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1269 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1270 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1271 return isInstance ? getCategoryInstanceMethod(Sel)
1272 : getCategoryClassMethod(Sel);
1275 typedef ObjCProtocolList::iterator protocol_iterator;
1276 typedef llvm::iterator_range<protocol_iterator> protocol_range;
1278 protocol_range protocols() const {
1279 return protocol_range(protocol_begin(), protocol_end());
1281 protocol_iterator protocol_begin() const {
1282 // FIXME: Should make sure no callers ever do this.
1283 if (!hasDefinition())
1284 return protocol_iterator();
1286 if (data().ExternallyCompleted)
1287 LoadExternalDefinition();
1289 return data().ReferencedProtocols.begin();
1291 protocol_iterator protocol_end() const {
1292 // FIXME: Should make sure no callers ever do this.
1293 if (!hasDefinition())
1294 return protocol_iterator();
1296 if (data().ExternallyCompleted)
1297 LoadExternalDefinition();
1299 return data().ReferencedProtocols.end();
1302 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1303 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1305 protocol_loc_range protocol_locs() const {
1306 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1308 protocol_loc_iterator protocol_loc_begin() const {
1309 // FIXME: Should make sure no callers ever do this.
1310 if (!hasDefinition())
1311 return protocol_loc_iterator();
1313 if (data().ExternallyCompleted)
1314 LoadExternalDefinition();
1316 return data().ReferencedProtocols.loc_begin();
1319 protocol_loc_iterator protocol_loc_end() const {
1320 // FIXME: Should make sure no callers ever do this.
1321 if (!hasDefinition())
1322 return protocol_loc_iterator();
1324 if (data().ExternallyCompleted)
1325 LoadExternalDefinition();
1327 return data().ReferencedProtocols.loc_end();
1330 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
1331 typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range;
1333 all_protocol_range all_referenced_protocols() const {
1334 return all_protocol_range(all_referenced_protocol_begin(),
1335 all_referenced_protocol_end());
1337 all_protocol_iterator all_referenced_protocol_begin() const {
1338 // FIXME: Should make sure no callers ever do this.
1339 if (!hasDefinition())
1340 return all_protocol_iterator();
1342 if (data().ExternallyCompleted)
1343 LoadExternalDefinition();
1345 return data().AllReferencedProtocols.empty()
1347 : data().AllReferencedProtocols.begin();
1349 all_protocol_iterator all_referenced_protocol_end() const {
1350 // FIXME: Should make sure no callers ever do this.
1351 if (!hasDefinition())
1352 return all_protocol_iterator();
1354 if (data().ExternallyCompleted)
1355 LoadExternalDefinition();
1357 return data().AllReferencedProtocols.empty()
1359 : data().AllReferencedProtocols.end();
1362 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1363 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
1365 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1366 ivar_iterator ivar_begin() const {
1367 if (const ObjCInterfaceDecl *Def = getDefinition())
1368 return ivar_iterator(Def->decls_begin());
1370 // FIXME: Should make sure no callers ever do this.
1371 return ivar_iterator();
1373 ivar_iterator ivar_end() const {
1374 if (const ObjCInterfaceDecl *Def = getDefinition())
1375 return ivar_iterator(Def->decls_end());
1377 // FIXME: Should make sure no callers ever do this.
1378 return ivar_iterator();
1381 unsigned ivar_size() const {
1382 return std::distance(ivar_begin(), ivar_end());
1385 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1387 ObjCIvarDecl *all_declared_ivar_begin();
1388 const ObjCIvarDecl *all_declared_ivar_begin() const {
1389 // Even though this modifies IvarList, it's conceptually const:
1390 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1391 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1393 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1395 /// setProtocolList - Set the list of protocols that this interface
1397 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1398 const SourceLocation *Locs, ASTContext &C) {
1399 data().ReferencedProtocols.set(List, Num, Locs, C);
1402 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1403 /// into the protocol list for this class.
1404 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1408 /// Produce a name to be used for class's metadata. It comes either via
1409 /// objc_runtime_name attribute or class name.
1410 StringRef getObjCRuntimeNameAsString() const;
1412 /// Returns the designated initializers for the interface.
1414 /// If this declaration does not have methods marked as designated
1415 /// initializers then the interface inherits the designated initializers of
1416 /// its super class.
1417 void getDesignatedInitializers(
1418 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1420 /// Returns true if the given selector is a designated initializer for the
1423 /// If this declaration does not have methods marked as designated
1424 /// initializers then the interface inherits the designated initializers of
1425 /// its super class.
1427 /// \param InitMethod if non-null and the function returns true, it receives
1428 /// the method that was marked as a designated initializer.
1430 isDesignatedInitializer(Selector Sel,
1431 const ObjCMethodDecl **InitMethod = nullptr) const;
1433 /// \brief Determine whether this particular declaration of this class is
1434 /// actually also a definition.
1435 bool isThisDeclarationADefinition() const {
1436 return getDefinition() == this;
1439 /// \brief Determine whether this class has been defined.
1440 bool hasDefinition() const {
1441 // If the name of this class is out-of-date, bring it up-to-date, which
1442 // might bring in a definition.
1443 // Note: a null value indicates that we don't have a definition and that
1444 // modules are enabled.
1445 if (!Data.getOpaqueValue())
1446 getMostRecentDecl();
1448 return Data.getPointer();
1451 /// \brief Retrieve the definition of this class, or NULL if this class
1452 /// has been forward-declared (with \@class) but not yet defined (with
1454 ObjCInterfaceDecl *getDefinition() {
1455 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1458 /// \brief Retrieve the definition of this class, or NULL if this class
1459 /// has been forward-declared (with \@class) but not yet defined (with
1461 const ObjCInterfaceDecl *getDefinition() const {
1462 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1465 /// \brief Starts the definition of this Objective-C class, taking it from
1466 /// a forward declaration (\@class) to a definition (\@interface).
1467 void startDefinition();
1469 /// Retrieve the superclass type.
1470 const ObjCObjectType *getSuperClassType() const {
1471 if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1472 return TInfo->getType()->castAs<ObjCObjectType>();
1477 // Retrieve the type source information for the superclass.
1478 TypeSourceInfo *getSuperClassTInfo() const {
1479 // FIXME: Should make sure no callers ever do this.
1480 if (!hasDefinition())
1483 if (data().ExternallyCompleted)
1484 LoadExternalDefinition();
1486 return data().SuperClassTInfo;
1489 // Retrieve the declaration for the superclass of this class, which
1490 // does not include any type arguments that apply to the superclass.
1491 ObjCInterfaceDecl *getSuperClass() const;
1493 void setSuperClass(TypeSourceInfo *superClass) {
1494 data().SuperClassTInfo = superClass;
1497 /// \brief Iterator that walks over the list of categories, filtering out
1498 /// those that do not meet specific criteria.
1500 /// This class template is used for the various permutations of category
1501 /// and extension iterators.
1502 template<bool (*Filter)(ObjCCategoryDecl *)>
1503 class filtered_category_iterator {
1504 ObjCCategoryDecl *Current;
1506 void findAcceptableCategory();
1509 typedef ObjCCategoryDecl * value_type;
1510 typedef value_type reference;
1511 typedef value_type pointer;
1512 typedef std::ptrdiff_t difference_type;
1513 typedef std::input_iterator_tag iterator_category;
1515 filtered_category_iterator() : Current(nullptr) { }
1516 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1519 findAcceptableCategory();
1522 reference operator*() const { return Current; }
1523 pointer operator->() const { return Current; }
1525 filtered_category_iterator &operator++();
1527 filtered_category_iterator operator++(int) {
1528 filtered_category_iterator Tmp = *this;
1533 friend bool operator==(filtered_category_iterator X,
1534 filtered_category_iterator Y) {
1535 return X.Current == Y.Current;
1538 friend bool operator!=(filtered_category_iterator X,
1539 filtered_category_iterator Y) {
1540 return X.Current != Y.Current;
1545 /// \brief Test whether the given category is visible.
1547 /// Used in the \c visible_categories_iterator.
1548 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1551 /// \brief Iterator that walks over the list of categories and extensions
1552 /// that are visible, i.e., not hidden in a non-imported submodule.
1553 typedef filtered_category_iterator<isVisibleCategory>
1554 visible_categories_iterator;
1556 typedef llvm::iterator_range<visible_categories_iterator>
1557 visible_categories_range;
1559 visible_categories_range visible_categories() const {
1560 return visible_categories_range(visible_categories_begin(),
1561 visible_categories_end());
1564 /// \brief Retrieve an iterator to the beginning of the visible-categories
1566 visible_categories_iterator visible_categories_begin() const {
1567 return visible_categories_iterator(getCategoryListRaw());
1570 /// \brief Retrieve an iterator to the end of the visible-categories list.
1571 visible_categories_iterator visible_categories_end() const {
1572 return visible_categories_iterator();
1575 /// \brief Determine whether the visible-categories list is empty.
1576 bool visible_categories_empty() const {
1577 return visible_categories_begin() == visible_categories_end();
1581 /// \brief Test whether the given category... is a category.
1583 /// Used in the \c known_categories_iterator.
1584 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1587 /// \brief Iterator that walks over all of the known categories and
1588 /// extensions, including those that are hidden.
1589 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1590 typedef llvm::iterator_range<known_categories_iterator>
1591 known_categories_range;
1593 known_categories_range known_categories() const {
1594 return known_categories_range(known_categories_begin(),
1595 known_categories_end());
1598 /// \brief Retrieve an iterator to the beginning of the known-categories
1600 known_categories_iterator known_categories_begin() const {
1601 return known_categories_iterator(getCategoryListRaw());
1604 /// \brief Retrieve an iterator to the end of the known-categories list.
1605 known_categories_iterator known_categories_end() const {
1606 return known_categories_iterator();
1609 /// \brief Determine whether the known-categories list is empty.
1610 bool known_categories_empty() const {
1611 return known_categories_begin() == known_categories_end();
1615 /// \brief Test whether the given category is a visible extension.
1617 /// Used in the \c visible_extensions_iterator.
1618 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1621 /// \brief Iterator that walks over all of the visible extensions, skipping
1622 /// any that are known but hidden.
1623 typedef filtered_category_iterator<isVisibleExtension>
1624 visible_extensions_iterator;
1626 typedef llvm::iterator_range<visible_extensions_iterator>
1627 visible_extensions_range;
1629 visible_extensions_range visible_extensions() const {
1630 return visible_extensions_range(visible_extensions_begin(),
1631 visible_extensions_end());
1634 /// \brief Retrieve an iterator to the beginning of the visible-extensions
1636 visible_extensions_iterator visible_extensions_begin() const {
1637 return visible_extensions_iterator(getCategoryListRaw());
1640 /// \brief Retrieve an iterator to the end of the visible-extensions list.
1641 visible_extensions_iterator visible_extensions_end() const {
1642 return visible_extensions_iterator();
1645 /// \brief Determine whether the visible-extensions list is empty.
1646 bool visible_extensions_empty() const {
1647 return visible_extensions_begin() == visible_extensions_end();
1651 /// \brief Test whether the given category is an extension.
1653 /// Used in the \c known_extensions_iterator.
1654 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1657 /// \brief Iterator that walks over all of the known extensions.
1658 typedef filtered_category_iterator<isKnownExtension>
1659 known_extensions_iterator;
1660 typedef llvm::iterator_range<known_extensions_iterator>
1661 known_extensions_range;
1663 known_extensions_range known_extensions() const {
1664 return known_extensions_range(known_extensions_begin(),
1665 known_extensions_end());
1668 /// \brief Retrieve an iterator to the beginning of the known-extensions
1670 known_extensions_iterator known_extensions_begin() const {
1671 return known_extensions_iterator(getCategoryListRaw());
1674 /// \brief Retrieve an iterator to the end of the known-extensions list.
1675 known_extensions_iterator known_extensions_end() const {
1676 return known_extensions_iterator();
1679 /// \brief Determine whether the known-extensions list is empty.
1680 bool known_extensions_empty() const {
1681 return known_extensions_begin() == known_extensions_end();
1684 /// \brief Retrieve the raw pointer to the start of the category/extension
1686 ObjCCategoryDecl* getCategoryListRaw() const {
1687 // FIXME: Should make sure no callers ever do this.
1688 if (!hasDefinition())
1691 if (data().ExternallyCompleted)
1692 LoadExternalDefinition();
1694 return data().CategoryList;
1697 /// \brief Set the raw pointer to the start of the category/extension
1699 void setCategoryListRaw(ObjCCategoryDecl *category) {
1700 data().CategoryList = category;
1704 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1705 ObjCPropertyQueryKind QueryKind) const;
1707 void collectPropertiesToImplement(PropertyMap &PM,
1708 PropertyDeclOrder &PO) const override;
1710 /// isSuperClassOf - Return true if this class is the specified class or is a
1711 /// super class of the specified interface class.
1712 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1713 // If RHS is derived from LHS it is OK; else it is not OK.
1714 while (I != nullptr) {
1715 if (declaresSameEntity(this, I))
1718 I = I->getSuperClass();
1723 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1724 /// to be incompatible with __weak references. Returns true if it is.
1725 bool isArcWeakrefUnavailable() const;
1727 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1728 /// classes must not be auto-synthesized. Returns class decl. if it must not
1729 /// be; 0, otherwise.
1730 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1732 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1733 ObjCInterfaceDecl *&ClassDeclared);
1734 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1735 ObjCInterfaceDecl *ClassDeclared;
1736 return lookupInstanceVariable(IVarName, ClassDeclared);
1739 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1741 // Lookup a method. First, we search locally. If a method isn't
1742 // found, we search referenced protocols and class categories.
1743 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1744 bool shallowCategoryLookup = false,
1745 bool followSuper = true,
1746 const ObjCCategoryDecl *C = nullptr) const;
1748 /// Lookup an instance method for a given selector.
1749 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1750 return lookupMethod(Sel, true/*isInstance*/);
1753 /// Lookup a class method for a given selector.
1754 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1755 return lookupMethod(Sel, false/*isInstance*/);
1757 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1759 /// \brief Lookup a method in the classes implementation hierarchy.
1760 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1761 bool Instance=true) const;
1763 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1764 return lookupPrivateMethod(Sel, false);
1767 /// \brief Lookup a setter or getter in the class hierarchy,
1768 /// including in all categories except for category passed
1770 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1771 const ObjCCategoryDecl *Cat,
1772 bool IsClassProperty) const {
1773 return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1774 false/*shallowCategoryLookup*/,
1775 true /* followsSuper */,
1779 SourceLocation getEndOfDefinitionLoc() const {
1780 if (!hasDefinition())
1781 return getLocation();
1783 return data().EndLoc;
1786 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1788 /// Retrieve the starting location of the superclass.
1789 SourceLocation getSuperClassLoc() const;
1791 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1792 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1793 /// declaration without an \@interface declaration.
1794 bool isImplicitInterfaceDecl() const {
1795 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1798 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1799 /// has been implemented in IDecl class, its super class or categories (if
1800 /// lookupCategory is true).
1801 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1802 bool lookupCategory,
1803 bool RHSIsQualifiedID = false);
1805 typedef redeclarable_base::redecl_range redecl_range;
1806 typedef redeclarable_base::redecl_iterator redecl_iterator;
1807 using redeclarable_base::redecls_begin;
1808 using redeclarable_base::redecls_end;
1809 using redeclarable_base::redecls;
1810 using redeclarable_base::getPreviousDecl;
1811 using redeclarable_base::getMostRecentDecl;
1812 using redeclarable_base::isFirstDecl;
1814 /// Retrieves the canonical declaration of this Objective-C class.
1815 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1816 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1818 // Low-level accessor
1819 const Type *getTypeForDecl() const { return TypeForDecl; }
1820 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1822 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1823 static bool classofKind(Kind K) { return K == ObjCInterface; }
1825 friend class ASTReader;
1826 friend class ASTDeclReader;
1827 friend class ASTDeclWriter;
1830 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1831 bool inheritsDesignatedInitializers() const;
1834 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1835 /// instance variables are identical to C. The only exception is Objective-C
1836 /// supports C++ style access control. For example:
1838 /// \@interface IvarExample : NSObject
1840 /// id defaultToProtected;
1842 /// id canBePublic; // same as C++.
1844 /// id canBeProtected; // same as C++.
1846 /// id canBePackage; // framework visibility (not available in C++).
1849 class ObjCIvarDecl : public FieldDecl {
1850 void anchor() override;
1853 enum AccessControl {
1854 None, Private, Protected, Public, Package
1858 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1859 SourceLocation IdLoc, IdentifierInfo *Id,
1860 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1862 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1863 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1864 NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {}
1867 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1868 SourceLocation StartLoc, SourceLocation IdLoc,
1869 IdentifierInfo *Id, QualType T,
1870 TypeSourceInfo *TInfo,
1871 AccessControl ac, Expr *BW = nullptr,
1872 bool synthesized=false);
1874 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1876 /// \brief Return the class interface that this ivar is logically contained
1877 /// in; this is either the interface where the ivar was declared, or the
1878 /// interface the ivar is conceptually a part of in the case of synthesized
1880 const ObjCInterfaceDecl *getContainingInterface() const;
1882 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1883 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1884 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1886 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1888 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1890 AccessControl getCanonicalAccessControl() const {
1891 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1894 void setSynthesize(bool synth) { Synthesized = synth; }
1895 bool getSynthesize() const { return Synthesized; }
1897 /// Retrieve the type of this instance variable when viewed as a member of a
1898 /// specific object type.
1899 QualType getUsageType(QualType objectType) const;
1901 // Implement isa/cast/dyncast/etc.
1902 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1903 static bool classofKind(Kind K) { return K == ObjCIvar; }
1905 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1906 /// extensions and class's implementation
1907 ObjCIvarDecl *NextIvar;
1909 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1910 unsigned DeclAccess : 3;
1911 unsigned Synthesized : 1;
1915 /// \brief Represents a field declaration created by an \@defs(...).
1916 class ObjCAtDefsFieldDecl : public FieldDecl {
1917 void anchor() override;
1918 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1919 SourceLocation IdLoc, IdentifierInfo *Id,
1920 QualType T, Expr *BW)
1921 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1922 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
1923 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1926 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1927 SourceLocation StartLoc,
1928 SourceLocation IdLoc, IdentifierInfo *Id,
1929 QualType T, Expr *BW);
1931 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1933 // Implement isa/cast/dyncast/etc.
1934 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1935 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1938 /// \brief Represents an Objective-C protocol declaration.
1940 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1941 /// variables are permitted). Protocols originally drew inspiration from
1942 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1943 /// syntax:-). Here is an example:
1946 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1947 /// - (NSWindow *)draggingDestinationWindow;
1948 /// - (NSImage *)draggedImage;
1952 /// This says that NSDraggingInfo requires two methods and requires everything
1953 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1957 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1961 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1962 /// protocols are in distinct namespaces. For example, Cocoa defines both
1963 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1964 /// protocols are referenced using angle brackets as follows:
1966 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1968 class ObjCProtocolDecl : public ObjCContainerDecl,
1969 public Redeclarable<ObjCProtocolDecl> {
1970 void anchor() override;
1972 struct DefinitionData {
1973 // \brief The declaration that defines this protocol.
1974 ObjCProtocolDecl *Definition;
1976 /// \brief Referenced protocols
1977 ObjCProtocolList ReferencedProtocols;
1980 /// \brief Contains a pointer to the data associated with this class,
1981 /// which will be NULL if this class has not yet been defined.
1983 /// The bit indicates when we don't need to check for out-of-date
1984 /// declarations. It will be set unless modules are enabled.
1985 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1987 DefinitionData &data() const {
1988 assert(Data.getPointer() && "Objective-C protocol has no definition!");
1989 return *Data.getPointer();
1992 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
1993 SourceLocation nameLoc, SourceLocation atStartLoc,
1994 ObjCProtocolDecl *PrevDecl);
1996 void allocateDefinitionData();
1998 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1999 ObjCProtocolDecl *getNextRedeclarationImpl() override {
2000 return getNextRedeclaration();
2002 ObjCProtocolDecl *getPreviousDeclImpl() override {
2003 return getPreviousDecl();
2005 ObjCProtocolDecl *getMostRecentDeclImpl() override {
2006 return getMostRecentDecl();
2010 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2012 SourceLocation nameLoc,
2013 SourceLocation atStartLoc,
2014 ObjCProtocolDecl *PrevDecl);
2016 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2018 const ObjCProtocolList &getReferencedProtocols() const {
2019 assert(hasDefinition() && "No definition available!");
2020 return data().ReferencedProtocols;
2022 typedef ObjCProtocolList::iterator protocol_iterator;
2023 typedef llvm::iterator_range<protocol_iterator> protocol_range;
2025 protocol_range protocols() const {
2026 return protocol_range(protocol_begin(), protocol_end());
2028 protocol_iterator protocol_begin() const {
2029 if (!hasDefinition())
2030 return protocol_iterator();
2032 return data().ReferencedProtocols.begin();
2034 protocol_iterator protocol_end() const {
2035 if (!hasDefinition())
2036 return protocol_iterator();
2038 return data().ReferencedProtocols.end();
2040 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
2041 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
2043 protocol_loc_range protocol_locs() const {
2044 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2046 protocol_loc_iterator protocol_loc_begin() const {
2047 if (!hasDefinition())
2048 return protocol_loc_iterator();
2050 return data().ReferencedProtocols.loc_begin();
2052 protocol_loc_iterator protocol_loc_end() const {
2053 if (!hasDefinition())
2054 return protocol_loc_iterator();
2056 return data().ReferencedProtocols.loc_end();
2058 unsigned protocol_size() const {
2059 if (!hasDefinition())
2062 return data().ReferencedProtocols.size();
2065 /// setProtocolList - Set the list of protocols that this interface
2067 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2068 const SourceLocation *Locs, ASTContext &C) {
2069 assert(hasDefinition() && "Protocol is not defined");
2070 data().ReferencedProtocols.set(List, Num, Locs, C);
2073 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2075 // Lookup a method. First, we search locally. If a method isn't
2076 // found, we search referenced protocols and class categories.
2077 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2078 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2079 return lookupMethod(Sel, true/*isInstance*/);
2081 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2082 return lookupMethod(Sel, false/*isInstance*/);
2085 /// \brief Determine whether this protocol has a definition.
2086 bool hasDefinition() const {
2087 // If the name of this protocol is out-of-date, bring it up-to-date, which
2088 // might bring in a definition.
2089 // Note: a null value indicates that we don't have a definition and that
2090 // modules are enabled.
2091 if (!Data.getOpaqueValue())
2092 getMostRecentDecl();
2094 return Data.getPointer();
2097 /// \brief Retrieve the definition of this protocol, if any.
2098 ObjCProtocolDecl *getDefinition() {
2099 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2102 /// \brief Retrieve the definition of this protocol, if any.
2103 const ObjCProtocolDecl *getDefinition() const {
2104 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2107 /// \brief Determine whether this particular declaration is also the
2109 bool isThisDeclarationADefinition() const {
2110 return getDefinition() == this;
2113 /// \brief Starts the definition of this Objective-C protocol.
2114 void startDefinition();
2116 /// Produce a name to be used for protocol's metadata. It comes either via
2117 /// objc_runtime_name attribute or protocol name.
2118 StringRef getObjCRuntimeNameAsString() const;
2120 SourceRange getSourceRange() const override LLVM_READONLY {
2121 if (isThisDeclarationADefinition())
2122 return ObjCContainerDecl::getSourceRange();
2124 return SourceRange(getAtStartLoc(), getLocation());
2127 typedef redeclarable_base::redecl_range redecl_range;
2128 typedef redeclarable_base::redecl_iterator redecl_iterator;
2129 using redeclarable_base::redecls_begin;
2130 using redeclarable_base::redecls_end;
2131 using redeclarable_base::redecls;
2132 using redeclarable_base::getPreviousDecl;
2133 using redeclarable_base::getMostRecentDecl;
2134 using redeclarable_base::isFirstDecl;
2136 /// Retrieves the canonical declaration of this Objective-C protocol.
2137 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2138 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2140 void collectPropertiesToImplement(PropertyMap &PM,
2141 PropertyDeclOrder &PO) const override;
2143 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2144 ProtocolPropertyMap &PM) const;
2146 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2147 static bool classofKind(Kind K) { return K == ObjCProtocol; }
2149 friend class ASTReader;
2150 friend class ASTDeclReader;
2151 friend class ASTDeclWriter;
2154 /// ObjCCategoryDecl - Represents a category declaration. A category allows
2155 /// you to add methods to an existing class (without subclassing or modifying
2156 /// the original class interface or implementation:-). Categories don't allow
2157 /// you to add instance data. The following example adds "myMethod" to all
2158 /// NSView's within a process:
2160 /// \@interface NSView (MyViewMethods)
2164 /// Categories also allow you to split the implementation of a class across
2165 /// several files (a feature more naturally supported in C++).
2167 /// Categories were originally inspired by dynamic languages such as Common
2168 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
2169 /// don't support this level of dynamism, which is both powerful and dangerous.
2171 class ObjCCategoryDecl : public ObjCContainerDecl {
2172 void anchor() override;
2174 /// Interface belonging to this category
2175 ObjCInterfaceDecl *ClassInterface;
2177 /// The type parameters associated with this category, if any.
2178 ObjCTypeParamList *TypeParamList;
2180 /// referenced protocols in this category.
2181 ObjCProtocolList ReferencedProtocols;
2183 /// Next category belonging to this class.
2184 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
2185 ObjCCategoryDecl *NextClassCategory;
2187 /// \brief The location of the category name in this declaration.
2188 SourceLocation CategoryNameLoc;
2190 /// class extension may have private ivars.
2191 SourceLocation IvarLBraceLoc;
2192 SourceLocation IvarRBraceLoc;
2194 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2195 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2196 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2197 ObjCTypeParamList *typeParamList,
2198 SourceLocation IvarLBraceLoc=SourceLocation(),
2199 SourceLocation IvarRBraceLoc=SourceLocation());
2203 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2204 SourceLocation AtLoc,
2205 SourceLocation ClassNameLoc,
2206 SourceLocation CategoryNameLoc,
2208 ObjCInterfaceDecl *IDecl,
2209 ObjCTypeParamList *typeParamList,
2210 SourceLocation IvarLBraceLoc=SourceLocation(),
2211 SourceLocation IvarRBraceLoc=SourceLocation());
2212 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2214 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2215 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2217 /// Retrieve the type parameter list associated with this category or
2219 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2221 /// Set the type parameters of this category.
2223 /// This function is used by the AST importer, which must import the type
2224 /// parameters after creating their DeclContext to avoid loops.
2225 void setTypeParamList(ObjCTypeParamList *TPL);
2228 ObjCCategoryImplDecl *getImplementation() const;
2229 void setImplementation(ObjCCategoryImplDecl *ImplD);
2231 /// setProtocolList - Set the list of protocols that this interface
2233 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2234 const SourceLocation *Locs, ASTContext &C) {
2235 ReferencedProtocols.set(List, Num, Locs, C);
2238 const ObjCProtocolList &getReferencedProtocols() const {
2239 return ReferencedProtocols;
2242 typedef ObjCProtocolList::iterator protocol_iterator;
2243 typedef llvm::iterator_range<protocol_iterator> protocol_range;
2245 protocol_range protocols() const {
2246 return protocol_range(protocol_begin(), protocol_end());
2248 protocol_iterator protocol_begin() const {
2249 return ReferencedProtocols.begin();
2251 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2252 unsigned protocol_size() const { return ReferencedProtocols.size(); }
2253 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
2254 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
2256 protocol_loc_range protocol_locs() const {
2257 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2259 protocol_loc_iterator protocol_loc_begin() const {
2260 return ReferencedProtocols.loc_begin();
2262 protocol_loc_iterator protocol_loc_end() const {
2263 return ReferencedProtocols.loc_end();
2266 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2268 /// \brief Retrieve the pointer to the next stored category (or extension),
2269 /// which may be hidden.
2270 ObjCCategoryDecl *getNextClassCategoryRaw() const {
2271 return NextClassCategory;
2274 bool IsClassExtension() const { return getIdentifier() == nullptr; }
2276 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2277 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2279 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2280 ivar_iterator ivar_begin() const {
2281 return ivar_iterator(decls_begin());
2283 ivar_iterator ivar_end() const {
2284 return ivar_iterator(decls_end());
2286 unsigned ivar_size() const {
2287 return std::distance(ivar_begin(), ivar_end());
2289 bool ivar_empty() const {
2290 return ivar_begin() == ivar_end();
2293 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2294 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2296 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2297 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2298 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2299 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2301 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2302 static bool classofKind(Kind K) { return K == ObjCCategory; }
2304 friend class ASTDeclReader;
2305 friend class ASTDeclWriter;
2308 class ObjCImplDecl : public ObjCContainerDecl {
2309 void anchor() override;
2311 /// Class interface for this class/category implementation
2312 ObjCInterfaceDecl *ClassInterface;
2315 ObjCImplDecl(Kind DK, DeclContext *DC,
2316 ObjCInterfaceDecl *classInterface,
2317 SourceLocation nameLoc, SourceLocation atStartLoc)
2318 : ObjCContainerDecl(DK, DC,
2319 classInterface? classInterface->getIdentifier()
2321 nameLoc, atStartLoc),
2322 ClassInterface(classInterface) {}
2325 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2326 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2327 void setClassInterface(ObjCInterfaceDecl *IFace);
2329 void addInstanceMethod(ObjCMethodDecl *method) {
2330 // FIXME: Context should be set correctly before we get here.
2331 method->setLexicalDeclContext(this);
2334 void addClassMethod(ObjCMethodDecl *method) {
2335 // FIXME: Context should be set correctly before we get here.
2336 method->setLexicalDeclContext(this);
2340 void addPropertyImplementation(ObjCPropertyImplDecl *property);
2342 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2343 ObjCPropertyQueryKind queryKind) const;
2344 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2346 // Iterator access to properties.
2347 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
2348 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>
2351 propimpl_range property_impls() const {
2352 return propimpl_range(propimpl_begin(), propimpl_end());
2354 propimpl_iterator propimpl_begin() const {
2355 return propimpl_iterator(decls_begin());
2357 propimpl_iterator propimpl_end() const {
2358 return propimpl_iterator(decls_end());
2361 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2362 static bool classofKind(Kind K) {
2363 return K >= firstObjCImpl && K <= lastObjCImpl;
2367 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
2368 /// \@implementation declaration. If a category class has declaration of a
2369 /// property, its implementation must be specified in the category's
2370 /// \@implementation declaration. Example:
2371 /// \@interface I \@end
2372 /// \@interface I(CATEGORY)
2373 /// \@property int p1, d1;
2375 /// \@implementation I(CATEGORY)
2376 /// \@dynamic p1,d1;
2379 /// ObjCCategoryImplDecl
2380 class ObjCCategoryImplDecl : public ObjCImplDecl {
2381 void anchor() override;
2386 // Category name location
2387 SourceLocation CategoryNameLoc;
2389 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2390 ObjCInterfaceDecl *classInterface,
2391 SourceLocation nameLoc, SourceLocation atStartLoc,
2392 SourceLocation CategoryNameLoc)
2393 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
2394 Id(Id), CategoryNameLoc(CategoryNameLoc) {}
2396 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2398 ObjCInterfaceDecl *classInterface,
2399 SourceLocation nameLoc,
2400 SourceLocation atStartLoc,
2401 SourceLocation CategoryNameLoc);
2402 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2404 /// getIdentifier - Get the identifier that names the category
2405 /// interface associated with this implementation.
2406 /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier()
2407 /// with a different meaning. For example:
2408 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
2409 /// returns the class interface name, whereas
2410 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
2411 /// returns the category name.
2412 IdentifierInfo *getIdentifier() const {
2415 void setIdentifier(IdentifierInfo *II) { Id = II; }
2417 ObjCCategoryDecl *getCategoryDecl() const;
2419 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2421 /// getName - Get the name of identifier for the class interface associated
2422 /// with this implementation as a StringRef.
2424 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2426 StringRef getName() const { return Id ? Id->getName() : StringRef(); }
2428 /// @brief Get the name of the class associated with this interface.
2430 // FIXME: Deprecated, move clients to getName().
2431 std::string getNameAsString() const {
2435 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2436 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2438 friend class ASTDeclReader;
2439 friend class ASTDeclWriter;
2442 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2444 /// ObjCImplementationDecl - Represents a class definition - this is where
2445 /// method definitions are specified. For example:
2448 /// \@implementation MyClass
2449 /// - (void)myMethod { /* do something */ }
2453 /// In a non-fragile runtime, instance variables can appear in the class
2454 /// interface, class extensions (nameless categories), and in the implementation
2455 /// itself, as well as being synthesized as backing storage for properties.
2457 /// In a fragile runtime, instance variables are specified in the class
2458 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2459 /// we allow instance variables to be specified in the implementation. When
2460 /// specified, they need to be \em identical to the interface.
2461 class ObjCImplementationDecl : public ObjCImplDecl {
2462 void anchor() override;
2463 /// Implementation Class's super class.
2464 ObjCInterfaceDecl *SuperClass;
2465 SourceLocation SuperLoc;
2467 /// \@implementation may have private ivars.
2468 SourceLocation IvarLBraceLoc;
2469 SourceLocation IvarRBraceLoc;
2471 /// Support for ivar initialization.
2472 /// \brief The arguments used to initialize the ivars
2473 LazyCXXCtorInitializersPtr IvarInitializers;
2474 unsigned NumIvarInitializers;
2476 /// Do the ivars of this class require initialization other than
2477 /// zero-initialization?
2478 bool HasNonZeroConstructors : 1;
2480 /// Do the ivars of this class require non-trivial destruction?
2481 bool HasDestructors : 1;
2483 ObjCImplementationDecl(DeclContext *DC,
2484 ObjCInterfaceDecl *classInterface,
2485 ObjCInterfaceDecl *superDecl,
2486 SourceLocation nameLoc, SourceLocation atStartLoc,
2487 SourceLocation superLoc = SourceLocation(),
2488 SourceLocation IvarLBraceLoc=SourceLocation(),
2489 SourceLocation IvarRBraceLoc=SourceLocation())
2490 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
2491 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
2492 IvarRBraceLoc(IvarRBraceLoc),
2493 IvarInitializers(nullptr), NumIvarInitializers(0),
2494 HasNonZeroConstructors(false), HasDestructors(false) {}
2496 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2497 ObjCInterfaceDecl *classInterface,
2498 ObjCInterfaceDecl *superDecl,
2499 SourceLocation nameLoc,
2500 SourceLocation atStartLoc,
2501 SourceLocation superLoc = SourceLocation(),
2502 SourceLocation IvarLBraceLoc=SourceLocation(),
2503 SourceLocation IvarRBraceLoc=SourceLocation());
2505 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2507 /// init_iterator - Iterates through the ivar initializer list.
2508 typedef CXXCtorInitializer **init_iterator;
2510 /// init_const_iterator - Iterates through the ivar initializer list.
2511 typedef CXXCtorInitializer * const * init_const_iterator;
2513 typedef llvm::iterator_range<init_iterator> init_range;
2514 typedef llvm::iterator_range<init_const_iterator> init_const_range;
2516 init_range inits() { return init_range(init_begin(), init_end()); }
2517 init_const_range inits() const {
2518 return init_const_range(init_begin(), init_end());
2521 /// init_begin() - Retrieve an iterator to the first initializer.
2522 init_iterator init_begin() {
2523 const auto *ConstThis = this;
2524 return const_cast<init_iterator>(ConstThis->init_begin());
2526 /// begin() - Retrieve an iterator to the first initializer.
2527 init_const_iterator init_begin() const;
2529 /// init_end() - Retrieve an iterator past the last initializer.
2530 init_iterator init_end() {
2531 return init_begin() + NumIvarInitializers;
2533 /// end() - Retrieve an iterator past the last initializer.
2534 init_const_iterator init_end() const {
2535 return init_begin() + NumIvarInitializers;
2537 /// getNumArgs - Number of ivars which must be initialized.
2538 unsigned getNumIvarInitializers() const {
2539 return NumIvarInitializers;
2542 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2543 NumIvarInitializers = numNumIvarInitializers;
2546 void setIvarInitializers(ASTContext &C,
2547 CXXCtorInitializer ** initializers,
2548 unsigned numInitializers);
2550 /// Do any of the ivars of this class (not counting its base classes)
2551 /// require construction other than zero-initialization?
2552 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2553 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2555 /// Do any of the ivars of this class (not counting its base classes)
2556 /// require non-trivial destruction?
2557 bool hasDestructors() const { return HasDestructors; }
2558 void setHasDestructors(bool val) { HasDestructors = val; }
2560 /// getIdentifier - Get the identifier that names the class
2561 /// interface associated with this implementation.
2562 IdentifierInfo *getIdentifier() const {
2563 return getClassInterface()->getIdentifier();
2566 /// getName - Get the name of identifier for the class interface associated
2567 /// with this implementation as a StringRef.
2569 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2571 StringRef getName() const {
2572 assert(getIdentifier() && "Name is not a simple identifier");
2573 return getIdentifier()->getName();
2576 /// @brief Get the name of the class associated with this interface.
2578 // FIXME: Move to StringRef API.
2579 std::string getNameAsString() const {
2583 /// Produce a name to be used for class's metadata. It comes either via
2584 /// class's objc_runtime_name attribute or class name.
2585 StringRef getObjCRuntimeNameAsString() const;
2587 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2588 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2589 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2591 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2593 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2594 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2595 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2596 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2598 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2599 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2601 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2602 ivar_iterator ivar_begin() const {
2603 return ivar_iterator(decls_begin());
2605 ivar_iterator ivar_end() const {
2606 return ivar_iterator(decls_end());
2608 unsigned ivar_size() const {
2609 return std::distance(ivar_begin(), ivar_end());
2611 bool ivar_empty() const {
2612 return ivar_begin() == ivar_end();
2615 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2616 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2618 friend class ASTDeclReader;
2619 friend class ASTDeclWriter;
2622 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2624 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2625 /// declared as \@compatibility_alias alias class.
2626 class ObjCCompatibleAliasDecl : public NamedDecl {
2627 void anchor() override;
2628 /// Class that this is an alias of.
2629 ObjCInterfaceDecl *AliasedClass;
2631 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2632 ObjCInterfaceDecl* aliasedClass)
2633 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2635 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2636 SourceLocation L, IdentifierInfo *Id,
2637 ObjCInterfaceDecl* aliasedClass);
2639 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2642 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2643 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2644 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2646 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2647 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2651 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2652 /// in a class or category implementation block. For example:
2653 /// \@synthesize prop1 = ivar1;
2655 class ObjCPropertyImplDecl : public Decl {
2662 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2664 /// \brief For \@synthesize, the location of the ivar, if it was written in
2665 /// the source code.
2668 /// \@synthesize int a = b
2670 SourceLocation IvarLoc;
2672 /// Property declaration being implemented
2673 ObjCPropertyDecl *PropertyDecl;
2675 /// Null for \@dynamic. Required for \@synthesize.
2676 ObjCIvarDecl *PropertyIvarDecl;
2678 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2680 Expr *GetterCXXConstructor;
2682 /// Null for \@dynamic. Non-null if property has assignment operator to call
2683 /// in Setter synthesis.
2684 Expr *SetterCXXAssignment;
2686 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2687 ObjCPropertyDecl *property,
2689 ObjCIvarDecl *ivarDecl,
2690 SourceLocation ivarLoc)
2691 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2692 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2693 GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) {
2694 assert (PK == Dynamic || PropertyIvarDecl);
2698 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2699 SourceLocation atLoc, SourceLocation L,
2700 ObjCPropertyDecl *property,
2702 ObjCIvarDecl *ivarDecl,
2703 SourceLocation ivarLoc);
2705 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2707 SourceRange getSourceRange() const override LLVM_READONLY;
2709 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2710 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2712 ObjCPropertyDecl *getPropertyDecl() const {
2713 return PropertyDecl;
2715 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2717 Kind getPropertyImplementation() const {
2718 return PropertyIvarDecl ? Synthesize : Dynamic;
2721 ObjCIvarDecl *getPropertyIvarDecl() const {
2722 return PropertyIvarDecl;
2724 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2726 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2727 SourceLocation IvarLoc) {
2728 PropertyIvarDecl = Ivar;
2729 this->IvarLoc = IvarLoc;
2732 /// \brief For \@synthesize, returns true if an ivar name was explicitly
2736 /// \@synthesize int a = b; // true
2737 /// \@synthesize int a; // false
2739 bool isIvarNameSpecified() const {
2740 return IvarLoc.isValid() && IvarLoc != getLocation();
2743 Expr *getGetterCXXConstructor() const {
2744 return GetterCXXConstructor;
2746 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2747 GetterCXXConstructor = getterCXXConstructor;
2750 Expr *getSetterCXXAssignment() const {
2751 return SetterCXXAssignment;
2753 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2754 SetterCXXAssignment = setterCXXAssignment;
2757 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2758 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2760 friend class ASTDeclReader;
2763 template<bool (*Filter)(ObjCCategoryDecl *)>
2765 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2766 findAcceptableCategory() {
2767 while (Current && !Filter(Current))
2768 Current = Current->getNextClassCategoryRaw();
2771 template<bool (*Filter)(ObjCCategoryDecl *)>
2772 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2773 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2774 Current = Current->getNextClassCategoryRaw();
2775 findAcceptableCategory();
2779 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2780 return !Cat->isHidden();
2783 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2784 return Cat->IsClassExtension() && !Cat->isHidden();
2787 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2788 return Cat->IsClassExtension();
2791 } // end namespace clang