1 //===- DeclObjC.h - Classes for representing declarations -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the DeclObjC interface and subclasses.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_DECLOBJC_H
14 #define LLVM_CLANG_AST_DECLOBJC_H
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/ExternalASTSource.h"
19 #include "clang/AST/Redeclarable.h"
20 #include "clang/AST/SelectorLocationsKind.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/IdentifierTable.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/Basic/SourceLocation.h"
25 #include "clang/Basic/Specifiers.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/None.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/ADT/iterator_range.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/TrailingObjects.h"
47 class CXXCtorInitializer;
49 class ObjCCategoryDecl;
50 class ObjCCategoryImplDecl;
51 class ObjCImplementationDecl;
52 class ObjCInterfaceDecl;
54 class ObjCPropertyDecl;
55 class ObjCPropertyImplDecl;
56 class ObjCProtocolDecl;
61 /// List is an array of pointers to objects that are not owned by this object.
62 void **List = nullptr;
66 ObjCListBase() = default;
67 ObjCListBase(const ObjCListBase &) = delete;
68 ObjCListBase &operator=(const ObjCListBase &) = delete;
70 unsigned size() const { return NumElts; }
71 bool empty() const { return NumElts == 0; }
74 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
77 /// ObjCList - This is a simple template class used to hold various lists of
78 /// decls etc, which is heavily used by the ObjC front-end. This only use case
79 /// this supports is setting the list all at once and then reading elements out
82 class ObjCList : public ObjCListBase {
84 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
85 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
88 using iterator = T* const *;
90 iterator begin() const { return (iterator)List; }
91 iterator end() const { return (iterator)List+NumElts; }
93 T* operator[](unsigned Idx) const {
94 assert(Idx < NumElts && "Invalid access");
99 /// A list of Objective-C protocols, along with the source
100 /// locations at which they were referenced.
101 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
102 SourceLocation *Locations = nullptr;
104 using ObjCList<ObjCProtocolDecl>::set;
107 ObjCProtocolList() = default;
109 using loc_iterator = const SourceLocation *;
111 loc_iterator loc_begin() const { return Locations; }
112 loc_iterator loc_end() const { return Locations + size(); }
114 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
115 const SourceLocation *Locs, ASTContext &Ctx);
118 /// ObjCMethodDecl - Represents an instance or class method declaration.
119 /// ObjC methods can be declared within 4 contexts: class interfaces,
120 /// categories, protocols, and class implementations. While C++ member
121 /// functions leverage C syntax, Objective-C method syntax is modeled after
122 /// Smalltalk (using colons to specify argument types/expressions).
123 /// Here are some brief examples:
125 /// Setter/getter instance methods:
126 /// - (void)setMenu:(NSMenu *)menu;
127 /// - (NSMenu *)menu;
129 /// Instance method that takes 2 NSView arguments:
130 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
132 /// Getter class method:
133 /// + (NSMenu *)defaultMenu;
135 /// A selector represents a unique name for a method. The selector names for
136 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
138 class ObjCMethodDecl : public NamedDecl, public DeclContext {
139 // This class stores some data in DeclContext::ObjCMethodDeclBits
140 // to save some space. Use the provided accessors to access it.
143 enum ImplementationControl { None, Required, Optional };
146 /// Return type of this method.
147 QualType MethodDeclType;
149 /// Type source information for the return type.
150 TypeSourceInfo *ReturnTInfo;
152 /// Array of ParmVarDecls for the formal parameters of this method
153 /// and optionally followed by selector locations.
154 void *ParamsAndSelLocs = nullptr;
155 unsigned NumParams = 0;
157 /// List of attributes for this method declaration.
158 SourceLocation DeclEndLoc; // the location of the ';' or '{'.
160 /// The following are only used for method definitions, null otherwise.
161 LazyDeclStmtPtr Body;
163 /// SelfDecl - Decl for the implicit self parameter. This is lazily
164 /// constructed by createImplicitParams.
165 ImplicitParamDecl *SelfDecl = nullptr;
167 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
168 /// constructed by createImplicitParams.
169 ImplicitParamDecl *CmdDecl = nullptr;
171 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
172 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
173 DeclContext *contextDecl, bool isInstance = true,
174 bool isVariadic = false, bool isPropertyAccessor = false,
175 bool isSynthesizedAccessorStub = false,
176 bool isImplicitlyDeclared = false, bool isDefined = false,
177 ImplementationControl impControl = None,
178 bool HasRelatedResultType = false);
180 SelectorLocationsKind getSelLocsKind() const {
181 return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
184 void setSelLocsKind(SelectorLocationsKind Kind) {
185 ObjCMethodDeclBits.SelLocsKind = Kind;
188 bool hasStandardSelLocs() const {
189 return getSelLocsKind() != SelLoc_NonStandard;
192 /// Get a pointer to the stored selector identifiers locations array.
193 /// No locations will be stored if HasStandardSelLocs is true.
194 SourceLocation *getStoredSelLocs() {
195 return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
197 const SourceLocation *getStoredSelLocs() const {
198 return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
201 /// Get a pointer to the stored selector identifiers locations array.
202 /// No locations will be stored if HasStandardSelLocs is true.
203 ParmVarDecl **getParams() {
204 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
206 const ParmVarDecl *const *getParams() const {
207 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
210 /// Get the number of stored selector identifiers locations.
211 /// No locations will be stored if HasStandardSelLocs is true.
212 unsigned getNumStoredSelLocs() const {
213 if (hasStandardSelLocs())
215 return getNumSelectorLocs();
218 void setParamsAndSelLocs(ASTContext &C,
219 ArrayRef<ParmVarDecl*> Params,
220 ArrayRef<SourceLocation> SelLocs);
222 /// A definition will return its interface declaration.
223 /// An interface declaration will return its definition.
224 /// Otherwise it will return itself.
225 ObjCMethodDecl *getNextRedeclarationImpl() override;
228 friend class ASTDeclReader;
229 friend class ASTDeclWriter;
231 static ObjCMethodDecl *
232 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
233 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
234 DeclContext *contextDecl, bool isInstance = true,
235 bool isVariadic = false, bool isPropertyAccessor = false,
236 bool isSynthesizedAccessorStub = false,
237 bool isImplicitlyDeclared = false, bool isDefined = false,
238 ImplementationControl impControl = None,
239 bool HasRelatedResultType = false);
241 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
243 ObjCMethodDecl *getCanonicalDecl() override;
244 const ObjCMethodDecl *getCanonicalDecl() const {
245 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
248 ObjCDeclQualifier getObjCDeclQualifier() const {
249 return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier);
252 void setObjCDeclQualifier(ObjCDeclQualifier QV) {
253 ObjCMethodDeclBits.objcDeclQualifier = QV;
256 /// Determine whether this method has a result type that is related
257 /// to the message receiver's type.
258 bool hasRelatedResultType() const {
259 return ObjCMethodDeclBits.RelatedResultType;
262 /// Note whether this method has a related result type.
263 void setRelatedResultType(bool RRT = true) {
264 ObjCMethodDeclBits.RelatedResultType = RRT;
267 /// True if this is a method redeclaration in the same interface.
268 bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; }
269 void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; }
270 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
272 /// True if redeclared in the same interface.
273 bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; }
274 void setHasRedeclaration(bool HRD) const {
275 ObjCMethodDeclBits.HasRedeclaration = HRD;
278 /// Returns the location where the declarator ends. It will be
279 /// the location of ';' for a method declaration and the location of '{'
280 /// for a method definition.
281 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
283 // Location information, modeled after the Stmt API.
284 SourceLocation getBeginLoc() const LLVM_READONLY { return getLocation(); }
285 SourceLocation getEndLoc() const LLVM_READONLY;
286 SourceRange getSourceRange() const override LLVM_READONLY {
287 return SourceRange(getLocation(), getEndLoc());
290 SourceLocation getSelectorStartLoc() const {
292 return getBeginLoc();
293 return getSelectorLoc(0);
296 SourceLocation getSelectorLoc(unsigned Index) const {
297 assert(Index < getNumSelectorLocs() && "Index out of range!");
298 if (hasStandardSelLocs())
299 return getStandardSelectorLoc(Index, getSelector(),
300 getSelLocsKind() == SelLoc_StandardWithSpace,
303 return getStoredSelLocs()[Index];
306 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
308 unsigned getNumSelectorLocs() const {
311 Selector Sel = getSelector();
312 if (Sel.isUnarySelector())
314 return Sel.getNumArgs();
317 ObjCInterfaceDecl *getClassInterface();
318 const ObjCInterfaceDecl *getClassInterface() const {
319 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
322 Selector getSelector() const { return getDeclName().getObjCSelector(); }
324 QualType getReturnType() const { return MethodDeclType; }
325 void setReturnType(QualType T) { MethodDeclType = T; }
326 SourceRange getReturnTypeSourceRange() const;
328 /// Determine the type of an expression that sends a message to this
329 /// function. This replaces the type parameters with the types they would
330 /// get if the receiver was parameterless (e.g. it may replace the type
331 /// parameter with 'id').
332 QualType getSendResultType() const;
334 /// Determine the type of an expression that sends a message to this
335 /// function with the given receiver type.
336 QualType getSendResultType(QualType receiverType) const;
338 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
339 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
341 // Iterator access to formal parameters.
342 unsigned param_size() const { return NumParams; }
344 using param_const_iterator = const ParmVarDecl *const *;
345 using param_iterator = ParmVarDecl *const *;
346 using param_range = llvm::iterator_range<param_iterator>;
347 using param_const_range = llvm::iterator_range<param_const_iterator>;
349 param_const_iterator param_begin() const {
350 return param_const_iterator(getParams());
353 param_const_iterator param_end() const {
354 return param_const_iterator(getParams() + NumParams);
357 param_iterator param_begin() { return param_iterator(getParams()); }
358 param_iterator param_end() { return param_iterator(getParams() + NumParams); }
360 // This method returns and of the parameters which are part of the selector
361 // name mangling requirements.
362 param_const_iterator sel_param_end() const {
363 return param_begin() + getSelector().getNumArgs();
366 // ArrayRef access to formal parameters. This should eventually
367 // replace the iterator interface above.
368 ArrayRef<ParmVarDecl*> parameters() const {
369 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
373 ParmVarDecl *getParamDecl(unsigned Idx) {
374 assert(Idx < NumParams && "Index out of bounds!");
375 return getParams()[Idx];
377 const ParmVarDecl *getParamDecl(unsigned Idx) const {
378 return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx);
381 /// Sets the method's parameters and selector source locations.
382 /// If the method is implicit (not coming from source) \p SelLocs is
384 void setMethodParams(ASTContext &C,
385 ArrayRef<ParmVarDecl*> Params,
386 ArrayRef<SourceLocation> SelLocs = llvm::None);
388 // Iterator access to parameter types.
390 QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); }
393 using param_type_iterator =
394 llvm::mapped_iterator<param_const_iterator, GetTypeFn>;
396 param_type_iterator param_type_begin() const {
397 return llvm::map_iterator(param_begin(), GetTypeFn());
400 param_type_iterator param_type_end() const {
401 return llvm::map_iterator(param_end(), GetTypeFn());
404 /// createImplicitParams - Used to lazily create the self and cmd
405 /// implict parameters. This must be called prior to using getSelfDecl()
406 /// or getCmdDecl(). The call is ignored if the implicit parameters
407 /// have already been created.
408 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
410 /// \return the type for \c self and set \arg selfIsPseudoStrong and
411 /// \arg selfIsConsumed accordingly.
412 QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
413 bool &selfIsPseudoStrong, bool &selfIsConsumed) const;
415 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
416 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
417 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
418 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
420 /// Determines the family of this method.
421 ObjCMethodFamily getMethodFamily() const;
423 bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; }
424 void setInstanceMethod(bool isInst) {
425 ObjCMethodDeclBits.IsInstance = isInst;
428 bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; }
429 void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; }
431 bool isClassMethod() const { return !isInstanceMethod(); }
433 bool isPropertyAccessor() const {
434 return ObjCMethodDeclBits.IsPropertyAccessor;
437 void setPropertyAccessor(bool isAccessor) {
438 ObjCMethodDeclBits.IsPropertyAccessor = isAccessor;
441 bool isSynthesizedAccessorStub() const {
442 return ObjCMethodDeclBits.IsSynthesizedAccessorStub;
445 void setSynthesizedAccessorStub(bool isSynthesizedAccessorStub) {
446 ObjCMethodDeclBits.IsSynthesizedAccessorStub = isSynthesizedAccessorStub;
449 bool isDefined() const { return ObjCMethodDeclBits.IsDefined; }
450 void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; }
452 /// Whether this method overrides any other in the class hierarchy.
454 /// A method is said to override any method in the class's
455 /// base classes, its protocols, or its categories' protocols, that has
456 /// the same selector and is of the same kind (class or instance).
457 /// A method in an implementation is not considered as overriding the same
458 /// method in the interface or its categories.
459 bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; }
460 void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; }
462 /// Return overridden methods for the given \p Method.
464 /// An ObjC method is considered to override any method in the class's
465 /// base classes (and base's categories), its protocols, or its categories'
466 /// protocols, that has
467 /// the same selector and is of the same kind (class or instance).
468 /// A method in an implementation is not considered as overriding the same
469 /// method in the interface or its categories.
470 void getOverriddenMethods(
471 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
473 /// True if the method was a definition but its body was skipped.
474 bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; }
475 void setHasSkippedBody(bool Skipped = true) {
476 ObjCMethodDeclBits.HasSkippedBody = Skipped;
479 /// True if the method is tagged as objc_direct
480 bool isDirectMethod() const;
482 /// Returns the property associated with this method's selector.
484 /// Note that even if this particular method is not marked as a property
485 /// accessor, it is still possible for it to match a property declared in a
486 /// superclass. Pass \c false if you only want to check the current class.
487 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
489 // Related to protocols declared in \@protocol
490 void setDeclImplementation(ImplementationControl ic) {
491 ObjCMethodDeclBits.DeclImplementation = ic;
494 ImplementationControl getImplementationControl() const {
495 return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
498 bool isOptional() const {
499 return getImplementationControl() == Optional;
502 /// Returns true if this specific method declaration is marked with the
503 /// designated initializer attribute.
504 bool isThisDeclarationADesignatedInitializer() const;
506 /// Returns true if the method selector resolves to a designated initializer
507 /// in the class's interface.
509 /// \param InitMethod if non-null and the function returns true, it receives
510 /// the method declaration that was marked with the designated initializer
512 bool isDesignatedInitializerForTheInterface(
513 const ObjCMethodDecl **InitMethod = nullptr) const;
515 /// Determine whether this method has a body.
516 bool hasBody() const override { return Body.isValid(); }
518 /// Retrieve the body of this method, if it has one.
519 Stmt *getBody() const override;
521 void setLazyBody(uint64_t Offset) { Body = Offset; }
523 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
524 void setBody(Stmt *B) { Body = B; }
526 /// Returns whether this specific method is a definition.
527 bool isThisDeclarationADefinition() const { return hasBody(); }
529 /// Is this method defined in the NSObject base class?
530 bool definedInNSObject(const ASTContext &) const;
532 // Implement isa/cast/dyncast/etc.
533 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
534 static bool classofKind(Kind K) { return K == ObjCMethod; }
536 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
537 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
540 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
541 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
545 /// Describes the variance of a given generic parameter.
546 enum class ObjCTypeParamVariance : uint8_t {
547 /// The parameter is invariant: must match exactly.
550 /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
551 /// the type parameter is covariant and T is a subtype of U.
554 /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
555 /// when the type parameter is covariant and U is a subtype of T.
559 /// Represents the declaration of an Objective-C type parameter.
562 /// @interface NSDictionary<Key : id<NSCopying>, Value>
566 /// In the example above, both \c Key and \c Value are represented by
567 /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
568 /// while \c Value gets an implicit bound of \c id.
570 /// Objective-C type parameters are typedef-names in the grammar,
571 class ObjCTypeParamDecl : public TypedefNameDecl {
572 /// Index of this type parameter in the type parameter list.
575 /// The variance of the type parameter.
576 unsigned Variance : 2;
578 /// The location of the variance, if any.
579 SourceLocation VarianceLoc;
581 /// The location of the ':', which will be valid when the bound was
582 /// explicitly specified.
583 SourceLocation ColonLoc;
585 ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
586 ObjCTypeParamVariance variance, SourceLocation varianceLoc,
588 SourceLocation nameLoc, IdentifierInfo *name,
589 SourceLocation colonLoc, TypeSourceInfo *boundInfo)
590 : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
592 Index(index), Variance(static_cast<unsigned>(variance)),
593 VarianceLoc(varianceLoc), ColonLoc(colonLoc) {}
595 void anchor() override;
598 friend class ASTDeclReader;
599 friend class ASTDeclWriter;
601 static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
602 ObjCTypeParamVariance variance,
603 SourceLocation varianceLoc,
605 SourceLocation nameLoc,
606 IdentifierInfo *name,
607 SourceLocation colonLoc,
608 TypeSourceInfo *boundInfo);
609 static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
611 SourceRange getSourceRange() const override LLVM_READONLY;
613 /// Determine the variance of this type parameter.
614 ObjCTypeParamVariance getVariance() const {
615 return static_cast<ObjCTypeParamVariance>(Variance);
618 /// Set the variance of this type parameter.
619 void setVariance(ObjCTypeParamVariance variance) {
620 Variance = static_cast<unsigned>(variance);
623 /// Retrieve the location of the variance keyword.
624 SourceLocation getVarianceLoc() const { return VarianceLoc; }
626 /// Retrieve the index into its type parameter list.
627 unsigned getIndex() const { return Index; }
629 /// Whether this type parameter has an explicitly-written type bound, e.g.,
631 bool hasExplicitBound() const { return ColonLoc.isValid(); }
633 /// Retrieve the location of the ':' separating the type parameter name
634 /// from the explicitly-specified bound.
635 SourceLocation getColonLoc() const { return ColonLoc; }
637 // Implement isa/cast/dyncast/etc.
638 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
639 static bool classofKind(Kind K) { return K == ObjCTypeParam; }
642 /// Stores a list of Objective-C type parameters for a parameterized class
643 /// or a category/extension thereof.
646 /// @interface NSArray<T> // stores the <T>
649 class ObjCTypeParamList final
650 : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
651 /// Stores the components of a SourceRange as a POD.
652 struct PODSourceRange {
658 /// Location of the left and right angle brackets.
659 PODSourceRange Brackets;
661 // Used only for alignment.
662 ObjCTypeParamDecl *AlignmentHack;
665 /// The number of parameters in the list, which are tail-allocated.
668 ObjCTypeParamList(SourceLocation lAngleLoc,
669 ArrayRef<ObjCTypeParamDecl *> typeParams,
670 SourceLocation rAngleLoc);
673 friend TrailingObjects;
675 /// Create a new Objective-C type parameter list.
676 static ObjCTypeParamList *create(ASTContext &ctx,
677 SourceLocation lAngleLoc,
678 ArrayRef<ObjCTypeParamDecl *> typeParams,
679 SourceLocation rAngleLoc);
681 /// Iterate through the type parameters in the list.
682 using iterator = ObjCTypeParamDecl **;
684 iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
686 iterator end() { return begin() + size(); }
688 /// Determine the number of type parameters in this list.
689 unsigned size() const { return NumParams; }
691 // Iterate through the type parameters in the list.
692 using const_iterator = ObjCTypeParamDecl * const *;
694 const_iterator begin() const {
695 return getTrailingObjects<ObjCTypeParamDecl *>();
698 const_iterator end() const {
699 return begin() + size();
702 ObjCTypeParamDecl *front() const {
703 assert(size() > 0 && "empty Objective-C type parameter list");
707 ObjCTypeParamDecl *back() const {
708 assert(size() > 0 && "empty Objective-C type parameter list");
712 SourceLocation getLAngleLoc() const {
713 return SourceLocation::getFromRawEncoding(Brackets.Begin);
716 SourceLocation getRAngleLoc() const {
717 return SourceLocation::getFromRawEncoding(Brackets.End);
720 SourceRange getSourceRange() const {
721 return SourceRange(getLAngleLoc(), getRAngleLoc());
724 /// Gather the default set of type arguments to be substituted for
725 /// these type parameters when dealing with an unspecialized type.
726 void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
729 enum class ObjCPropertyQueryKind : uint8_t {
730 OBJC_PR_query_unknown = 0x00,
731 OBJC_PR_query_instance,
735 /// Represents one property declaration in an Objective-C interface.
739 /// \@property (assign, readwrite) int MyProperty;
741 class ObjCPropertyDecl : public NamedDecl {
742 void anchor() override;
745 enum PropertyAttributeKind {
746 OBJC_PR_noattr = 0x00,
747 OBJC_PR_readonly = 0x01,
748 OBJC_PR_getter = 0x02,
749 OBJC_PR_assign = 0x04,
750 OBJC_PR_readwrite = 0x08,
751 OBJC_PR_retain = 0x10,
753 OBJC_PR_nonatomic = 0x40,
754 OBJC_PR_setter = 0x80,
755 OBJC_PR_atomic = 0x100,
756 OBJC_PR_weak = 0x200,
757 OBJC_PR_strong = 0x400,
758 OBJC_PR_unsafe_unretained = 0x800,
759 /// Indicates that the nullability of the type was spelled with a
760 /// property attribute rather than a type qualifier.
761 OBJC_PR_nullability = 0x1000,
762 OBJC_PR_null_resettable = 0x2000,
763 OBJC_PR_class = 0x4000,
764 OBJC_PR_direct = 0x8000
765 // Adding a property should change NumPropertyAttrsBits
769 /// Number of bits fitting all the property attributes.
770 NumPropertyAttrsBits = 16
773 enum SetterKind { Assign, Retain, Copy, Weak };
774 enum PropertyControl { None, Required, Optional };
777 // location of \@property
778 SourceLocation AtLoc;
780 // location of '(' starting attribute list or null.
781 SourceLocation LParenLoc;
784 TypeSourceInfo *DeclTypeSourceInfo;
785 unsigned PropertyAttributes : NumPropertyAttrsBits;
786 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
788 // \@required/\@optional
789 unsigned PropertyImplementation : 2;
791 // getter name of NULL if no getter
794 // setter name of NULL if no setter
797 // location of the getter attribute's value
798 SourceLocation GetterNameLoc;
800 // location of the setter attribute's value
801 SourceLocation SetterNameLoc;
803 // Declaration of getter instance method
804 ObjCMethodDecl *GetterMethodDecl = nullptr;
806 // Declaration of setter instance method
807 ObjCMethodDecl *SetterMethodDecl = nullptr;
809 // Synthesize ivar for this property
810 ObjCIvarDecl *PropertyIvarDecl = nullptr;
812 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
813 SourceLocation AtLocation, SourceLocation LParenLocation,
814 QualType T, TypeSourceInfo *TSI,
815 PropertyControl propControl)
816 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
817 LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
818 PropertyAttributes(OBJC_PR_noattr),
819 PropertyAttributesAsWritten(OBJC_PR_noattr),
820 PropertyImplementation(propControl), GetterName(Selector()),
821 SetterName(Selector()) {}
824 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
826 IdentifierInfo *Id, SourceLocation AtLocation,
827 SourceLocation LParenLocation,
830 PropertyControl propControl = None);
832 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
834 SourceLocation getAtLoc() const { return AtLoc; }
835 void setAtLoc(SourceLocation L) { AtLoc = L; }
837 SourceLocation getLParenLoc() const { return LParenLoc; }
838 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
840 TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
842 QualType getType() const { return DeclType; }
844 void setType(QualType T, TypeSourceInfo *TSI) {
846 DeclTypeSourceInfo = TSI;
849 /// Retrieve the type when this property is used with a specific base object
851 QualType getUsageType(QualType objectType) const;
853 PropertyAttributeKind getPropertyAttributes() const {
854 return PropertyAttributeKind(PropertyAttributes);
857 void setPropertyAttributes(PropertyAttributeKind PRVal) {
858 PropertyAttributes |= PRVal;
861 void overwritePropertyAttributes(unsigned PRVal) {
862 PropertyAttributes = PRVal;
865 PropertyAttributeKind getPropertyAttributesAsWritten() const {
866 return PropertyAttributeKind(PropertyAttributesAsWritten);
869 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
870 PropertyAttributesAsWritten = PRVal;
873 // Helper methods for accessing attributes.
875 /// isReadOnly - Return true iff the property has a setter.
876 bool isReadOnly() const {
877 return (PropertyAttributes & OBJC_PR_readonly);
880 /// isAtomic - Return true if the property is atomic.
881 bool isAtomic() const {
882 return (PropertyAttributes & OBJC_PR_atomic);
885 /// isRetaining - Return true if the property retains its value.
886 bool isRetaining() const {
887 return (PropertyAttributes &
888 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
891 bool isInstanceProperty() const { return !isClassProperty(); }
892 bool isClassProperty() const { return PropertyAttributes & OBJC_PR_class; }
893 bool isDirectProperty() const { return PropertyAttributes & OBJC_PR_direct; }
895 ObjCPropertyQueryKind getQueryKind() const {
896 return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
897 ObjCPropertyQueryKind::OBJC_PR_query_instance;
900 static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
901 return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
902 ObjCPropertyQueryKind::OBJC_PR_query_instance;
905 /// getSetterKind - Return the method used for doing assignment in
906 /// the property setter. This is only valid if the property has been
907 /// defined to have a setter.
908 SetterKind getSetterKind() const {
909 if (PropertyAttributes & OBJC_PR_strong)
910 return getType()->isBlockPointerType() ? Copy : Retain;
911 if (PropertyAttributes & OBJC_PR_retain)
913 if (PropertyAttributes & OBJC_PR_copy)
915 if (PropertyAttributes & OBJC_PR_weak)
920 Selector getGetterName() const { return GetterName; }
921 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
923 void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
928 Selector getSetterName() const { return SetterName; }
929 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
931 void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
936 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
937 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
939 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
940 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
942 // Related to \@optional/\@required declared in \@protocol
943 void setPropertyImplementation(PropertyControl pc) {
944 PropertyImplementation = pc;
947 PropertyControl getPropertyImplementation() const {
948 return PropertyControl(PropertyImplementation);
951 bool isOptional() const {
952 return getPropertyImplementation() == PropertyControl::Optional;
955 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
956 PropertyIvarDecl = Ivar;
959 ObjCIvarDecl *getPropertyIvarDecl() const {
960 return PropertyIvarDecl;
963 SourceRange getSourceRange() const override LLVM_READONLY {
964 return SourceRange(AtLoc, getLocation());
967 /// Get the default name of the synthesized ivar.
968 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
970 /// Lookup a property by name in the specified DeclContext.
971 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
972 const IdentifierInfo *propertyID,
973 ObjCPropertyQueryKind queryKind);
975 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
976 static bool classofKind(Kind K) { return K == ObjCProperty; }
979 /// ObjCContainerDecl - Represents a container for method declarations.
980 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
981 /// ObjCProtocolDecl, and ObjCImplDecl.
983 class ObjCContainerDecl : public NamedDecl, public DeclContext {
984 // This class stores some data in DeclContext::ObjCContainerDeclBits
985 // to save some space. Use the provided accessors to access it.
987 // These two locations in the range mark the end of the method container.
988 // The first points to the '@' token, and the second to the 'end' token.
991 void anchor() override;
994 ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id,
995 SourceLocation nameLoc, SourceLocation atStartLoc);
997 // Iterator access to instance/class properties.
998 using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>;
1000 llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>;
1002 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
1004 prop_iterator prop_begin() const {
1005 return prop_iterator(decls_begin());
1008 prop_iterator prop_end() const {
1009 return prop_iterator(decls_end());
1012 using instprop_iterator =
1013 filtered_decl_iterator<ObjCPropertyDecl,
1014 &ObjCPropertyDecl::isInstanceProperty>;
1015 using instprop_range = llvm::iterator_range<instprop_iterator>;
1017 instprop_range instance_properties() const {
1018 return instprop_range(instprop_begin(), instprop_end());
1021 instprop_iterator instprop_begin() const {
1022 return instprop_iterator(decls_begin());
1025 instprop_iterator instprop_end() const {
1026 return instprop_iterator(decls_end());
1029 using classprop_iterator =
1030 filtered_decl_iterator<ObjCPropertyDecl,
1031 &ObjCPropertyDecl::isClassProperty>;
1032 using classprop_range = llvm::iterator_range<classprop_iterator>;
1034 classprop_range class_properties() const {
1035 return classprop_range(classprop_begin(), classprop_end());
1038 classprop_iterator classprop_begin() const {
1039 return classprop_iterator(decls_begin());
1042 classprop_iterator classprop_end() const {
1043 return classprop_iterator(decls_end());
1046 // Iterator access to instance/class methods.
1047 using method_iterator = specific_decl_iterator<ObjCMethodDecl>;
1048 using method_range =
1049 llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>;
1051 method_range methods() const {
1052 return method_range(meth_begin(), meth_end());
1055 method_iterator meth_begin() const {
1056 return method_iterator(decls_begin());
1059 method_iterator meth_end() const {
1060 return method_iterator(decls_end());
1063 using instmeth_iterator =
1064 filtered_decl_iterator<ObjCMethodDecl,
1065 &ObjCMethodDecl::isInstanceMethod>;
1066 using instmeth_range = llvm::iterator_range<instmeth_iterator>;
1068 instmeth_range instance_methods() const {
1069 return instmeth_range(instmeth_begin(), instmeth_end());
1072 instmeth_iterator instmeth_begin() const {
1073 return instmeth_iterator(decls_begin());
1076 instmeth_iterator instmeth_end() const {
1077 return instmeth_iterator(decls_end());
1080 using classmeth_iterator =
1081 filtered_decl_iterator<ObjCMethodDecl,
1082 &ObjCMethodDecl::isClassMethod>;
1083 using classmeth_range = llvm::iterator_range<classmeth_iterator>;
1085 classmeth_range class_methods() const {
1086 return classmeth_range(classmeth_begin(), classmeth_end());
1089 classmeth_iterator classmeth_begin() const {
1090 return classmeth_iterator(decls_begin());
1093 classmeth_iterator classmeth_end() const {
1094 return classmeth_iterator(decls_end());
1097 // Get the local instance/class method declared in this interface.
1098 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1099 bool AllowHidden = false) const;
1101 ObjCMethodDecl *getInstanceMethod(Selector Sel,
1102 bool AllowHidden = false) const {
1103 return getMethod(Sel, true/*isInstance*/, AllowHidden);
1106 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1107 return getMethod(Sel, false/*isInstance*/, AllowHidden);
1110 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1111 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1114 FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1115 ObjCPropertyQueryKind QueryKind) const;
1118 llvm::DenseMap<std::pair<IdentifierInfo *, unsigned/*isClassProperty*/>,
1119 ObjCPropertyDecl *>;
1120 using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>;
1121 using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>;
1123 /// This routine collects list of properties to be implemented in the class.
1124 /// This includes, class's and its conforming protocols' properties.
1125 /// Note, the superclass's properties are not included in the list.
1126 virtual void collectPropertiesToImplement(PropertyMap &PM,
1127 PropertyDeclOrder &PO) const {}
1129 SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
1131 void setAtStartLoc(SourceLocation Loc) {
1132 ObjCContainerDeclBits.AtStart = Loc;
1135 // Marks the end of the container.
1136 SourceRange getAtEndRange() const { return AtEnd; }
1138 void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; }
1140 SourceRange getSourceRange() const override LLVM_READONLY {
1141 return SourceRange(getAtStartLoc(), getAtEndRange().getEnd());
1144 // Implement isa/cast/dyncast/etc.
1145 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1147 static bool classofKind(Kind K) {
1148 return K >= firstObjCContainer &&
1149 K <= lastObjCContainer;
1152 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1153 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1156 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1157 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1161 /// Represents an ObjC class declaration.
1166 /// // MostPrimitive declares no super class (not particularly useful).
1167 /// \@interface MostPrimitive
1168 /// // no instance variables or methods.
1171 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1172 /// \@interface NSResponder : NSObject \<NSCoding>
1173 /// { // instance variables are represented by ObjCIvarDecl.
1174 /// id nextResponder; // nextResponder instance variable.
1176 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1177 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1178 /// \@end // to an NSEvent.
1181 /// Unlike C/C++, forward class declarations are accomplished with \@class.
1182 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
1183 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1184 /// typically inherit from NSObject (an exception is NSProxy).
1186 class ObjCInterfaceDecl : public ObjCContainerDecl
1187 , public Redeclarable<ObjCInterfaceDecl> {
1188 friend class ASTContext;
1190 /// TypeForDecl - This indicates the Type object that represents this
1191 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
1192 mutable const Type *TypeForDecl = nullptr;
1194 struct DefinitionData {
1195 /// The definition of this class, for quick access from any
1197 ObjCInterfaceDecl *Definition = nullptr;
1199 /// When non-null, this is always an ObjCObjectType.
1200 TypeSourceInfo *SuperClassTInfo = nullptr;
1202 /// Protocols referenced in the \@interface declaration
1203 ObjCProtocolList ReferencedProtocols;
1205 /// Protocols reference in both the \@interface and class extensions.
1206 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1208 /// List of categories and class extensions defined for this class.
1210 /// Categories are stored as a linked list in the AST, since the categories
1211 /// and class extensions come long after the initial interface declaration,
1212 /// and we avoid dynamically-resized arrays in the AST wherever possible.
1213 ObjCCategoryDecl *CategoryList = nullptr;
1215 /// IvarList - List of all ivars defined by this class; including class
1216 /// extensions and implementation. This list is built lazily.
1217 ObjCIvarDecl *IvarList = nullptr;
1219 /// Indicates that the contents of this Objective-C class will be
1220 /// completed by the external AST source when required.
1221 mutable unsigned ExternallyCompleted : 1;
1223 /// Indicates that the ivar cache does not yet include ivars
1224 /// declared in the implementation.
1225 mutable unsigned IvarListMissingImplementation : 1;
1227 /// Indicates that this interface decl contains at least one initializer
1228 /// marked with the 'objc_designated_initializer' attribute.
1229 unsigned HasDesignatedInitializers : 1;
1231 enum InheritedDesignatedInitializersState {
1232 /// We didn't calculate whether the designated initializers should be
1233 /// inherited or not.
1236 /// Designated initializers are inherited for the super class.
1239 /// The class does not inherit designated initializers.
1240 IDI_NotInherited = 2
1243 /// One of the \c InheritedDesignatedInitializersState enumeratos.
1244 mutable unsigned InheritedDesignatedInitializers : 2;
1246 /// The location of the last location in this declaration, before
1247 /// the properties/methods. For example, this will be the '>', '}', or
1249 SourceLocation EndLoc;
1252 : ExternallyCompleted(false), IvarListMissingImplementation(true),
1253 HasDesignatedInitializers(false),
1254 InheritedDesignatedInitializers(IDI_Unknown) {}
1257 /// The type parameters associated with this class, if any.
1258 ObjCTypeParamList *TypeParamList = nullptr;
1260 /// Contains a pointer to the data associated with this class,
1261 /// which will be NULL if this class has not yet been defined.
1263 /// The bit indicates when we don't need to check for out-of-date
1264 /// declarations. It will be set unless modules are enabled.
1265 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1267 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1268 IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1269 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1272 void anchor() override;
1274 void LoadExternalDefinition() const;
1276 DefinitionData &data() const {
1277 assert(Data.getPointer() && "Declaration has no definition!");
1278 return *Data.getPointer();
1281 /// Allocate the definition data for this class.
1282 void allocateDefinitionData();
1284 using redeclarable_base = Redeclarable<ObjCInterfaceDecl>;
1286 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1287 return getNextRedeclaration();
1290 ObjCInterfaceDecl *getPreviousDeclImpl() override {
1291 return getPreviousDecl();
1294 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1295 return getMostRecentDecl();
1299 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1300 SourceLocation atLoc,
1302 ObjCTypeParamList *typeParamList,
1303 ObjCInterfaceDecl *PrevDecl,
1304 SourceLocation ClassLoc = SourceLocation(),
1305 bool isInternal = false);
1307 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1309 /// Retrieve the type parameters of this class.
1311 /// This function looks for a type parameter list for the given
1312 /// class; if the class has been declared (with \c \@class) but not
1313 /// defined (with \c \@interface), it will search for a declaration that
1314 /// has type parameters, skipping any declarations that do not.
1315 ObjCTypeParamList *getTypeParamList() const;
1317 /// Set the type parameters of this class.
1319 /// This function is used by the AST importer, which must import the type
1320 /// parameters after creating their DeclContext to avoid loops.
1321 void setTypeParamList(ObjCTypeParamList *TPL);
1323 /// Retrieve the type parameters written on this particular declaration of
1325 ObjCTypeParamList *getTypeParamListAsWritten() const {
1326 return TypeParamList;
1329 SourceRange getSourceRange() const override LLVM_READONLY {
1330 if (isThisDeclarationADefinition())
1331 return ObjCContainerDecl::getSourceRange();
1333 return SourceRange(getAtStartLoc(), getLocation());
1336 /// Indicate that this Objective-C class is complete, but that
1337 /// the external AST source will be responsible for filling in its contents
1338 /// when a complete class is required.
1339 void setExternallyCompleted();
1341 /// Indicate that this interface decl contains at least one initializer
1342 /// marked with the 'objc_designated_initializer' attribute.
1343 void setHasDesignatedInitializers();
1345 /// Returns true if this interface decl contains at least one initializer
1346 /// marked with the 'objc_designated_initializer' attribute.
1347 bool hasDesignatedInitializers() const;
1349 /// Returns true if this interface decl declares a designated initializer
1350 /// or it inherites one from its super class.
1351 bool declaresOrInheritsDesignatedInitializers() const {
1352 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1355 const ObjCProtocolList &getReferencedProtocols() const {
1356 assert(hasDefinition() && "Caller did not check for forward reference!");
1357 if (data().ExternallyCompleted)
1358 LoadExternalDefinition();
1360 return data().ReferencedProtocols;
1363 ObjCImplementationDecl *getImplementation() const;
1364 void setImplementation(ObjCImplementationDecl *ImplD);
1366 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1368 // Get the local instance/class method declared in a category.
1369 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1370 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1372 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1373 return isInstance ? getCategoryInstanceMethod(Sel)
1374 : getCategoryClassMethod(Sel);
1377 using protocol_iterator = ObjCProtocolList::iterator;
1378 using protocol_range = llvm::iterator_range<protocol_iterator>;
1380 protocol_range protocols() const {
1381 return protocol_range(protocol_begin(), protocol_end());
1384 protocol_iterator protocol_begin() const {
1385 // FIXME: Should make sure no callers ever do this.
1386 if (!hasDefinition())
1387 return protocol_iterator();
1389 if (data().ExternallyCompleted)
1390 LoadExternalDefinition();
1392 return data().ReferencedProtocols.begin();
1395 protocol_iterator protocol_end() const {
1396 // FIXME: Should make sure no callers ever do this.
1397 if (!hasDefinition())
1398 return protocol_iterator();
1400 if (data().ExternallyCompleted)
1401 LoadExternalDefinition();
1403 return data().ReferencedProtocols.end();
1406 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
1407 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
1409 protocol_loc_range protocol_locs() const {
1410 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1413 protocol_loc_iterator protocol_loc_begin() const {
1414 // FIXME: Should make sure no callers ever do this.
1415 if (!hasDefinition())
1416 return protocol_loc_iterator();
1418 if (data().ExternallyCompleted)
1419 LoadExternalDefinition();
1421 return data().ReferencedProtocols.loc_begin();
1424 protocol_loc_iterator protocol_loc_end() const {
1425 // FIXME: Should make sure no callers ever do this.
1426 if (!hasDefinition())
1427 return protocol_loc_iterator();
1429 if (data().ExternallyCompleted)
1430 LoadExternalDefinition();
1432 return data().ReferencedProtocols.loc_end();
1435 using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator;
1436 using all_protocol_range = llvm::iterator_range<all_protocol_iterator>;
1438 all_protocol_range all_referenced_protocols() const {
1439 return all_protocol_range(all_referenced_protocol_begin(),
1440 all_referenced_protocol_end());
1443 all_protocol_iterator all_referenced_protocol_begin() const {
1444 // FIXME: Should make sure no callers ever do this.
1445 if (!hasDefinition())
1446 return all_protocol_iterator();
1448 if (data().ExternallyCompleted)
1449 LoadExternalDefinition();
1451 return data().AllReferencedProtocols.empty()
1453 : data().AllReferencedProtocols.begin();
1456 all_protocol_iterator all_referenced_protocol_end() const {
1457 // FIXME: Should make sure no callers ever do this.
1458 if (!hasDefinition())
1459 return all_protocol_iterator();
1461 if (data().ExternallyCompleted)
1462 LoadExternalDefinition();
1464 return data().AllReferencedProtocols.empty()
1466 : data().AllReferencedProtocols.end();
1469 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
1470 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
1472 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1474 ivar_iterator ivar_begin() const {
1475 if (const ObjCInterfaceDecl *Def = getDefinition())
1476 return ivar_iterator(Def->decls_begin());
1478 // FIXME: Should make sure no callers ever do this.
1479 return ivar_iterator();
1482 ivar_iterator ivar_end() const {
1483 if (const ObjCInterfaceDecl *Def = getDefinition())
1484 return ivar_iterator(Def->decls_end());
1486 // FIXME: Should make sure no callers ever do this.
1487 return ivar_iterator();
1490 unsigned ivar_size() const {
1491 return std::distance(ivar_begin(), ivar_end());
1494 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1496 ObjCIvarDecl *all_declared_ivar_begin();
1497 const ObjCIvarDecl *all_declared_ivar_begin() const {
1498 // Even though this modifies IvarList, it's conceptually const:
1499 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1500 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1502 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1504 /// setProtocolList - Set the list of protocols that this interface
1506 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1507 const SourceLocation *Locs, ASTContext &C) {
1508 data().ReferencedProtocols.set(List, Num, Locs, C);
1511 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1512 /// into the protocol list for this class.
1513 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1517 /// Produce a name to be used for class's metadata. It comes either via
1518 /// objc_runtime_name attribute or class name.
1519 StringRef getObjCRuntimeNameAsString() const;
1521 /// Returns the designated initializers for the interface.
1523 /// If this declaration does not have methods marked as designated
1524 /// initializers then the interface inherits the designated initializers of
1525 /// its super class.
1526 void getDesignatedInitializers(
1527 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1529 /// Returns true if the given selector is a designated initializer for the
1532 /// If this declaration does not have methods marked as designated
1533 /// initializers then the interface inherits the designated initializers of
1534 /// its super class.
1536 /// \param InitMethod if non-null and the function returns true, it receives
1537 /// the method that was marked as a designated initializer.
1539 isDesignatedInitializer(Selector Sel,
1540 const ObjCMethodDecl **InitMethod = nullptr) const;
1542 /// Determine whether this particular declaration of this class is
1543 /// actually also a definition.
1544 bool isThisDeclarationADefinition() const {
1545 return getDefinition() == this;
1548 /// Determine whether this class has been defined.
1549 bool hasDefinition() const {
1550 // If the name of this class is out-of-date, bring it up-to-date, which
1551 // might bring in a definition.
1552 // Note: a null value indicates that we don't have a definition and that
1553 // modules are enabled.
1554 if (!Data.getOpaqueValue())
1555 getMostRecentDecl();
1557 return Data.getPointer();
1560 /// Retrieve the definition of this class, or NULL if this class
1561 /// has been forward-declared (with \@class) but not yet defined (with
1563 ObjCInterfaceDecl *getDefinition() {
1564 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1567 /// Retrieve the definition of this class, or NULL if this class
1568 /// has been forward-declared (with \@class) but not yet defined (with
1570 const ObjCInterfaceDecl *getDefinition() const {
1571 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1574 /// Starts the definition of this Objective-C class, taking it from
1575 /// a forward declaration (\@class) to a definition (\@interface).
1576 void startDefinition();
1578 /// Retrieve the superclass type.
1579 const ObjCObjectType *getSuperClassType() const {
1580 if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1581 return TInfo->getType()->castAs<ObjCObjectType>();
1586 // Retrieve the type source information for the superclass.
1587 TypeSourceInfo *getSuperClassTInfo() const {
1588 // FIXME: Should make sure no callers ever do this.
1589 if (!hasDefinition())
1592 if (data().ExternallyCompleted)
1593 LoadExternalDefinition();
1595 return data().SuperClassTInfo;
1598 // Retrieve the declaration for the superclass of this class, which
1599 // does not include any type arguments that apply to the superclass.
1600 ObjCInterfaceDecl *getSuperClass() const;
1602 void setSuperClass(TypeSourceInfo *superClass) {
1603 data().SuperClassTInfo = superClass;
1606 /// Iterator that walks over the list of categories, filtering out
1607 /// those that do not meet specific criteria.
1609 /// This class template is used for the various permutations of category
1610 /// and extension iterators.
1611 template<bool (*Filter)(ObjCCategoryDecl *)>
1612 class filtered_category_iterator {
1613 ObjCCategoryDecl *Current = nullptr;
1615 void findAcceptableCategory();
1618 using value_type = ObjCCategoryDecl *;
1619 using reference = value_type;
1620 using pointer = value_type;
1621 using difference_type = std::ptrdiff_t;
1622 using iterator_category = std::input_iterator_tag;
1624 filtered_category_iterator() = default;
1625 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1626 : Current(Current) {
1627 findAcceptableCategory();
1630 reference operator*() const { return Current; }
1631 pointer operator->() const { return Current; }
1633 filtered_category_iterator &operator++();
1635 filtered_category_iterator operator++(int) {
1636 filtered_category_iterator Tmp = *this;
1641 friend bool operator==(filtered_category_iterator X,
1642 filtered_category_iterator Y) {
1643 return X.Current == Y.Current;
1646 friend bool operator!=(filtered_category_iterator X,
1647 filtered_category_iterator Y) {
1648 return X.Current != Y.Current;
1653 /// Test whether the given category is visible.
1655 /// Used in the \c visible_categories_iterator.
1656 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1659 /// Iterator that walks over the list of categories and extensions
1660 /// that are visible, i.e., not hidden in a non-imported submodule.
1661 using visible_categories_iterator =
1662 filtered_category_iterator<isVisibleCategory>;
1664 using visible_categories_range =
1665 llvm::iterator_range<visible_categories_iterator>;
1667 visible_categories_range visible_categories() const {
1668 return visible_categories_range(visible_categories_begin(),
1669 visible_categories_end());
1672 /// Retrieve an iterator to the beginning of the visible-categories
1674 visible_categories_iterator visible_categories_begin() const {
1675 return visible_categories_iterator(getCategoryListRaw());
1678 /// Retrieve an iterator to the end of the visible-categories list.
1679 visible_categories_iterator visible_categories_end() const {
1680 return visible_categories_iterator();
1683 /// Determine whether the visible-categories list is empty.
1684 bool visible_categories_empty() const {
1685 return visible_categories_begin() == visible_categories_end();
1689 /// Test whether the given category... is a category.
1691 /// Used in the \c known_categories_iterator.
1692 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1695 /// Iterator that walks over all of the known categories and
1696 /// extensions, including those that are hidden.
1697 using known_categories_iterator = filtered_category_iterator<isKnownCategory>;
1698 using known_categories_range =
1699 llvm::iterator_range<known_categories_iterator>;
1701 known_categories_range known_categories() const {
1702 return known_categories_range(known_categories_begin(),
1703 known_categories_end());
1706 /// Retrieve an iterator to the beginning of the known-categories
1708 known_categories_iterator known_categories_begin() const {
1709 return known_categories_iterator(getCategoryListRaw());
1712 /// Retrieve an iterator to the end of the known-categories list.
1713 known_categories_iterator known_categories_end() const {
1714 return known_categories_iterator();
1717 /// Determine whether the known-categories list is empty.
1718 bool known_categories_empty() const {
1719 return known_categories_begin() == known_categories_end();
1723 /// Test whether the given category is a visible extension.
1725 /// Used in the \c visible_extensions_iterator.
1726 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1729 /// Iterator that walks over all of the visible extensions, skipping
1730 /// any that are known but hidden.
1731 using visible_extensions_iterator =
1732 filtered_category_iterator<isVisibleExtension>;
1734 using visible_extensions_range =
1735 llvm::iterator_range<visible_extensions_iterator>;
1737 visible_extensions_range visible_extensions() const {
1738 return visible_extensions_range(visible_extensions_begin(),
1739 visible_extensions_end());
1742 /// Retrieve an iterator to the beginning of the visible-extensions
1744 visible_extensions_iterator visible_extensions_begin() const {
1745 return visible_extensions_iterator(getCategoryListRaw());
1748 /// Retrieve an iterator to the end of the visible-extensions list.
1749 visible_extensions_iterator visible_extensions_end() const {
1750 return visible_extensions_iterator();
1753 /// Determine whether the visible-extensions list is empty.
1754 bool visible_extensions_empty() const {
1755 return visible_extensions_begin() == visible_extensions_end();
1759 /// Test whether the given category is an extension.
1761 /// Used in the \c known_extensions_iterator.
1762 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1765 friend class ASTDeclReader;
1766 friend class ASTDeclWriter;
1767 friend class ASTReader;
1769 /// Iterator that walks over all of the known extensions.
1770 using known_extensions_iterator =
1771 filtered_category_iterator<isKnownExtension>;
1772 using known_extensions_range =
1773 llvm::iterator_range<known_extensions_iterator>;
1775 known_extensions_range known_extensions() const {
1776 return known_extensions_range(known_extensions_begin(),
1777 known_extensions_end());
1780 /// Retrieve an iterator to the beginning of the known-extensions
1782 known_extensions_iterator known_extensions_begin() const {
1783 return known_extensions_iterator(getCategoryListRaw());
1786 /// Retrieve an iterator to the end of the known-extensions list.
1787 known_extensions_iterator known_extensions_end() const {
1788 return known_extensions_iterator();
1791 /// Determine whether the known-extensions list is empty.
1792 bool known_extensions_empty() const {
1793 return known_extensions_begin() == known_extensions_end();
1796 /// Retrieve the raw pointer to the start of the category/extension
1798 ObjCCategoryDecl* getCategoryListRaw() const {
1799 // FIXME: Should make sure no callers ever do this.
1800 if (!hasDefinition())
1803 if (data().ExternallyCompleted)
1804 LoadExternalDefinition();
1806 return data().CategoryList;
1809 /// Set the raw pointer to the start of the category/extension
1811 void setCategoryListRaw(ObjCCategoryDecl *category) {
1812 data().CategoryList = category;
1816 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1817 ObjCPropertyQueryKind QueryKind) const;
1819 void collectPropertiesToImplement(PropertyMap &PM,
1820 PropertyDeclOrder &PO) const override;
1822 /// isSuperClassOf - Return true if this class is the specified class or is a
1823 /// super class of the specified interface class.
1824 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1825 // If RHS is derived from LHS it is OK; else it is not OK.
1826 while (I != nullptr) {
1827 if (declaresSameEntity(this, I))
1830 I = I->getSuperClass();
1835 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1836 /// to be incompatible with __weak references. Returns true if it is.
1837 bool isArcWeakrefUnavailable() const;
1839 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1840 /// classes must not be auto-synthesized. Returns class decl. if it must not
1841 /// be; 0, otherwise.
1842 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1844 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1845 ObjCInterfaceDecl *&ClassDeclared);
1846 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1847 ObjCInterfaceDecl *ClassDeclared;
1848 return lookupInstanceVariable(IVarName, ClassDeclared);
1851 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1853 // Lookup a method. First, we search locally. If a method isn't
1854 // found, we search referenced protocols and class categories.
1855 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1856 bool shallowCategoryLookup = false,
1857 bool followSuper = true,
1858 const ObjCCategoryDecl *C = nullptr) const;
1860 /// Lookup an instance method for a given selector.
1861 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1862 return lookupMethod(Sel, true/*isInstance*/);
1865 /// Lookup a class method for a given selector.
1866 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1867 return lookupMethod(Sel, false/*isInstance*/);
1870 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1872 /// Lookup a method in the classes implementation hierarchy.
1873 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1874 bool Instance=true) const;
1876 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1877 return lookupPrivateMethod(Sel, false);
1880 /// Lookup a setter or getter in the class hierarchy,
1881 /// including in all categories except for category passed
1883 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1884 const ObjCCategoryDecl *Cat,
1885 bool IsClassProperty) const {
1886 return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1887 false/*shallowCategoryLookup*/,
1888 true /* followsSuper */,
1892 SourceLocation getEndOfDefinitionLoc() const {
1893 if (!hasDefinition())
1894 return getLocation();
1896 return data().EndLoc;
1899 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1901 /// Retrieve the starting location of the superclass.
1902 SourceLocation getSuperClassLoc() const;
1904 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1905 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1906 /// declaration without an \@interface declaration.
1907 bool isImplicitInterfaceDecl() const {
1908 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1911 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1912 /// has been implemented in IDecl class, its super class or categories (if
1913 /// lookupCategory is true).
1914 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1915 bool lookupCategory,
1916 bool RHSIsQualifiedID = false);
1918 using redecl_range = redeclarable_base::redecl_range;
1919 using redecl_iterator = redeclarable_base::redecl_iterator;
1921 using redeclarable_base::redecls_begin;
1922 using redeclarable_base::redecls_end;
1923 using redeclarable_base::redecls;
1924 using redeclarable_base::getPreviousDecl;
1925 using redeclarable_base::getMostRecentDecl;
1926 using redeclarable_base::isFirstDecl;
1928 /// Retrieves the canonical declaration of this Objective-C class.
1929 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1930 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1932 // Low-level accessor
1933 const Type *getTypeForDecl() const { return TypeForDecl; }
1934 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1936 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1937 static bool classofKind(Kind K) { return K == ObjCInterface; }
1940 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1941 bool inheritsDesignatedInitializers() const;
1944 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1945 /// instance variables are identical to C. The only exception is Objective-C
1946 /// supports C++ style access control. For example:
1948 /// \@interface IvarExample : NSObject
1950 /// id defaultToProtected;
1952 /// id canBePublic; // same as C++.
1954 /// id canBeProtected; // same as C++.
1956 /// id canBePackage; // framework visibility (not available in C++).
1959 class ObjCIvarDecl : public FieldDecl {
1960 void anchor() override;
1963 enum AccessControl {
1964 None, Private, Protected, Public, Package
1968 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1969 SourceLocation IdLoc, IdentifierInfo *Id,
1970 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1972 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1973 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1974 DeclAccess(ac), Synthesized(synthesized) {}
1977 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1978 SourceLocation StartLoc, SourceLocation IdLoc,
1979 IdentifierInfo *Id, QualType T,
1980 TypeSourceInfo *TInfo,
1981 AccessControl ac, Expr *BW = nullptr,
1982 bool synthesized=false);
1984 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1986 /// Return the class interface that this ivar is logically contained
1987 /// in; this is either the interface where the ivar was declared, or the
1988 /// interface the ivar is conceptually a part of in the case of synthesized
1990 const ObjCInterfaceDecl *getContainingInterface() const;
1992 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1993 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1994 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1996 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1998 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
2000 AccessControl getCanonicalAccessControl() const {
2001 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
2004 void setSynthesize(bool synth) { Synthesized = synth; }
2005 bool getSynthesize() const { return Synthesized; }
2007 /// Retrieve the type of this instance variable when viewed as a member of a
2008 /// specific object type.
2009 QualType getUsageType(QualType objectType) const;
2011 // Implement isa/cast/dyncast/etc.
2012 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2013 static bool classofKind(Kind K) { return K == ObjCIvar; }
2016 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
2017 /// extensions and class's implementation
2018 ObjCIvarDecl *NextIvar = nullptr;
2020 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
2021 unsigned DeclAccess : 3;
2022 unsigned Synthesized : 1;
2025 /// Represents a field declaration created by an \@defs(...).
2026 class ObjCAtDefsFieldDecl : public FieldDecl {
2027 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
2028 SourceLocation IdLoc, IdentifierInfo *Id,
2029 QualType T, Expr *BW)
2030 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
2031 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
2032 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
2034 void anchor() override;
2037 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
2038 SourceLocation StartLoc,
2039 SourceLocation IdLoc, IdentifierInfo *Id,
2040 QualType T, Expr *BW);
2042 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2044 // Implement isa/cast/dyncast/etc.
2045 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2046 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
2049 /// Represents an Objective-C protocol declaration.
2051 /// Objective-C protocols declare a pure abstract type (i.e., no instance
2052 /// variables are permitted). Protocols originally drew inspiration from
2053 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
2054 /// syntax:-). Here is an example:
2057 /// \@protocol NSDraggingInfo <refproto1, refproto2>
2058 /// - (NSWindow *)draggingDestinationWindow;
2059 /// - (NSImage *)draggedImage;
2063 /// This says that NSDraggingInfo requires two methods and requires everything
2064 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
2068 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
2072 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
2073 /// protocols are in distinct namespaces. For example, Cocoa defines both
2074 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
2075 /// protocols are referenced using angle brackets as follows:
2077 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
2078 class ObjCProtocolDecl : public ObjCContainerDecl,
2079 public Redeclarable<ObjCProtocolDecl> {
2080 struct DefinitionData {
2081 // The declaration that defines this protocol.
2082 ObjCProtocolDecl *Definition;
2084 /// Referenced protocols
2085 ObjCProtocolList ReferencedProtocols;
2088 /// Contains a pointer to the data associated with this class,
2089 /// which will be NULL if this class has not yet been defined.
2091 /// The bit indicates when we don't need to check for out-of-date
2092 /// declarations. It will be set unless modules are enabled.
2093 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
2095 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
2096 SourceLocation nameLoc, SourceLocation atStartLoc,
2097 ObjCProtocolDecl *PrevDecl);
2099 void anchor() override;
2101 DefinitionData &data() const {
2102 assert(Data.getPointer() && "Objective-C protocol has no definition!");
2103 return *Data.getPointer();
2106 void allocateDefinitionData();
2108 using redeclarable_base = Redeclarable<ObjCProtocolDecl>;
2110 ObjCProtocolDecl *getNextRedeclarationImpl() override {
2111 return getNextRedeclaration();
2114 ObjCProtocolDecl *getPreviousDeclImpl() override {
2115 return getPreviousDecl();
2118 ObjCProtocolDecl *getMostRecentDeclImpl() override {
2119 return getMostRecentDecl();
2123 friend class ASTDeclReader;
2124 friend class ASTDeclWriter;
2125 friend class ASTReader;
2127 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2129 SourceLocation nameLoc,
2130 SourceLocation atStartLoc,
2131 ObjCProtocolDecl *PrevDecl);
2133 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2135 const ObjCProtocolList &getReferencedProtocols() const {
2136 assert(hasDefinition() && "No definition available!");
2137 return data().ReferencedProtocols;
2140 using protocol_iterator = ObjCProtocolList::iterator;
2141 using protocol_range = llvm::iterator_range<protocol_iterator>;
2143 protocol_range protocols() const {
2144 return protocol_range(protocol_begin(), protocol_end());
2147 protocol_iterator protocol_begin() const {
2148 if (!hasDefinition())
2149 return protocol_iterator();
2151 return data().ReferencedProtocols.begin();
2154 protocol_iterator protocol_end() const {
2155 if (!hasDefinition())
2156 return protocol_iterator();
2158 return data().ReferencedProtocols.end();
2161 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2162 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2164 protocol_loc_range protocol_locs() const {
2165 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2168 protocol_loc_iterator protocol_loc_begin() const {
2169 if (!hasDefinition())
2170 return protocol_loc_iterator();
2172 return data().ReferencedProtocols.loc_begin();
2175 protocol_loc_iterator protocol_loc_end() const {
2176 if (!hasDefinition())
2177 return protocol_loc_iterator();
2179 return data().ReferencedProtocols.loc_end();
2182 unsigned protocol_size() const {
2183 if (!hasDefinition())
2186 return data().ReferencedProtocols.size();
2189 /// setProtocolList - Set the list of protocols that this interface
2191 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2192 const SourceLocation *Locs, ASTContext &C) {
2193 assert(hasDefinition() && "Protocol is not defined");
2194 data().ReferencedProtocols.set(List, Num, Locs, C);
2197 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2199 // Lookup a method. First, we search locally. If a method isn't
2200 // found, we search referenced protocols and class categories.
2201 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2203 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2204 return lookupMethod(Sel, true/*isInstance*/);
2207 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2208 return lookupMethod(Sel, false/*isInstance*/);
2211 /// Determine whether this protocol has a definition.
2212 bool hasDefinition() const {
2213 // If the name of this protocol is out-of-date, bring it up-to-date, which
2214 // might bring in a definition.
2215 // Note: a null value indicates that we don't have a definition and that
2216 // modules are enabled.
2217 if (!Data.getOpaqueValue())
2218 getMostRecentDecl();
2220 return Data.getPointer();
2223 /// Retrieve the definition of this protocol, if any.
2224 ObjCProtocolDecl *getDefinition() {
2225 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2228 /// Retrieve the definition of this protocol, if any.
2229 const ObjCProtocolDecl *getDefinition() const {
2230 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2233 /// Determine whether this particular declaration is also the
2235 bool isThisDeclarationADefinition() const {
2236 return getDefinition() == this;
2239 /// Starts the definition of this Objective-C protocol.
2240 void startDefinition();
2242 /// Produce a name to be used for protocol's metadata. It comes either via
2243 /// objc_runtime_name attribute or protocol name.
2244 StringRef getObjCRuntimeNameAsString() const;
2246 SourceRange getSourceRange() const override LLVM_READONLY {
2247 if (isThisDeclarationADefinition())
2248 return ObjCContainerDecl::getSourceRange();
2250 return SourceRange(getAtStartLoc(), getLocation());
2253 using redecl_range = redeclarable_base::redecl_range;
2254 using redecl_iterator = redeclarable_base::redecl_iterator;
2256 using redeclarable_base::redecls_begin;
2257 using redeclarable_base::redecls_end;
2258 using redeclarable_base::redecls;
2259 using redeclarable_base::getPreviousDecl;
2260 using redeclarable_base::getMostRecentDecl;
2261 using redeclarable_base::isFirstDecl;
2263 /// Retrieves the canonical declaration of this Objective-C protocol.
2264 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2265 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2267 void collectPropertiesToImplement(PropertyMap &PM,
2268 PropertyDeclOrder &PO) const override;
2270 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2271 ProtocolPropertySet &PS,
2272 PropertyDeclOrder &PO) const;
2274 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2275 static bool classofKind(Kind K) { return K == ObjCProtocol; }
2278 /// ObjCCategoryDecl - Represents a category declaration. A category allows
2279 /// you to add methods to an existing class (without subclassing or modifying
2280 /// the original class interface or implementation:-). Categories don't allow
2281 /// you to add instance data. The following example adds "myMethod" to all
2282 /// NSView's within a process:
2284 /// \@interface NSView (MyViewMethods)
2288 /// Categories also allow you to split the implementation of a class across
2289 /// several files (a feature more naturally supported in C++).
2291 /// Categories were originally inspired by dynamic languages such as Common
2292 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
2293 /// don't support this level of dynamism, which is both powerful and dangerous.
2294 class ObjCCategoryDecl : public ObjCContainerDecl {
2295 /// Interface belonging to this category
2296 ObjCInterfaceDecl *ClassInterface;
2298 /// The type parameters associated with this category, if any.
2299 ObjCTypeParamList *TypeParamList = nullptr;
2301 /// referenced protocols in this category.
2302 ObjCProtocolList ReferencedProtocols;
2304 /// Next category belonging to this class.
2305 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
2306 ObjCCategoryDecl *NextClassCategory = nullptr;
2308 /// The location of the category name in this declaration.
2309 SourceLocation CategoryNameLoc;
2311 /// class extension may have private ivars.
2312 SourceLocation IvarLBraceLoc;
2313 SourceLocation IvarRBraceLoc;
2315 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2316 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2317 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2318 ObjCTypeParamList *typeParamList,
2319 SourceLocation IvarLBraceLoc = SourceLocation(),
2320 SourceLocation IvarRBraceLoc = SourceLocation());
2322 void anchor() override;
2325 friend class ASTDeclReader;
2326 friend class ASTDeclWriter;
2328 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2329 SourceLocation AtLoc,
2330 SourceLocation ClassNameLoc,
2331 SourceLocation CategoryNameLoc,
2333 ObjCInterfaceDecl *IDecl,
2334 ObjCTypeParamList *typeParamList,
2335 SourceLocation IvarLBraceLoc=SourceLocation(),
2336 SourceLocation IvarRBraceLoc=SourceLocation());
2337 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2339 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2340 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2342 /// Retrieve the type parameter list associated with this category or
2344 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2346 /// Set the type parameters of this category.
2348 /// This function is used by the AST importer, which must import the type
2349 /// parameters after creating their DeclContext to avoid loops.
2350 void setTypeParamList(ObjCTypeParamList *TPL);
2353 ObjCCategoryImplDecl *getImplementation() const;
2354 void setImplementation(ObjCCategoryImplDecl *ImplD);
2356 /// setProtocolList - Set the list of protocols that this interface
2358 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2359 const SourceLocation *Locs, ASTContext &C) {
2360 ReferencedProtocols.set(List, Num, Locs, C);
2363 const ObjCProtocolList &getReferencedProtocols() const {
2364 return ReferencedProtocols;
2367 using protocol_iterator = ObjCProtocolList::iterator;
2368 using protocol_range = llvm::iterator_range<protocol_iterator>;
2370 protocol_range protocols() const {
2371 return protocol_range(protocol_begin(), protocol_end());
2374 protocol_iterator protocol_begin() const {
2375 return ReferencedProtocols.begin();
2378 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2379 unsigned protocol_size() const { return ReferencedProtocols.size(); }
2381 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2382 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2384 protocol_loc_range protocol_locs() const {
2385 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2388 protocol_loc_iterator protocol_loc_begin() const {
2389 return ReferencedProtocols.loc_begin();
2392 protocol_loc_iterator protocol_loc_end() const {
2393 return ReferencedProtocols.loc_end();
2396 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2398 /// Retrieve the pointer to the next stored category (or extension),
2399 /// which may be hidden.
2400 ObjCCategoryDecl *getNextClassCategoryRaw() const {
2401 return NextClassCategory;
2404 bool IsClassExtension() const { return getIdentifier() == nullptr; }
2406 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2407 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2409 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2411 ivar_iterator ivar_begin() const {
2412 return ivar_iterator(decls_begin());
2415 ivar_iterator ivar_end() const {
2416 return ivar_iterator(decls_end());
2419 unsigned ivar_size() const {
2420 return std::distance(ivar_begin(), ivar_end());
2423 bool ivar_empty() const {
2424 return ivar_begin() == ivar_end();
2427 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2428 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2430 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2431 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2432 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2433 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2435 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2436 static bool classofKind(Kind K) { return K == ObjCCategory; }
2439 class ObjCImplDecl : public ObjCContainerDecl {
2440 /// Class interface for this class/category implementation
2441 ObjCInterfaceDecl *ClassInterface;
2443 void anchor() override;
2446 ObjCImplDecl(Kind DK, DeclContext *DC,
2447 ObjCInterfaceDecl *classInterface,
2449 SourceLocation nameLoc, SourceLocation atStartLoc)
2450 : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc),
2451 ClassInterface(classInterface) {}
2454 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2455 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2456 void setClassInterface(ObjCInterfaceDecl *IFace);
2458 void addInstanceMethod(ObjCMethodDecl *method) {
2459 // FIXME: Context should be set correctly before we get here.
2460 method->setLexicalDeclContext(this);
2464 void addClassMethod(ObjCMethodDecl *method) {
2465 // FIXME: Context should be set correctly before we get here.
2466 method->setLexicalDeclContext(this);
2470 void addPropertyImplementation(ObjCPropertyImplDecl *property);
2472 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2473 ObjCPropertyQueryKind queryKind) const;
2474 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2476 // Iterator access to properties.
2477 using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>;
2478 using propimpl_range =
2479 llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>;
2481 propimpl_range property_impls() const {
2482 return propimpl_range(propimpl_begin(), propimpl_end());
2485 propimpl_iterator propimpl_begin() const {
2486 return propimpl_iterator(decls_begin());
2489 propimpl_iterator propimpl_end() const {
2490 return propimpl_iterator(decls_end());
2493 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2495 static bool classofKind(Kind K) {
2496 return K >= firstObjCImpl && K <= lastObjCImpl;
2500 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
2501 /// \@implementation declaration. If a category class has declaration of a
2502 /// property, its implementation must be specified in the category's
2503 /// \@implementation declaration. Example:
2504 /// \@interface I \@end
2505 /// \@interface I(CATEGORY)
2506 /// \@property int p1, d1;
2508 /// \@implementation I(CATEGORY)
2509 /// \@dynamic p1,d1;
2512 /// ObjCCategoryImplDecl
2513 class ObjCCategoryImplDecl : public ObjCImplDecl {
2514 // Category name location
2515 SourceLocation CategoryNameLoc;
2517 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2518 ObjCInterfaceDecl *classInterface,
2519 SourceLocation nameLoc, SourceLocation atStartLoc,
2520 SourceLocation CategoryNameLoc)
2521 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id,
2522 nameLoc, atStartLoc),
2523 CategoryNameLoc(CategoryNameLoc) {}
2525 void anchor() override;
2528 friend class ASTDeclReader;
2529 friend class ASTDeclWriter;
2531 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2533 ObjCInterfaceDecl *classInterface,
2534 SourceLocation nameLoc,
2535 SourceLocation atStartLoc,
2536 SourceLocation CategoryNameLoc);
2537 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2539 ObjCCategoryDecl *getCategoryDecl() const;
2541 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2543 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2544 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2547 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2549 /// ObjCImplementationDecl - Represents a class definition - this is where
2550 /// method definitions are specified. For example:
2553 /// \@implementation MyClass
2554 /// - (void)myMethod { /* do something */ }
2558 /// In a non-fragile runtime, instance variables can appear in the class
2559 /// interface, class extensions (nameless categories), and in the implementation
2560 /// itself, as well as being synthesized as backing storage for properties.
2562 /// In a fragile runtime, instance variables are specified in the class
2563 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2564 /// we allow instance variables to be specified in the implementation. When
2565 /// specified, they need to be \em identical to the interface.
2566 class ObjCImplementationDecl : public ObjCImplDecl {
2567 /// Implementation Class's super class.
2568 ObjCInterfaceDecl *SuperClass;
2569 SourceLocation SuperLoc;
2571 /// \@implementation may have private ivars.
2572 SourceLocation IvarLBraceLoc;
2573 SourceLocation IvarRBraceLoc;
2575 /// Support for ivar initialization.
2576 /// The arguments used to initialize the ivars
2577 LazyCXXCtorInitializersPtr IvarInitializers;
2578 unsigned NumIvarInitializers = 0;
2580 /// Do the ivars of this class require initialization other than
2581 /// zero-initialization?
2582 bool HasNonZeroConstructors : 1;
2584 /// Do the ivars of this class require non-trivial destruction?
2585 bool HasDestructors : 1;
2587 ObjCImplementationDecl(DeclContext *DC,
2588 ObjCInterfaceDecl *classInterface,
2589 ObjCInterfaceDecl *superDecl,
2590 SourceLocation nameLoc, SourceLocation atStartLoc,
2591 SourceLocation superLoc = SourceLocation(),
2592 SourceLocation IvarLBraceLoc=SourceLocation(),
2593 SourceLocation IvarRBraceLoc=SourceLocation())
2594 : ObjCImplDecl(ObjCImplementation, DC, classInterface,
2595 classInterface ? classInterface->getIdentifier()
2597 nameLoc, atStartLoc),
2598 SuperClass(superDecl), SuperLoc(superLoc),
2599 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc),
2600 HasNonZeroConstructors(false), HasDestructors(false) {}
2602 void anchor() override;
2605 friend class ASTDeclReader;
2606 friend class ASTDeclWriter;
2608 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2609 ObjCInterfaceDecl *classInterface,
2610 ObjCInterfaceDecl *superDecl,
2611 SourceLocation nameLoc,
2612 SourceLocation atStartLoc,
2613 SourceLocation superLoc = SourceLocation(),
2614 SourceLocation IvarLBraceLoc=SourceLocation(),
2615 SourceLocation IvarRBraceLoc=SourceLocation());
2617 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2619 /// init_iterator - Iterates through the ivar initializer list.
2620 using init_iterator = CXXCtorInitializer **;
2622 /// init_const_iterator - Iterates through the ivar initializer list.
2623 using init_const_iterator = CXXCtorInitializer * const *;
2625 using init_range = llvm::iterator_range<init_iterator>;
2626 using init_const_range = llvm::iterator_range<init_const_iterator>;
2628 init_range inits() { return init_range(init_begin(), init_end()); }
2630 init_const_range inits() const {
2631 return init_const_range(init_begin(), init_end());
2634 /// init_begin() - Retrieve an iterator to the first initializer.
2635 init_iterator init_begin() {
2636 const auto *ConstThis = this;
2637 return const_cast<init_iterator>(ConstThis->init_begin());
2640 /// begin() - Retrieve an iterator to the first initializer.
2641 init_const_iterator init_begin() const;
2643 /// init_end() - Retrieve an iterator past the last initializer.
2644 init_iterator init_end() {
2645 return init_begin() + NumIvarInitializers;
2648 /// end() - Retrieve an iterator past the last initializer.
2649 init_const_iterator init_end() const {
2650 return init_begin() + NumIvarInitializers;
2653 /// getNumArgs - Number of ivars which must be initialized.
2654 unsigned getNumIvarInitializers() const {
2655 return NumIvarInitializers;
2658 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2659 NumIvarInitializers = numNumIvarInitializers;
2662 void setIvarInitializers(ASTContext &C,
2663 CXXCtorInitializer ** initializers,
2664 unsigned numInitializers);
2666 /// Do any of the ivars of this class (not counting its base classes)
2667 /// require construction other than zero-initialization?
2668 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2669 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2671 /// Do any of the ivars of this class (not counting its base classes)
2672 /// require non-trivial destruction?
2673 bool hasDestructors() const { return HasDestructors; }
2674 void setHasDestructors(bool val) { HasDestructors = val; }
2676 /// getIdentifier - Get the identifier that names the class
2677 /// interface associated with this implementation.
2678 IdentifierInfo *getIdentifier() const {
2679 return getClassInterface()->getIdentifier();
2682 /// getName - Get the name of identifier for the class interface associated
2683 /// with this implementation as a StringRef.
2685 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2687 StringRef getName() const {
2688 assert(getIdentifier() && "Name is not a simple identifier");
2689 return getIdentifier()->getName();
2692 /// Get the name of the class associated with this interface.
2694 // FIXME: Move to StringRef API.
2695 std::string getNameAsString() const {
2699 /// Produce a name to be used for class's metadata. It comes either via
2700 /// class's objc_runtime_name attribute or class name.
2701 StringRef getObjCRuntimeNameAsString() const;
2703 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2704 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2705 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2707 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2709 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2710 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2711 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2712 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2714 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2715 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2717 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2719 ivar_iterator ivar_begin() const {
2720 return ivar_iterator(decls_begin());
2723 ivar_iterator ivar_end() const {
2724 return ivar_iterator(decls_end());
2727 unsigned ivar_size() const {
2728 return std::distance(ivar_begin(), ivar_end());
2731 bool ivar_empty() const {
2732 return ivar_begin() == ivar_end();
2735 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2736 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2739 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2741 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2742 /// declared as \@compatibility_alias alias class.
2743 class ObjCCompatibleAliasDecl : public NamedDecl {
2744 /// Class that this is an alias of.
2745 ObjCInterfaceDecl *AliasedClass;
2747 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2748 ObjCInterfaceDecl* aliasedClass)
2749 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2751 void anchor() override;
2754 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2755 SourceLocation L, IdentifierInfo *Id,
2756 ObjCInterfaceDecl* aliasedClass);
2758 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2761 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2762 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2763 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2765 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2766 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2769 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2770 /// in a class or category implementation block. For example:
2771 /// \@synthesize prop1 = ivar1;
2773 class ObjCPropertyImplDecl : public Decl {
2781 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2783 /// For \@synthesize, the location of the ivar, if it was written in
2784 /// the source code.
2787 /// \@synthesize int a = b
2789 SourceLocation IvarLoc;
2791 /// Property declaration being implemented
2792 ObjCPropertyDecl *PropertyDecl;
2794 /// Null for \@dynamic. Required for \@synthesize.
2795 ObjCIvarDecl *PropertyIvarDecl;
2797 /// The getter's definition, which has an empty body if synthesized.
2798 ObjCMethodDecl *GetterMethodDecl = nullptr;
2799 /// The getter's definition, which has an empty body if synthesized.
2800 ObjCMethodDecl *SetterMethodDecl = nullptr;
2802 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2804 Expr *GetterCXXConstructor = nullptr;
2806 /// Null for \@dynamic. Non-null if property has assignment operator to call
2807 /// in Setter synthesis.
2808 Expr *SetterCXXAssignment = nullptr;
2810 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2811 ObjCPropertyDecl *property,
2813 ObjCIvarDecl *ivarDecl,
2814 SourceLocation ivarLoc)
2815 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2816 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) {
2817 assert(PK == Dynamic || PropertyIvarDecl);
2821 friend class ASTDeclReader;
2823 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2824 SourceLocation atLoc, SourceLocation L,
2825 ObjCPropertyDecl *property,
2827 ObjCIvarDecl *ivarDecl,
2828 SourceLocation ivarLoc);
2830 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2832 SourceRange getSourceRange() const override LLVM_READONLY;
2834 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
2835 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2837 ObjCPropertyDecl *getPropertyDecl() const {
2838 return PropertyDecl;
2840 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2842 Kind getPropertyImplementation() const {
2843 return PropertyIvarDecl ? Synthesize : Dynamic;
2846 ObjCIvarDecl *getPropertyIvarDecl() const {
2847 return PropertyIvarDecl;
2849 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2851 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2852 SourceLocation IvarLoc) {
2853 PropertyIvarDecl = Ivar;
2854 this->IvarLoc = IvarLoc;
2857 /// For \@synthesize, returns true if an ivar name was explicitly
2861 /// \@synthesize int a = b; // true
2862 /// \@synthesize int a; // false
2864 bool isIvarNameSpecified() const {
2865 return IvarLoc.isValid() && IvarLoc != getLocation();
2868 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2869 void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; }
2871 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2872 void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; }
2874 Expr *getGetterCXXConstructor() const {
2875 return GetterCXXConstructor;
2878 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2879 GetterCXXConstructor = getterCXXConstructor;
2882 Expr *getSetterCXXAssignment() const {
2883 return SetterCXXAssignment;
2886 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2887 SetterCXXAssignment = setterCXXAssignment;
2890 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2891 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2894 template<bool (*Filter)(ObjCCategoryDecl *)>
2896 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2897 findAcceptableCategory() {
2898 while (Current && !Filter(Current))
2899 Current = Current->getNextClassCategoryRaw();
2902 template<bool (*Filter)(ObjCCategoryDecl *)>
2903 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2904 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2905 Current = Current->getNextClassCategoryRaw();
2906 findAcceptableCategory();
2910 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2911 return !Cat->isHidden();
2914 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2915 return Cat->IsClassExtension() && !Cat->isHidden();
2918 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2919 return Cat->IsClassExtension();
2922 } // namespace clang
2924 #endif // LLVM_CLANG_AST_DECLOBJC_H