1 //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Objective-C related Decl classes.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Stmt.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 using namespace clang
;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 void ObjCListBase::set(void *const* InList
, unsigned Elts
, ASTContext
&Ctx
) {
29 if (Elts
== 0) return; // Setting to an empty list is a noop.
32 List
= new (Ctx
) void*[Elts
];
34 memcpy(List
, InList
, sizeof(void*)*Elts
);
37 void ObjCProtocolList::set(ObjCProtocolDecl
* const* InList
, unsigned Elts
,
38 const SourceLocation
*Locs
, ASTContext
&Ctx
) {
42 Locations
= new (Ctx
) SourceLocation
[Elts
];
43 memcpy(Locations
, Locs
, sizeof(SourceLocation
) * Elts
);
44 set(InList
, Elts
, Ctx
);
47 //===----------------------------------------------------------------------===//
49 //===----------------------------------------------------------------------===//
51 void ObjCContainerDecl::anchor() { }
53 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
56 ObjCContainerDecl::getIvarDecl(IdentifierInfo
*Id
) const {
57 lookup_const_result R
= lookup(Id
);
58 for (lookup_const_iterator Ivar
= R
.begin(), IvarEnd
= R
.end();
59 Ivar
!= IvarEnd
; ++Ivar
) {
60 if (ObjCIvarDecl
*ivar
= dyn_cast
<ObjCIvarDecl
>(*Ivar
))
66 // Get the local instance/class method declared in this interface.
68 ObjCContainerDecl::getMethod(Selector Sel
, bool isInstance
,
69 bool AllowHidden
) const {
70 // If this context is a hidden protocol definition, don't find any
72 if (const ObjCProtocolDecl
*Proto
= dyn_cast
<ObjCProtocolDecl
>(this)) {
73 if (const ObjCProtocolDecl
*Def
= Proto
->getDefinition())
74 if (Def
->isHidden() && !AllowHidden
)
78 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
86 lookup_const_result R
= lookup(Sel
);
87 for (lookup_const_iterator Meth
= R
.begin(), MethEnd
= R
.end();
88 Meth
!= MethEnd
; ++Meth
) {
89 ObjCMethodDecl
*MD
= dyn_cast
<ObjCMethodDecl
>(*Meth
);
90 if (MD
&& MD
->isInstanceMethod() == isInstance
)
96 /// \brief This routine returns 'true' if a user declared setter method was
97 /// found in the class, its protocols, its super classes or categories.
98 /// It also returns 'true' if one of its categories has declared a 'readwrite'
99 /// property. This is because, user must provide a setter method for the
100 /// category's 'readwrite' property.
101 bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl
*Property
) const {
103 Selector Sel
= Property
->getSetterName();
104 lookup_const_result R
= lookup(Sel
);
105 for (lookup_const_iterator Meth
= R
.begin(), MethEnd
= R
.end();
106 Meth
!= MethEnd
; ++Meth
) {
107 ObjCMethodDecl
*MD
= dyn_cast
<ObjCMethodDecl
>(*Meth
);
108 if (MD
&& MD
->isInstanceMethod() && !MD
->isImplicit())
112 if (const ObjCInterfaceDecl
*ID
= dyn_cast
<ObjCInterfaceDecl
>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
115 for (const auto *Cat
: ID
->visible_categories()) {
116 if (ObjCMethodDecl
*MD
= Cat
->getInstanceMethod(Sel
))
117 if (!MD
->isImplicit())
119 if (Cat
->IsClassExtension())
121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
125 for (const auto *P
: Cat
->properties())
126 if (P
->getIdentifier() == Property
->getIdentifier()) {
127 if (P
->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite
)
133 // Also look into protocols, for a user declared instance method.
134 for (const auto *Proto
: ID
->all_referenced_protocols())
135 if (Proto
->HasUserDeclaredSetterMethod(Property
))
138 // And in its super class.
139 ObjCInterfaceDecl
*OSC
= ID
->getSuperClass();
141 if (OSC
->HasUserDeclaredSetterMethod(Property
))
143 OSC
= OSC
->getSuperClass();
146 if (const ObjCProtocolDecl
*PD
= dyn_cast
<ObjCProtocolDecl
>(this))
147 for (const auto *PI
: PD
->protocols())
148 if (PI
->HasUserDeclaredSetterMethod(Property
))
154 ObjCPropertyDecl::findPropertyDecl(const DeclContext
*DC
,
155 const IdentifierInfo
*propertyID
) {
156 // If this context is a hidden protocol definition, don't find any
158 if (const ObjCProtocolDecl
*Proto
= dyn_cast
<ObjCProtocolDecl
>(DC
)) {
159 if (const ObjCProtocolDecl
*Def
= Proto
->getDefinition())
164 DeclContext::lookup_const_result R
= DC
->lookup(propertyID
);
165 for (DeclContext::lookup_const_iterator I
= R
.begin(), E
= R
.end(); I
!= E
;
167 if (ObjCPropertyDecl
*PD
= dyn_cast
<ObjCPropertyDecl
>(*I
))
174 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext
&Ctx
) const {
175 SmallString
<128> ivarName
;
177 llvm::raw_svector_ostream
os(ivarName
);
178 os
<< '_' << getIdentifier()->getName();
180 return &Ctx
.Idents
.get(ivarName
.str());
183 /// FindPropertyDeclaration - Finds declaration of the property given its name
184 /// in 'PropertyId' and returns it. It returns 0, if not found.
185 ObjCPropertyDecl
*ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo
*PropertyId
) const {
187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl
*Proto
= dyn_cast
<ObjCProtocolDecl
>(this)) {
189 if (const ObjCProtocolDecl
*Def
= Proto
->getDefinition())
194 if (ObjCPropertyDecl
*PD
=
195 ObjCPropertyDecl::findPropertyDecl(cast
<DeclContext
>(this), PropertyId
))
201 case Decl::ObjCProtocol
: {
202 const ObjCProtocolDecl
*PID
= cast
<ObjCProtocolDecl
>(this);
203 for (const auto *I
: PID
->protocols())
204 if (ObjCPropertyDecl
*P
= I
->FindPropertyDeclaration(PropertyId
))
208 case Decl::ObjCInterface
: {
209 const ObjCInterfaceDecl
*OID
= cast
<ObjCInterfaceDecl
>(this);
210 // Look through categories (but not extensions).
211 for (const auto *Cat
: OID
->visible_categories()) {
212 if (!Cat
->IsClassExtension())
213 if (ObjCPropertyDecl
*P
= Cat
->FindPropertyDeclaration(PropertyId
))
217 // Look through protocols.
218 for (const auto *I
: OID
->all_referenced_protocols())
219 if (ObjCPropertyDecl
*P
= I
->FindPropertyDeclaration(PropertyId
))
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl
*superClass
= OID
->getSuperClass())
224 return superClass
->FindPropertyDeclaration(PropertyId
);
227 case Decl::ObjCCategory
: {
228 const ObjCCategoryDecl
*OCD
= cast
<ObjCCategoryDecl
>(this);
229 // Look through protocols.
230 if (!OCD
->IsClassExtension())
231 for (const auto *I
: OCD
->protocols())
232 if (ObjCPropertyDecl
*P
= I
->FindPropertyDeclaration(PropertyId
))
240 void ObjCInterfaceDecl::anchor() { }
242 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
243 /// with name 'PropertyId' in the primary class; including those in protocols
244 /// (direct or indirect) used by the primary class.
247 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
248 IdentifierInfo
*PropertyId
) const {
249 // FIXME: Should make sure no callers ever do this.
250 if (!hasDefinition())
253 if (data().ExternallyCompleted
)
254 LoadExternalDefinition();
256 if (ObjCPropertyDecl
*PD
=
257 ObjCPropertyDecl::findPropertyDecl(cast
<DeclContext
>(this), PropertyId
))
260 // Look through protocols.
261 for (const auto *I
: all_referenced_protocols())
262 if (ObjCPropertyDecl
*P
= I
->FindPropertyDeclaration(PropertyId
))
268 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap
&PM
,
269 PropertyDeclOrder
&PO
) const {
270 for (auto *Prop
: properties()) {
271 PM
[Prop
->getIdentifier()] = Prop
;
274 for (const auto *PI
: all_referenced_protocols())
275 PI
->collectPropertiesToImplement(PM
, PO
);
276 // Note, the properties declared only in class extensions are still copied
277 // into the main @interface's property list, and therefore we don't
278 // explicitly, have to search class extension properties.
281 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
282 const ObjCInterfaceDecl
*Class
= this;
284 if (Class
->hasAttr
<ArcWeakrefUnavailableAttr
>())
286 Class
= Class
->getSuperClass();
291 const ObjCInterfaceDecl
*ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
292 const ObjCInterfaceDecl
*Class
= this;
294 if (Class
->hasAttr
<ObjCRequiresPropertyDefsAttr
>())
296 Class
= Class
->getSuperClass();
301 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
302 ObjCProtocolDecl
*const* ExtList
, unsigned ExtNum
,
305 if (data().ExternallyCompleted
)
306 LoadExternalDefinition();
308 if (data().AllReferencedProtocols
.empty() &&
309 data().ReferencedProtocols
.empty()) {
310 data().AllReferencedProtocols
.set(ExtList
, ExtNum
, C
);
314 // Check for duplicate protocol in class's protocol list.
315 // This is O(n*m). But it is extremely rare and number of protocols in
316 // class or its extension are very few.
317 SmallVector
<ObjCProtocolDecl
*, 8> ProtocolRefs
;
318 for (unsigned i
= 0; i
< ExtNum
; i
++) {
319 bool protocolExists
= false;
320 ObjCProtocolDecl
*ProtoInExtension
= ExtList
[i
];
321 for (auto *Proto
: all_referenced_protocols()) {
322 if (C
.ProtocolCompatibleWithProtocol(ProtoInExtension
, Proto
)) {
323 protocolExists
= true;
327 // Do we want to warn on a protocol in extension class which
328 // already exist in the class? Probably not.
330 ProtocolRefs
.push_back(ProtoInExtension
);
333 if (ProtocolRefs
.empty())
336 // Merge ProtocolRefs into class's protocol list;
337 for (auto *P
: all_referenced_protocols()) {
338 ProtocolRefs
.push_back(P
);
341 data().AllReferencedProtocols
.set(ProtocolRefs
.data(), ProtocolRefs
.size(),C
);
344 const ObjCInterfaceDecl
*
345 ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
346 const ObjCInterfaceDecl
*IFace
= this;
348 if (IFace
->hasDesignatedInitializers())
350 if (!IFace
->inheritsDesignatedInitializers())
352 IFace
= IFace
->getSuperClass();
357 static bool isIntroducingInitializers(const ObjCInterfaceDecl
*D
) {
358 for (const auto *MD
: D
->instance_methods()) {
359 if (MD
->getMethodFamily() == OMF_init
&& !MD
->isOverriding())
362 for (const auto *Ext
: D
->visible_extensions()) {
363 for (const auto *MD
: Ext
->instance_methods()) {
364 if (MD
->getMethodFamily() == OMF_init
&& !MD
->isOverriding())
368 if (const auto *ImplD
= D
->getImplementation()) {
369 for (const auto *MD
: ImplD
->instance_methods()) {
370 if (MD
->getMethodFamily() == OMF_init
&& !MD
->isOverriding())
377 bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
378 switch (data().InheritedDesignatedInitializers
) {
379 case DefinitionData::IDI_Inherited
:
381 case DefinitionData::IDI_NotInherited
:
383 case DefinitionData::IDI_Unknown
: {
384 // If the class introduced initializers we conservatively assume that we
385 // don't know if any of them is a designated initializer to avoid possible
386 // misleading warnings.
387 if (isIntroducingInitializers(this)) {
388 data().InheritedDesignatedInitializers
= DefinitionData::IDI_NotInherited
;
390 if (auto SuperD
= getSuperClass()) {
391 data().InheritedDesignatedInitializers
=
392 SuperD
->declaresOrInheritsDesignatedInitializers() ?
393 DefinitionData::IDI_Inherited
:
394 DefinitionData::IDI_NotInherited
;
396 data().InheritedDesignatedInitializers
=
397 DefinitionData::IDI_NotInherited
;
400 assert(data().InheritedDesignatedInitializers
401 != DefinitionData::IDI_Unknown
);
402 return data().InheritedDesignatedInitializers
==
403 DefinitionData::IDI_Inherited
;
407 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
410 void ObjCInterfaceDecl::getDesignatedInitializers(
411 llvm::SmallVectorImpl
<const ObjCMethodDecl
*> &Methods
) const {
412 // Check for a complete definition and recover if not so.
413 if (!isThisDeclarationADefinition())
415 if (data().ExternallyCompleted
)
416 LoadExternalDefinition();
418 const ObjCInterfaceDecl
*IFace
= findInterfaceWithDesignatedInitializers();
422 for (const auto *MD
: IFace
->instance_methods())
423 if (MD
->isThisDeclarationADesignatedInitializer())
424 Methods
.push_back(MD
);
425 for (const auto *Ext
: IFace
->visible_extensions()) {
426 for (const auto *MD
: Ext
->instance_methods())
427 if (MD
->isThisDeclarationADesignatedInitializer())
428 Methods
.push_back(MD
);
432 bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel
,
433 const ObjCMethodDecl
**InitMethod
) const {
434 // Check for a complete definition and recover if not so.
435 if (!isThisDeclarationADefinition())
437 if (data().ExternallyCompleted
)
438 LoadExternalDefinition();
440 const ObjCInterfaceDecl
*IFace
= findInterfaceWithDesignatedInitializers();
444 if (const ObjCMethodDecl
*MD
= IFace
->getInstanceMethod(Sel
)) {
445 if (MD
->isThisDeclarationADesignatedInitializer()) {
451 for (const auto *Ext
: IFace
->visible_extensions()) {
452 if (const ObjCMethodDecl
*MD
= Ext
->getInstanceMethod(Sel
)) {
453 if (MD
->isThisDeclarationADesignatedInitializer()) {
463 void ObjCInterfaceDecl::allocateDefinitionData() {
464 assert(!hasDefinition() && "ObjC class already has a definition");
465 Data
.setPointer(new (getASTContext()) DefinitionData());
466 Data
.getPointer()->Definition
= this;
468 // Make the type point at the definition, now that we have one.
470 cast
<ObjCInterfaceType
>(TypeForDecl
)->Decl
= this;
473 void ObjCInterfaceDecl::startDefinition() {
474 allocateDefinitionData();
476 // Update all of the declarations with a pointer to the definition.
477 for (auto RD
: redecls()) {
483 ObjCIvarDecl
*ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo
*ID
,
484 ObjCInterfaceDecl
*&clsDeclared
) {
485 // FIXME: Should make sure no callers ever do this.
486 if (!hasDefinition())
489 if (data().ExternallyCompleted
)
490 LoadExternalDefinition();
492 ObjCInterfaceDecl
* ClassDecl
= this;
493 while (ClassDecl
!= nullptr) {
494 if (ObjCIvarDecl
*I
= ClassDecl
->getIvarDecl(ID
)) {
495 clsDeclared
= ClassDecl
;
499 for (const auto *Ext
: ClassDecl
->visible_extensions()) {
500 if (ObjCIvarDecl
*I
= Ext
->getIvarDecl(ID
)) {
501 clsDeclared
= ClassDecl
;
506 ClassDecl
= ClassDecl
->getSuperClass();
511 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
512 /// class whose name is passed as argument. If it is not one of the super classes
513 /// the it returns NULL.
514 ObjCInterfaceDecl
*ObjCInterfaceDecl::lookupInheritedClass(
515 const IdentifierInfo
*ICName
) {
516 // FIXME: Should make sure no callers ever do this.
517 if (!hasDefinition())
520 if (data().ExternallyCompleted
)
521 LoadExternalDefinition();
523 ObjCInterfaceDecl
* ClassDecl
= this;
524 while (ClassDecl
!= nullptr) {
525 if (ClassDecl
->getIdentifier() == ICName
)
527 ClassDecl
= ClassDecl
->getSuperClass();
533 ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo
*Name
) {
534 for (auto *P
: all_referenced_protocols())
535 if (P
->lookupProtocolNamed(Name
))
537 ObjCInterfaceDecl
*SuperClass
= getSuperClass();
538 return SuperClass
? SuperClass
->lookupNestedProtocol(Name
) : nullptr;
541 /// lookupMethod - This method returns an instance/class method by looking in
542 /// the class, its categories, and its super classes (using a linear search).
543 /// When argument category "C" is specified, any implicit method found
544 /// in this category is ignored.
545 ObjCMethodDecl
*ObjCInterfaceDecl::lookupMethod(Selector Sel
,
547 bool shallowCategoryLookup
,
549 const ObjCCategoryDecl
*C
) const
551 // FIXME: Should make sure no callers ever do this.
552 if (!hasDefinition())
555 const ObjCInterfaceDecl
* ClassDecl
= this;
556 ObjCMethodDecl
*MethodDecl
= nullptr;
558 if (data().ExternallyCompleted
)
559 LoadExternalDefinition();
562 // 1. Look through primary class.
563 if ((MethodDecl
= ClassDecl
->getMethod(Sel
, isInstance
)))
566 // 2. Didn't find one yet - now look through categories.
567 for (const auto *Cat
: ClassDecl
->visible_categories())
568 if ((MethodDecl
= Cat
->getMethod(Sel
, isInstance
)))
569 if (C
!= Cat
|| !MethodDecl
->isImplicit())
572 // 3. Didn't find one yet - look through primary class's protocols.
573 for (const auto *I
: ClassDecl
->protocols())
574 if ((MethodDecl
= I
->lookupMethod(Sel
, isInstance
)))
577 // 4. Didn't find one yet - now look through categories' protocols
578 if (!shallowCategoryLookup
)
579 for (const auto *Cat
: ClassDecl
->visible_categories()) {
580 // Didn't find one yet - look through protocols.
581 const ObjCList
<ObjCProtocolDecl
> &Protocols
=
582 Cat
->getReferencedProtocols();
583 for (ObjCList
<ObjCProtocolDecl
>::iterator I
= Protocols
.begin(),
584 E
= Protocols
.end(); I
!= E
; ++I
)
585 if ((MethodDecl
= (*I
)->lookupMethod(Sel
, isInstance
)))
586 if (C
!= Cat
|| !MethodDecl
->isImplicit())
594 // 5. Get to the super class (if any).
595 ClassDecl
= ClassDecl
->getSuperClass();
600 // Will search "local" class/category implementations for a method decl.
601 // If failed, then we search in class's root for an instance method.
602 // Returns 0 if no method is found.
603 ObjCMethodDecl
*ObjCInterfaceDecl::lookupPrivateMethod(
605 bool Instance
) const {
606 // FIXME: Should make sure no callers ever do this.
607 if (!hasDefinition())
610 if (data().ExternallyCompleted
)
611 LoadExternalDefinition();
613 ObjCMethodDecl
*Method
= nullptr;
614 if (ObjCImplementationDecl
*ImpDecl
= getImplementation())
615 Method
= Instance
? ImpDecl
->getInstanceMethod(Sel
)
616 : ImpDecl
->getClassMethod(Sel
);
618 // Look through local category implementations associated with the class.
620 Method
= Instance
? getCategoryInstanceMethod(Sel
)
621 : getCategoryClassMethod(Sel
);
623 // Before we give up, check if the selector is an instance method.
624 // But only in the root. This matches gcc's behavior and what the
626 if (!Instance
&& !Method
&& !getSuperClass()) {
627 Method
= lookupInstanceMethod(Sel
);
628 // Look through local category implementations associated
629 // with the root class.
631 Method
= lookupPrivateMethod(Sel
, true);
634 if (!Method
&& getSuperClass())
635 return getSuperClass()->lookupPrivateMethod(Sel
, Instance
);
639 //===----------------------------------------------------------------------===//
641 //===----------------------------------------------------------------------===//
643 ObjCMethodDecl
*ObjCMethodDecl::Create(
644 ASTContext
&C
, SourceLocation beginLoc
, SourceLocation endLoc
,
645 Selector SelInfo
, QualType T
, TypeSourceInfo
*ReturnTInfo
,
646 DeclContext
*contextDecl
, bool isInstance
, bool isVariadic
,
647 bool isPropertyAccessor
, bool isImplicitlyDeclared
, bool isDefined
,
648 ImplementationControl impControl
, bool HasRelatedResultType
) {
649 return new (C
, contextDecl
) ObjCMethodDecl(
650 beginLoc
, endLoc
, SelInfo
, T
, ReturnTInfo
, contextDecl
, isInstance
,
651 isVariadic
, isPropertyAccessor
, isImplicitlyDeclared
, isDefined
,
652 impControl
, HasRelatedResultType
);
655 ObjCMethodDecl
*ObjCMethodDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
656 return new (C
, ID
) ObjCMethodDecl(SourceLocation(), SourceLocation(),
657 Selector(), QualType(), nullptr, nullptr);
660 bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
661 return getMethodFamily() == OMF_init
&&
662 hasAttr
<ObjCDesignatedInitializerAttr
>();
665 bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
666 const ObjCMethodDecl
**InitMethod
) const {
667 if (getMethodFamily() != OMF_init
)
669 const DeclContext
*DC
= getDeclContext();
670 if (isa
<ObjCProtocolDecl
>(DC
))
672 if (const ObjCInterfaceDecl
*ID
= getClassInterface())
673 return ID
->isDesignatedInitializer(getSelector(), InitMethod
);
677 Stmt
*ObjCMethodDecl::getBody() const {
678 return Body
.get(getASTContext().getExternalSource());
681 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl
*PrevMethod
) {
683 getASTContext().setObjCMethodRedeclaration(PrevMethod
, this);
684 IsRedeclaration
= true;
685 PrevMethod
->HasRedeclaration
= true;
688 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext
&C
,
689 ArrayRef
<ParmVarDecl
*> Params
,
690 ArrayRef
<SourceLocation
> SelLocs
) {
691 ParamsAndSelLocs
= nullptr;
692 NumParams
= Params
.size();
693 if (Params
.empty() && SelLocs
.empty())
696 unsigned Size
= sizeof(ParmVarDecl
*) * NumParams
+
697 sizeof(SourceLocation
) * SelLocs
.size();
698 ParamsAndSelLocs
= C
.Allocate(Size
);
699 std::copy(Params
.begin(), Params
.end(), getParams());
700 std::copy(SelLocs
.begin(), SelLocs
.end(), getStoredSelLocs());
703 void ObjCMethodDecl::getSelectorLocs(
704 SmallVectorImpl
<SourceLocation
> &SelLocs
) const {
705 for (unsigned i
= 0, e
= getNumSelectorLocs(); i
!= e
; ++i
)
706 SelLocs
.push_back(getSelectorLoc(i
));
709 void ObjCMethodDecl::setMethodParams(ASTContext
&C
,
710 ArrayRef
<ParmVarDecl
*> Params
,
711 ArrayRef
<SourceLocation
> SelLocs
) {
712 assert((!SelLocs
.empty() || isImplicit()) &&
713 "No selector locs for non-implicit method");
715 return setParamsAndSelLocs(C
, Params
, llvm::None
);
717 SelLocsKind
= hasStandardSelectorLocs(getSelector(), SelLocs
, Params
,
719 if (SelLocsKind
!= SelLoc_NonStandard
)
720 return setParamsAndSelLocs(C
, Params
, llvm::None
);
722 setParamsAndSelLocs(C
, Params
, SelLocs
);
725 /// \brief A definition will return its interface declaration.
726 /// An interface declaration will return its definition.
727 /// Otherwise it will return itself.
728 ObjCMethodDecl
*ObjCMethodDecl::getNextRedeclarationImpl() {
729 ASTContext
&Ctx
= getASTContext();
730 ObjCMethodDecl
*Redecl
= nullptr;
731 if (HasRedeclaration
)
732 Redecl
= const_cast<ObjCMethodDecl
*>(Ctx
.getObjCMethodRedeclaration(this));
736 Decl
*CtxD
= cast
<Decl
>(getDeclContext());
738 if (!CtxD
->isInvalidDecl()) {
739 if (ObjCInterfaceDecl
*IFD
= dyn_cast
<ObjCInterfaceDecl
>(CtxD
)) {
740 if (ObjCImplementationDecl
*ImplD
= Ctx
.getObjCImplementation(IFD
))
741 if (!ImplD
->isInvalidDecl())
742 Redecl
= ImplD
->getMethod(getSelector(), isInstanceMethod());
744 } else if (ObjCCategoryDecl
*CD
= dyn_cast
<ObjCCategoryDecl
>(CtxD
)) {
745 if (ObjCCategoryImplDecl
*ImplD
= Ctx
.getObjCImplementation(CD
))
746 if (!ImplD
->isInvalidDecl())
747 Redecl
= ImplD
->getMethod(getSelector(), isInstanceMethod());
749 } else if (ObjCImplementationDecl
*ImplD
=
750 dyn_cast
<ObjCImplementationDecl
>(CtxD
)) {
751 if (ObjCInterfaceDecl
*IFD
= ImplD
->getClassInterface())
752 if (!IFD
->isInvalidDecl())
753 Redecl
= IFD
->getMethod(getSelector(), isInstanceMethod());
755 } else if (ObjCCategoryImplDecl
*CImplD
=
756 dyn_cast
<ObjCCategoryImplDecl
>(CtxD
)) {
757 if (ObjCCategoryDecl
*CatD
= CImplD
->getCategoryDecl())
758 if (!CatD
->isInvalidDecl())
759 Redecl
= CatD
->getMethod(getSelector(), isInstanceMethod());
763 if (!Redecl
&& isRedeclaration()) {
764 // This is the last redeclaration, go back to the first method.
765 return cast
<ObjCContainerDecl
>(CtxD
)->getMethod(getSelector(),
769 return Redecl
? Redecl
: this;
772 ObjCMethodDecl
*ObjCMethodDecl::getCanonicalDecl() {
773 Decl
*CtxD
= cast
<Decl
>(getDeclContext());
775 if (ObjCImplementationDecl
*ImplD
= dyn_cast
<ObjCImplementationDecl
>(CtxD
)) {
776 if (ObjCInterfaceDecl
*IFD
= ImplD
->getClassInterface())
777 if (ObjCMethodDecl
*MD
= IFD
->getMethod(getSelector(),
781 } else if (ObjCCategoryImplDecl
*CImplD
=
782 dyn_cast
<ObjCCategoryImplDecl
>(CtxD
)) {
783 if (ObjCCategoryDecl
*CatD
= CImplD
->getCategoryDecl())
784 if (ObjCMethodDecl
*MD
= CatD
->getMethod(getSelector(),
789 if (isRedeclaration())
790 return cast
<ObjCContainerDecl
>(CtxD
)->getMethod(getSelector(),
796 SourceLocation
ObjCMethodDecl::getLocEnd() const {
797 if (Stmt
*Body
= getBody())
798 return Body
->getLocEnd();
802 ObjCMethodFamily
ObjCMethodDecl::getMethodFamily() const {
803 ObjCMethodFamily family
= static_cast<ObjCMethodFamily
>(Family
);
804 if (family
!= static_cast<unsigned>(InvalidObjCMethodFamily
))
807 // Check for an explicit attribute.
808 if (const ObjCMethodFamilyAttr
*attr
= getAttr
<ObjCMethodFamilyAttr
>()) {
809 // The unfortunate necessity of mapping between enums here is due
810 // to the attributes framework.
811 switch (attr
->getFamily()) {
812 case ObjCMethodFamilyAttr::OMF_None
: family
= OMF_None
; break;
813 case ObjCMethodFamilyAttr::OMF_alloc
: family
= OMF_alloc
; break;
814 case ObjCMethodFamilyAttr::OMF_copy
: family
= OMF_copy
; break;
815 case ObjCMethodFamilyAttr::OMF_init
: family
= OMF_init
; break;
816 case ObjCMethodFamilyAttr::OMF_mutableCopy
: family
= OMF_mutableCopy
; break;
817 case ObjCMethodFamilyAttr::OMF_new
: family
= OMF_new
; break;
819 Family
= static_cast<unsigned>(family
);
823 family
= getSelector().getMethodFamily();
825 case OMF_None
: break;
827 // init only has a conventional meaning for an instance method, and
828 // it has to return an object.
830 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
834 // alloc/copy/new have a conventional meaning for both class and
835 // instance methods, but they require an object return.
838 case OMF_mutableCopy
:
840 if (!getReturnType()->isObjCObjectPointerType())
844 // These selectors have a conventional meaning only for instance methods.
849 case OMF_autorelease
:
850 case OMF_retainCount
:
852 if (!isInstanceMethod())
857 if (isInstanceMethod() || !getReturnType()->isVoidType())
861 case OMF_performSelector
:
862 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
865 unsigned noParams
= param_size();
866 if (noParams
< 1 || noParams
> 3)
869 ObjCMethodDecl::param_type_iterator it
= param_type_begin();
870 QualType ArgT
= (*it
);
871 if (!ArgT
->isObjCSelType()) {
878 if (!ArgT
->isObjCIdType()) {
890 Family
= static_cast<unsigned>(family
);
894 void ObjCMethodDecl::createImplicitParams(ASTContext
&Context
,
895 const ObjCInterfaceDecl
*OID
) {
897 if (isInstanceMethod()) {
898 // There may be no interface context due to error in declaration
899 // of the interface (which has been reported). Recover gracefully.
901 selfTy
= Context
.getObjCInterfaceType(OID
);
902 selfTy
= Context
.getObjCObjectPointerType(selfTy
);
904 selfTy
= Context
.getObjCIdType();
906 } else // we have a factory method.
907 selfTy
= Context
.getObjCClassType();
909 bool selfIsPseudoStrong
= false;
910 bool selfIsConsumed
= false;
912 if (Context
.getLangOpts().ObjCAutoRefCount
) {
913 if (isInstanceMethod()) {
914 selfIsConsumed
= hasAttr
<NSConsumesSelfAttr
>();
916 // 'self' is always __strong. It's actually pseudo-strong except
917 // in init methods (or methods labeled ns_consumes_self), though.
919 qs
.setObjCLifetime(Qualifiers::OCL_Strong
);
920 selfTy
= Context
.getQualifiedType(selfTy
, qs
);
922 // In addition, 'self' is const unless this is an init method.
923 if (getMethodFamily() != OMF_init
&& !selfIsConsumed
) {
924 selfTy
= selfTy
.withConst();
925 selfIsPseudoStrong
= true;
929 assert(isClassMethod());
930 // 'self' is always const in class methods.
931 selfTy
= selfTy
.withConst();
932 selfIsPseudoStrong
= true;
936 ImplicitParamDecl
*self
937 = ImplicitParamDecl::Create(Context
, this, SourceLocation(),
938 &Context
.Idents
.get("self"), selfTy
);
942 self
->addAttr(NSConsumedAttr::CreateImplicit(Context
));
944 if (selfIsPseudoStrong
)
945 self
->setARCPseudoStrong(true);
947 setCmdDecl(ImplicitParamDecl::Create(Context
, this, SourceLocation(),
948 &Context
.Idents
.get("_cmd"),
949 Context
.getObjCSelType()));
952 ObjCInterfaceDecl
*ObjCMethodDecl::getClassInterface() {
953 if (ObjCInterfaceDecl
*ID
= dyn_cast
<ObjCInterfaceDecl
>(getDeclContext()))
955 if (ObjCCategoryDecl
*CD
= dyn_cast
<ObjCCategoryDecl
>(getDeclContext()))
956 return CD
->getClassInterface();
957 if (ObjCImplDecl
*IMD
= dyn_cast
<ObjCImplDecl
>(getDeclContext()))
958 return IMD
->getClassInterface();
959 if (isa
<ObjCProtocolDecl
>(getDeclContext()))
961 llvm_unreachable("unknown method context");
964 SourceRange
ObjCMethodDecl::getReturnTypeSourceRange() const {
965 const auto *TSI
= getReturnTypeSourceInfo();
967 return TSI
->getTypeLoc().getSourceRange();
968 return SourceRange();
971 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl
*Container
,
972 const ObjCMethodDecl
*Method
,
973 SmallVectorImpl
<const ObjCMethodDecl
*> &Methods
,
978 // In categories look for overriden methods from protocols. A method from
979 // category is not "overriden" since it is considered as the "same" method
980 // (same USR) as the one from the interface.
981 if (const ObjCCategoryDecl
*
982 Category
= dyn_cast
<ObjCCategoryDecl
>(Container
)) {
983 // Check whether we have a matching method at this category but only if we
984 // are at the super class level.
987 Overridden
= Container
->getMethod(Method
->getSelector(),
988 Method
->isInstanceMethod(),
989 /*AllowHidden=*/true))
990 if (Method
!= Overridden
) {
991 // We found an override at this category; there is no need to look
992 // into its protocols.
993 Methods
.push_back(Overridden
);
997 for (const auto *P
: Category
->protocols())
998 CollectOverriddenMethodsRecurse(P
, Method
, Methods
, MovedToSuper
);
1002 // Check whether we have a matching method at this level.
1003 if (const ObjCMethodDecl
*
1004 Overridden
= Container
->getMethod(Method
->getSelector(),
1005 Method
->isInstanceMethod(),
1006 /*AllowHidden=*/true))
1007 if (Method
!= Overridden
) {
1008 // We found an override at this level; there is no need to look
1009 // into other protocols or categories.
1010 Methods
.push_back(Overridden
);
1014 if (const ObjCProtocolDecl
*Protocol
= dyn_cast
<ObjCProtocolDecl
>(Container
)){
1015 for (const auto *P
: Protocol
->protocols())
1016 CollectOverriddenMethodsRecurse(P
, Method
, Methods
, MovedToSuper
);
1019 if (const ObjCInterfaceDecl
*
1020 Interface
= dyn_cast
<ObjCInterfaceDecl
>(Container
)) {
1021 for (const auto *P
: Interface
->protocols())
1022 CollectOverriddenMethodsRecurse(P
, Method
, Methods
, MovedToSuper
);
1024 for (const auto *Cat
: Interface
->known_categories())
1025 CollectOverriddenMethodsRecurse(Cat
, Method
, Methods
, MovedToSuper
);
1027 if (const ObjCInterfaceDecl
*Super
= Interface
->getSuperClass())
1028 return CollectOverriddenMethodsRecurse(Super
, Method
, Methods
,
1029 /*MovedToSuper=*/true);
1033 static inline void CollectOverriddenMethods(const ObjCContainerDecl
*Container
,
1034 const ObjCMethodDecl
*Method
,
1035 SmallVectorImpl
<const ObjCMethodDecl
*> &Methods
) {
1036 CollectOverriddenMethodsRecurse(Container
, Method
, Methods
,
1037 /*MovedToSuper=*/false);
1040 static void collectOverriddenMethodsSlow(const ObjCMethodDecl
*Method
,
1041 SmallVectorImpl
<const ObjCMethodDecl
*> &overridden
) {
1042 assert(Method
->isOverriding());
1044 if (const ObjCProtocolDecl
*
1045 ProtD
= dyn_cast
<ObjCProtocolDecl
>(Method
->getDeclContext())) {
1046 CollectOverriddenMethods(ProtD
, Method
, overridden
);
1048 } else if (const ObjCImplDecl
*
1049 IMD
= dyn_cast
<ObjCImplDecl
>(Method
->getDeclContext())) {
1050 const ObjCInterfaceDecl
*ID
= IMD
->getClassInterface();
1053 // Start searching for overridden methods using the method from the
1054 // interface as starting point.
1055 if (const ObjCMethodDecl
*IFaceMeth
= ID
->getMethod(Method
->getSelector(),
1056 Method
->isInstanceMethod(),
1057 /*AllowHidden=*/true))
1059 CollectOverriddenMethods(ID
, Method
, overridden
);
1061 } else if (const ObjCCategoryDecl
*
1062 CatD
= dyn_cast
<ObjCCategoryDecl
>(Method
->getDeclContext())) {
1063 const ObjCInterfaceDecl
*ID
= CatD
->getClassInterface();
1066 // Start searching for overridden methods using the method from the
1067 // interface as starting point.
1068 if (const ObjCMethodDecl
*IFaceMeth
= ID
->getMethod(Method
->getSelector(),
1069 Method
->isInstanceMethod(),
1070 /*AllowHidden=*/true))
1072 CollectOverriddenMethods(ID
, Method
, overridden
);
1075 CollectOverriddenMethods(
1076 dyn_cast_or_null
<ObjCContainerDecl
>(Method
->getDeclContext()),
1077 Method
, overridden
);
1081 void ObjCMethodDecl::getOverriddenMethods(
1082 SmallVectorImpl
<const ObjCMethodDecl
*> &Overridden
) const {
1083 const ObjCMethodDecl
*Method
= this;
1085 if (Method
->isRedeclaration()) {
1086 Method
= cast
<ObjCContainerDecl
>(Method
->getDeclContext())->
1087 getMethod(Method
->getSelector(), Method
->isInstanceMethod());
1090 if (Method
->isOverriding()) {
1091 collectOverriddenMethodsSlow(Method
, Overridden
);
1092 assert(!Overridden
.empty() &&
1093 "ObjCMethodDecl's overriding bit is not as expected");
1097 const ObjCPropertyDecl
*
1098 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides
) const {
1099 Selector Sel
= getSelector();
1100 unsigned NumArgs
= Sel
.getNumArgs();
1104 if (!isInstanceMethod() || getMethodFamily() != OMF_None
)
1107 if (isPropertyAccessor()) {
1108 const ObjCContainerDecl
*Container
= cast
<ObjCContainerDecl
>(getParent());
1109 // If container is class extension, find its primary class.
1110 if (const ObjCCategoryDecl
*CatDecl
= dyn_cast
<ObjCCategoryDecl
>(Container
))
1111 if (CatDecl
->IsClassExtension())
1112 Container
= CatDecl
->getClassInterface();
1114 bool IsGetter
= (NumArgs
== 0);
1116 for (const auto *I
: Container
->properties()) {
1117 Selector NextSel
= IsGetter
? I
->getGetterName()
1118 : I
->getSetterName();
1123 llvm_unreachable("Marked as a property accessor but no property found!");
1126 if (!CheckOverrides
)
1129 typedef SmallVector
<const ObjCMethodDecl
*, 8> OverridesTy
;
1130 OverridesTy Overrides
;
1131 getOverriddenMethods(Overrides
);
1132 for (OverridesTy::const_iterator I
= Overrides
.begin(), E
= Overrides
.end();
1134 if (const ObjCPropertyDecl
*Prop
= (*I
)->findPropertyDecl(false))
1141 //===----------------------------------------------------------------------===//
1142 // ObjCInterfaceDecl
1143 //===----------------------------------------------------------------------===//
1145 ObjCInterfaceDecl
*ObjCInterfaceDecl::Create(const ASTContext
&C
,
1147 SourceLocation atLoc
,
1149 ObjCInterfaceDecl
*PrevDecl
,
1150 SourceLocation ClassLoc
,
1152 ObjCInterfaceDecl
*Result
= new (C
, DC
)
1153 ObjCInterfaceDecl(C
, DC
, atLoc
, Id
, ClassLoc
, PrevDecl
, isInternal
);
1154 Result
->Data
.setInt(!C
.getLangOpts().Modules
);
1155 C
.getObjCInterfaceType(Result
, PrevDecl
);
1159 ObjCInterfaceDecl
*ObjCInterfaceDecl::CreateDeserialized(const ASTContext
&C
,
1161 ObjCInterfaceDecl
*Result
= new (C
, ID
) ObjCInterfaceDecl(C
, nullptr,
1166 Result
->Data
.setInt(!C
.getLangOpts().Modules
);
1170 ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext
&C
, DeclContext
*DC
,
1171 SourceLocation AtLoc
, IdentifierInfo
*Id
,
1172 SourceLocation CLoc
,
1173 ObjCInterfaceDecl
*PrevDecl
,
1175 : ObjCContainerDecl(ObjCInterface
, DC
, Id
, CLoc
, AtLoc
),
1176 redeclarable_base(C
), TypeForDecl(nullptr), Data() {
1177 setPreviousDecl(PrevDecl
);
1179 // Copy the 'data' pointer over.
1181 Data
= PrevDecl
->Data
;
1183 setImplicit(IsInternal
);
1186 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1187 assert(data().ExternallyCompleted
&& "Class is not externally completed");
1188 data().ExternallyCompleted
= false;
1189 getASTContext().getExternalSource()->CompleteType(
1190 const_cast<ObjCInterfaceDecl
*>(this));
1193 void ObjCInterfaceDecl::setExternallyCompleted() {
1194 assert(getASTContext().getExternalSource() &&
1195 "Class can't be externally completed without an external source");
1196 assert(hasDefinition() &&
1197 "Forward declarations can't be externally completed");
1198 data().ExternallyCompleted
= true;
1201 void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1202 // Check for a complete definition and recover if not so.
1203 if (!isThisDeclarationADefinition())
1205 data().HasDesignatedInitializers
= true;
1208 bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1209 // Check for a complete definition and recover if not so.
1210 if (!isThisDeclarationADefinition())
1212 if (data().ExternallyCompleted
)
1213 LoadExternalDefinition();
1215 return data().HasDesignatedInitializers
;
1219 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1220 if (ObjCRuntimeNameAttr
*ObjCRTName
= getAttr
<ObjCRuntimeNameAttr
>())
1221 return ObjCRTName
->getMetadataName();
1227 ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1228 if (ObjCInterfaceDecl
*ID
=
1229 const_cast<ObjCImplementationDecl
*>(this)->getClassInterface())
1230 return ID
->getObjCRuntimeNameAsString();
1235 ObjCImplementationDecl
*ObjCInterfaceDecl::getImplementation() const {
1236 if (const ObjCInterfaceDecl
*Def
= getDefinition()) {
1237 if (data().ExternallyCompleted
)
1238 LoadExternalDefinition();
1240 return getASTContext().getObjCImplementation(
1241 const_cast<ObjCInterfaceDecl
*>(Def
));
1244 // FIXME: Should make sure no callers ever do this.
1248 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl
*ImplD
) {
1249 getASTContext().setObjCImplementation(getDefinition(), ImplD
);
1253 struct SynthesizeIvarChunk
{
1256 SynthesizeIvarChunk(uint64_t size
, ObjCIvarDecl
*ivar
)
1257 : Size(size
), Ivar(ivar
) {}
1260 bool operator<(const SynthesizeIvarChunk
& LHS
,
1261 const SynthesizeIvarChunk
&RHS
) {
1262 return LHS
.Size
< RHS
.Size
;
1266 /// all_declared_ivar_begin - return first ivar declared in this class,
1267 /// its extensions and its implementation. Lazily build the list on first
1270 /// Caveat: The list returned by this method reflects the current
1271 /// state of the parser. The cache will be updated for every ivar
1272 /// added by an extension or the implementation when they are
1274 /// See also ObjCIvarDecl::Create().
1275 ObjCIvarDecl
*ObjCInterfaceDecl::all_declared_ivar_begin() {
1276 // FIXME: Should make sure no callers ever do this.
1277 if (!hasDefinition())
1280 ObjCIvarDecl
*curIvar
= nullptr;
1281 if (!data().IvarList
) {
1282 if (!ivar_empty()) {
1283 ObjCInterfaceDecl::ivar_iterator I
= ivar_begin(), E
= ivar_end();
1284 data().IvarList
= *I
; ++I
;
1285 for (curIvar
= data().IvarList
; I
!= E
; curIvar
= *I
, ++I
)
1286 curIvar
->setNextIvar(*I
);
1289 for (const auto *Ext
: known_extensions()) {
1290 if (!Ext
->ivar_empty()) {
1291 ObjCCategoryDecl::ivar_iterator
1292 I
= Ext
->ivar_begin(),
1293 E
= Ext
->ivar_end();
1294 if (!data().IvarList
) {
1295 data().IvarList
= *I
; ++I
;
1296 curIvar
= data().IvarList
;
1298 for ( ;I
!= E
; curIvar
= *I
, ++I
)
1299 curIvar
->setNextIvar(*I
);
1302 data().IvarListMissingImplementation
= true;
1305 // cached and complete!
1306 if (!data().IvarListMissingImplementation
)
1307 return data().IvarList
;
1309 if (ObjCImplementationDecl
*ImplDecl
= getImplementation()) {
1310 data().IvarListMissingImplementation
= false;
1311 if (!ImplDecl
->ivar_empty()) {
1312 SmallVector
<SynthesizeIvarChunk
, 16> layout
;
1313 for (auto *IV
: ImplDecl
->ivars()) {
1314 if (IV
->getSynthesize() && !IV
->isInvalidDecl()) {
1315 layout
.push_back(SynthesizeIvarChunk(
1316 IV
->getASTContext().getTypeSize(IV
->getType()), IV
));
1319 if (!data().IvarList
)
1320 data().IvarList
= IV
;
1322 curIvar
->setNextIvar(IV
);
1326 if (!layout
.empty()) {
1327 // Order synthesized ivars by their size.
1328 std::stable_sort(layout
.begin(), layout
.end());
1329 unsigned Ix
= 0, EIx
= layout
.size();
1330 if (!data().IvarList
) {
1331 data().IvarList
= layout
[0].Ivar
; Ix
++;
1332 curIvar
= data().IvarList
;
1334 for ( ; Ix
!= EIx
; curIvar
= layout
[Ix
].Ivar
, Ix
++)
1335 curIvar
->setNextIvar(layout
[Ix
].Ivar
);
1339 return data().IvarList
;
1342 /// FindCategoryDeclaration - Finds category declaration in the list of
1343 /// categories for this class and returns it. Name of the category is passed
1344 /// in 'CategoryId'. If category not found, return 0;
1347 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo
*CategoryId
) const {
1348 // FIXME: Should make sure no callers ever do this.
1349 if (!hasDefinition())
1352 if (data().ExternallyCompleted
)
1353 LoadExternalDefinition();
1355 for (auto *Cat
: visible_categories())
1356 if (Cat
->getIdentifier() == CategoryId
)
1363 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel
) const {
1364 for (const auto *Cat
: visible_categories()) {
1365 if (ObjCCategoryImplDecl
*Impl
= Cat
->getImplementation())
1366 if (ObjCMethodDecl
*MD
= Impl
->getInstanceMethod(Sel
))
1373 ObjCMethodDecl
*ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel
) const {
1374 for (const auto *Cat
: visible_categories()) {
1375 if (ObjCCategoryImplDecl
*Impl
= Cat
->getImplementation())
1376 if (ObjCMethodDecl
*MD
= Impl
->getClassMethod(Sel
))
1383 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1384 /// has been implemented in IDecl class, its super class or categories (if
1385 /// lookupCategory is true).
1386 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl
*lProto
,
1387 bool lookupCategory
,
1388 bool RHSIsQualifiedID
) {
1389 if (!hasDefinition())
1392 ObjCInterfaceDecl
*IDecl
= this;
1393 // 1st, look up the class.
1394 for (auto *PI
: IDecl
->protocols()){
1395 if (getASTContext().ProtocolCompatibleWithProtocol(lProto
, PI
))
1397 // This is dubious and is added to be compatible with gcc. In gcc, it is
1398 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1399 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1400 // object. This IMO, should be a bug.
1401 // FIXME: Treat this as an extension, and flag this as an error when GCC
1402 // extensions are not enabled.
1403 if (RHSIsQualifiedID
&&
1404 getASTContext().ProtocolCompatibleWithProtocol(PI
, lProto
))
1408 // 2nd, look up the category.
1410 for (const auto *Cat
: visible_categories()) {
1411 for (auto *PI
: Cat
->protocols())
1412 if (getASTContext().ProtocolCompatibleWithProtocol(lProto
, PI
))
1416 // 3rd, look up the super class(s)
1417 if (IDecl
->getSuperClass())
1419 IDecl
->getSuperClass()->ClassImplementsProtocol(lProto
, lookupCategory
,
1425 //===----------------------------------------------------------------------===//
1427 //===----------------------------------------------------------------------===//
1429 void ObjCIvarDecl::anchor() { }
1431 ObjCIvarDecl
*ObjCIvarDecl::Create(ASTContext
&C
, ObjCContainerDecl
*DC
,
1432 SourceLocation StartLoc
,
1433 SourceLocation IdLoc
, IdentifierInfo
*Id
,
1434 QualType T
, TypeSourceInfo
*TInfo
,
1435 AccessControl ac
, Expr
*BW
,
1438 // Ivar's can only appear in interfaces, implementations (via synthesized
1439 // properties), and class extensions (via direct declaration, or synthesized
1442 // FIXME: This should really be asserting this:
1443 // (isa<ObjCCategoryDecl>(DC) &&
1444 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1445 // but unfortunately we sometimes place ivars into non-class extension
1446 // categories on error. This breaks an AST invariant, and should not be
1448 assert((isa
<ObjCInterfaceDecl
>(DC
) || isa
<ObjCImplementationDecl
>(DC
) ||
1449 isa
<ObjCCategoryDecl
>(DC
)) &&
1450 "Invalid ivar decl context!");
1451 // Once a new ivar is created in any of class/class-extension/implementation
1452 // decl contexts, the previously built IvarList must be rebuilt.
1453 ObjCInterfaceDecl
*ID
= dyn_cast
<ObjCInterfaceDecl
>(DC
);
1455 if (ObjCImplementationDecl
*IM
= dyn_cast
<ObjCImplementationDecl
>(DC
))
1456 ID
= IM
->getClassInterface();
1458 ID
= cast
<ObjCCategoryDecl
>(DC
)->getClassInterface();
1460 ID
->setIvarList(nullptr);
1463 return new (C
, DC
) ObjCIvarDecl(DC
, StartLoc
, IdLoc
, Id
, T
, TInfo
, ac
, BW
,
1467 ObjCIvarDecl
*ObjCIvarDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
1468 return new (C
, ID
) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1469 nullptr, QualType(), nullptr,
1470 ObjCIvarDecl::None
, nullptr, false);
1473 const ObjCInterfaceDecl
*ObjCIvarDecl::getContainingInterface() const {
1474 const ObjCContainerDecl
*DC
= cast
<ObjCContainerDecl
>(getDeclContext());
1476 switch (DC
->getKind()) {
1478 case ObjCCategoryImpl
:
1480 llvm_unreachable("invalid ivar container!");
1482 // Ivars can only appear in class extension categories.
1483 case ObjCCategory
: {
1484 const ObjCCategoryDecl
*CD
= cast
<ObjCCategoryDecl
>(DC
);
1485 assert(CD
->IsClassExtension() && "invalid container for ivar!");
1486 return CD
->getClassInterface();
1489 case ObjCImplementation
:
1490 return cast
<ObjCImplementationDecl
>(DC
)->getClassInterface();
1493 return cast
<ObjCInterfaceDecl
>(DC
);
1497 //===----------------------------------------------------------------------===//
1498 // ObjCAtDefsFieldDecl
1499 //===----------------------------------------------------------------------===//
1501 void ObjCAtDefsFieldDecl::anchor() { }
1504 *ObjCAtDefsFieldDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1505 SourceLocation StartLoc
, SourceLocation IdLoc
,
1506 IdentifierInfo
*Id
, QualType T
, Expr
*BW
) {
1507 return new (C
, DC
) ObjCAtDefsFieldDecl(DC
, StartLoc
, IdLoc
, Id
, T
, BW
);
1510 ObjCAtDefsFieldDecl
*ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext
&C
,
1512 return new (C
, ID
) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1513 SourceLocation(), nullptr, QualType(),
1517 //===----------------------------------------------------------------------===//
1519 //===----------------------------------------------------------------------===//
1521 void ObjCProtocolDecl::anchor() { }
1523 ObjCProtocolDecl::ObjCProtocolDecl(ASTContext
&C
, DeclContext
*DC
,
1524 IdentifierInfo
*Id
, SourceLocation nameLoc
,
1525 SourceLocation atStartLoc
,
1526 ObjCProtocolDecl
*PrevDecl
)
1527 : ObjCContainerDecl(ObjCProtocol
, DC
, Id
, nameLoc
, atStartLoc
),
1528 redeclarable_base(C
), Data() {
1529 setPreviousDecl(PrevDecl
);
1531 Data
= PrevDecl
->Data
;
1534 ObjCProtocolDecl
*ObjCProtocolDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1536 SourceLocation nameLoc
,
1537 SourceLocation atStartLoc
,
1538 ObjCProtocolDecl
*PrevDecl
) {
1539 ObjCProtocolDecl
*Result
=
1540 new (C
, DC
) ObjCProtocolDecl(C
, DC
, Id
, nameLoc
, atStartLoc
, PrevDecl
);
1541 Result
->Data
.setInt(!C
.getLangOpts().Modules
);
1545 ObjCProtocolDecl
*ObjCProtocolDecl::CreateDeserialized(ASTContext
&C
,
1547 ObjCProtocolDecl
*Result
=
1548 new (C
, ID
) ObjCProtocolDecl(C
, nullptr, nullptr, SourceLocation(),
1549 SourceLocation(), nullptr);
1550 Result
->Data
.setInt(!C
.getLangOpts().Modules
);
1554 ObjCProtocolDecl
*ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo
*Name
) {
1555 ObjCProtocolDecl
*PDecl
= this;
1557 if (Name
== getIdentifier())
1560 for (auto *I
: protocols())
1561 if ((PDecl
= I
->lookupProtocolNamed(Name
)))
1567 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1569 ObjCMethodDecl
*ObjCProtocolDecl::lookupMethod(Selector Sel
,
1570 bool isInstance
) const {
1571 ObjCMethodDecl
*MethodDecl
= nullptr;
1573 // If there is no definition or the definition is hidden, we don't find
1575 const ObjCProtocolDecl
*Def
= getDefinition();
1576 if (!Def
|| Def
->isHidden())
1579 if ((MethodDecl
= getMethod(Sel
, isInstance
)))
1582 for (const auto *I
: protocols())
1583 if ((MethodDecl
= I
->lookupMethod(Sel
, isInstance
)))
1588 void ObjCProtocolDecl::allocateDefinitionData() {
1589 assert(!Data
.getPointer() && "Protocol already has a definition!");
1590 Data
.setPointer(new (getASTContext()) DefinitionData
);
1591 Data
.getPointer()->Definition
= this;
1594 void ObjCProtocolDecl::startDefinition() {
1595 allocateDefinitionData();
1597 // Update all of the declarations with a pointer to the definition.
1598 for (auto RD
: redecls())
1599 RD
->Data
= this->Data
;
1602 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap
&PM
,
1603 PropertyDeclOrder
&PO
) const {
1605 if (const ObjCProtocolDecl
*PDecl
= getDefinition()) {
1606 for (auto *Prop
: PDecl
->properties()) {
1607 // Insert into PM if not there already.
1608 PM
.insert(std::make_pair(Prop
->getIdentifier(), Prop
));
1611 // Scan through protocol's protocols.
1612 for (const auto *PI
: PDecl
->protocols())
1613 PI
->collectPropertiesToImplement(PM
, PO
);
1618 void ObjCProtocolDecl::collectInheritedProtocolProperties(
1619 const ObjCPropertyDecl
*Property
,
1620 ProtocolPropertyMap
&PM
) const {
1621 if (const ObjCProtocolDecl
*PDecl
= getDefinition()) {
1622 bool MatchFound
= false;
1623 for (auto *Prop
: PDecl
->properties()) {
1624 if (Prop
== Property
)
1626 if (Prop
->getIdentifier() == Property
->getIdentifier()) {
1632 // Scan through protocol's protocols which did not have a matching property.
1634 for (const auto *PI
: PDecl
->protocols())
1635 PI
->collectInheritedProtocolProperties(Property
, PM
);
1640 ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1641 if (ObjCRuntimeNameAttr
*ObjCRTName
= getAttr
<ObjCRuntimeNameAttr
>())
1642 return ObjCRTName
->getMetadataName();
1647 //===----------------------------------------------------------------------===//
1649 //===----------------------------------------------------------------------===//
1651 void ObjCCategoryDecl::anchor() { }
1653 ObjCCategoryDecl
*ObjCCategoryDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1654 SourceLocation AtLoc
,
1655 SourceLocation ClassNameLoc
,
1656 SourceLocation CategoryNameLoc
,
1658 ObjCInterfaceDecl
*IDecl
,
1659 SourceLocation IvarLBraceLoc
,
1660 SourceLocation IvarRBraceLoc
) {
1661 ObjCCategoryDecl
*CatDecl
=
1662 new (C
, DC
) ObjCCategoryDecl(DC
, AtLoc
, ClassNameLoc
, CategoryNameLoc
, Id
,
1663 IDecl
, IvarLBraceLoc
, IvarRBraceLoc
);
1665 // Link this category into its class's category list.
1666 CatDecl
->NextClassCategory
= IDecl
->getCategoryListRaw();
1667 if (IDecl
->hasDefinition()) {
1668 IDecl
->setCategoryListRaw(CatDecl
);
1669 if (ASTMutationListener
*L
= C
.getASTMutationListener())
1670 L
->AddedObjCCategoryToInterface(CatDecl
, IDecl
);
1677 ObjCCategoryDecl
*ObjCCategoryDecl::CreateDeserialized(ASTContext
&C
,
1679 return new (C
, ID
) ObjCCategoryDecl(nullptr, SourceLocation(),
1680 SourceLocation(), SourceLocation(),
1684 ObjCCategoryImplDecl
*ObjCCategoryDecl::getImplementation() const {
1685 return getASTContext().getObjCImplementation(
1686 const_cast<ObjCCategoryDecl
*>(this));
1689 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl
*ImplD
) {
1690 getASTContext().setObjCImplementation(this, ImplD
);
1694 //===----------------------------------------------------------------------===//
1695 // ObjCCategoryImplDecl
1696 //===----------------------------------------------------------------------===//
1698 void ObjCCategoryImplDecl::anchor() { }
1700 ObjCCategoryImplDecl
*
1701 ObjCCategoryImplDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1703 ObjCInterfaceDecl
*ClassInterface
,
1704 SourceLocation nameLoc
,
1705 SourceLocation atStartLoc
,
1706 SourceLocation CategoryNameLoc
) {
1707 if (ClassInterface
&& ClassInterface
->hasDefinition())
1708 ClassInterface
= ClassInterface
->getDefinition();
1709 return new (C
, DC
) ObjCCategoryImplDecl(DC
, Id
, ClassInterface
, nameLoc
,
1710 atStartLoc
, CategoryNameLoc
);
1713 ObjCCategoryImplDecl
*ObjCCategoryImplDecl::CreateDeserialized(ASTContext
&C
,
1715 return new (C
, ID
) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1716 SourceLocation(), SourceLocation(),
1720 ObjCCategoryDecl
*ObjCCategoryImplDecl::getCategoryDecl() const {
1721 // The class interface might be NULL if we are working with invalid code.
1722 if (const ObjCInterfaceDecl
*ID
= getClassInterface())
1723 return ID
->FindCategoryDeclaration(getIdentifier());
1728 void ObjCImplDecl::anchor() { }
1730 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl
*property
) {
1731 // FIXME: The context should be correct before we get here.
1732 property
->setLexicalDeclContext(this);
1736 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl
*IFace
) {
1737 ASTContext
&Ctx
= getASTContext();
1739 if (ObjCImplementationDecl
*ImplD
1740 = dyn_cast_or_null
<ObjCImplementationDecl
>(this)) {
1742 Ctx
.setObjCImplementation(IFace
, ImplD
);
1744 } else if (ObjCCategoryImplDecl
*ImplD
=
1745 dyn_cast_or_null
<ObjCCategoryImplDecl
>(this)) {
1746 if (ObjCCategoryDecl
*CD
= IFace
->FindCategoryDeclaration(getIdentifier()))
1747 Ctx
.setObjCImplementation(CD
, ImplD
);
1750 ClassInterface
= IFace
;
1753 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1754 /// properties implemented in this \@implementation block and returns
1755 /// the implemented property that uses it.
1757 ObjCPropertyImplDecl
*ObjCImplDecl::
1758 FindPropertyImplIvarDecl(IdentifierInfo
*ivarId
) const {
1759 for (auto *PID
: property_impls())
1760 if (PID
->getPropertyIvarDecl() &&
1761 PID
->getPropertyIvarDecl()->getIdentifier() == ivarId
)
1766 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1767 /// added to the list of those properties \@synthesized/\@dynamic in this
1768 /// category \@implementation block.
1770 ObjCPropertyImplDecl
*ObjCImplDecl::
1771 FindPropertyImplDecl(IdentifierInfo
*Id
) const {
1772 for (auto *PID
: property_impls())
1773 if (PID
->getPropertyDecl()->getIdentifier() == Id
)
1778 raw_ostream
&clang::operator<<(raw_ostream
&OS
,
1779 const ObjCCategoryImplDecl
&CID
) {
1780 OS
<< CID
.getName();
1784 //===----------------------------------------------------------------------===//
1785 // ObjCImplementationDecl
1786 //===----------------------------------------------------------------------===//
1788 void ObjCImplementationDecl::anchor() { }
1790 ObjCImplementationDecl
*
1791 ObjCImplementationDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1792 ObjCInterfaceDecl
*ClassInterface
,
1793 ObjCInterfaceDecl
*SuperDecl
,
1794 SourceLocation nameLoc
,
1795 SourceLocation atStartLoc
,
1796 SourceLocation superLoc
,
1797 SourceLocation IvarLBraceLoc
,
1798 SourceLocation IvarRBraceLoc
) {
1799 if (ClassInterface
&& ClassInterface
->hasDefinition())
1800 ClassInterface
= ClassInterface
->getDefinition();
1801 return new (C
, DC
) ObjCImplementationDecl(DC
, ClassInterface
, SuperDecl
,
1802 nameLoc
, atStartLoc
, superLoc
,
1803 IvarLBraceLoc
, IvarRBraceLoc
);
1806 ObjCImplementationDecl
*
1807 ObjCImplementationDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
1808 return new (C
, ID
) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1809 SourceLocation(), SourceLocation());
1812 void ObjCImplementationDecl::setIvarInitializers(ASTContext
&C
,
1813 CXXCtorInitializer
** initializers
,
1814 unsigned numInitializers
) {
1815 if (numInitializers
> 0) {
1816 NumIvarInitializers
= numInitializers
;
1817 CXXCtorInitializer
**ivarInitializers
=
1818 new (C
) CXXCtorInitializer
*[NumIvarInitializers
];
1819 memcpy(ivarInitializers
, initializers
,
1820 numInitializers
* sizeof(CXXCtorInitializer
*));
1821 IvarInitializers
= ivarInitializers
;
1825 raw_ostream
&clang::operator<<(raw_ostream
&OS
,
1826 const ObjCImplementationDecl
&ID
) {
1831 //===----------------------------------------------------------------------===//
1832 // ObjCCompatibleAliasDecl
1833 //===----------------------------------------------------------------------===//
1835 void ObjCCompatibleAliasDecl::anchor() { }
1837 ObjCCompatibleAliasDecl
*
1838 ObjCCompatibleAliasDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1841 ObjCInterfaceDecl
* AliasedClass
) {
1842 return new (C
, DC
) ObjCCompatibleAliasDecl(DC
, L
, Id
, AliasedClass
);
1845 ObjCCompatibleAliasDecl
*
1846 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
1847 return new (C
, ID
) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1851 //===----------------------------------------------------------------------===//
1853 //===----------------------------------------------------------------------===//
1855 void ObjCPropertyDecl::anchor() { }
1857 ObjCPropertyDecl
*ObjCPropertyDecl::Create(ASTContext
&C
, DeclContext
*DC
,
1860 SourceLocation AtLoc
,
1861 SourceLocation LParenLoc
,
1863 PropertyControl propControl
) {
1864 return new (C
, DC
) ObjCPropertyDecl(DC
, L
, Id
, AtLoc
, LParenLoc
, T
);
1867 ObjCPropertyDecl
*ObjCPropertyDecl::CreateDeserialized(ASTContext
&C
,
1869 return new (C
, ID
) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1870 SourceLocation(), SourceLocation(),
1874 //===----------------------------------------------------------------------===//
1875 // ObjCPropertyImplDecl
1876 //===----------------------------------------------------------------------===//
1878 ObjCPropertyImplDecl
*ObjCPropertyImplDecl::Create(ASTContext
&C
,
1880 SourceLocation atLoc
,
1882 ObjCPropertyDecl
*property
,
1885 SourceLocation ivarLoc
) {
1886 return new (C
, DC
) ObjCPropertyImplDecl(DC
, atLoc
, L
, property
, PK
, ivar
,
1890 ObjCPropertyImplDecl
*ObjCPropertyImplDecl::CreateDeserialized(ASTContext
&C
,
1892 return new (C
, ID
) ObjCPropertyImplDecl(nullptr, SourceLocation(),
1893 SourceLocation(), nullptr, Dynamic
,
1894 nullptr, SourceLocation());
1897 SourceRange
ObjCPropertyImplDecl::getSourceRange() const {
1898 SourceLocation EndLoc
= getLocation();
1899 if (IvarLoc
.isValid())
1902 return SourceRange(AtLoc
, EndLoc
);