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 isImplicitlyDeclared = false, bool isDefined = false,
176 ImplementationControl impControl = None,
177 bool HasRelatedResultType = false);
179 SelectorLocationsKind getSelLocsKind() const {
180 return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
183 void setSelLocsKind(SelectorLocationsKind Kind) {
184 ObjCMethodDeclBits.SelLocsKind = Kind;
187 bool hasStandardSelLocs() const {
188 return getSelLocsKind() != SelLoc_NonStandard;
191 /// Get a pointer to the stored selector identifiers locations array.
192 /// No locations will be stored if HasStandardSelLocs is true.
193 SourceLocation *getStoredSelLocs() {
194 return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
196 const SourceLocation *getStoredSelLocs() const {
197 return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
200 /// Get a pointer to the stored selector identifiers locations array.
201 /// No locations will be stored if HasStandardSelLocs is true.
202 ParmVarDecl **getParams() {
203 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
205 const ParmVarDecl *const *getParams() const {
206 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
209 /// Get the number of stored selector identifiers locations.
210 /// No locations will be stored if HasStandardSelLocs is true.
211 unsigned getNumStoredSelLocs() const {
212 if (hasStandardSelLocs())
214 return getNumSelectorLocs();
217 void setParamsAndSelLocs(ASTContext &C,
218 ArrayRef<ParmVarDecl*> Params,
219 ArrayRef<SourceLocation> SelLocs);
221 /// A definition will return its interface declaration.
222 /// An interface declaration will return its definition.
223 /// Otherwise it will return itself.
224 ObjCMethodDecl *getNextRedeclarationImpl() override;
227 friend class ASTDeclReader;
228 friend class ASTDeclWriter;
230 static ObjCMethodDecl *
231 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
232 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
233 DeclContext *contextDecl, bool isInstance = true,
234 bool isVariadic = false, bool isPropertyAccessor = false,
235 bool isImplicitlyDeclared = false, bool isDefined = false,
236 ImplementationControl impControl = None,
237 bool HasRelatedResultType = false);
239 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
241 ObjCMethodDecl *getCanonicalDecl() override;
242 const ObjCMethodDecl *getCanonicalDecl() const {
243 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
246 ObjCDeclQualifier getObjCDeclQualifier() const {
247 return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier);
250 void setObjCDeclQualifier(ObjCDeclQualifier QV) {
251 ObjCMethodDeclBits.objcDeclQualifier = QV;
254 /// Determine whether this method has a result type that is related
255 /// to the message receiver's type.
256 bool hasRelatedResultType() const {
257 return ObjCMethodDeclBits.RelatedResultType;
260 /// Note whether this method has a related result type.
261 void setRelatedResultType(bool RRT = true) {
262 ObjCMethodDeclBits.RelatedResultType = RRT;
265 /// True if this is a method redeclaration in the same interface.
266 bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; }
267 void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; }
268 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
270 /// True if redeclared in the same interface.
271 bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; }
272 void setHasRedeclaration(bool HRD) const {
273 ObjCMethodDeclBits.HasRedeclaration = HRD;
276 /// Returns the location where the declarator ends. It will be
277 /// the location of ';' for a method declaration and the location of '{'
278 /// for a method definition.
279 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
281 // Location information, modeled after the Stmt API.
282 SourceLocation getBeginLoc() const LLVM_READONLY { return getLocation(); }
283 SourceLocation getEndLoc() const LLVM_READONLY;
284 SourceRange getSourceRange() const override LLVM_READONLY {
285 return SourceRange(getLocation(), getEndLoc());
288 SourceLocation getSelectorStartLoc() const {
290 return getBeginLoc();
291 return getSelectorLoc(0);
294 SourceLocation getSelectorLoc(unsigned Index) const {
295 assert(Index < getNumSelectorLocs() && "Index out of range!");
296 if (hasStandardSelLocs())
297 return getStandardSelectorLoc(Index, getSelector(),
298 getSelLocsKind() == SelLoc_StandardWithSpace,
301 return getStoredSelLocs()[Index];
304 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
306 unsigned getNumSelectorLocs() const {
309 Selector Sel = getSelector();
310 if (Sel.isUnarySelector())
312 return Sel.getNumArgs();
315 ObjCInterfaceDecl *getClassInterface();
316 const ObjCInterfaceDecl *getClassInterface() const {
317 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
320 Selector getSelector() const { return getDeclName().getObjCSelector(); }
322 QualType getReturnType() const { return MethodDeclType; }
323 void setReturnType(QualType T) { MethodDeclType = T; }
324 SourceRange getReturnTypeSourceRange() const;
326 /// Determine the type of an expression that sends a message to this
327 /// function. This replaces the type parameters with the types they would
328 /// get if the receiver was parameterless (e.g. it may replace the type
329 /// parameter with 'id').
330 QualType getSendResultType() const;
332 /// Determine the type of an expression that sends a message to this
333 /// function with the given receiver type.
334 QualType getSendResultType(QualType receiverType) const;
336 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
337 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
339 // Iterator access to formal parameters.
340 unsigned param_size() const { return NumParams; }
342 using param_const_iterator = const ParmVarDecl *const *;
343 using param_iterator = ParmVarDecl *const *;
344 using param_range = llvm::iterator_range<param_iterator>;
345 using param_const_range = llvm::iterator_range<param_const_iterator>;
347 param_const_iterator param_begin() const {
348 return param_const_iterator(getParams());
351 param_const_iterator param_end() const {
352 return param_const_iterator(getParams() + NumParams);
355 param_iterator param_begin() { return param_iterator(getParams()); }
356 param_iterator param_end() { return param_iterator(getParams() + NumParams); }
358 // This method returns and of the parameters which are part of the selector
359 // name mangling requirements.
360 param_const_iterator sel_param_end() const {
361 return param_begin() + getSelector().getNumArgs();
364 // ArrayRef access to formal parameters. This should eventually
365 // replace the iterator interface above.
366 ArrayRef<ParmVarDecl*> parameters() const {
367 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
371 ParmVarDecl *getParamDecl(unsigned Idx) {
372 assert(Idx < NumParams && "Index out of bounds!");
373 return getParams()[Idx];
375 const ParmVarDecl *getParamDecl(unsigned Idx) const {
376 return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx);
379 /// Sets the method's parameters and selector source locations.
380 /// If the method is implicit (not coming from source) \p SelLocs is
382 void setMethodParams(ASTContext &C,
383 ArrayRef<ParmVarDecl*> Params,
384 ArrayRef<SourceLocation> SelLocs = llvm::None);
386 // Iterator access to parameter types.
388 QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); }
391 using param_type_iterator =
392 llvm::mapped_iterator<param_const_iterator, GetTypeFn>;
394 param_type_iterator param_type_begin() const {
395 return llvm::map_iterator(param_begin(), GetTypeFn());
398 param_type_iterator param_type_end() const {
399 return llvm::map_iterator(param_end(), GetTypeFn());
402 /// createImplicitParams - Used to lazily create the self and cmd
403 /// implict parameters. This must be called prior to using getSelfDecl()
404 /// or getCmdDecl(). The call is ignored if the implicit parameters
405 /// have already been created.
406 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
408 /// \return the type for \c self and set \arg selfIsPseudoStrong and
409 /// \arg selfIsConsumed accordingly.
410 QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
411 bool &selfIsPseudoStrong, bool &selfIsConsumed);
413 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
414 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
415 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
416 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
418 /// Determines the family of this method.
419 ObjCMethodFamily getMethodFamily() const;
421 bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; }
422 void setInstanceMethod(bool isInst) {
423 ObjCMethodDeclBits.IsInstance = isInst;
426 bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; }
427 void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; }
429 bool isClassMethod() const { return !isInstanceMethod(); }
431 bool isPropertyAccessor() const {
432 return ObjCMethodDeclBits.IsPropertyAccessor;
435 void setPropertyAccessor(bool isAccessor) {
436 ObjCMethodDeclBits.IsPropertyAccessor = isAccessor;
439 bool isDefined() const { return ObjCMethodDeclBits.IsDefined; }
440 void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; }
442 /// Whether this method overrides any other in the class hierarchy.
444 /// A method is said to override any method in the class's
445 /// base classes, its protocols, or its categories' protocols, that has
446 /// the same selector and is of the same kind (class or instance).
447 /// A method in an implementation is not considered as overriding the same
448 /// method in the interface or its categories.
449 bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; }
450 void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; }
452 /// Return overridden methods for the given \p Method.
454 /// An ObjC method is considered to override any method in the class's
455 /// base classes (and base's categories), its protocols, or its categories'
456 /// protocols, that has
457 /// the same selector and is of the same kind (class or instance).
458 /// A method in an implementation is not considered as overriding the same
459 /// method in the interface or its categories.
460 void getOverriddenMethods(
461 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
463 /// True if the method was a definition but its body was skipped.
464 bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; }
465 void setHasSkippedBody(bool Skipped = true) {
466 ObjCMethodDeclBits.HasSkippedBody = Skipped;
469 /// Returns the property associated with this method's selector.
471 /// Note that even if this particular method is not marked as a property
472 /// accessor, it is still possible for it to match a property declared in a
473 /// superclass. Pass \c false if you only want to check the current class.
474 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
476 // Related to protocols declared in \@protocol
477 void setDeclImplementation(ImplementationControl ic) {
478 ObjCMethodDeclBits.DeclImplementation = ic;
481 ImplementationControl getImplementationControl() const {
482 return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
485 bool isOptional() const {
486 return getImplementationControl() == Optional;
489 /// Returns true if this specific method declaration is marked with the
490 /// designated initializer attribute.
491 bool isThisDeclarationADesignatedInitializer() const;
493 /// Returns true if the method selector resolves to a designated initializer
494 /// in the class's interface.
496 /// \param InitMethod if non-null and the function returns true, it receives
497 /// the method declaration that was marked with the designated initializer
499 bool isDesignatedInitializerForTheInterface(
500 const ObjCMethodDecl **InitMethod = nullptr) const;
502 /// Determine whether this method has a body.
503 bool hasBody() const override { return Body.isValid(); }
505 /// Retrieve the body of this method, if it has one.
506 Stmt *getBody() const override;
508 void setLazyBody(uint64_t Offset) { Body = Offset; }
510 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
511 void setBody(Stmt *B) { Body = B; }
513 /// Returns whether this specific method is a definition.
514 bool isThisDeclarationADefinition() const { return hasBody(); }
516 /// Is this method defined in the NSObject base class?
517 bool definedInNSObject(const ASTContext &) const;
519 // Implement isa/cast/dyncast/etc.
520 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
521 static bool classofKind(Kind K) { return K == ObjCMethod; }
523 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
524 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
527 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
528 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
532 /// Describes the variance of a given generic parameter.
533 enum class ObjCTypeParamVariance : uint8_t {
534 /// The parameter is invariant: must match exactly.
537 /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
538 /// the type parameter is covariant and T is a subtype of U.
541 /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
542 /// when the type parameter is covariant and U is a subtype of T.
546 /// Represents the declaration of an Objective-C type parameter.
549 /// @interface NSDictionary<Key : id<NSCopying>, Value>
553 /// In the example above, both \c Key and \c Value are represented by
554 /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
555 /// while \c Value gets an implicit bound of \c id.
557 /// Objective-C type parameters are typedef-names in the grammar,
558 class ObjCTypeParamDecl : public TypedefNameDecl {
559 /// Index of this type parameter in the type parameter list.
562 /// The variance of the type parameter.
563 unsigned Variance : 2;
565 /// The location of the variance, if any.
566 SourceLocation VarianceLoc;
568 /// The location of the ':', which will be valid when the bound was
569 /// explicitly specified.
570 SourceLocation ColonLoc;
572 ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
573 ObjCTypeParamVariance variance, SourceLocation varianceLoc,
575 SourceLocation nameLoc, IdentifierInfo *name,
576 SourceLocation colonLoc, TypeSourceInfo *boundInfo)
577 : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
579 Index(index), Variance(static_cast<unsigned>(variance)),
580 VarianceLoc(varianceLoc), ColonLoc(colonLoc) {}
582 void anchor() override;
585 friend class ASTDeclReader;
586 friend class ASTDeclWriter;
588 static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
589 ObjCTypeParamVariance variance,
590 SourceLocation varianceLoc,
592 SourceLocation nameLoc,
593 IdentifierInfo *name,
594 SourceLocation colonLoc,
595 TypeSourceInfo *boundInfo);
596 static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
598 SourceRange getSourceRange() const override LLVM_READONLY;
600 /// Determine the variance of this type parameter.
601 ObjCTypeParamVariance getVariance() const {
602 return static_cast<ObjCTypeParamVariance>(Variance);
605 /// Set the variance of this type parameter.
606 void setVariance(ObjCTypeParamVariance variance) {
607 Variance = static_cast<unsigned>(variance);
610 /// Retrieve the location of the variance keyword.
611 SourceLocation getVarianceLoc() const { return VarianceLoc; }
613 /// Retrieve the index into its type parameter list.
614 unsigned getIndex() const { return Index; }
616 /// Whether this type parameter has an explicitly-written type bound, e.g.,
618 bool hasExplicitBound() const { return ColonLoc.isValid(); }
620 /// Retrieve the location of the ':' separating the type parameter name
621 /// from the explicitly-specified bound.
622 SourceLocation getColonLoc() const { return ColonLoc; }
624 // Implement isa/cast/dyncast/etc.
625 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
626 static bool classofKind(Kind K) { return K == ObjCTypeParam; }
629 /// Stores a list of Objective-C type parameters for a parameterized class
630 /// or a category/extension thereof.
633 /// @interface NSArray<T> // stores the <T>
636 class ObjCTypeParamList final
637 : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
638 /// Stores the components of a SourceRange as a POD.
639 struct PODSourceRange {
645 /// Location of the left and right angle brackets.
646 PODSourceRange Brackets;
648 // Used only for alignment.
649 ObjCTypeParamDecl *AlignmentHack;
652 /// The number of parameters in the list, which are tail-allocated.
655 ObjCTypeParamList(SourceLocation lAngleLoc,
656 ArrayRef<ObjCTypeParamDecl *> typeParams,
657 SourceLocation rAngleLoc);
660 friend TrailingObjects;
662 /// Create a new Objective-C type parameter list.
663 static ObjCTypeParamList *create(ASTContext &ctx,
664 SourceLocation lAngleLoc,
665 ArrayRef<ObjCTypeParamDecl *> typeParams,
666 SourceLocation rAngleLoc);
668 /// Iterate through the type parameters in the list.
669 using iterator = ObjCTypeParamDecl **;
671 iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
673 iterator end() { return begin() + size(); }
675 /// Determine the number of type parameters in this list.
676 unsigned size() const { return NumParams; }
678 // Iterate through the type parameters in the list.
679 using const_iterator = ObjCTypeParamDecl * const *;
681 const_iterator begin() const {
682 return getTrailingObjects<ObjCTypeParamDecl *>();
685 const_iterator end() const {
686 return begin() + size();
689 ObjCTypeParamDecl *front() const {
690 assert(size() > 0 && "empty Objective-C type parameter list");
694 ObjCTypeParamDecl *back() const {
695 assert(size() > 0 && "empty Objective-C type parameter list");
699 SourceLocation getLAngleLoc() const {
700 return SourceLocation::getFromRawEncoding(Brackets.Begin);
703 SourceLocation getRAngleLoc() const {
704 return SourceLocation::getFromRawEncoding(Brackets.End);
707 SourceRange getSourceRange() const {
708 return SourceRange(getLAngleLoc(), getRAngleLoc());
711 /// Gather the default set of type arguments to be substituted for
712 /// these type parameters when dealing with an unspecialized type.
713 void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
716 enum class ObjCPropertyQueryKind : uint8_t {
717 OBJC_PR_query_unknown = 0x00,
718 OBJC_PR_query_instance,
722 /// Represents one property declaration in an Objective-C interface.
726 /// \@property (assign, readwrite) int MyProperty;
728 class ObjCPropertyDecl : public NamedDecl {
729 void anchor() override;
732 enum PropertyAttributeKind {
733 OBJC_PR_noattr = 0x00,
734 OBJC_PR_readonly = 0x01,
735 OBJC_PR_getter = 0x02,
736 OBJC_PR_assign = 0x04,
737 OBJC_PR_readwrite = 0x08,
738 OBJC_PR_retain = 0x10,
740 OBJC_PR_nonatomic = 0x40,
741 OBJC_PR_setter = 0x80,
742 OBJC_PR_atomic = 0x100,
743 OBJC_PR_weak = 0x200,
744 OBJC_PR_strong = 0x400,
745 OBJC_PR_unsafe_unretained = 0x800,
746 /// Indicates that the nullability of the type was spelled with a
747 /// property attribute rather than a type qualifier.
748 OBJC_PR_nullability = 0x1000,
749 OBJC_PR_null_resettable = 0x2000,
750 OBJC_PR_class = 0x4000
751 // Adding a property should change NumPropertyAttrsBits
755 /// Number of bits fitting all the property attributes.
756 NumPropertyAttrsBits = 15
759 enum SetterKind { Assign, Retain, Copy, Weak };
760 enum PropertyControl { None, Required, Optional };
763 // location of \@property
764 SourceLocation AtLoc;
766 // location of '(' starting attribute list or null.
767 SourceLocation LParenLoc;
770 TypeSourceInfo *DeclTypeSourceInfo;
771 unsigned PropertyAttributes : NumPropertyAttrsBits;
772 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
774 // \@required/\@optional
775 unsigned PropertyImplementation : 2;
777 // getter name of NULL if no getter
780 // setter name of NULL if no setter
783 // location of the getter attribute's value
784 SourceLocation GetterNameLoc;
786 // location of the setter attribute's value
787 SourceLocation SetterNameLoc;
789 // Declaration of getter instance method
790 ObjCMethodDecl *GetterMethodDecl = nullptr;
792 // Declaration of setter instance method
793 ObjCMethodDecl *SetterMethodDecl = nullptr;
795 // Synthesize ivar for this property
796 ObjCIvarDecl *PropertyIvarDecl = nullptr;
798 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
799 SourceLocation AtLocation, SourceLocation LParenLocation,
800 QualType T, TypeSourceInfo *TSI,
801 PropertyControl propControl)
802 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
803 LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
804 PropertyAttributes(OBJC_PR_noattr),
805 PropertyAttributesAsWritten(OBJC_PR_noattr),
806 PropertyImplementation(propControl), GetterName(Selector()),
807 SetterName(Selector()) {}
810 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
812 IdentifierInfo *Id, SourceLocation AtLocation,
813 SourceLocation LParenLocation,
816 PropertyControl propControl = None);
818 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
820 SourceLocation getAtLoc() const { return AtLoc; }
821 void setAtLoc(SourceLocation L) { AtLoc = L; }
823 SourceLocation getLParenLoc() const { return LParenLoc; }
824 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
826 TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
828 QualType getType() const { return DeclType; }
830 void setType(QualType T, TypeSourceInfo *TSI) {
832 DeclTypeSourceInfo = TSI;
835 /// Retrieve the type when this property is used with a specific base object
837 QualType getUsageType(QualType objectType) const;
839 PropertyAttributeKind getPropertyAttributes() const {
840 return PropertyAttributeKind(PropertyAttributes);
843 void setPropertyAttributes(PropertyAttributeKind PRVal) {
844 PropertyAttributes |= PRVal;
847 void overwritePropertyAttributes(unsigned PRVal) {
848 PropertyAttributes = PRVal;
851 PropertyAttributeKind getPropertyAttributesAsWritten() const {
852 return PropertyAttributeKind(PropertyAttributesAsWritten);
855 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
856 PropertyAttributesAsWritten = PRVal;
859 // Helper methods for accessing attributes.
861 /// isReadOnly - Return true iff the property has a setter.
862 bool isReadOnly() const {
863 return (PropertyAttributes & OBJC_PR_readonly);
866 /// isAtomic - Return true if the property is atomic.
867 bool isAtomic() const {
868 return (PropertyAttributes & OBJC_PR_atomic);
871 /// isRetaining - Return true if the property retains its value.
872 bool isRetaining() const {
873 return (PropertyAttributes &
874 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
877 bool isInstanceProperty() const { return !isClassProperty(); }
878 bool isClassProperty() const { return PropertyAttributes & OBJC_PR_class; }
880 ObjCPropertyQueryKind getQueryKind() const {
881 return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
882 ObjCPropertyQueryKind::OBJC_PR_query_instance;
885 static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
886 return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
887 ObjCPropertyQueryKind::OBJC_PR_query_instance;
890 /// getSetterKind - Return the method used for doing assignment in
891 /// the property setter. This is only valid if the property has been
892 /// defined to have a setter.
893 SetterKind getSetterKind() const {
894 if (PropertyAttributes & OBJC_PR_strong)
895 return getType()->isBlockPointerType() ? Copy : Retain;
896 if (PropertyAttributes & OBJC_PR_retain)
898 if (PropertyAttributes & OBJC_PR_copy)
900 if (PropertyAttributes & OBJC_PR_weak)
905 Selector getGetterName() const { return GetterName; }
906 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
908 void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
913 Selector getSetterName() const { return SetterName; }
914 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
916 void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
921 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
922 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
924 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
925 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
927 // Related to \@optional/\@required declared in \@protocol
928 void setPropertyImplementation(PropertyControl pc) {
929 PropertyImplementation = pc;
932 PropertyControl getPropertyImplementation() const {
933 return PropertyControl(PropertyImplementation);
936 bool isOptional() const {
937 return getPropertyImplementation() == PropertyControl::Optional;
940 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
941 PropertyIvarDecl = Ivar;
944 ObjCIvarDecl *getPropertyIvarDecl() const {
945 return PropertyIvarDecl;
948 SourceRange getSourceRange() const override LLVM_READONLY {
949 return SourceRange(AtLoc, getLocation());
952 /// Get the default name of the synthesized ivar.
953 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
955 /// Lookup a property by name in the specified DeclContext.
956 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
957 const IdentifierInfo *propertyID,
958 ObjCPropertyQueryKind queryKind);
960 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
961 static bool classofKind(Kind K) { return K == ObjCProperty; }
964 /// ObjCContainerDecl - Represents a container for method declarations.
965 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
966 /// ObjCProtocolDecl, and ObjCImplDecl.
968 class ObjCContainerDecl : public NamedDecl, public DeclContext {
969 // This class stores some data in DeclContext::ObjCContainerDeclBits
970 // to save some space. Use the provided accessors to access it.
972 // These two locations in the range mark the end of the method container.
973 // The first points to the '@' token, and the second to the 'end' token.
976 void anchor() override;
979 ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id,
980 SourceLocation nameLoc, SourceLocation atStartLoc);
982 // Iterator access to instance/class properties.
983 using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>;
985 llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>;
987 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
989 prop_iterator prop_begin() const {
990 return prop_iterator(decls_begin());
993 prop_iterator prop_end() const {
994 return prop_iterator(decls_end());
997 using instprop_iterator =
998 filtered_decl_iterator<ObjCPropertyDecl,
999 &ObjCPropertyDecl::isInstanceProperty>;
1000 using instprop_range = llvm::iterator_range<instprop_iterator>;
1002 instprop_range instance_properties() const {
1003 return instprop_range(instprop_begin(), instprop_end());
1006 instprop_iterator instprop_begin() const {
1007 return instprop_iterator(decls_begin());
1010 instprop_iterator instprop_end() const {
1011 return instprop_iterator(decls_end());
1014 using classprop_iterator =
1015 filtered_decl_iterator<ObjCPropertyDecl,
1016 &ObjCPropertyDecl::isClassProperty>;
1017 using classprop_range = llvm::iterator_range<classprop_iterator>;
1019 classprop_range class_properties() const {
1020 return classprop_range(classprop_begin(), classprop_end());
1023 classprop_iterator classprop_begin() const {
1024 return classprop_iterator(decls_begin());
1027 classprop_iterator classprop_end() const {
1028 return classprop_iterator(decls_end());
1031 // Iterator access to instance/class methods.
1032 using method_iterator = specific_decl_iterator<ObjCMethodDecl>;
1033 using method_range =
1034 llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>;
1036 method_range methods() const {
1037 return method_range(meth_begin(), meth_end());
1040 method_iterator meth_begin() const {
1041 return method_iterator(decls_begin());
1044 method_iterator meth_end() const {
1045 return method_iterator(decls_end());
1048 using instmeth_iterator =
1049 filtered_decl_iterator<ObjCMethodDecl,
1050 &ObjCMethodDecl::isInstanceMethod>;
1051 using instmeth_range = llvm::iterator_range<instmeth_iterator>;
1053 instmeth_range instance_methods() const {
1054 return instmeth_range(instmeth_begin(), instmeth_end());
1057 instmeth_iterator instmeth_begin() const {
1058 return instmeth_iterator(decls_begin());
1061 instmeth_iterator instmeth_end() const {
1062 return instmeth_iterator(decls_end());
1065 using classmeth_iterator =
1066 filtered_decl_iterator<ObjCMethodDecl,
1067 &ObjCMethodDecl::isClassMethod>;
1068 using classmeth_range = llvm::iterator_range<classmeth_iterator>;
1070 classmeth_range class_methods() const {
1071 return classmeth_range(classmeth_begin(), classmeth_end());
1074 classmeth_iterator classmeth_begin() const {
1075 return classmeth_iterator(decls_begin());
1078 classmeth_iterator classmeth_end() const {
1079 return classmeth_iterator(decls_end());
1082 // Get the local instance/class method declared in this interface.
1083 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1084 bool AllowHidden = false) const;
1086 ObjCMethodDecl *getInstanceMethod(Selector Sel,
1087 bool AllowHidden = false) const {
1088 return getMethod(Sel, true/*isInstance*/, AllowHidden);
1091 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1092 return getMethod(Sel, false/*isInstance*/, AllowHidden);
1095 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1096 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1099 FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1100 ObjCPropertyQueryKind QueryKind) const;
1103 llvm::DenseMap<std::pair<IdentifierInfo *, unsigned/*isClassProperty*/>,
1104 ObjCPropertyDecl *>;
1105 using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>;
1106 using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>;
1108 /// This routine collects list of properties to be implemented in the class.
1109 /// This includes, class's and its conforming protocols' properties.
1110 /// Note, the superclass's properties are not included in the list.
1111 virtual void collectPropertiesToImplement(PropertyMap &PM,
1112 PropertyDeclOrder &PO) const {}
1114 SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
1116 void setAtStartLoc(SourceLocation Loc) {
1117 ObjCContainerDeclBits.AtStart = Loc;
1120 // Marks the end of the container.
1121 SourceRange getAtEndRange() const { return AtEnd; }
1123 void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; }
1125 SourceRange getSourceRange() const override LLVM_READONLY {
1126 return SourceRange(getAtStartLoc(), getAtEndRange().getEnd());
1129 // Implement isa/cast/dyncast/etc.
1130 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1132 static bool classofKind(Kind K) {
1133 return K >= firstObjCContainer &&
1134 K <= lastObjCContainer;
1137 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1138 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1141 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1142 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1146 /// Represents an ObjC class declaration.
1151 /// // MostPrimitive declares no super class (not particularly useful).
1152 /// \@interface MostPrimitive
1153 /// // no instance variables or methods.
1156 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1157 /// \@interface NSResponder : NSObject \<NSCoding>
1158 /// { // instance variables are represented by ObjCIvarDecl.
1159 /// id nextResponder; // nextResponder instance variable.
1161 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1162 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1163 /// \@end // to an NSEvent.
1166 /// Unlike C/C++, forward class declarations are accomplished with \@class.
1167 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
1168 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1169 /// typically inherit from NSObject (an exception is NSProxy).
1171 class ObjCInterfaceDecl : public ObjCContainerDecl
1172 , public Redeclarable<ObjCInterfaceDecl> {
1173 friend class ASTContext;
1175 /// TypeForDecl - This indicates the Type object that represents this
1176 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
1177 mutable const Type *TypeForDecl = nullptr;
1179 struct DefinitionData {
1180 /// The definition of this class, for quick access from any
1182 ObjCInterfaceDecl *Definition = nullptr;
1184 /// When non-null, this is always an ObjCObjectType.
1185 TypeSourceInfo *SuperClassTInfo = nullptr;
1187 /// Protocols referenced in the \@interface declaration
1188 ObjCProtocolList ReferencedProtocols;
1190 /// Protocols reference in both the \@interface and class extensions.
1191 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1193 /// List of categories and class extensions defined for this class.
1195 /// Categories are stored as a linked list in the AST, since the categories
1196 /// and class extensions come long after the initial interface declaration,
1197 /// and we avoid dynamically-resized arrays in the AST wherever possible.
1198 ObjCCategoryDecl *CategoryList = nullptr;
1200 /// IvarList - List of all ivars defined by this class; including class
1201 /// extensions and implementation. This list is built lazily.
1202 ObjCIvarDecl *IvarList = nullptr;
1204 /// Indicates that the contents of this Objective-C class will be
1205 /// completed by the external AST source when required.
1206 mutable unsigned ExternallyCompleted : 1;
1208 /// Indicates that the ivar cache does not yet include ivars
1209 /// declared in the implementation.
1210 mutable unsigned IvarListMissingImplementation : 1;
1212 /// Indicates that this interface decl contains at least one initializer
1213 /// marked with the 'objc_designated_initializer' attribute.
1214 unsigned HasDesignatedInitializers : 1;
1216 enum InheritedDesignatedInitializersState {
1217 /// We didn't calculate whether the designated initializers should be
1218 /// inherited or not.
1221 /// Designated initializers are inherited for the super class.
1224 /// The class does not inherit designated initializers.
1225 IDI_NotInherited = 2
1228 /// One of the \c InheritedDesignatedInitializersState enumeratos.
1229 mutable unsigned InheritedDesignatedInitializers : 2;
1231 /// The location of the last location in this declaration, before
1232 /// the properties/methods. For example, this will be the '>', '}', or
1234 SourceLocation EndLoc;
1237 : ExternallyCompleted(false), IvarListMissingImplementation(true),
1238 HasDesignatedInitializers(false),
1239 InheritedDesignatedInitializers(IDI_Unknown) {}
1242 /// The type parameters associated with this class, if any.
1243 ObjCTypeParamList *TypeParamList = nullptr;
1245 /// Contains a pointer to the data associated with this class,
1246 /// which will be NULL if this class has not yet been defined.
1248 /// The bit indicates when we don't need to check for out-of-date
1249 /// declarations. It will be set unless modules are enabled.
1250 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1252 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1253 IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1254 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1257 void anchor() override;
1259 void LoadExternalDefinition() const;
1261 DefinitionData &data() const {
1262 assert(Data.getPointer() && "Declaration has no definition!");
1263 return *Data.getPointer();
1266 /// Allocate the definition data for this class.
1267 void allocateDefinitionData();
1269 using redeclarable_base = Redeclarable<ObjCInterfaceDecl>;
1271 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1272 return getNextRedeclaration();
1275 ObjCInterfaceDecl *getPreviousDeclImpl() override {
1276 return getPreviousDecl();
1279 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1280 return getMostRecentDecl();
1284 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1285 SourceLocation atLoc,
1287 ObjCTypeParamList *typeParamList,
1288 ObjCInterfaceDecl *PrevDecl,
1289 SourceLocation ClassLoc = SourceLocation(),
1290 bool isInternal = false);
1292 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1294 /// Retrieve the type parameters of this class.
1296 /// This function looks for a type parameter list for the given
1297 /// class; if the class has been declared (with \c \@class) but not
1298 /// defined (with \c \@interface), it will search for a declaration that
1299 /// has type parameters, skipping any declarations that do not.
1300 ObjCTypeParamList *getTypeParamList() const;
1302 /// Set the type parameters of this class.
1304 /// This function is used by the AST importer, which must import the type
1305 /// parameters after creating their DeclContext to avoid loops.
1306 void setTypeParamList(ObjCTypeParamList *TPL);
1308 /// Retrieve the type parameters written on this particular declaration of
1310 ObjCTypeParamList *getTypeParamListAsWritten() const {
1311 return TypeParamList;
1314 SourceRange getSourceRange() const override LLVM_READONLY {
1315 if (isThisDeclarationADefinition())
1316 return ObjCContainerDecl::getSourceRange();
1318 return SourceRange(getAtStartLoc(), getLocation());
1321 /// Indicate that this Objective-C class is complete, but that
1322 /// the external AST source will be responsible for filling in its contents
1323 /// when a complete class is required.
1324 void setExternallyCompleted();
1326 /// Indicate that this interface decl contains at least one initializer
1327 /// marked with the 'objc_designated_initializer' attribute.
1328 void setHasDesignatedInitializers();
1330 /// Returns true if this interface decl contains at least one initializer
1331 /// marked with the 'objc_designated_initializer' attribute.
1332 bool hasDesignatedInitializers() const;
1334 /// Returns true if this interface decl declares a designated initializer
1335 /// or it inherites one from its super class.
1336 bool declaresOrInheritsDesignatedInitializers() const {
1337 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1340 const ObjCProtocolList &getReferencedProtocols() const {
1341 assert(hasDefinition() && "Caller did not check for forward reference!");
1342 if (data().ExternallyCompleted)
1343 LoadExternalDefinition();
1345 return data().ReferencedProtocols;
1348 ObjCImplementationDecl *getImplementation() const;
1349 void setImplementation(ObjCImplementationDecl *ImplD);
1351 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1353 // Get the local instance/class method declared in a category.
1354 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1355 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1357 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1358 return isInstance ? getCategoryInstanceMethod(Sel)
1359 : getCategoryClassMethod(Sel);
1362 using protocol_iterator = ObjCProtocolList::iterator;
1363 using protocol_range = llvm::iterator_range<protocol_iterator>;
1365 protocol_range protocols() const {
1366 return protocol_range(protocol_begin(), protocol_end());
1369 protocol_iterator protocol_begin() const {
1370 // FIXME: Should make sure no callers ever do this.
1371 if (!hasDefinition())
1372 return protocol_iterator();
1374 if (data().ExternallyCompleted)
1375 LoadExternalDefinition();
1377 return data().ReferencedProtocols.begin();
1380 protocol_iterator protocol_end() const {
1381 // FIXME: Should make sure no callers ever do this.
1382 if (!hasDefinition())
1383 return protocol_iterator();
1385 if (data().ExternallyCompleted)
1386 LoadExternalDefinition();
1388 return data().ReferencedProtocols.end();
1391 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
1392 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
1394 protocol_loc_range protocol_locs() const {
1395 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1398 protocol_loc_iterator protocol_loc_begin() const {
1399 // FIXME: Should make sure no callers ever do this.
1400 if (!hasDefinition())
1401 return protocol_loc_iterator();
1403 if (data().ExternallyCompleted)
1404 LoadExternalDefinition();
1406 return data().ReferencedProtocols.loc_begin();
1409 protocol_loc_iterator protocol_loc_end() const {
1410 // FIXME: Should make sure no callers ever do this.
1411 if (!hasDefinition())
1412 return protocol_loc_iterator();
1414 if (data().ExternallyCompleted)
1415 LoadExternalDefinition();
1417 return data().ReferencedProtocols.loc_end();
1420 using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator;
1421 using all_protocol_range = llvm::iterator_range<all_protocol_iterator>;
1423 all_protocol_range all_referenced_protocols() const {
1424 return all_protocol_range(all_referenced_protocol_begin(),
1425 all_referenced_protocol_end());
1428 all_protocol_iterator all_referenced_protocol_begin() const {
1429 // FIXME: Should make sure no callers ever do this.
1430 if (!hasDefinition())
1431 return all_protocol_iterator();
1433 if (data().ExternallyCompleted)
1434 LoadExternalDefinition();
1436 return data().AllReferencedProtocols.empty()
1438 : data().AllReferencedProtocols.begin();
1441 all_protocol_iterator all_referenced_protocol_end() const {
1442 // FIXME: Should make sure no callers ever do this.
1443 if (!hasDefinition())
1444 return all_protocol_iterator();
1446 if (data().ExternallyCompleted)
1447 LoadExternalDefinition();
1449 return data().AllReferencedProtocols.empty()
1451 : data().AllReferencedProtocols.end();
1454 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
1455 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
1457 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1459 ivar_iterator ivar_begin() const {
1460 if (const ObjCInterfaceDecl *Def = getDefinition())
1461 return ivar_iterator(Def->decls_begin());
1463 // FIXME: Should make sure no callers ever do this.
1464 return ivar_iterator();
1467 ivar_iterator ivar_end() const {
1468 if (const ObjCInterfaceDecl *Def = getDefinition())
1469 return ivar_iterator(Def->decls_end());
1471 // FIXME: Should make sure no callers ever do this.
1472 return ivar_iterator();
1475 unsigned ivar_size() const {
1476 return std::distance(ivar_begin(), ivar_end());
1479 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1481 ObjCIvarDecl *all_declared_ivar_begin();
1482 const ObjCIvarDecl *all_declared_ivar_begin() const {
1483 // Even though this modifies IvarList, it's conceptually const:
1484 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1485 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1487 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1489 /// setProtocolList - Set the list of protocols that this interface
1491 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1492 const SourceLocation *Locs, ASTContext &C) {
1493 data().ReferencedProtocols.set(List, Num, Locs, C);
1496 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1497 /// into the protocol list for this class.
1498 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1502 /// Produce a name to be used for class's metadata. It comes either via
1503 /// objc_runtime_name attribute or class name.
1504 StringRef getObjCRuntimeNameAsString() const;
1506 /// Returns the designated initializers for the interface.
1508 /// If this declaration does not have methods marked as designated
1509 /// initializers then the interface inherits the designated initializers of
1510 /// its super class.
1511 void getDesignatedInitializers(
1512 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1514 /// Returns true if the given selector is a designated initializer for the
1517 /// If this declaration does not have methods marked as designated
1518 /// initializers then the interface inherits the designated initializers of
1519 /// its super class.
1521 /// \param InitMethod if non-null and the function returns true, it receives
1522 /// the method that was marked as a designated initializer.
1524 isDesignatedInitializer(Selector Sel,
1525 const ObjCMethodDecl **InitMethod = nullptr) const;
1527 /// Determine whether this particular declaration of this class is
1528 /// actually also a definition.
1529 bool isThisDeclarationADefinition() const {
1530 return getDefinition() == this;
1533 /// Determine whether this class has been defined.
1534 bool hasDefinition() const {
1535 // If the name of this class is out-of-date, bring it up-to-date, which
1536 // might bring in a definition.
1537 // Note: a null value indicates that we don't have a definition and that
1538 // modules are enabled.
1539 if (!Data.getOpaqueValue())
1540 getMostRecentDecl();
1542 return Data.getPointer();
1545 /// Retrieve the definition of this class, or NULL if this class
1546 /// has been forward-declared (with \@class) but not yet defined (with
1548 ObjCInterfaceDecl *getDefinition() {
1549 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1552 /// Retrieve the definition of this class, or NULL if this class
1553 /// has been forward-declared (with \@class) but not yet defined (with
1555 const ObjCInterfaceDecl *getDefinition() const {
1556 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1559 /// Starts the definition of this Objective-C class, taking it from
1560 /// a forward declaration (\@class) to a definition (\@interface).
1561 void startDefinition();
1563 /// Retrieve the superclass type.
1564 const ObjCObjectType *getSuperClassType() const {
1565 if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1566 return TInfo->getType()->castAs<ObjCObjectType>();
1571 // Retrieve the type source information for the superclass.
1572 TypeSourceInfo *getSuperClassTInfo() const {
1573 // FIXME: Should make sure no callers ever do this.
1574 if (!hasDefinition())
1577 if (data().ExternallyCompleted)
1578 LoadExternalDefinition();
1580 return data().SuperClassTInfo;
1583 // Retrieve the declaration for the superclass of this class, which
1584 // does not include any type arguments that apply to the superclass.
1585 ObjCInterfaceDecl *getSuperClass() const;
1587 void setSuperClass(TypeSourceInfo *superClass) {
1588 data().SuperClassTInfo = superClass;
1591 /// Iterator that walks over the list of categories, filtering out
1592 /// those that do not meet specific criteria.
1594 /// This class template is used for the various permutations of category
1595 /// and extension iterators.
1596 template<bool (*Filter)(ObjCCategoryDecl *)>
1597 class filtered_category_iterator {
1598 ObjCCategoryDecl *Current = nullptr;
1600 void findAcceptableCategory();
1603 using value_type = ObjCCategoryDecl *;
1604 using reference = value_type;
1605 using pointer = value_type;
1606 using difference_type = std::ptrdiff_t;
1607 using iterator_category = std::input_iterator_tag;
1609 filtered_category_iterator() = default;
1610 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1611 : Current(Current) {
1612 findAcceptableCategory();
1615 reference operator*() const { return Current; }
1616 pointer operator->() const { return Current; }
1618 filtered_category_iterator &operator++();
1620 filtered_category_iterator operator++(int) {
1621 filtered_category_iterator Tmp = *this;
1626 friend bool operator==(filtered_category_iterator X,
1627 filtered_category_iterator Y) {
1628 return X.Current == Y.Current;
1631 friend bool operator!=(filtered_category_iterator X,
1632 filtered_category_iterator Y) {
1633 return X.Current != Y.Current;
1638 /// Test whether the given category is visible.
1640 /// Used in the \c visible_categories_iterator.
1641 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1644 /// Iterator that walks over the list of categories and extensions
1645 /// that are visible, i.e., not hidden in a non-imported submodule.
1646 using visible_categories_iterator =
1647 filtered_category_iterator<isVisibleCategory>;
1649 using visible_categories_range =
1650 llvm::iterator_range<visible_categories_iterator>;
1652 visible_categories_range visible_categories() const {
1653 return visible_categories_range(visible_categories_begin(),
1654 visible_categories_end());
1657 /// Retrieve an iterator to the beginning of the visible-categories
1659 visible_categories_iterator visible_categories_begin() const {
1660 return visible_categories_iterator(getCategoryListRaw());
1663 /// Retrieve an iterator to the end of the visible-categories list.
1664 visible_categories_iterator visible_categories_end() const {
1665 return visible_categories_iterator();
1668 /// Determine whether the visible-categories list is empty.
1669 bool visible_categories_empty() const {
1670 return visible_categories_begin() == visible_categories_end();
1674 /// Test whether the given category... is a category.
1676 /// Used in the \c known_categories_iterator.
1677 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1680 /// Iterator that walks over all of the known categories and
1681 /// extensions, including those that are hidden.
1682 using known_categories_iterator = filtered_category_iterator<isKnownCategory>;
1683 using known_categories_range =
1684 llvm::iterator_range<known_categories_iterator>;
1686 known_categories_range known_categories() const {
1687 return known_categories_range(known_categories_begin(),
1688 known_categories_end());
1691 /// Retrieve an iterator to the beginning of the known-categories
1693 known_categories_iterator known_categories_begin() const {
1694 return known_categories_iterator(getCategoryListRaw());
1697 /// Retrieve an iterator to the end of the known-categories list.
1698 known_categories_iterator known_categories_end() const {
1699 return known_categories_iterator();
1702 /// Determine whether the known-categories list is empty.
1703 bool known_categories_empty() const {
1704 return known_categories_begin() == known_categories_end();
1708 /// Test whether the given category is a visible extension.
1710 /// Used in the \c visible_extensions_iterator.
1711 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1714 /// Iterator that walks over all of the visible extensions, skipping
1715 /// any that are known but hidden.
1716 using visible_extensions_iterator =
1717 filtered_category_iterator<isVisibleExtension>;
1719 using visible_extensions_range =
1720 llvm::iterator_range<visible_extensions_iterator>;
1722 visible_extensions_range visible_extensions() const {
1723 return visible_extensions_range(visible_extensions_begin(),
1724 visible_extensions_end());
1727 /// Retrieve an iterator to the beginning of the visible-extensions
1729 visible_extensions_iterator visible_extensions_begin() const {
1730 return visible_extensions_iterator(getCategoryListRaw());
1733 /// Retrieve an iterator to the end of the visible-extensions list.
1734 visible_extensions_iterator visible_extensions_end() const {
1735 return visible_extensions_iterator();
1738 /// Determine whether the visible-extensions list is empty.
1739 bool visible_extensions_empty() const {
1740 return visible_extensions_begin() == visible_extensions_end();
1744 /// Test whether the given category is an extension.
1746 /// Used in the \c known_extensions_iterator.
1747 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1750 friend class ASTDeclReader;
1751 friend class ASTDeclWriter;
1752 friend class ASTReader;
1754 /// Iterator that walks over all of the known extensions.
1755 using known_extensions_iterator =
1756 filtered_category_iterator<isKnownExtension>;
1757 using known_extensions_range =
1758 llvm::iterator_range<known_extensions_iterator>;
1760 known_extensions_range known_extensions() const {
1761 return known_extensions_range(known_extensions_begin(),
1762 known_extensions_end());
1765 /// Retrieve an iterator to the beginning of the known-extensions
1767 known_extensions_iterator known_extensions_begin() const {
1768 return known_extensions_iterator(getCategoryListRaw());
1771 /// Retrieve an iterator to the end of the known-extensions list.
1772 known_extensions_iterator known_extensions_end() const {
1773 return known_extensions_iterator();
1776 /// Determine whether the known-extensions list is empty.
1777 bool known_extensions_empty() const {
1778 return known_extensions_begin() == known_extensions_end();
1781 /// Retrieve the raw pointer to the start of the category/extension
1783 ObjCCategoryDecl* getCategoryListRaw() const {
1784 // FIXME: Should make sure no callers ever do this.
1785 if (!hasDefinition())
1788 if (data().ExternallyCompleted)
1789 LoadExternalDefinition();
1791 return data().CategoryList;
1794 /// Set the raw pointer to the start of the category/extension
1796 void setCategoryListRaw(ObjCCategoryDecl *category) {
1797 data().CategoryList = category;
1801 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1802 ObjCPropertyQueryKind QueryKind) const;
1804 void collectPropertiesToImplement(PropertyMap &PM,
1805 PropertyDeclOrder &PO) const override;
1807 /// isSuperClassOf - Return true if this class is the specified class or is a
1808 /// super class of the specified interface class.
1809 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1810 // If RHS is derived from LHS it is OK; else it is not OK.
1811 while (I != nullptr) {
1812 if (declaresSameEntity(this, I))
1815 I = I->getSuperClass();
1820 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1821 /// to be incompatible with __weak references. Returns true if it is.
1822 bool isArcWeakrefUnavailable() const;
1824 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1825 /// classes must not be auto-synthesized. Returns class decl. if it must not
1826 /// be; 0, otherwise.
1827 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1829 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1830 ObjCInterfaceDecl *&ClassDeclared);
1831 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1832 ObjCInterfaceDecl *ClassDeclared;
1833 return lookupInstanceVariable(IVarName, ClassDeclared);
1836 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1838 // Lookup a method. First, we search locally. If a method isn't
1839 // found, we search referenced protocols and class categories.
1840 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1841 bool shallowCategoryLookup = false,
1842 bool followSuper = true,
1843 const ObjCCategoryDecl *C = nullptr) const;
1845 /// Lookup an instance method for a given selector.
1846 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1847 return lookupMethod(Sel, true/*isInstance*/);
1850 /// Lookup a class method for a given selector.
1851 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1852 return lookupMethod(Sel, false/*isInstance*/);
1855 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1857 /// Lookup a method in the classes implementation hierarchy.
1858 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1859 bool Instance=true) const;
1861 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1862 return lookupPrivateMethod(Sel, false);
1865 /// Lookup a setter or getter in the class hierarchy,
1866 /// including in all categories except for category passed
1868 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1869 const ObjCCategoryDecl *Cat,
1870 bool IsClassProperty) const {
1871 return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1872 false/*shallowCategoryLookup*/,
1873 true /* followsSuper */,
1877 SourceLocation getEndOfDefinitionLoc() const {
1878 if (!hasDefinition())
1879 return getLocation();
1881 return data().EndLoc;
1884 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1886 /// Retrieve the starting location of the superclass.
1887 SourceLocation getSuperClassLoc() const;
1889 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1890 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1891 /// declaration without an \@interface declaration.
1892 bool isImplicitInterfaceDecl() const {
1893 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1896 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1897 /// has been implemented in IDecl class, its super class or categories (if
1898 /// lookupCategory is true).
1899 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1900 bool lookupCategory,
1901 bool RHSIsQualifiedID = false);
1903 using redecl_range = redeclarable_base::redecl_range;
1904 using redecl_iterator = redeclarable_base::redecl_iterator;
1906 using redeclarable_base::redecls_begin;
1907 using redeclarable_base::redecls_end;
1908 using redeclarable_base::redecls;
1909 using redeclarable_base::getPreviousDecl;
1910 using redeclarable_base::getMostRecentDecl;
1911 using redeclarable_base::isFirstDecl;
1913 /// Retrieves the canonical declaration of this Objective-C class.
1914 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1915 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1917 // Low-level accessor
1918 const Type *getTypeForDecl() const { return TypeForDecl; }
1919 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1921 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1922 static bool classofKind(Kind K) { return K == ObjCInterface; }
1925 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1926 bool inheritsDesignatedInitializers() const;
1929 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1930 /// instance variables are identical to C. The only exception is Objective-C
1931 /// supports C++ style access control. For example:
1933 /// \@interface IvarExample : NSObject
1935 /// id defaultToProtected;
1937 /// id canBePublic; // same as C++.
1939 /// id canBeProtected; // same as C++.
1941 /// id canBePackage; // framework visibility (not available in C++).
1944 class ObjCIvarDecl : public FieldDecl {
1945 void anchor() override;
1948 enum AccessControl {
1949 None, Private, Protected, Public, Package
1953 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1954 SourceLocation IdLoc, IdentifierInfo *Id,
1955 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1957 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1958 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1959 DeclAccess(ac), Synthesized(synthesized) {}
1962 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1963 SourceLocation StartLoc, SourceLocation IdLoc,
1964 IdentifierInfo *Id, QualType T,
1965 TypeSourceInfo *TInfo,
1966 AccessControl ac, Expr *BW = nullptr,
1967 bool synthesized=false);
1969 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1971 /// Return the class interface that this ivar is logically contained
1972 /// in; this is either the interface where the ivar was declared, or the
1973 /// interface the ivar is conceptually a part of in the case of synthesized
1975 const ObjCInterfaceDecl *getContainingInterface() const;
1977 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1978 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1979 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1981 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1983 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1985 AccessControl getCanonicalAccessControl() const {
1986 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1989 void setSynthesize(bool synth) { Synthesized = synth; }
1990 bool getSynthesize() const { return Synthesized; }
1992 /// Retrieve the type of this instance variable when viewed as a member of a
1993 /// specific object type.
1994 QualType getUsageType(QualType objectType) const;
1996 // Implement isa/cast/dyncast/etc.
1997 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1998 static bool classofKind(Kind K) { return K == ObjCIvar; }
2001 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
2002 /// extensions and class's implementation
2003 ObjCIvarDecl *NextIvar = nullptr;
2005 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
2006 unsigned DeclAccess : 3;
2007 unsigned Synthesized : 1;
2010 /// Represents a field declaration created by an \@defs(...).
2011 class ObjCAtDefsFieldDecl : public FieldDecl {
2012 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
2013 SourceLocation IdLoc, IdentifierInfo *Id,
2014 QualType T, Expr *BW)
2015 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
2016 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
2017 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
2019 void anchor() override;
2022 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
2023 SourceLocation StartLoc,
2024 SourceLocation IdLoc, IdentifierInfo *Id,
2025 QualType T, Expr *BW);
2027 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2029 // Implement isa/cast/dyncast/etc.
2030 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2031 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
2034 /// Represents an Objective-C protocol declaration.
2036 /// Objective-C protocols declare a pure abstract type (i.e., no instance
2037 /// variables are permitted). Protocols originally drew inspiration from
2038 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
2039 /// syntax:-). Here is an example:
2042 /// \@protocol NSDraggingInfo <refproto1, refproto2>
2043 /// - (NSWindow *)draggingDestinationWindow;
2044 /// - (NSImage *)draggedImage;
2048 /// This says that NSDraggingInfo requires two methods and requires everything
2049 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
2053 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
2057 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
2058 /// protocols are in distinct namespaces. For example, Cocoa defines both
2059 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
2060 /// protocols are referenced using angle brackets as follows:
2062 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
2063 class ObjCProtocolDecl : public ObjCContainerDecl,
2064 public Redeclarable<ObjCProtocolDecl> {
2065 struct DefinitionData {
2066 // The declaration that defines this protocol.
2067 ObjCProtocolDecl *Definition;
2069 /// Referenced protocols
2070 ObjCProtocolList ReferencedProtocols;
2073 /// Contains a pointer to the data associated with this class,
2074 /// which will be NULL if this class has not yet been defined.
2076 /// The bit indicates when we don't need to check for out-of-date
2077 /// declarations. It will be set unless modules are enabled.
2078 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
2080 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
2081 SourceLocation nameLoc, SourceLocation atStartLoc,
2082 ObjCProtocolDecl *PrevDecl);
2084 void anchor() override;
2086 DefinitionData &data() const {
2087 assert(Data.getPointer() && "Objective-C protocol has no definition!");
2088 return *Data.getPointer();
2091 void allocateDefinitionData();
2093 using redeclarable_base = Redeclarable<ObjCProtocolDecl>;
2095 ObjCProtocolDecl *getNextRedeclarationImpl() override {
2096 return getNextRedeclaration();
2099 ObjCProtocolDecl *getPreviousDeclImpl() override {
2100 return getPreviousDecl();
2103 ObjCProtocolDecl *getMostRecentDeclImpl() override {
2104 return getMostRecentDecl();
2108 friend class ASTDeclReader;
2109 friend class ASTDeclWriter;
2110 friend class ASTReader;
2112 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2114 SourceLocation nameLoc,
2115 SourceLocation atStartLoc,
2116 ObjCProtocolDecl *PrevDecl);
2118 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2120 const ObjCProtocolList &getReferencedProtocols() const {
2121 assert(hasDefinition() && "No definition available!");
2122 return data().ReferencedProtocols;
2125 using protocol_iterator = ObjCProtocolList::iterator;
2126 using protocol_range = llvm::iterator_range<protocol_iterator>;
2128 protocol_range protocols() const {
2129 return protocol_range(protocol_begin(), protocol_end());
2132 protocol_iterator protocol_begin() const {
2133 if (!hasDefinition())
2134 return protocol_iterator();
2136 return data().ReferencedProtocols.begin();
2139 protocol_iterator protocol_end() const {
2140 if (!hasDefinition())
2141 return protocol_iterator();
2143 return data().ReferencedProtocols.end();
2146 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2147 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2149 protocol_loc_range protocol_locs() const {
2150 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2153 protocol_loc_iterator protocol_loc_begin() const {
2154 if (!hasDefinition())
2155 return protocol_loc_iterator();
2157 return data().ReferencedProtocols.loc_begin();
2160 protocol_loc_iterator protocol_loc_end() const {
2161 if (!hasDefinition())
2162 return protocol_loc_iterator();
2164 return data().ReferencedProtocols.loc_end();
2167 unsigned protocol_size() const {
2168 if (!hasDefinition())
2171 return data().ReferencedProtocols.size();
2174 /// setProtocolList - Set the list of protocols that this interface
2176 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2177 const SourceLocation *Locs, ASTContext &C) {
2178 assert(hasDefinition() && "Protocol is not defined");
2179 data().ReferencedProtocols.set(List, Num, Locs, C);
2182 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2184 // Lookup a method. First, we search locally. If a method isn't
2185 // found, we search referenced protocols and class categories.
2186 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2188 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2189 return lookupMethod(Sel, true/*isInstance*/);
2192 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2193 return lookupMethod(Sel, false/*isInstance*/);
2196 /// Determine whether this protocol has a definition.
2197 bool hasDefinition() const {
2198 // If the name of this protocol is out-of-date, bring it up-to-date, which
2199 // might bring in a definition.
2200 // Note: a null value indicates that we don't have a definition and that
2201 // modules are enabled.
2202 if (!Data.getOpaqueValue())
2203 getMostRecentDecl();
2205 return Data.getPointer();
2208 /// Retrieve the definition of this protocol, if any.
2209 ObjCProtocolDecl *getDefinition() {
2210 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2213 /// Retrieve the definition of this protocol, if any.
2214 const ObjCProtocolDecl *getDefinition() const {
2215 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2218 /// Determine whether this particular declaration is also the
2220 bool isThisDeclarationADefinition() const {
2221 return getDefinition() == this;
2224 /// Starts the definition of this Objective-C protocol.
2225 void startDefinition();
2227 /// Produce a name to be used for protocol's metadata. It comes either via
2228 /// objc_runtime_name attribute or protocol name.
2229 StringRef getObjCRuntimeNameAsString() const;
2231 SourceRange getSourceRange() const override LLVM_READONLY {
2232 if (isThisDeclarationADefinition())
2233 return ObjCContainerDecl::getSourceRange();
2235 return SourceRange(getAtStartLoc(), getLocation());
2238 using redecl_range = redeclarable_base::redecl_range;
2239 using redecl_iterator = redeclarable_base::redecl_iterator;
2241 using redeclarable_base::redecls_begin;
2242 using redeclarable_base::redecls_end;
2243 using redeclarable_base::redecls;
2244 using redeclarable_base::getPreviousDecl;
2245 using redeclarable_base::getMostRecentDecl;
2246 using redeclarable_base::isFirstDecl;
2248 /// Retrieves the canonical declaration of this Objective-C protocol.
2249 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2250 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2252 void collectPropertiesToImplement(PropertyMap &PM,
2253 PropertyDeclOrder &PO) const override;
2255 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2256 ProtocolPropertySet &PS,
2257 PropertyDeclOrder &PO) const;
2259 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2260 static bool classofKind(Kind K) { return K == ObjCProtocol; }
2263 /// ObjCCategoryDecl - Represents a category declaration. A category allows
2264 /// you to add methods to an existing class (without subclassing or modifying
2265 /// the original class interface or implementation:-). Categories don't allow
2266 /// you to add instance data. The following example adds "myMethod" to all
2267 /// NSView's within a process:
2269 /// \@interface NSView (MyViewMethods)
2273 /// Categories also allow you to split the implementation of a class across
2274 /// several files (a feature more naturally supported in C++).
2276 /// Categories were originally inspired by dynamic languages such as Common
2277 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
2278 /// don't support this level of dynamism, which is both powerful and dangerous.
2279 class ObjCCategoryDecl : public ObjCContainerDecl {
2280 /// Interface belonging to this category
2281 ObjCInterfaceDecl *ClassInterface;
2283 /// The type parameters associated with this category, if any.
2284 ObjCTypeParamList *TypeParamList = nullptr;
2286 /// referenced protocols in this category.
2287 ObjCProtocolList ReferencedProtocols;
2289 /// Next category belonging to this class.
2290 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
2291 ObjCCategoryDecl *NextClassCategory = nullptr;
2293 /// The location of the category name in this declaration.
2294 SourceLocation CategoryNameLoc;
2296 /// class extension may have private ivars.
2297 SourceLocation IvarLBraceLoc;
2298 SourceLocation IvarRBraceLoc;
2300 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2301 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2302 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2303 ObjCTypeParamList *typeParamList,
2304 SourceLocation IvarLBraceLoc = SourceLocation(),
2305 SourceLocation IvarRBraceLoc = SourceLocation());
2307 void anchor() override;
2310 friend class ASTDeclReader;
2311 friend class ASTDeclWriter;
2313 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2314 SourceLocation AtLoc,
2315 SourceLocation ClassNameLoc,
2316 SourceLocation CategoryNameLoc,
2318 ObjCInterfaceDecl *IDecl,
2319 ObjCTypeParamList *typeParamList,
2320 SourceLocation IvarLBraceLoc=SourceLocation(),
2321 SourceLocation IvarRBraceLoc=SourceLocation());
2322 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2324 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2325 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2327 /// Retrieve the type parameter list associated with this category or
2329 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2331 /// Set the type parameters of this category.
2333 /// This function is used by the AST importer, which must import the type
2334 /// parameters after creating their DeclContext to avoid loops.
2335 void setTypeParamList(ObjCTypeParamList *TPL);
2338 ObjCCategoryImplDecl *getImplementation() const;
2339 void setImplementation(ObjCCategoryImplDecl *ImplD);
2341 /// setProtocolList - Set the list of protocols that this interface
2343 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2344 const SourceLocation *Locs, ASTContext &C) {
2345 ReferencedProtocols.set(List, Num, Locs, C);
2348 const ObjCProtocolList &getReferencedProtocols() const {
2349 return ReferencedProtocols;
2352 using protocol_iterator = ObjCProtocolList::iterator;
2353 using protocol_range = llvm::iterator_range<protocol_iterator>;
2355 protocol_range protocols() const {
2356 return protocol_range(protocol_begin(), protocol_end());
2359 protocol_iterator protocol_begin() const {
2360 return ReferencedProtocols.begin();
2363 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2364 unsigned protocol_size() const { return ReferencedProtocols.size(); }
2366 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2367 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2369 protocol_loc_range protocol_locs() const {
2370 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2373 protocol_loc_iterator protocol_loc_begin() const {
2374 return ReferencedProtocols.loc_begin();
2377 protocol_loc_iterator protocol_loc_end() const {
2378 return ReferencedProtocols.loc_end();
2381 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2383 /// Retrieve the pointer to the next stored category (or extension),
2384 /// which may be hidden.
2385 ObjCCategoryDecl *getNextClassCategoryRaw() const {
2386 return NextClassCategory;
2389 bool IsClassExtension() const { return getIdentifier() == nullptr; }
2391 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2392 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2394 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2396 ivar_iterator ivar_begin() const {
2397 return ivar_iterator(decls_begin());
2400 ivar_iterator ivar_end() const {
2401 return ivar_iterator(decls_end());
2404 unsigned ivar_size() const {
2405 return std::distance(ivar_begin(), ivar_end());
2408 bool ivar_empty() const {
2409 return ivar_begin() == ivar_end();
2412 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2413 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2415 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2416 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2417 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2418 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2420 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2421 static bool classofKind(Kind K) { return K == ObjCCategory; }
2424 class ObjCImplDecl : public ObjCContainerDecl {
2425 /// Class interface for this class/category implementation
2426 ObjCInterfaceDecl *ClassInterface;
2428 void anchor() override;
2431 ObjCImplDecl(Kind DK, DeclContext *DC,
2432 ObjCInterfaceDecl *classInterface,
2434 SourceLocation nameLoc, SourceLocation atStartLoc)
2435 : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc),
2436 ClassInterface(classInterface) {}
2439 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2440 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2441 void setClassInterface(ObjCInterfaceDecl *IFace);
2443 void addInstanceMethod(ObjCMethodDecl *method) {
2444 // FIXME: Context should be set correctly before we get here.
2445 method->setLexicalDeclContext(this);
2449 void addClassMethod(ObjCMethodDecl *method) {
2450 // FIXME: Context should be set correctly before we get here.
2451 method->setLexicalDeclContext(this);
2455 void addPropertyImplementation(ObjCPropertyImplDecl *property);
2457 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2458 ObjCPropertyQueryKind queryKind) const;
2459 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2461 // Iterator access to properties.
2462 using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>;
2463 using propimpl_range =
2464 llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>;
2466 propimpl_range property_impls() const {
2467 return propimpl_range(propimpl_begin(), propimpl_end());
2470 propimpl_iterator propimpl_begin() const {
2471 return propimpl_iterator(decls_begin());
2474 propimpl_iterator propimpl_end() const {
2475 return propimpl_iterator(decls_end());
2478 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2480 static bool classofKind(Kind K) {
2481 return K >= firstObjCImpl && K <= lastObjCImpl;
2485 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
2486 /// \@implementation declaration. If a category class has declaration of a
2487 /// property, its implementation must be specified in the category's
2488 /// \@implementation declaration. Example:
2489 /// \@interface I \@end
2490 /// \@interface I(CATEGORY)
2491 /// \@property int p1, d1;
2493 /// \@implementation I(CATEGORY)
2494 /// \@dynamic p1,d1;
2497 /// ObjCCategoryImplDecl
2498 class ObjCCategoryImplDecl : public ObjCImplDecl {
2499 // Category name location
2500 SourceLocation CategoryNameLoc;
2502 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2503 ObjCInterfaceDecl *classInterface,
2504 SourceLocation nameLoc, SourceLocation atStartLoc,
2505 SourceLocation CategoryNameLoc)
2506 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id,
2507 nameLoc, atStartLoc),
2508 CategoryNameLoc(CategoryNameLoc) {}
2510 void anchor() override;
2513 friend class ASTDeclReader;
2514 friend class ASTDeclWriter;
2516 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2518 ObjCInterfaceDecl *classInterface,
2519 SourceLocation nameLoc,
2520 SourceLocation atStartLoc,
2521 SourceLocation CategoryNameLoc);
2522 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2524 ObjCCategoryDecl *getCategoryDecl() const;
2526 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2528 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2529 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2532 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2534 /// ObjCImplementationDecl - Represents a class definition - this is where
2535 /// method definitions are specified. For example:
2538 /// \@implementation MyClass
2539 /// - (void)myMethod { /* do something */ }
2543 /// In a non-fragile runtime, instance variables can appear in the class
2544 /// interface, class extensions (nameless categories), and in the implementation
2545 /// itself, as well as being synthesized as backing storage for properties.
2547 /// In a fragile runtime, instance variables are specified in the class
2548 /// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2549 /// we allow instance variables to be specified in the implementation. When
2550 /// specified, they need to be \em identical to the interface.
2551 class ObjCImplementationDecl : public ObjCImplDecl {
2552 /// Implementation Class's super class.
2553 ObjCInterfaceDecl *SuperClass;
2554 SourceLocation SuperLoc;
2556 /// \@implementation may have private ivars.
2557 SourceLocation IvarLBraceLoc;
2558 SourceLocation IvarRBraceLoc;
2560 /// Support for ivar initialization.
2561 /// The arguments used to initialize the ivars
2562 LazyCXXCtorInitializersPtr IvarInitializers;
2563 unsigned NumIvarInitializers = 0;
2565 /// Do the ivars of this class require initialization other than
2566 /// zero-initialization?
2567 bool HasNonZeroConstructors : 1;
2569 /// Do the ivars of this class require non-trivial destruction?
2570 bool HasDestructors : 1;
2572 ObjCImplementationDecl(DeclContext *DC,
2573 ObjCInterfaceDecl *classInterface,
2574 ObjCInterfaceDecl *superDecl,
2575 SourceLocation nameLoc, SourceLocation atStartLoc,
2576 SourceLocation superLoc = SourceLocation(),
2577 SourceLocation IvarLBraceLoc=SourceLocation(),
2578 SourceLocation IvarRBraceLoc=SourceLocation())
2579 : ObjCImplDecl(ObjCImplementation, DC, classInterface,
2580 classInterface ? classInterface->getIdentifier()
2582 nameLoc, atStartLoc),
2583 SuperClass(superDecl), SuperLoc(superLoc),
2584 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc),
2585 HasNonZeroConstructors(false), HasDestructors(false) {}
2587 void anchor() override;
2590 friend class ASTDeclReader;
2591 friend class ASTDeclWriter;
2593 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2594 ObjCInterfaceDecl *classInterface,
2595 ObjCInterfaceDecl *superDecl,
2596 SourceLocation nameLoc,
2597 SourceLocation atStartLoc,
2598 SourceLocation superLoc = SourceLocation(),
2599 SourceLocation IvarLBraceLoc=SourceLocation(),
2600 SourceLocation IvarRBraceLoc=SourceLocation());
2602 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2604 /// init_iterator - Iterates through the ivar initializer list.
2605 using init_iterator = CXXCtorInitializer **;
2607 /// init_const_iterator - Iterates through the ivar initializer list.
2608 using init_const_iterator = CXXCtorInitializer * const *;
2610 using init_range = llvm::iterator_range<init_iterator>;
2611 using init_const_range = llvm::iterator_range<init_const_iterator>;
2613 init_range inits() { return init_range(init_begin(), init_end()); }
2615 init_const_range inits() const {
2616 return init_const_range(init_begin(), init_end());
2619 /// init_begin() - Retrieve an iterator to the first initializer.
2620 init_iterator init_begin() {
2621 const auto *ConstThis = this;
2622 return const_cast<init_iterator>(ConstThis->init_begin());
2625 /// begin() - Retrieve an iterator to the first initializer.
2626 init_const_iterator init_begin() const;
2628 /// init_end() - Retrieve an iterator past the last initializer.
2629 init_iterator init_end() {
2630 return init_begin() + NumIvarInitializers;
2633 /// end() - Retrieve an iterator past the last initializer.
2634 init_const_iterator init_end() const {
2635 return init_begin() + NumIvarInitializers;
2638 /// getNumArgs - Number of ivars which must be initialized.
2639 unsigned getNumIvarInitializers() const {
2640 return NumIvarInitializers;
2643 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2644 NumIvarInitializers = numNumIvarInitializers;
2647 void setIvarInitializers(ASTContext &C,
2648 CXXCtorInitializer ** initializers,
2649 unsigned numInitializers);
2651 /// Do any of the ivars of this class (not counting its base classes)
2652 /// require construction other than zero-initialization?
2653 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2654 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2656 /// Do any of the ivars of this class (not counting its base classes)
2657 /// require non-trivial destruction?
2658 bool hasDestructors() const { return HasDestructors; }
2659 void setHasDestructors(bool val) { HasDestructors = val; }
2661 /// getIdentifier - Get the identifier that names the class
2662 /// interface associated with this implementation.
2663 IdentifierInfo *getIdentifier() const {
2664 return getClassInterface()->getIdentifier();
2667 /// getName - Get the name of identifier for the class interface associated
2668 /// with this implementation as a StringRef.
2670 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2672 StringRef getName() const {
2673 assert(getIdentifier() && "Name is not a simple identifier");
2674 return getIdentifier()->getName();
2677 /// Get the name of the class associated with this interface.
2679 // FIXME: Move to StringRef API.
2680 std::string getNameAsString() const {
2684 /// Produce a name to be used for class's metadata. It comes either via
2685 /// class's objc_runtime_name attribute or class name.
2686 StringRef getObjCRuntimeNameAsString() const;
2688 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2689 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2690 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2692 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2694 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2695 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2696 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2697 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2699 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2700 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2702 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2704 ivar_iterator ivar_begin() const {
2705 return ivar_iterator(decls_begin());
2708 ivar_iterator ivar_end() const {
2709 return ivar_iterator(decls_end());
2712 unsigned ivar_size() const {
2713 return std::distance(ivar_begin(), ivar_end());
2716 bool ivar_empty() const {
2717 return ivar_begin() == ivar_end();
2720 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2721 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2724 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2726 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2727 /// declared as \@compatibility_alias alias class.
2728 class ObjCCompatibleAliasDecl : public NamedDecl {
2729 /// Class that this is an alias of.
2730 ObjCInterfaceDecl *AliasedClass;
2732 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2733 ObjCInterfaceDecl* aliasedClass)
2734 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2736 void anchor() override;
2739 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2740 SourceLocation L, IdentifierInfo *Id,
2741 ObjCInterfaceDecl* aliasedClass);
2743 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2746 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2747 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2748 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2750 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2751 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2754 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2755 /// in a class or category implementation block. For example:
2756 /// \@synthesize prop1 = ivar1;
2758 class ObjCPropertyImplDecl : public Decl {
2766 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2768 /// For \@synthesize, the location of the ivar, if it was written in
2769 /// the source code.
2772 /// \@synthesize int a = b
2774 SourceLocation IvarLoc;
2776 /// Property declaration being implemented
2777 ObjCPropertyDecl *PropertyDecl;
2779 /// Null for \@dynamic. Required for \@synthesize.
2780 ObjCIvarDecl *PropertyIvarDecl;
2782 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2784 Expr *GetterCXXConstructor = nullptr;
2786 /// Null for \@dynamic. Non-null if property has assignment operator to call
2787 /// in Setter synthesis.
2788 Expr *SetterCXXAssignment = nullptr;
2790 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2791 ObjCPropertyDecl *property,
2793 ObjCIvarDecl *ivarDecl,
2794 SourceLocation ivarLoc)
2795 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2796 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) {
2797 assert(PK == Dynamic || PropertyIvarDecl);
2801 friend class ASTDeclReader;
2803 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2804 SourceLocation atLoc, SourceLocation L,
2805 ObjCPropertyDecl *property,
2807 ObjCIvarDecl *ivarDecl,
2808 SourceLocation ivarLoc);
2810 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2812 SourceRange getSourceRange() const override LLVM_READONLY;
2814 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
2815 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2817 ObjCPropertyDecl *getPropertyDecl() const {
2818 return PropertyDecl;
2820 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2822 Kind getPropertyImplementation() const {
2823 return PropertyIvarDecl ? Synthesize : Dynamic;
2826 ObjCIvarDecl *getPropertyIvarDecl() const {
2827 return PropertyIvarDecl;
2829 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2831 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2832 SourceLocation IvarLoc) {
2833 PropertyIvarDecl = Ivar;
2834 this->IvarLoc = IvarLoc;
2837 /// For \@synthesize, returns true if an ivar name was explicitly
2841 /// \@synthesize int a = b; // true
2842 /// \@synthesize int a; // false
2844 bool isIvarNameSpecified() const {
2845 return IvarLoc.isValid() && IvarLoc != getLocation();
2848 Expr *getGetterCXXConstructor() const {
2849 return GetterCXXConstructor;
2852 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2853 GetterCXXConstructor = getterCXXConstructor;
2856 Expr *getSetterCXXAssignment() const {
2857 return SetterCXXAssignment;
2860 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2861 SetterCXXAssignment = setterCXXAssignment;
2864 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2865 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2868 template<bool (*Filter)(ObjCCategoryDecl *)>
2870 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2871 findAcceptableCategory() {
2872 while (Current && !Filter(Current))
2873 Current = Current->getNextClassCategoryRaw();
2876 template<bool (*Filter)(ObjCCategoryDecl *)>
2877 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2878 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2879 Current = Current->getNextClassCategoryRaw();
2880 findAcceptableCategory();
2884 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2885 return !Cat->isHidden();
2888 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2889 return Cat->IsClassExtension() && !Cat->isHidden();
2892 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2893 return Cat->IsClassExtension();
2896 } // namespace clang
2898 #endif // LLVM_CLANG_AST_DECLOBJC_H