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 parameters
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);
466 bool isOptional() const {
467 return getImplementationControl() == Optional;
470 /// Returns true if this specific method declaration is marked with the
471 /// designated initializer attribute.
472 bool isThisDeclarationADesignatedInitializer() const;
474 /// Returns true if the method selector resolves to a designated initializer
475 /// in the class's interface.
477 /// \param InitMethod if non-null and the function returns true, it receives
478 /// the method declaration that was marked with the designated initializer
480 bool isDesignatedInitializerForTheInterface(
481 const ObjCMethodDecl **InitMethod = nullptr) const;
483 /// \brief Determine whether this method has a body.
484 bool hasBody() const override { return Body.isValid(); }
486 /// \brief Retrieve the body of this method, if it has one.
487 Stmt *getBody() const override;
489 void setLazyBody(uint64_t Offset) { Body = Offset; }
491 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
492 void setBody(Stmt *B) { Body = B; }
494 /// \brief Returns whether this specific method is a definition.
495 bool isThisDeclarationADefinition() const { return hasBody(); }
497 // Implement isa/cast/dyncast/etc.
498 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
499 static bool classofKind(Kind K) { return K == ObjCMethod; }
500 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
501 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
503 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
504 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
507 friend class ASTDeclReader;
508 friend class ASTDeclWriter;
511 /// Describes the variance of a given generic parameter.
512 enum class ObjCTypeParamVariance : uint8_t {
513 /// The parameter is invariant: must match exactly.
515 /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
516 /// the type parameter is covariant and T is a subtype of U.
518 /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
519 /// when the type parameter is covariant and U is a subtype of T.
523 /// Represents the declaration of an Objective-C type parameter.
526 /// @interface NSDictionary<Key : id<NSCopying>, Value>
530 /// In the example above, both \c Key and \c Value are represented by
531 /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
532 /// while \c Value gets an implicit bound of \c id.
534 /// Objective-C type parameters are typedef-names in the grammar,
535 class ObjCTypeParamDecl : public TypedefNameDecl {
536 void anchor() override;
538 /// Index of this type parameter in the type parameter list.
541 /// The variance of the type parameter.
542 unsigned Variance : 2;
544 /// The location of the variance, if any.
545 SourceLocation VarianceLoc;
547 /// The location of the ':', which will be valid when the bound was
548 /// explicitly specified.
549 SourceLocation ColonLoc;
551 ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
552 ObjCTypeParamVariance variance, SourceLocation varianceLoc,
554 SourceLocation nameLoc, IdentifierInfo *name,
555 SourceLocation colonLoc, TypeSourceInfo *boundInfo)
556 : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
558 Index(index), Variance(static_cast<unsigned>(variance)),
559 VarianceLoc(varianceLoc), ColonLoc(colonLoc) { }
562 static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
563 ObjCTypeParamVariance variance,
564 SourceLocation varianceLoc,
566 SourceLocation nameLoc,
567 IdentifierInfo *name,
568 SourceLocation colonLoc,
569 TypeSourceInfo *boundInfo);
570 static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
572 SourceRange getSourceRange() const override LLVM_READONLY;
574 /// Determine the variance of this type parameter.
575 ObjCTypeParamVariance getVariance() const {
576 return static_cast<ObjCTypeParamVariance>(Variance);
579 /// Set the variance of this type parameter.
580 void setVariance(ObjCTypeParamVariance variance) {
581 Variance = static_cast<unsigned>(variance);
584 /// Retrieve the location of the variance keyword.
585 SourceLocation getVarianceLoc() const { return VarianceLoc; }
587 /// Retrieve the index into its type parameter list.
588 unsigned getIndex() const { return Index; }
590 /// Whether this type parameter has an explicitly-written type bound, e.g.,
592 bool hasExplicitBound() const { return ColonLoc.isValid(); }
594 /// Retrieve the location of the ':' separating the type parameter name
595 /// from the explicitly-specified bound.
596 SourceLocation getColonLoc() const { return ColonLoc; }
598 // Implement isa/cast/dyncast/etc.
599 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
600 static bool classofKind(Kind K) { return K == ObjCTypeParam; }
602 friend class ASTDeclReader;
603 friend class ASTDeclWriter;
606 /// Stores a list of Objective-C type parameters for a parameterized class
607 /// or a category/extension thereof.
610 /// @interface NSArray<T> // stores the <T>
613 class ObjCTypeParamList final
614 : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
615 /// Stores the components of a SourceRange as a POD.
616 struct PODSourceRange {
622 /// Location of the left and right angle brackets.
623 PODSourceRange Brackets;
625 // Used only for alignment.
626 ObjCTypeParamDecl *AlignmentHack;
629 /// The number of parameters in the list, which are tail-allocated.
632 ObjCTypeParamList(SourceLocation lAngleLoc,
633 ArrayRef<ObjCTypeParamDecl *> typeParams,
634 SourceLocation rAngleLoc);
637 /// Create a new Objective-C type parameter list.
638 static ObjCTypeParamList *create(ASTContext &ctx,
639 SourceLocation lAngleLoc,
640 ArrayRef<ObjCTypeParamDecl *> typeParams,
641 SourceLocation rAngleLoc);
643 /// Iterate through the type parameters in the list.
644 typedef ObjCTypeParamDecl **iterator;
646 iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
648 iterator end() { return begin() + size(); }
650 /// Determine the number of type parameters in this list.
651 unsigned size() const { return NumParams; }
653 // Iterate through the type parameters in the list.
654 typedef ObjCTypeParamDecl * const *const_iterator;
656 const_iterator begin() const {
657 return getTrailingObjects<ObjCTypeParamDecl *>();
660 const_iterator end() const {
661 return begin() + size();
664 ObjCTypeParamDecl *front() const {
665 assert(size() > 0 && "empty Objective-C type parameter list");
669 ObjCTypeParamDecl *back() const {
670 assert(size() > 0 && "empty Objective-C type parameter list");
674 SourceLocation getLAngleLoc() const {
675 return SourceLocation::getFromRawEncoding(Brackets.Begin);
677 SourceLocation getRAngleLoc() const {
678 return SourceLocation::getFromRawEncoding(Brackets.End);
680 SourceRange getSourceRange() const {
681 return SourceRange(getLAngleLoc(), getRAngleLoc());
684 /// Gather the default set of type arguments to be substituted for
685 /// these type parameters when dealing with an unspecialized type.
686 void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
687 friend TrailingObjects;
690 enum class ObjCPropertyQueryKind : uint8_t {
691 OBJC_PR_query_unknown = 0x00,
692 OBJC_PR_query_instance,
696 /// \brief Represents one property declaration in an Objective-C interface.
700 /// \@property (assign, readwrite) int MyProperty;
702 class ObjCPropertyDecl : public NamedDecl {
703 void anchor() override;
705 enum PropertyAttributeKind {
706 OBJC_PR_noattr = 0x00,
707 OBJC_PR_readonly = 0x01,
708 OBJC_PR_getter = 0x02,
709 OBJC_PR_assign = 0x04,
710 OBJC_PR_readwrite = 0x08,
711 OBJC_PR_retain = 0x10,
713 OBJC_PR_nonatomic = 0x40,
714 OBJC_PR_setter = 0x80,
715 OBJC_PR_atomic = 0x100,
716 OBJC_PR_weak = 0x200,
717 OBJC_PR_strong = 0x400,
718 OBJC_PR_unsafe_unretained = 0x800,
719 /// Indicates that the nullability of the type was spelled with a
720 /// property attribute rather than a type qualifier.
721 OBJC_PR_nullability = 0x1000,
722 OBJC_PR_null_resettable = 0x2000,
723 OBJC_PR_class = 0x4000
724 // Adding a property should change NumPropertyAttrsBits
728 /// \brief Number of bits fitting all the property attributes.
729 NumPropertyAttrsBits = 15
732 enum SetterKind { Assign, Retain, Copy, Weak };
733 enum PropertyControl { None, Required, Optional };
735 SourceLocation AtLoc; // location of \@property
736 SourceLocation LParenLoc; // location of '(' starting attribute list or null.
738 TypeSourceInfo *DeclTypeSourceInfo;
739 unsigned PropertyAttributes : NumPropertyAttrsBits;
740 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
741 // \@required/\@optional
742 unsigned PropertyImplementation : 2;
744 Selector GetterName; // getter name of NULL if no getter
745 Selector SetterName; // setter name of NULL if no setter
747 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
748 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
749 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
751 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
752 SourceLocation AtLocation, SourceLocation LParenLocation,
753 QualType T, TypeSourceInfo *TSI,
754 PropertyControl propControl)
755 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
756 LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
757 PropertyAttributes(OBJC_PR_noattr),
758 PropertyAttributesAsWritten(OBJC_PR_noattr),
759 PropertyImplementation(propControl),
760 GetterName(Selector()),
761 SetterName(Selector()),
762 GetterMethodDecl(nullptr), SetterMethodDecl(nullptr),
763 PropertyIvarDecl(nullptr) {}
766 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
768 IdentifierInfo *Id, SourceLocation AtLocation,
769 SourceLocation LParenLocation,
772 PropertyControl propControl = None);
774 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
776 SourceLocation getAtLoc() const { return AtLoc; }
777 void setAtLoc(SourceLocation L) { AtLoc = L; }
779 SourceLocation getLParenLoc() const { return LParenLoc; }
780 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
782 TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
784 QualType getType() const { return DeclType; }
786 void setType(QualType T, TypeSourceInfo *TSI) {
788 DeclTypeSourceInfo = TSI;
791 /// Retrieve the type when this property is used with a specific base object
793 QualType getUsageType(QualType objectType) const;
795 PropertyAttributeKind getPropertyAttributes() const {
796 return PropertyAttributeKind(PropertyAttributes);
798 void setPropertyAttributes(PropertyAttributeKind PRVal) {
799 PropertyAttributes |= PRVal;
801 void overwritePropertyAttributes(unsigned PRVal) {
802 PropertyAttributes = PRVal;
805 PropertyAttributeKind getPropertyAttributesAsWritten() const {
806 return PropertyAttributeKind(PropertyAttributesAsWritten);
809 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
810 PropertyAttributesAsWritten = PRVal;
813 // Helper methods for accessing attributes.
815 /// isReadOnly - Return true iff the property has a setter.
816 bool isReadOnly() const {
817 return (PropertyAttributes & OBJC_PR_readonly);
820 /// isAtomic - Return true if the property is atomic.
821 bool isAtomic() const {
822 return (PropertyAttributes & OBJC_PR_atomic);
825 /// isRetaining - Return true if the property retains its value.
826 bool isRetaining() const {
827 return (PropertyAttributes &
828 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
831 bool isInstanceProperty() const { return !isClassProperty(); }
832 bool isClassProperty() const { return PropertyAttributes & OBJC_PR_class; }
833 ObjCPropertyQueryKind getQueryKind() const {
834 return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
835 ObjCPropertyQueryKind::OBJC_PR_query_instance;
837 static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
838 return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
839 ObjCPropertyQueryKind::OBJC_PR_query_instance;
842 /// getSetterKind - Return the method used for doing assignment in
843 /// the property setter. This is only valid if the property has been
844 /// defined to have a setter.
845 SetterKind getSetterKind() const {
846 if (PropertyAttributes & OBJC_PR_strong)
847 return getType()->isBlockPointerType() ? Copy : Retain;
848 if (PropertyAttributes & OBJC_PR_retain)
850 if (PropertyAttributes & OBJC_PR_copy)
852 if (PropertyAttributes & OBJC_PR_weak)
857 Selector getGetterName() const { return GetterName; }
858 void setGetterName(Selector Sel) { GetterName = Sel; }
860 Selector getSetterName() const { return SetterName; }
861 void setSetterName(Selector Sel) { SetterName = Sel; }
863 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
864 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
866 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
867 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
869 // Related to \@optional/\@required declared in \@protocol
870 void setPropertyImplementation(PropertyControl pc) {
871 PropertyImplementation = pc;
873 PropertyControl getPropertyImplementation() const {
874 return PropertyControl(PropertyImplementation);
876 bool isOptional() const {
877 return getPropertyImplementation() == PropertyControl::Optional;
880 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
881 PropertyIvarDecl = Ivar;
883 ObjCIvarDecl *getPropertyIvarDecl() const {
884 return PropertyIvarDecl;
887 SourceRange getSourceRange() const override LLVM_READONLY {
888 return SourceRange(AtLoc, getLocation());
891 /// Get the default name of the synthesized ivar.
892 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
894 /// Lookup a property by name in the specified DeclContext.
895 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
896 const IdentifierInfo *propertyID,
897 ObjCPropertyQueryKind queryKind);
899 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
900 static bool classofKind(Kind K) { return K == ObjCProperty; }
903 /// ObjCContainerDecl - Represents a container for method declarations.
904 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
905 /// ObjCProtocolDecl, and ObjCImplDecl.
907 class ObjCContainerDecl : public NamedDecl, public DeclContext {
908 void anchor() override;
910 SourceLocation AtStart;
912 // These two locations in the range mark the end of the method container.
913 // The first points to the '@' token, and the second to the 'end' token.
917 ObjCContainerDecl(Kind DK, DeclContext *DC,
918 IdentifierInfo *Id, SourceLocation nameLoc,
919 SourceLocation atStartLoc)
920 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
922 // Iterator access to instance/class properties.
923 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
924 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>
927 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
928 prop_iterator prop_begin() const {
929 return prop_iterator(decls_begin());
931 prop_iterator prop_end() const {
932 return prop_iterator(decls_end());
935 typedef filtered_decl_iterator<ObjCPropertyDecl,
936 &ObjCPropertyDecl::isInstanceProperty>
938 typedef llvm::iterator_range<instprop_iterator> instprop_range;
940 instprop_range instance_properties() const {
941 return instprop_range(instprop_begin(), instprop_end());
943 instprop_iterator instprop_begin() const {
944 return instprop_iterator(decls_begin());
946 instprop_iterator instprop_end() const {
947 return instprop_iterator(decls_end());
950 typedef filtered_decl_iterator<ObjCPropertyDecl,
951 &ObjCPropertyDecl::isClassProperty>
953 typedef llvm::iterator_range<classprop_iterator> classprop_range;
955 classprop_range class_properties() const {
956 return classprop_range(classprop_begin(), classprop_end());
958 classprop_iterator classprop_begin() const {
959 return classprop_iterator(decls_begin());
961 classprop_iterator classprop_end() const {
962 return classprop_iterator(decls_end());
965 // Iterator access to instance/class methods.
966 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
967 typedef llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>
970 method_range methods() const {
971 return method_range(meth_begin(), meth_end());
973 method_iterator meth_begin() const {
974 return method_iterator(decls_begin());
976 method_iterator meth_end() const {
977 return method_iterator(decls_end());
980 typedef filtered_decl_iterator<ObjCMethodDecl,
981 &ObjCMethodDecl::isInstanceMethod>
983 typedef llvm::iterator_range<instmeth_iterator> instmeth_range;
985 instmeth_range instance_methods() const {
986 return instmeth_range(instmeth_begin(), instmeth_end());
988 instmeth_iterator instmeth_begin() const {
989 return instmeth_iterator(decls_begin());
991 instmeth_iterator instmeth_end() const {
992 return instmeth_iterator(decls_end());
995 typedef filtered_decl_iterator<ObjCMethodDecl,
996 &ObjCMethodDecl::isClassMethod>
998 typedef llvm::iterator_range<classmeth_iterator> classmeth_range;
1000 classmeth_range class_methods() const {
1001 return classmeth_range(classmeth_begin(), classmeth_end());
1003 classmeth_iterator classmeth_begin() const {
1004 return classmeth_iterator(decls_begin());
1006 classmeth_iterator classmeth_end() const {
1007 return classmeth_iterator(decls_end());
1010 // Get the local instance/class method declared in this interface.
1011 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1012 bool AllowHidden = false) const;
1013 ObjCMethodDecl *getInstanceMethod(Selector Sel,
1014 bool AllowHidden = false) const {
1015 return getMethod(Sel, true/*isInstance*/, AllowHidden);
1017 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1018 return getMethod(Sel, false/*isInstance*/, AllowHidden);
1020 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1021 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1024 FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1025 ObjCPropertyQueryKind QueryKind) const;
1027 typedef llvm::DenseMap<std::pair<IdentifierInfo*,
1028 unsigned/*isClassProperty*/>,
1029 ObjCPropertyDecl*> PropertyMap;
1031 typedef llvm::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*>
1032 ProtocolPropertyMap;
1034 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
1036 /// This routine collects list of properties to be implemented in the class.
1037 /// This includes, class's and its conforming protocols' properties.
1038 /// Note, the superclass's properties are not included in the list.
1039 virtual void collectPropertiesToImplement(PropertyMap &PM,
1040 PropertyDeclOrder &PO) const {}
1042 SourceLocation getAtStartLoc() const { return AtStart; }
1043 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
1045 // Marks the end of the container.
1046 SourceRange getAtEndRange() const {
1049 void setAtEndRange(SourceRange atEnd) {
1053 SourceRange getSourceRange() const override LLVM_READONLY {
1054 return SourceRange(AtStart, getAtEndRange().getEnd());
1057 // Implement isa/cast/dyncast/etc.
1058 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1059 static bool classofKind(Kind K) {
1060 return K >= firstObjCContainer &&
1061 K <= lastObjCContainer;
1064 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1065 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1067 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1068 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1072 /// \brief Represents an ObjC class declaration.
1077 /// // MostPrimitive declares no super class (not particularly useful).
1078 /// \@interface MostPrimitive
1079 /// // no instance variables or methods.
1082 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1083 /// \@interface NSResponder : NSObject \<NSCoding>
1084 /// { // instance variables are represented by ObjCIvarDecl.
1085 /// id nextResponder; // nextResponder instance variable.
1087 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1088 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1089 /// \@end // to an NSEvent.
1092 /// Unlike C/C++, forward class declarations are accomplished with \@class.
1093 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
1094 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1095 /// typically inherit from NSObject (an exception is NSProxy).
1097 class ObjCInterfaceDecl : public ObjCContainerDecl
1098 , public Redeclarable<ObjCInterfaceDecl> {
1099 void anchor() override;
1101 /// TypeForDecl - This indicates the Type object that represents this
1102 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
1103 mutable const Type *TypeForDecl;
1104 friend class ASTContext;
1106 struct DefinitionData {
1107 /// \brief The definition of this class, for quick access from any
1109 ObjCInterfaceDecl *Definition;
1111 /// When non-null, this is always an ObjCObjectType.
1112 TypeSourceInfo *SuperClassTInfo;
1114 /// Protocols referenced in the \@interface declaration
1115 ObjCProtocolList ReferencedProtocols;
1117 /// Protocols reference in both the \@interface and class extensions.
1118 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1120 /// \brief List of categories and class extensions defined for this class.
1122 /// Categories are stored as a linked list in the AST, since the categories
1123 /// and class extensions come long after the initial interface declaration,
1124 /// and we avoid dynamically-resized arrays in the AST wherever possible.
1125 ObjCCategoryDecl *CategoryList;
1127 /// IvarList - List of all ivars defined by this class; including class
1128 /// extensions and implementation. This list is built lazily.
1129 ObjCIvarDecl *IvarList;
1131 /// \brief Indicates that the contents of this Objective-C class will be
1132 /// completed by the external AST source when required.
1133 mutable unsigned ExternallyCompleted : 1;
1135 /// \brief Indicates that the ivar cache does not yet include ivars
1136 /// declared in the implementation.
1137 mutable unsigned IvarListMissingImplementation : 1;
1139 /// Indicates that this interface decl contains at least one initializer
1140 /// marked with the 'objc_designated_initializer' attribute.
1141 unsigned HasDesignatedInitializers : 1;
1143 enum InheritedDesignatedInitializersState {
1144 /// We didn't calculate whether the designated initializers should be
1145 /// inherited or not.
1147 /// Designated initializers are inherited for the super class.
1149 /// The class does not inherit designated initializers.
1150 IDI_NotInherited = 2
1152 /// One of the \c InheritedDesignatedInitializersState enumeratos.
1153 mutable unsigned InheritedDesignatedInitializers : 2;
1155 /// \brief The location of the last location in this declaration, before
1156 /// the properties/methods. For example, this will be the '>', '}', or
1158 SourceLocation EndLoc;
1160 DefinitionData() : Definition(), SuperClassTInfo(), CategoryList(), IvarList(),
1161 ExternallyCompleted(),
1162 IvarListMissingImplementation(true),
1163 HasDesignatedInitializers(),
1164 InheritedDesignatedInitializers(IDI_Unknown) { }
1167 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1168 IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1169 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1172 void LoadExternalDefinition() const;
1174 /// The type parameters associated with this class, if any.
1175 ObjCTypeParamList *TypeParamList;
1177 /// \brief Contains a pointer to the data associated with this class,
1178 /// which will be NULL if this class has not yet been defined.
1180 /// The bit indicates when we don't need to check for out-of-date
1181 /// declarations. It will be set unless modules are enabled.
1182 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1184 DefinitionData &data() const {
1185 assert(Data.getPointer() && "Declaration has no definition!");
1186 return *Data.getPointer();
1189 /// \brief Allocate the definition data for this class.
1190 void allocateDefinitionData();
1192 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
1193 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1194 return getNextRedeclaration();
1196 ObjCInterfaceDecl *getPreviousDeclImpl() override {
1197 return getPreviousDecl();
1199 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1200 return getMostRecentDecl();
1204 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1205 SourceLocation atLoc,
1207 ObjCTypeParamList *typeParamList,
1208 ObjCInterfaceDecl *PrevDecl,
1209 SourceLocation ClassLoc = SourceLocation(),
1210 bool isInternal = false);
1212 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1214 /// Retrieve the type parameters of this class.
1216 /// This function looks for a type parameter list for the given
1217 /// class; if the class has been declared (with \c \@class) but not
1218 /// defined (with \c \@interface), it will search for a declaration that
1219 /// has type parameters, skipping any declarations that do not.
1220 ObjCTypeParamList *getTypeParamList() const;
1222 /// Set the type parameters of this class.
1224 /// This function is used by the AST importer, which must import the type
1225 /// parameters after creating their DeclContext to avoid loops.
1226 void setTypeParamList(ObjCTypeParamList *TPL);
1228 /// Retrieve the type parameters written on this particular declaration of
1230 ObjCTypeParamList *getTypeParamListAsWritten() const {
1231 return TypeParamList;
1234 SourceRange getSourceRange() const override LLVM_READONLY {
1235 if (isThisDeclarationADefinition())
1236 return ObjCContainerDecl::getSourceRange();
1238 return SourceRange(getAtStartLoc(), getLocation());
1241 /// \brief Indicate that this Objective-C class is complete, but that
1242 /// the external AST source will be responsible for filling in its contents
1243 /// when a complete class is required.
1244 void setExternallyCompleted();
1246 /// Indicate that this interface decl contains at least one initializer
1247 /// marked with the 'objc_designated_initializer' attribute.
1248 void setHasDesignatedInitializers();
1250 /// Returns true if this interface decl contains at least one initializer
1251 /// marked with the 'objc_designated_initializer' attribute.
1252 bool hasDesignatedInitializers() const;
1254 /// Returns true if this interface decl declares a designated initializer
1255 /// or it inherites one from its super class.
1256 bool declaresOrInheritsDesignatedInitializers() const {
1257 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1260 const ObjCProtocolList &getReferencedProtocols() const {
1261 assert(hasDefinition() && "Caller did not check for forward reference!");
1262 if (data().ExternallyCompleted)
1263 LoadExternalDefinition();
1265 return data().ReferencedProtocols;
1268 ObjCImplementationDecl *getImplementation() const;
1269 void setImplementation(ObjCImplementationDecl *ImplD);
1271 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1273 // Get the local instance/class method declared in a category.
1274 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1275 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1276 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1277 return isInstance ? getCategoryInstanceMethod(Sel)
1278 : getCategoryClassMethod(Sel);
1281 typedef ObjCProtocolList::iterator protocol_iterator;
1282 typedef llvm::iterator_range<protocol_iterator> protocol_range;
1284 protocol_range protocols() const {
1285 return protocol_range(protocol_begin(), protocol_end());
1287 protocol_iterator protocol_begin() const {
1288 // FIXME: Should make sure no callers ever do this.
1289 if (!hasDefinition())
1290 return protocol_iterator();
1292 if (data().ExternallyCompleted)
1293 LoadExternalDefinition();
1295 return data().ReferencedProtocols.begin();
1297 protocol_iterator protocol_end() const {
1298 // FIXME: Should make sure no callers ever do this.
1299 if (!hasDefinition())
1300 return protocol_iterator();
1302 if (data().ExternallyCompleted)
1303 LoadExternalDefinition();
1305 return data().ReferencedProtocols.end();
1308 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1309 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
1311 protocol_loc_range protocol_locs() const {
1312 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1314 protocol_loc_iterator protocol_loc_begin() const {
1315 // FIXME: Should make sure no callers ever do this.
1316 if (!hasDefinition())
1317 return protocol_loc_iterator();
1319 if (data().ExternallyCompleted)
1320 LoadExternalDefinition();
1322 return data().ReferencedProtocols.loc_begin();
1325 protocol_loc_iterator protocol_loc_end() const {
1326 // FIXME: Should make sure no callers ever do this.
1327 if (!hasDefinition())
1328 return protocol_loc_iterator();
1330 if (data().ExternallyCompleted)
1331 LoadExternalDefinition();
1333 return data().ReferencedProtocols.loc_end();
1336 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
1337 typedef llvm::iterator_range<all_protocol_iterator> all_protocol_range;
1339 all_protocol_range all_referenced_protocols() const {
1340 return all_protocol_range(all_referenced_protocol_begin(),
1341 all_referenced_protocol_end());
1343 all_protocol_iterator all_referenced_protocol_begin() const {
1344 // FIXME: Should make sure no callers ever do this.
1345 if (!hasDefinition())
1346 return all_protocol_iterator();
1348 if (data().ExternallyCompleted)
1349 LoadExternalDefinition();
1351 return data().AllReferencedProtocols.empty()
1353 : data().AllReferencedProtocols.begin();
1355 all_protocol_iterator all_referenced_protocol_end() const {
1356 // FIXME: Should make sure no callers ever do this.
1357 if (!hasDefinition())
1358 return all_protocol_iterator();
1360 if (data().ExternallyCompleted)
1361 LoadExternalDefinition();
1363 return data().AllReferencedProtocols.empty()
1365 : data().AllReferencedProtocols.end();
1368 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1369 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
1371 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1372 ivar_iterator ivar_begin() const {
1373 if (const ObjCInterfaceDecl *Def = getDefinition())
1374 return ivar_iterator(Def->decls_begin());
1376 // FIXME: Should make sure no callers ever do this.
1377 return ivar_iterator();
1379 ivar_iterator ivar_end() const {
1380 if (const ObjCInterfaceDecl *Def = getDefinition())
1381 return ivar_iterator(Def->decls_end());
1383 // FIXME: Should make sure no callers ever do this.
1384 return ivar_iterator();
1387 unsigned ivar_size() const {
1388 return std::distance(ivar_begin(), ivar_end());
1391 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1393 ObjCIvarDecl *all_declared_ivar_begin();
1394 const ObjCIvarDecl *all_declared_ivar_begin() const {
1395 // Even though this modifies IvarList, it's conceptually const:
1396 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1397 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1399 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1401 /// setProtocolList - Set the list of protocols that this interface
1403 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1404 const SourceLocation *Locs, ASTContext &C) {
1405 data().ReferencedProtocols.set(List, Num, Locs, C);
1408 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1409 /// into the protocol list for this class.
1410 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1414 /// Produce a name to be used for class's metadata. It comes either via
1415 /// objc_runtime_name attribute or class name.
1416 StringRef getObjCRuntimeNameAsString() const;
1418 /// Returns the designated initializers for the interface.
1420 /// If this declaration does not have methods marked as designated
1421 /// initializers then the interface inherits the designated initializers of
1422 /// its super class.
1423 void getDesignatedInitializers(
1424 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1426 /// Returns true if the given selector is a designated initializer for the
1429 /// If this declaration does not have methods marked as designated
1430 /// initializers then the interface inherits the designated initializers of
1431 /// its super class.
1433 /// \param InitMethod if non-null and the function returns true, it receives
1434 /// the method that was marked as a designated initializer.
1436 isDesignatedInitializer(Selector Sel,
1437 const ObjCMethodDecl **InitMethod = nullptr) const;
1439 /// \brief Determine whether this particular declaration of this class is
1440 /// actually also a definition.
1441 bool isThisDeclarationADefinition() const {
1442 return getDefinition() == this;
1445 /// \brief Determine whether this class has been defined.
1446 bool hasDefinition() const {
1447 // If the name of this class is out-of-date, bring it up-to-date, which
1448 // might bring in a definition.
1449 // Note: a null value indicates that we don't have a definition and that
1450 // modules are enabled.
1451 if (!Data.getOpaqueValue())
1452 getMostRecentDecl();
1454 return Data.getPointer();
1457 /// \brief Retrieve the definition of this class, or NULL if this class
1458 /// has been forward-declared (with \@class) but not yet defined (with
1460 ObjCInterfaceDecl *getDefinition() {
1461 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1464 /// \brief Retrieve the definition of this class, or NULL if this class
1465 /// has been forward-declared (with \@class) but not yet defined (with
1467 const ObjCInterfaceDecl *getDefinition() const {
1468 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1471 /// \brief Starts the definition of this Objective-C class, taking it from
1472 /// a forward declaration (\@class) to a definition (\@interface).
1473 void startDefinition();
1475 /// Retrieve the superclass type.
1476 const ObjCObjectType *getSuperClassType() const {
1477 if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1478 return TInfo->getType()->castAs<ObjCObjectType>();
1483 // Retrieve the type source information for the superclass.
1484 TypeSourceInfo *getSuperClassTInfo() const {
1485 // FIXME: Should make sure no callers ever do this.
1486 if (!hasDefinition())
1489 if (data().ExternallyCompleted)
1490 LoadExternalDefinition();
1492 return data().SuperClassTInfo;
1495 // Retrieve the declaration for the superclass of this class, which
1496 // does not include any type arguments that apply to the superclass.
1497 ObjCInterfaceDecl *getSuperClass() const;
1499 void setSuperClass(TypeSourceInfo *superClass) {
1500 data().SuperClassTInfo = superClass;
1503 /// \brief Iterator that walks over the list of categories, filtering out
1504 /// those that do not meet specific criteria.
1506 /// This class template is used for the various permutations of category
1507 /// and extension iterators.
1508 template<bool (*Filter)(ObjCCategoryDecl *)>
1509 class filtered_category_iterator {
1510 ObjCCategoryDecl *Current;
1512 void findAcceptableCategory();
1515 typedef ObjCCategoryDecl * value_type;
1516 typedef value_type reference;
1517 typedef value_type pointer;
1518 typedef std::ptrdiff_t difference_type;
1519 typedef std::input_iterator_tag iterator_category;
1521 filtered_category_iterator() : Current(nullptr) { }
1522 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1525 findAcceptableCategory();
1528 reference operator*() const { return Current; }
1529 pointer operator->() const { return Current; }
1531 filtered_category_iterator &operator++();
1533 filtered_category_iterator operator++(int) {
1534 filtered_category_iterator Tmp = *this;
1539 friend bool operator==(filtered_category_iterator X,
1540 filtered_category_iterator Y) {
1541 return X.Current == Y.Current;
1544 friend bool operator!=(filtered_category_iterator X,
1545 filtered_category_iterator Y) {
1546 return X.Current != Y.Current;
1551 /// \brief Test whether the given category is visible.
1553 /// Used in the \c visible_categories_iterator.
1554 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1557 /// \brief Iterator that walks over the list of categories and extensions
1558 /// that are visible, i.e., not hidden in a non-imported submodule.
1559 typedef filtered_category_iterator<isVisibleCategory>
1560 visible_categories_iterator;
1562 typedef llvm::iterator_range<visible_categories_iterator>
1563 visible_categories_range;
1565 visible_categories_range visible_categories() const {
1566 return visible_categories_range(visible_categories_begin(),
1567 visible_categories_end());
1570 /// \brief Retrieve an iterator to the beginning of the visible-categories
1572 visible_categories_iterator visible_categories_begin() const {
1573 return visible_categories_iterator(getCategoryListRaw());
1576 /// \brief Retrieve an iterator to the end of the visible-categories list.
1577 visible_categories_iterator visible_categories_end() const {
1578 return visible_categories_iterator();
1581 /// \brief Determine whether the visible-categories list is empty.
1582 bool visible_categories_empty() const {
1583 return visible_categories_begin() == visible_categories_end();
1587 /// \brief Test whether the given category... is a category.
1589 /// Used in the \c known_categories_iterator.
1590 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1593 /// \brief Iterator that walks over all of the known categories and
1594 /// extensions, including those that are hidden.
1595 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1596 typedef llvm::iterator_range<known_categories_iterator>
1597 known_categories_range;
1599 known_categories_range known_categories() const {
1600 return known_categories_range(known_categories_begin(),
1601 known_categories_end());
1604 /// \brief Retrieve an iterator to the beginning of the known-categories
1606 known_categories_iterator known_categories_begin() const {
1607 return known_categories_iterator(getCategoryListRaw());
1610 /// \brief Retrieve an iterator to the end of the known-categories list.
1611 known_categories_iterator known_categories_end() const {
1612 return known_categories_iterator();
1615 /// \brief Determine whether the known-categories list is empty.
1616 bool known_categories_empty() const {
1617 return known_categories_begin() == known_categories_end();
1621 /// \brief Test whether the given category is a visible extension.
1623 /// Used in the \c visible_extensions_iterator.
1624 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1627 /// \brief Iterator that walks over all of the visible extensions, skipping
1628 /// any that are known but hidden.
1629 typedef filtered_category_iterator<isVisibleExtension>
1630 visible_extensions_iterator;
1632 typedef llvm::iterator_range<visible_extensions_iterator>
1633 visible_extensions_range;
1635 visible_extensions_range visible_extensions() const {
1636 return visible_extensions_range(visible_extensions_begin(),
1637 visible_extensions_end());
1640 /// \brief Retrieve an iterator to the beginning of the visible-extensions
1642 visible_extensions_iterator visible_extensions_begin() const {
1643 return visible_extensions_iterator(getCategoryListRaw());
1646 /// \brief Retrieve an iterator to the end of the visible-extensions list.
1647 visible_extensions_iterator visible_extensions_end() const {
1648 return visible_extensions_iterator();
1651 /// \brief Determine whether the visible-extensions list is empty.
1652 bool visible_extensions_empty() const {
1653 return visible_extensions_begin() == visible_extensions_end();
1657 /// \brief Test whether the given category is an extension.
1659 /// Used in the \c known_extensions_iterator.
1660 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1663 /// \brief Iterator that walks over all of the known extensions.
1664 typedef filtered_category_iterator<isKnownExtension>
1665 known_extensions_iterator;
1666 typedef llvm::iterator_range<known_extensions_iterator>
1667 known_extensions_range;
1669 known_extensions_range known_extensions() const {
1670 return known_extensions_range(known_extensions_begin(),
1671 known_extensions_end());
1674 /// \brief Retrieve an iterator to the beginning of the known-extensions
1676 known_extensions_iterator known_extensions_begin() const {
1677 return known_extensions_iterator(getCategoryListRaw());
1680 /// \brief Retrieve an iterator to the end of the known-extensions list.
1681 known_extensions_iterator known_extensions_end() const {
1682 return known_extensions_iterator();
1685 /// \brief Determine whether the known-extensions list is empty.
1686 bool known_extensions_empty() const {
1687 return known_extensions_begin() == known_extensions_end();
1690 /// \brief Retrieve the raw pointer to the start of the category/extension
1692 ObjCCategoryDecl* getCategoryListRaw() const {
1693 // FIXME: Should make sure no callers ever do this.
1694 if (!hasDefinition())
1697 if (data().ExternallyCompleted)
1698 LoadExternalDefinition();
1700 return data().CategoryList;
1703 /// \brief Set the raw pointer to the start of the category/extension
1705 void setCategoryListRaw(ObjCCategoryDecl *category) {
1706 data().CategoryList = category;
1710 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1711 ObjCPropertyQueryKind QueryKind) const;
1713 void collectPropertiesToImplement(PropertyMap &PM,
1714 PropertyDeclOrder &PO) const override;
1716 /// isSuperClassOf - Return true if this class is the specified class or is a
1717 /// super class of the specified interface class.
1718 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1719 // If RHS is derived from LHS it is OK; else it is not OK.
1720 while (I != nullptr) {
1721 if (declaresSameEntity(this, I))
1724 I = I->getSuperClass();
1729 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1730 /// to be incompatible with __weak references. Returns true if it is.
1731 bool isArcWeakrefUnavailable() const;
1733 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1734 /// classes must not be auto-synthesized. Returns class decl. if it must not
1735 /// be; 0, otherwise.
1736 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1738 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1739 ObjCInterfaceDecl *&ClassDeclared);
1740 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1741 ObjCInterfaceDecl *ClassDeclared;
1742 return lookupInstanceVariable(IVarName, ClassDeclared);
1745 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1747 // Lookup a method. First, we search locally. If a method isn't
1748 // found, we search referenced protocols and class categories.
1749 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1750 bool shallowCategoryLookup = false,
1751 bool followSuper = true,
1752 const ObjCCategoryDecl *C = nullptr) const;
1754 /// Lookup an instance method for a given selector.
1755 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1756 return lookupMethod(Sel, true/*isInstance*/);
1759 /// Lookup a class method for a given selector.
1760 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1761 return lookupMethod(Sel, false/*isInstance*/);
1763 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1765 /// \brief Lookup a method in the classes implementation hierarchy.
1766 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1767 bool Instance=true) const;
1769 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1770 return lookupPrivateMethod(Sel, false);
1773 /// \brief Lookup a setter or getter in the class hierarchy,
1774 /// including in all categories except for category passed
1776 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1777 const ObjCCategoryDecl *Cat,
1778 bool IsClassProperty) const {
1779 return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1780 false/*shallowCategoryLookup*/,
1781 true /* followsSuper */,
1785 SourceLocation getEndOfDefinitionLoc() const {
1786 if (!hasDefinition())
1787 return getLocation();
1789 return data().EndLoc;
1792 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1794 /// Retrieve the starting location of the superclass.
1795 SourceLocation getSuperClassLoc() const;
1797 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1798 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1799 /// declaration without an \@interface declaration.
1800 bool isImplicitInterfaceDecl() const {
1801 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1804 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1805 /// has been implemented in IDecl class, its super class or categories (if
1806 /// lookupCategory is true).
1807 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1808 bool lookupCategory,
1809 bool RHSIsQualifiedID = false);
1811 typedef redeclarable_base::redecl_range redecl_range;
1812 typedef redeclarable_base::redecl_iterator redecl_iterator;
1813 using redeclarable_base::redecls_begin;
1814 using redeclarable_base::redecls_end;
1815 using redeclarable_base::redecls;
1816 using redeclarable_base::getPreviousDecl;
1817 using redeclarable_base::getMostRecentDecl;
1818 using redeclarable_base::isFirstDecl;
1820 /// Retrieves the canonical declaration of this Objective-C class.
1821 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1822 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1824 // Low-level accessor
1825 const Type *getTypeForDecl() const { return TypeForDecl; }
1826 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1828 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1829 static bool classofKind(Kind K) { return K == ObjCInterface; }
1831 friend class ASTReader;
1832 friend class ASTDeclReader;
1833 friend class ASTDeclWriter;
1836 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1837 bool inheritsDesignatedInitializers() const;
1840 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1841 /// instance variables are identical to C. The only exception is Objective-C
1842 /// supports C++ style access control. For example:
1844 /// \@interface IvarExample : NSObject
1846 /// id defaultToProtected;
1848 /// id canBePublic; // same as C++.
1850 /// id canBeProtected; // same as C++.
1852 /// id canBePackage; // framework visibility (not available in C++).
1855 class ObjCIvarDecl : public FieldDecl {
1856 void anchor() override;
1859 enum AccessControl {
1860 None, Private, Protected, Public, Package
1864 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1865 SourceLocation IdLoc, IdentifierInfo *Id,
1866 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1868 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1869 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1870 NextIvar(nullptr), DeclAccess(ac), Synthesized(synthesized) {}
1873 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1874 SourceLocation StartLoc, SourceLocation IdLoc,
1875 IdentifierInfo *Id, QualType T,
1876 TypeSourceInfo *TInfo,
1877 AccessControl ac, Expr *BW = nullptr,
1878 bool synthesized=false);
1880 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1882 /// \brief Return the class interface that this ivar is logically contained
1883 /// in; this is either the interface where the ivar was declared, or the
1884 /// interface the ivar is conceptually a part of in the case of synthesized
1886 const ObjCInterfaceDecl *getContainingInterface() const;
1888 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1889 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1890 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1892 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1894 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1896 AccessControl getCanonicalAccessControl() const {
1897 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1900 void setSynthesize(bool synth) { Synthesized = synth; }
1901 bool getSynthesize() const { return Synthesized; }
1903 /// Retrieve the type of this instance variable when viewed as a member of a
1904 /// specific object type.
1905 QualType getUsageType(QualType objectType) const;
1907 // Implement isa/cast/dyncast/etc.
1908 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1909 static bool classofKind(Kind K) { return K == ObjCIvar; }
1911 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1912 /// extensions and class's implementation
1913 ObjCIvarDecl *NextIvar;
1915 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1916 unsigned DeclAccess : 3;
1917 unsigned Synthesized : 1;
1921 /// \brief Represents a field declaration created by an \@defs(...).
1922 class ObjCAtDefsFieldDecl : public FieldDecl {
1923 void anchor() override;
1924 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1925 SourceLocation IdLoc, IdentifierInfo *Id,
1926 QualType T, Expr *BW)
1927 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1928 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
1929 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1932 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1933 SourceLocation StartLoc,
1934 SourceLocation IdLoc, IdentifierInfo *Id,
1935 QualType T, Expr *BW);
1937 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1939 // Implement isa/cast/dyncast/etc.
1940 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1941 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1944 /// \brief Represents an Objective-C protocol declaration.
1946 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1947 /// variables are permitted). Protocols originally drew inspiration from
1948 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1949 /// syntax:-). Here is an example:
1952 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1953 /// - (NSWindow *)draggingDestinationWindow;
1954 /// - (NSImage *)draggedImage;
1958 /// This says that NSDraggingInfo requires two methods and requires everything
1959 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1963 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1967 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1968 /// protocols are in distinct namespaces. For example, Cocoa defines both
1969 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1970 /// protocols are referenced using angle brackets as follows:
1972 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1974 class ObjCProtocolDecl : public ObjCContainerDecl,
1975 public Redeclarable<ObjCProtocolDecl> {
1976 void anchor() override;
1978 struct DefinitionData {
1979 // \brief The declaration that defines this protocol.
1980 ObjCProtocolDecl *Definition;
1982 /// \brief Referenced protocols
1983 ObjCProtocolList ReferencedProtocols;
1986 /// \brief Contains a pointer to the data associated with this class,
1987 /// which will be NULL if this class has not yet been defined.
1989 /// The bit indicates when we don't need to check for out-of-date
1990 /// declarations. It will be set unless modules are enabled.
1991 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1993 DefinitionData &data() const {
1994 assert(Data.getPointer() && "Objective-C protocol has no definition!");
1995 return *Data.getPointer();
1998 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
1999 SourceLocation nameLoc, SourceLocation atStartLoc,
2000 ObjCProtocolDecl *PrevDecl);
2002 void allocateDefinitionData();
2004 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
2005 ObjCProtocolDecl *getNextRedeclarationImpl() override {
2006 return getNextRedeclaration();
2008 ObjCProtocolDecl *getPreviousDeclImpl() override {
2009 return getPreviousDecl();
2011 ObjCProtocolDecl *getMostRecentDeclImpl() override {
2012 return getMostRecentDecl();
2016 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2018 SourceLocation nameLoc,
2019 SourceLocation atStartLoc,
2020 ObjCProtocolDecl *PrevDecl);
2022 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2024 const ObjCProtocolList &getReferencedProtocols() const {
2025 assert(hasDefinition() && "No definition available!");
2026 return data().ReferencedProtocols;
2028 typedef ObjCProtocolList::iterator protocol_iterator;
2029 typedef llvm::iterator_range<protocol_iterator> protocol_range;
2031 protocol_range protocols() const {
2032 return protocol_range(protocol_begin(), protocol_end());
2034 protocol_iterator protocol_begin() const {
2035 if (!hasDefinition())
2036 return protocol_iterator();
2038 return data().ReferencedProtocols.begin();
2040 protocol_iterator protocol_end() const {
2041 if (!hasDefinition())
2042 return protocol_iterator();
2044 return data().ReferencedProtocols.end();
2046 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
2047 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
2049 protocol_loc_range protocol_locs() const {
2050 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2052 protocol_loc_iterator protocol_loc_begin() const {
2053 if (!hasDefinition())
2054 return protocol_loc_iterator();
2056 return data().ReferencedProtocols.loc_begin();
2058 protocol_loc_iterator protocol_loc_end() const {
2059 if (!hasDefinition())
2060 return protocol_loc_iterator();
2062 return data().ReferencedProtocols.loc_end();
2064 unsigned protocol_size() const {
2065 if (!hasDefinition())
2068 return data().ReferencedProtocols.size();
2071 /// setProtocolList - Set the list of protocols that this interface
2073 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2074 const SourceLocation *Locs, ASTContext &C) {
2075 assert(hasDefinition() && "Protocol is not defined");
2076 data().ReferencedProtocols.set(List, Num, Locs, C);
2079 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2081 // Lookup a method. First, we search locally. If a method isn't
2082 // found, we search referenced protocols and class categories.
2083 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2084 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2085 return lookupMethod(Sel, true/*isInstance*/);
2087 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2088 return lookupMethod(Sel, false/*isInstance*/);
2091 /// \brief Determine whether this protocol has a definition.
2092 bool hasDefinition() const {
2093 // If the name of this protocol is out-of-date, bring it up-to-date, which
2094 // might bring in a definition.
2095 // Note: a null value indicates that we don't have a definition and that
2096 // modules are enabled.
2097 if (!Data.getOpaqueValue())
2098 getMostRecentDecl();
2100 return Data.getPointer();
2103 /// \brief Retrieve the definition of this protocol, if any.
2104 ObjCProtocolDecl *getDefinition() {
2105 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2108 /// \brief Retrieve the definition of this protocol, if any.
2109 const ObjCProtocolDecl *getDefinition() const {
2110 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2113 /// \brief Determine whether this particular declaration is also the
2115 bool isThisDeclarationADefinition() const {
2116 return getDefinition() == this;
2119 /// \brief Starts the definition of this Objective-C protocol.
2120 void startDefinition();
2122 /// Produce a name to be used for protocol's metadata. It comes either via
2123 /// objc_runtime_name attribute or protocol name.
2124 StringRef getObjCRuntimeNameAsString() const;
2126 SourceRange getSourceRange() const override LLVM_READONLY {
2127 if (isThisDeclarationADefinition())
2128 return ObjCContainerDecl::getSourceRange();
2130 return SourceRange(getAtStartLoc(), getLocation());
2133 typedef redeclarable_base::redecl_range redecl_range;
2134 typedef redeclarable_base::redecl_iterator redecl_iterator;
2135 using redeclarable_base::redecls_begin;
2136 using redeclarable_base::redecls_end;
2137 using redeclarable_base::redecls;
2138 using redeclarable_base::getPreviousDecl;
2139 using redeclarable_base::getMostRecentDecl;
2140 using redeclarable_base::isFirstDecl;
2142 /// Retrieves the canonical declaration of this Objective-C protocol.
2143 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2144 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2146 void collectPropertiesToImplement(PropertyMap &PM,
2147 PropertyDeclOrder &PO) const override;
2149 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2150 ProtocolPropertyMap &PM) const;
2152 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2153 static bool classofKind(Kind K) { return K == ObjCProtocol; }
2155 friend class ASTReader;
2156 friend class ASTDeclReader;
2157 friend class ASTDeclWriter;
2160 /// ObjCCategoryDecl - Represents a category declaration. A category allows
2161 /// you to add methods to an existing class (without subclassing or modifying
2162 /// the original class interface or implementation:-). Categories don't allow
2163 /// you to add instance data. The following example adds "myMethod" to all
2164 /// NSView's within a process:
2166 /// \@interface NSView (MyViewMethods)
2170 /// Categories also allow you to split the implementation of a class across
2171 /// several files (a feature more naturally supported in C++).
2173 /// Categories were originally inspired by dynamic languages such as Common
2174 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
2175 /// don't support this level of dynamism, which is both powerful and dangerous.
2177 class ObjCCategoryDecl : public ObjCContainerDecl {
2178 void anchor() override;
2180 /// Interface belonging to this category
2181 ObjCInterfaceDecl *ClassInterface;
2183 /// The type parameters associated with this category, if any.
2184 ObjCTypeParamList *TypeParamList;
2186 /// referenced protocols in this category.
2187 ObjCProtocolList ReferencedProtocols;
2189 /// Next category belonging to this class.
2190 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
2191 ObjCCategoryDecl *NextClassCategory;
2193 /// \brief The location of the category name in this declaration.
2194 SourceLocation CategoryNameLoc;
2196 /// class extension may have private ivars.
2197 SourceLocation IvarLBraceLoc;
2198 SourceLocation IvarRBraceLoc;
2200 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2201 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2202 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2203 ObjCTypeParamList *typeParamList,
2204 SourceLocation IvarLBraceLoc=SourceLocation(),
2205 SourceLocation IvarRBraceLoc=SourceLocation());
2209 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2210 SourceLocation AtLoc,
2211 SourceLocation ClassNameLoc,
2212 SourceLocation CategoryNameLoc,
2214 ObjCInterfaceDecl *IDecl,
2215 ObjCTypeParamList *typeParamList,
2216 SourceLocation IvarLBraceLoc=SourceLocation(),
2217 SourceLocation IvarRBraceLoc=SourceLocation());
2218 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2220 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2221 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2223 /// Retrieve the type parameter list associated with this category or
2225 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2227 /// Set the type parameters of this category.
2229 /// This function is used by the AST importer, which must import the type
2230 /// parameters after creating their DeclContext to avoid loops.
2231 void setTypeParamList(ObjCTypeParamList *TPL);
2234 ObjCCategoryImplDecl *getImplementation() const;
2235 void setImplementation(ObjCCategoryImplDecl *ImplD);
2237 /// setProtocolList - Set the list of protocols that this interface
2239 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2240 const SourceLocation *Locs, ASTContext &C) {
2241 ReferencedProtocols.set(List, Num, Locs, C);
2244 const ObjCProtocolList &getReferencedProtocols() const {
2245 return ReferencedProtocols;
2248 typedef ObjCProtocolList::iterator protocol_iterator;
2249 typedef llvm::iterator_range<protocol_iterator> protocol_range;
2251 protocol_range protocols() const {
2252 return protocol_range(protocol_begin(), protocol_end());
2254 protocol_iterator protocol_begin() const {
2255 return ReferencedProtocols.begin();
2257 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2258 unsigned protocol_size() const { return ReferencedProtocols.size(); }
2259 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
2260 typedef llvm::iterator_range<protocol_loc_iterator> protocol_loc_range;
2262 protocol_loc_range protocol_locs() const {
2263 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2265 protocol_loc_iterator protocol_loc_begin() const {
2266 return ReferencedProtocols.loc_begin();
2268 protocol_loc_iterator protocol_loc_end() const {
2269 return ReferencedProtocols.loc_end();
2272 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2274 /// \brief Retrieve the pointer to the next stored category (or extension),
2275 /// which may be hidden.
2276 ObjCCategoryDecl *getNextClassCategoryRaw() const {
2277 return NextClassCategory;
2280 bool IsClassExtension() const { return getIdentifier() == nullptr; }
2282 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2283 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2285 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2286 ivar_iterator ivar_begin() const {
2287 return ivar_iterator(decls_begin());
2289 ivar_iterator ivar_end() const {
2290 return ivar_iterator(decls_end());
2292 unsigned ivar_size() const {
2293 return std::distance(ivar_begin(), ivar_end());
2295 bool ivar_empty() const {
2296 return ivar_begin() == ivar_end();
2299 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2300 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2302 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2303 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2304 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2305 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2307 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2308 static bool classofKind(Kind K) { return K == ObjCCategory; }
2310 friend class ASTDeclReader;
2311 friend class ASTDeclWriter;
2314 class ObjCImplDecl : public ObjCContainerDecl {
2315 void anchor() override;
2317 /// Class interface for this class/category implementation
2318 ObjCInterfaceDecl *ClassInterface;
2321 ObjCImplDecl(Kind DK, DeclContext *DC,
2322 ObjCInterfaceDecl *classInterface,
2323 SourceLocation nameLoc, SourceLocation atStartLoc)
2324 : ObjCContainerDecl(DK, DC,
2325 classInterface? classInterface->getIdentifier()
2327 nameLoc, atStartLoc),
2328 ClassInterface(classInterface) {}
2331 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2332 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2333 void setClassInterface(ObjCInterfaceDecl *IFace);
2335 void addInstanceMethod(ObjCMethodDecl *method) {
2336 // FIXME: Context should be set correctly before we get here.
2337 method->setLexicalDeclContext(this);
2340 void addClassMethod(ObjCMethodDecl *method) {
2341 // FIXME: Context should be set correctly before we get here.
2342 method->setLexicalDeclContext(this);
2346 void addPropertyImplementation(ObjCPropertyImplDecl *property);
2348 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2349 ObjCPropertyQueryKind queryKind) const;
2350 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2352 // Iterator access to properties.
2353 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
2354 typedef llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>
2357 propimpl_range property_impls() const {
2358 return propimpl_range(propimpl_begin(), propimpl_end());
2360 propimpl_iterator propimpl_begin() const {
2361 return propimpl_iterator(decls_begin());
2363 propimpl_iterator propimpl_end() const {
2364 return propimpl_iterator(decls_end());
2367 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2368 static bool classofKind(Kind K) {
2369 return K >= firstObjCImpl && K <= lastObjCImpl;
2373 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
2374 /// \@implementation declaration. If a category class has declaration of a
2375 /// property, its implementation must be specified in the category's
2376 /// \@implementation declaration. Example:
2377 /// \@interface I \@end
2378 /// \@interface I(CATEGORY)
2379 /// \@property int p1, d1;
2381 /// \@implementation I(CATEGORY)
2382 /// \@dynamic p1,d1;
2385 /// ObjCCategoryImplDecl
2386 class ObjCCategoryImplDecl : public ObjCImplDecl {
2387 void anchor() override;
2392 // Category name location
2393 SourceLocation CategoryNameLoc;
2395 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2396 ObjCInterfaceDecl *classInterface,
2397 SourceLocation nameLoc, SourceLocation atStartLoc,
2398 SourceLocation CategoryNameLoc)
2399 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
2400 Id(Id), CategoryNameLoc(CategoryNameLoc) {}
2402 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2404 ObjCInterfaceDecl *classInterface,
2405 SourceLocation nameLoc,
2406 SourceLocation atStartLoc,
2407 SourceLocation CategoryNameLoc);
2408 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2410 /// getIdentifier - Get the identifier that names the category
2411 /// interface associated with this implementation.
2412 /// FIXME: This is a bad API, we are hiding NamedDecl::getIdentifier()
2413 /// with a different meaning. For example:
2414 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
2415 /// returns the class interface name, whereas
2416 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
2417 /// returns the category name.
2418 IdentifierInfo *getIdentifier() const {
2421 void setIdentifier(IdentifierInfo *II) { Id = II; }
2423 ObjCCategoryDecl *getCategoryDecl() const;
2425 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2427 /// getName - Get the name of identifier for the class interface associated
2428 /// with this implementation as a StringRef.
2430 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2432 StringRef getName() const { return Id ? Id->getName() : StringRef(); }
2434 /// @brief Get the name of the class associated with this interface.
2436 // FIXME: Deprecated, move clients to getName().
2437 std::string getNameAsString() const {
2441 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2442 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2444 friend class ASTDeclReader;
2445 friend class ASTDeclWriter;
2448 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2450 /// ObjCImplementationDecl - Represents a class definition - this is where
2451 /// method definitions are specified. For example:
2454 /// \@implementation MyClass
2455 /// - (void)myMethod { /* do something */ }
2459 /// In a non-fragile runtime, instance variables can appear in the class
2460 /// interface, class extensions (nameless categories), and in the implementation
2461 /// itself, as well as being synthesized as backing storage for properties.
2463 /// In a fragile runtime, instance variables are specified in the class
2464 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2465 /// we allow instance variables to be specified in the implementation. When
2466 /// specified, they need to be \em identical to the interface.
2467 class ObjCImplementationDecl : public ObjCImplDecl {
2468 void anchor() override;
2469 /// Implementation Class's super class.
2470 ObjCInterfaceDecl *SuperClass;
2471 SourceLocation SuperLoc;
2473 /// \@implementation may have private ivars.
2474 SourceLocation IvarLBraceLoc;
2475 SourceLocation IvarRBraceLoc;
2477 /// Support for ivar initialization.
2478 /// \brief The arguments used to initialize the ivars
2479 LazyCXXCtorInitializersPtr IvarInitializers;
2480 unsigned NumIvarInitializers;
2482 /// Do the ivars of this class require initialization other than
2483 /// zero-initialization?
2484 bool HasNonZeroConstructors : 1;
2486 /// Do the ivars of this class require non-trivial destruction?
2487 bool HasDestructors : 1;
2489 ObjCImplementationDecl(DeclContext *DC,
2490 ObjCInterfaceDecl *classInterface,
2491 ObjCInterfaceDecl *superDecl,
2492 SourceLocation nameLoc, SourceLocation atStartLoc,
2493 SourceLocation superLoc = SourceLocation(),
2494 SourceLocation IvarLBraceLoc=SourceLocation(),
2495 SourceLocation IvarRBraceLoc=SourceLocation())
2496 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
2497 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
2498 IvarRBraceLoc(IvarRBraceLoc),
2499 IvarInitializers(nullptr), NumIvarInitializers(0),
2500 HasNonZeroConstructors(false), HasDestructors(false) {}
2502 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2503 ObjCInterfaceDecl *classInterface,
2504 ObjCInterfaceDecl *superDecl,
2505 SourceLocation nameLoc,
2506 SourceLocation atStartLoc,
2507 SourceLocation superLoc = SourceLocation(),
2508 SourceLocation IvarLBraceLoc=SourceLocation(),
2509 SourceLocation IvarRBraceLoc=SourceLocation());
2511 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2513 /// init_iterator - Iterates through the ivar initializer list.
2514 typedef CXXCtorInitializer **init_iterator;
2516 /// init_const_iterator - Iterates through the ivar initializer list.
2517 typedef CXXCtorInitializer * const * init_const_iterator;
2519 typedef llvm::iterator_range<init_iterator> init_range;
2520 typedef llvm::iterator_range<init_const_iterator> init_const_range;
2522 init_range inits() { return init_range(init_begin(), init_end()); }
2523 init_const_range inits() const {
2524 return init_const_range(init_begin(), init_end());
2527 /// init_begin() - Retrieve an iterator to the first initializer.
2528 init_iterator init_begin() {
2529 const auto *ConstThis = this;
2530 return const_cast<init_iterator>(ConstThis->init_begin());
2532 /// begin() - Retrieve an iterator to the first initializer.
2533 init_const_iterator init_begin() const;
2535 /// init_end() - Retrieve an iterator past the last initializer.
2536 init_iterator init_end() {
2537 return init_begin() + NumIvarInitializers;
2539 /// end() - Retrieve an iterator past the last initializer.
2540 init_const_iterator init_end() const {
2541 return init_begin() + NumIvarInitializers;
2543 /// getNumArgs - Number of ivars which must be initialized.
2544 unsigned getNumIvarInitializers() const {
2545 return NumIvarInitializers;
2548 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2549 NumIvarInitializers = numNumIvarInitializers;
2552 void setIvarInitializers(ASTContext &C,
2553 CXXCtorInitializer ** initializers,
2554 unsigned numInitializers);
2556 /// Do any of the ivars of this class (not counting its base classes)
2557 /// require construction other than zero-initialization?
2558 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2559 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2561 /// Do any of the ivars of this class (not counting its base classes)
2562 /// require non-trivial destruction?
2563 bool hasDestructors() const { return HasDestructors; }
2564 void setHasDestructors(bool val) { HasDestructors = val; }
2566 /// getIdentifier - Get the identifier that names the class
2567 /// interface associated with this implementation.
2568 IdentifierInfo *getIdentifier() const {
2569 return getClassInterface()->getIdentifier();
2572 /// getName - Get the name of identifier for the class interface associated
2573 /// with this implementation as a StringRef.
2575 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2577 StringRef getName() const {
2578 assert(getIdentifier() && "Name is not a simple identifier");
2579 return getIdentifier()->getName();
2582 /// @brief Get the name of the class associated with this interface.
2584 // FIXME: Move to StringRef API.
2585 std::string getNameAsString() const {
2589 /// Produce a name to be used for class's metadata. It comes either via
2590 /// class's objc_runtime_name attribute or class name.
2591 StringRef getObjCRuntimeNameAsString() const;
2593 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2594 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2595 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2597 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2599 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2600 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2601 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2602 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2604 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
2605 typedef llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>> ivar_range;
2607 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2608 ivar_iterator ivar_begin() const {
2609 return ivar_iterator(decls_begin());
2611 ivar_iterator ivar_end() const {
2612 return ivar_iterator(decls_end());
2614 unsigned ivar_size() const {
2615 return std::distance(ivar_begin(), ivar_end());
2617 bool ivar_empty() const {
2618 return ivar_begin() == ivar_end();
2621 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2622 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2624 friend class ASTDeclReader;
2625 friend class ASTDeclWriter;
2628 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2630 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2631 /// declared as \@compatibility_alias alias class.
2632 class ObjCCompatibleAliasDecl : public NamedDecl {
2633 void anchor() override;
2634 /// Class that this is an alias of.
2635 ObjCInterfaceDecl *AliasedClass;
2637 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2638 ObjCInterfaceDecl* aliasedClass)
2639 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2641 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2642 SourceLocation L, IdentifierInfo *Id,
2643 ObjCInterfaceDecl* aliasedClass);
2645 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2648 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2649 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2650 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2652 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2653 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2657 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2658 /// in a class or category implementation block. For example:
2659 /// \@synthesize prop1 = ivar1;
2661 class ObjCPropertyImplDecl : public Decl {
2668 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2670 /// \brief For \@synthesize, the location of the ivar, if it was written in
2671 /// the source code.
2674 /// \@synthesize int a = b
2676 SourceLocation IvarLoc;
2678 /// Property declaration being implemented
2679 ObjCPropertyDecl *PropertyDecl;
2681 /// Null for \@dynamic. Required for \@synthesize.
2682 ObjCIvarDecl *PropertyIvarDecl;
2684 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2686 Expr *GetterCXXConstructor;
2688 /// Null for \@dynamic. Non-null if property has assignment operator to call
2689 /// in Setter synthesis.
2690 Expr *SetterCXXAssignment;
2692 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2693 ObjCPropertyDecl *property,
2695 ObjCIvarDecl *ivarDecl,
2696 SourceLocation ivarLoc)
2697 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2698 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2699 GetterCXXConstructor(nullptr), SetterCXXAssignment(nullptr) {
2700 assert (PK == Dynamic || PropertyIvarDecl);
2704 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2705 SourceLocation atLoc, SourceLocation L,
2706 ObjCPropertyDecl *property,
2708 ObjCIvarDecl *ivarDecl,
2709 SourceLocation ivarLoc);
2711 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2713 SourceRange getSourceRange() const override LLVM_READONLY;
2715 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2716 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2718 ObjCPropertyDecl *getPropertyDecl() const {
2719 return PropertyDecl;
2721 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2723 Kind getPropertyImplementation() const {
2724 return PropertyIvarDecl ? Synthesize : Dynamic;
2727 ObjCIvarDecl *getPropertyIvarDecl() const {
2728 return PropertyIvarDecl;
2730 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2732 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2733 SourceLocation IvarLoc) {
2734 PropertyIvarDecl = Ivar;
2735 this->IvarLoc = IvarLoc;
2738 /// \brief For \@synthesize, returns true if an ivar name was explicitly
2742 /// \@synthesize int a = b; // true
2743 /// \@synthesize int a; // false
2745 bool isIvarNameSpecified() const {
2746 return IvarLoc.isValid() && IvarLoc != getLocation();
2749 Expr *getGetterCXXConstructor() const {
2750 return GetterCXXConstructor;
2752 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2753 GetterCXXConstructor = getterCXXConstructor;
2756 Expr *getSetterCXXAssignment() const {
2757 return SetterCXXAssignment;
2759 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2760 SetterCXXAssignment = setterCXXAssignment;
2763 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2764 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2766 friend class ASTDeclReader;
2769 template<bool (*Filter)(ObjCCategoryDecl *)>
2771 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2772 findAcceptableCategory() {
2773 while (Current && !Filter(Current))
2774 Current = Current->getNextClassCategoryRaw();
2777 template<bool (*Filter)(ObjCCategoryDecl *)>
2778 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2779 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2780 Current = Current->getNextClassCategoryRaw();
2781 findAcceptableCategory();
2785 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2786 return !Cat->isHidden();
2789 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2790 return Cat->IsClassExtension() && !Cat->isHidden();
2793 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2794 return Cat->IsClassExtension();
2797 } // end namespace clang