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/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/Stmt.h"
21 #include "clang/AST/Type.h"
22 #include "clang/AST/TypeLoc.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/LangOptions.h"
26 #include "clang/Basic/SourceLocation.h"
27 #include "llvm/ADT/None.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
39 using namespace clang;
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
47 if (Elts == 0) return; // Setting to an empty list is a noop.
49 List = new (Ctx) void*[Elts];
51 memcpy(List, InList, sizeof(void*)*Elts);
54 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
55 const SourceLocation *Locs, ASTContext &Ctx) {
59 Locations = new (Ctx) SourceLocation[Elts];
60 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
61 set(InList, Elts, Ctx);
64 //===----------------------------------------------------------------------===//
66 //===----------------------------------------------------------------------===//
68 ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
69 IdentifierInfo *Id, SourceLocation nameLoc,
70 SourceLocation atStartLoc)
71 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
72 setAtStartLoc(atStartLoc);
75 void ObjCContainerDecl::anchor() {}
77 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
80 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
81 lookup_result R = lookup(Id);
82 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
83 Ivar != IvarEnd; ++Ivar) {
84 if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
90 // Get the local instance/class method declared in this interface.
92 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
93 bool AllowHidden) const {
94 // If this context is a hidden protocol definition, don't find any
96 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
97 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
98 if (Def->isHidden() && !AllowHidden)
102 // Since instance & class methods can have the same name, the loop below
103 // ensures we get the correct method.
105 // @interface Whatever
106 // - (int) class_method;
107 // + (float) class_method;
109 lookup_result R = lookup(Sel);
110 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
111 Meth != MethEnd; ++Meth) {
112 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
113 if (MD && MD->isInstanceMethod() == isInstance)
119 /// This routine returns 'true' if a user declared setter method was
120 /// found in the class, its protocols, its super classes or categories.
121 /// It also returns 'true' if one of its categories has declared a 'readwrite'
122 /// property. This is because, user must provide a setter method for the
123 /// category's 'readwrite' property.
124 bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
125 const ObjCPropertyDecl *Property) const {
126 Selector Sel = Property->getSetterName();
127 lookup_result R = lookup(Sel);
128 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
129 Meth != MethEnd; ++Meth) {
130 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
131 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
135 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
136 // Also look into categories, including class extensions, looking
137 // for a user declared instance method.
138 for (const auto *Cat : ID->visible_categories()) {
139 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
140 if (!MD->isImplicit())
142 if (Cat->IsClassExtension())
144 // Also search through the categories looking for a 'readwrite'
145 // declaration of this property. If one found, presumably a setter will
146 // be provided (properties declared in categories will not get
147 // auto-synthesized).
148 for (const auto *P : Cat->properties())
149 if (P->getIdentifier() == Property->getIdentifier()) {
150 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
156 // Also look into protocols, for a user declared instance method.
157 for (const auto *Proto : ID->all_referenced_protocols())
158 if (Proto->HasUserDeclaredSetterMethod(Property))
161 // And in its super class.
162 ObjCInterfaceDecl *OSC = ID->getSuperClass();
164 if (OSC->HasUserDeclaredSetterMethod(Property))
166 OSC = OSC->getSuperClass();
169 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
170 for (const auto *PI : PD->protocols())
171 if (PI->HasUserDeclaredSetterMethod(Property))
177 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
178 const IdentifierInfo *propertyID,
179 ObjCPropertyQueryKind queryKind) {
180 // If this context is a hidden protocol definition, don't find any
182 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
183 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
188 // If context is class, then lookup property in its visible extensions.
189 // This comes before property is looked up in primary class.
190 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
191 for (const auto *Ext : IDecl->visible_extensions())
192 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
198 DeclContext::lookup_result R = DC->lookup(propertyID);
199 ObjCPropertyDecl *classProp = nullptr;
200 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
202 if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
203 // If queryKind is unknown, we return the instance property if one
204 // exists; otherwise we return the class property.
205 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
206 !PD->isClassProperty()) ||
207 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
208 PD->isClassProperty()) ||
209 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
210 !PD->isClassProperty()))
213 if (PD->isClassProperty())
217 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
218 // We can't find the instance property, return the class property.
225 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
226 SmallString<128> ivarName;
228 llvm::raw_svector_ostream os(ivarName);
229 os << '_' << getIdentifier()->getName();
231 return &Ctx.Idents.get(ivarName.str());
234 /// FindPropertyDeclaration - Finds declaration of the property given its name
235 /// in 'PropertyId' and returns it. It returns 0, if not found.
236 ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
237 const IdentifierInfo *PropertyId,
238 ObjCPropertyQueryKind QueryKind) const {
239 // Don't find properties within hidden protocol definitions.
240 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
241 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
246 // Search the extensions of a class first; they override what's in
248 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
249 for (const auto *Ext : ClassDecl->visible_extensions()) {
250 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
255 if (ObjCPropertyDecl *PD =
256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
263 case Decl::ObjCProtocol: {
264 const auto *PID = cast<ObjCProtocolDecl>(this);
265 for (const auto *I : PID->protocols())
266 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
271 case Decl::ObjCInterface: {
272 const auto *OID = cast<ObjCInterfaceDecl>(this);
273 // Look through categories (but not extensions; they were handled above).
274 for (const auto *Cat : OID->visible_categories()) {
275 if (!Cat->IsClassExtension())
276 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
277 PropertyId, QueryKind))
281 // Look through protocols.
282 for (const auto *I : OID->all_referenced_protocols())
283 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
287 // Finally, check the super class.
288 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
289 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
292 case Decl::ObjCCategory: {
293 const auto *OCD = cast<ObjCCategoryDecl>(this);
294 // Look through protocols.
295 if (!OCD->IsClassExtension())
296 for (const auto *I : OCD->protocols())
297 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
306 void ObjCInterfaceDecl::anchor() {}
308 ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
309 // If this particular declaration has a type parameter list, return it.
310 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
313 // If there is a definition, return its type parameter list.
314 if (const ObjCInterfaceDecl *def = getDefinition())
315 return def->getTypeParamListAsWritten();
317 // Otherwise, look at previous declarations to determine whether any
318 // of them has a type parameter list, skipping over those
319 // declarations that do not.
320 for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
321 decl = decl->getPreviousDecl()) {
322 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
329 void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
333 // Set the declaration context of each of the type parameters.
334 for (auto *typeParam : *TypeParamList)
335 typeParam->setDeclContext(this);
338 ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
339 // FIXME: Should make sure no callers ever do this.
340 if (!hasDefinition())
343 if (data().ExternallyCompleted)
344 LoadExternalDefinition();
346 if (const ObjCObjectType *superType = getSuperClassType()) {
347 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
348 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
358 SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
359 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
360 return superTInfo->getTypeLoc().getBeginLoc();
362 return SourceLocation();
365 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
366 /// with name 'PropertyId' in the primary class; including those in protocols
367 /// (direct or indirect) used by the primary class.
369 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
370 IdentifierInfo *PropertyId,
371 ObjCPropertyQueryKind QueryKind) const {
372 // FIXME: Should make sure no callers ever do this.
373 if (!hasDefinition())
376 if (data().ExternallyCompleted)
377 LoadExternalDefinition();
379 if (ObjCPropertyDecl *PD =
380 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
384 // Look through protocols.
385 for (const auto *I : all_referenced_protocols())
386 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
393 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
394 PropertyDeclOrder &PO) const {
395 for (auto *Prop : properties()) {
396 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
399 for (const auto *Ext : known_extensions()) {
400 const ObjCCategoryDecl *ClassExt = Ext;
401 for (auto *Prop : ClassExt->properties()) {
402 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
406 for (const auto *PI : all_referenced_protocols())
407 PI->collectPropertiesToImplement(PM, PO);
408 // Note, the properties declared only in class extensions are still copied
409 // into the main @interface's property list, and therefore we don't
410 // explicitly, have to search class extension properties.
413 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
414 const ObjCInterfaceDecl *Class = this;
416 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
418 Class = Class->getSuperClass();
423 const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
424 const ObjCInterfaceDecl *Class = this;
426 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
428 Class = Class->getSuperClass();
433 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
434 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
436 if (data().ExternallyCompleted)
437 LoadExternalDefinition();
439 if (data().AllReferencedProtocols.empty() &&
440 data().ReferencedProtocols.empty()) {
441 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
445 // Check for duplicate protocol in class's protocol list.
446 // This is O(n*m). But it is extremely rare and number of protocols in
447 // class or its extension are very few.
448 SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
449 for (unsigned i = 0; i < ExtNum; i++) {
450 bool protocolExists = false;
451 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
452 for (auto *Proto : all_referenced_protocols()) {
453 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
454 protocolExists = true;
458 // Do we want to warn on a protocol in extension class which
459 // already exist in the class? Probably not.
461 ProtocolRefs.push_back(ProtoInExtension);
464 if (ProtocolRefs.empty())
467 // Merge ProtocolRefs into class's protocol list;
468 ProtocolRefs.append(all_referenced_protocol_begin(),
469 all_referenced_protocol_end());
471 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
474 const ObjCInterfaceDecl *
475 ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
476 const ObjCInterfaceDecl *IFace = this;
478 if (IFace->hasDesignatedInitializers())
480 if (!IFace->inheritsDesignatedInitializers())
482 IFace = IFace->getSuperClass();
487 static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
488 for (const auto *MD : D->instance_methods()) {
489 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
492 for (const auto *Ext : D->visible_extensions()) {
493 for (const auto *MD : Ext->instance_methods()) {
494 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
498 if (const auto *ImplD = D->getImplementation()) {
499 for (const auto *MD : ImplD->instance_methods()) {
500 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
507 bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
508 switch (data().InheritedDesignatedInitializers) {
509 case DefinitionData::IDI_Inherited:
511 case DefinitionData::IDI_NotInherited:
513 case DefinitionData::IDI_Unknown:
514 // If the class introduced initializers we conservatively assume that we
515 // don't know if any of them is a designated initializer to avoid possible
516 // misleading warnings.
517 if (isIntroducingInitializers(this)) {
518 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
520 if (auto SuperD = getSuperClass()) {
521 data().InheritedDesignatedInitializers =
522 SuperD->declaresOrInheritsDesignatedInitializers() ?
523 DefinitionData::IDI_Inherited :
524 DefinitionData::IDI_NotInherited;
526 data().InheritedDesignatedInitializers =
527 DefinitionData::IDI_NotInherited;
530 assert(data().InheritedDesignatedInitializers
531 != DefinitionData::IDI_Unknown);
532 return data().InheritedDesignatedInitializers ==
533 DefinitionData::IDI_Inherited;
536 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
539 void ObjCInterfaceDecl::getDesignatedInitializers(
540 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
541 // Check for a complete definition and recover if not so.
542 if (!isThisDeclarationADefinition())
544 if (data().ExternallyCompleted)
545 LoadExternalDefinition();
547 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
551 for (const auto *MD : IFace->instance_methods())
552 if (MD->isThisDeclarationADesignatedInitializer())
553 Methods.push_back(MD);
554 for (const auto *Ext : IFace->visible_extensions()) {
555 for (const auto *MD : Ext->instance_methods())
556 if (MD->isThisDeclarationADesignatedInitializer())
557 Methods.push_back(MD);
561 bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
562 const ObjCMethodDecl **InitMethod) const {
563 bool HasCompleteDef = isThisDeclarationADefinition();
564 // During deserialization the data record for the ObjCInterfaceDecl could
565 // be made invariant by reusing the canonical decl. Take this into account
566 // when checking for the complete definition.
567 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
568 getCanonicalDecl()->getDefinition() == getDefinition())
569 HasCompleteDef = true;
571 // Check for a complete definition and recover if not so.
575 if (data().ExternallyCompleted)
576 LoadExternalDefinition();
578 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
582 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
583 if (MD->isThisDeclarationADesignatedInitializer()) {
589 for (const auto *Ext : IFace->visible_extensions()) {
590 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
591 if (MD->isThisDeclarationADesignatedInitializer()) {
601 void ObjCInterfaceDecl::allocateDefinitionData() {
602 assert(!hasDefinition() && "ObjC class already has a definition");
603 Data.setPointer(new (getASTContext()) DefinitionData());
604 Data.getPointer()->Definition = this;
606 // Make the type point at the definition, now that we have one.
608 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
611 void ObjCInterfaceDecl::startDefinition() {
612 allocateDefinitionData();
614 // Update all of the declarations with a pointer to the definition.
615 for (auto *RD : redecls()) {
621 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
622 ObjCInterfaceDecl *&clsDeclared) {
623 // FIXME: Should make sure no callers ever do this.
624 if (!hasDefinition())
627 if (data().ExternallyCompleted)
628 LoadExternalDefinition();
630 ObjCInterfaceDecl* ClassDecl = this;
631 while (ClassDecl != nullptr) {
632 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
633 clsDeclared = ClassDecl;
637 for (const auto *Ext : ClassDecl->visible_extensions()) {
638 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
639 clsDeclared = ClassDecl;
644 ClassDecl = ClassDecl->getSuperClass();
649 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
650 /// class whose name is passed as argument. If it is not one of the super classes
651 /// the it returns NULL.
652 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
653 const IdentifierInfo*ICName) {
654 // FIXME: Should make sure no callers ever do this.
655 if (!hasDefinition())
658 if (data().ExternallyCompleted)
659 LoadExternalDefinition();
661 ObjCInterfaceDecl* ClassDecl = this;
662 while (ClassDecl != nullptr) {
663 if (ClassDecl->getIdentifier() == ICName)
665 ClassDecl = ClassDecl->getSuperClass();
671 ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
672 for (auto *P : all_referenced_protocols())
673 if (P->lookupProtocolNamed(Name))
675 ObjCInterfaceDecl *SuperClass = getSuperClass();
676 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
679 /// lookupMethod - This method returns an instance/class method by looking in
680 /// the class, its categories, and its super classes (using a linear search).
681 /// When argument category "C" is specified, any implicit method found
682 /// in this category is ignored.
683 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
685 bool shallowCategoryLookup,
687 const ObjCCategoryDecl *C) const
689 // FIXME: Should make sure no callers ever do this.
690 if (!hasDefinition())
693 const ObjCInterfaceDecl* ClassDecl = this;
694 ObjCMethodDecl *MethodDecl = nullptr;
696 if (data().ExternallyCompleted)
697 LoadExternalDefinition();
700 // 1. Look through primary class.
701 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
704 // 2. Didn't find one yet - now look through categories.
705 for (const auto *Cat : ClassDecl->visible_categories())
706 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
707 if (C != Cat || !MethodDecl->isImplicit())
710 // 3. Didn't find one yet - look through primary class's protocols.
711 for (const auto *I : ClassDecl->protocols())
712 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
715 // 4. Didn't find one yet - now look through categories' protocols
716 if (!shallowCategoryLookup)
717 for (const auto *Cat : ClassDecl->visible_categories()) {
718 // Didn't find one yet - look through protocols.
719 const ObjCList<ObjCProtocolDecl> &Protocols =
720 Cat->getReferencedProtocols();
721 for (auto *Protocol : Protocols)
722 if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
723 if (C != Cat || !MethodDecl->isImplicit())
731 // 5. Get to the super class (if any).
732 ClassDecl = ClassDecl->getSuperClass();
737 // Will search "local" class/category implementations for a method decl.
738 // If failed, then we search in class's root for an instance method.
739 // Returns 0 if no method is found.
740 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
742 bool Instance) const {
743 // FIXME: Should make sure no callers ever do this.
744 if (!hasDefinition())
747 if (data().ExternallyCompleted)
748 LoadExternalDefinition();
750 ObjCMethodDecl *Method = nullptr;
751 if (ObjCImplementationDecl *ImpDecl = getImplementation())
752 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
753 : ImpDecl->getClassMethod(Sel);
755 // Look through local category implementations associated with the class.
757 Method = getCategoryMethod(Sel, Instance);
759 // Before we give up, check if the selector is an instance method.
760 // But only in the root. This matches gcc's behavior and what the
762 if (!Instance && !Method && !getSuperClass()) {
763 Method = lookupInstanceMethod(Sel);
764 // Look through local category implementations associated
765 // with the root class.
767 Method = lookupPrivateMethod(Sel, true);
770 if (!Method && getSuperClass())
771 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
775 //===----------------------------------------------------------------------===//
777 //===----------------------------------------------------------------------===//
779 ObjCMethodDecl::ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
780 Selector SelInfo, QualType T,
781 TypeSourceInfo *ReturnTInfo,
782 DeclContext *contextDecl, bool isInstance,
783 bool isVariadic, bool isPropertyAccessor,
784 bool isImplicitlyDeclared, bool isDefined,
785 ImplementationControl impControl,
786 bool HasRelatedResultType)
787 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
788 DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
791 // Initialized the bits stored in DeclContext.
792 ObjCMethodDeclBits.Family =
793 static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
794 setInstanceMethod(isInstance);
795 setVariadic(isVariadic);
796 setPropertyAccessor(isPropertyAccessor);
797 setDefined(isDefined);
798 setIsRedeclaration(false);
799 setHasRedeclaration(false);
800 setDeclImplementation(impControl);
801 setObjCDeclQualifier(OBJC_TQ_None);
802 setRelatedResultType(HasRelatedResultType);
803 setSelLocsKind(SelLoc_StandardNoSpace);
804 setOverriding(false);
805 setHasSkippedBody(false);
807 setImplicit(isImplicitlyDeclared);
810 ObjCMethodDecl *ObjCMethodDecl::Create(
811 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
812 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
813 DeclContext *contextDecl, bool isInstance, bool isVariadic,
814 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
815 ImplementationControl impControl, bool HasRelatedResultType) {
816 return new (C, contextDecl) ObjCMethodDecl(
817 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
818 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
819 impControl, HasRelatedResultType);
822 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
823 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
824 Selector(), QualType(), nullptr, nullptr);
827 bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
828 return getMethodFamily() == OMF_init &&
829 hasAttr<ObjCDesignatedInitializerAttr>();
832 bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
833 if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
834 return PD->getIdentifier() == Ctx.getNSObjectName();
835 if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
836 return ID->getIdentifier() == Ctx.getNSObjectName();
840 bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
841 const ObjCMethodDecl **InitMethod) const {
842 if (getMethodFamily() != OMF_init)
844 const DeclContext *DC = getDeclContext();
845 if (isa<ObjCProtocolDecl>(DC))
847 if (const ObjCInterfaceDecl *ID = getClassInterface())
848 return ID->isDesignatedInitializer(getSelector(), InitMethod);
852 Stmt *ObjCMethodDecl::getBody() const {
853 return Body.get(getASTContext().getExternalSource());
856 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
858 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
859 setIsRedeclaration(true);
860 PrevMethod->setHasRedeclaration(true);
863 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
864 ArrayRef<ParmVarDecl*> Params,
865 ArrayRef<SourceLocation> SelLocs) {
866 ParamsAndSelLocs = nullptr;
867 NumParams = Params.size();
868 if (Params.empty() && SelLocs.empty())
871 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
872 "Alignment not sufficient for SourceLocation");
874 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
875 sizeof(SourceLocation) * SelLocs.size();
876 ParamsAndSelLocs = C.Allocate(Size);
877 std::copy(Params.begin(), Params.end(), getParams());
878 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
881 void ObjCMethodDecl::getSelectorLocs(
882 SmallVectorImpl<SourceLocation> &SelLocs) const {
883 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
884 SelLocs.push_back(getSelectorLoc(i));
887 void ObjCMethodDecl::setMethodParams(ASTContext &C,
888 ArrayRef<ParmVarDecl*> Params,
889 ArrayRef<SourceLocation> SelLocs) {
890 assert((!SelLocs.empty() || isImplicit()) &&
891 "No selector locs for non-implicit method");
893 return setParamsAndSelLocs(C, Params, llvm::None);
895 setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
897 if (getSelLocsKind() != SelLoc_NonStandard)
898 return setParamsAndSelLocs(C, Params, llvm::None);
900 setParamsAndSelLocs(C, Params, SelLocs);
903 /// A definition will return its interface declaration.
904 /// An interface declaration will return its definition.
905 /// Otherwise it will return itself.
906 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
907 ASTContext &Ctx = getASTContext();
908 ObjCMethodDecl *Redecl = nullptr;
909 if (hasRedeclaration())
910 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
914 auto *CtxD = cast<Decl>(getDeclContext());
916 if (!CtxD->isInvalidDecl()) {
917 if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
918 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
919 if (!ImplD->isInvalidDecl())
920 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
922 } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
923 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
924 if (!ImplD->isInvalidDecl())
925 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
927 } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
928 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
929 if (!IFD->isInvalidDecl())
930 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
932 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
933 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
934 if (!CatD->isInvalidDecl())
935 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
939 // Ensure that the discovered method redeclaration has a valid declaration
940 // context. Used to prevent infinite loops when iterating redeclarations in
941 // a partially invalid AST.
942 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
945 if (!Redecl && isRedeclaration()) {
946 // This is the last redeclaration, go back to the first method.
947 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
951 return Redecl ? Redecl : this;
954 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
955 auto *CtxD = cast<Decl>(getDeclContext());
957 if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
958 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
959 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
962 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
963 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
964 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
969 if (isRedeclaration()) {
970 // It is possible that we have not done deserializing the ObjCMethod yet.
972 cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
974 return MD ? MD : this;
980 SourceLocation ObjCMethodDecl::getEndLoc() const {
981 if (Stmt *Body = getBody())
982 return Body->getEndLoc();
986 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
987 auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
988 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
991 // Check for an explicit attribute.
992 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
993 // The unfortunate necessity of mapping between enums here is due
994 // to the attributes framework.
995 switch (attr->getFamily()) {
996 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
997 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
998 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
999 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
1000 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
1001 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
1003 ObjCMethodDeclBits.Family = family;
1007 family = getSelector().getMethodFamily();
1009 case OMF_None: break;
1011 // init only has a conventional meaning for an instance method, and
1012 // it has to return an object.
1014 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
1018 // alloc/copy/new have a conventional meaning for both class and
1019 // instance methods, but they require an object return.
1022 case OMF_mutableCopy:
1024 if (!getReturnType()->isObjCObjectPointerType())
1028 // These selectors have a conventional meaning only for instance methods.
1033 case OMF_autorelease:
1034 case OMF_retainCount:
1036 if (!isInstanceMethod())
1040 case OMF_initialize:
1041 if (isInstanceMethod() || !getReturnType()->isVoidType())
1045 case OMF_performSelector:
1046 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
1049 unsigned noParams = param_size();
1050 if (noParams < 1 || noParams > 3)
1053 ObjCMethodDecl::param_type_iterator it = param_type_begin();
1054 QualType ArgT = (*it);
1055 if (!ArgT->isObjCSelType()) {
1059 while (--noParams) {
1062 if (!ArgT->isObjCIdType()) {
1073 // Cache the result.
1074 ObjCMethodDeclBits.Family = family;
1078 QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1079 const ObjCInterfaceDecl *OID,
1080 bool &selfIsPseudoStrong,
1081 bool &selfIsConsumed) {
1083 selfIsPseudoStrong = false;
1084 selfIsConsumed = false;
1085 if (isInstanceMethod()) {
1086 // There may be no interface context due to error in declaration
1087 // of the interface (which has been reported). Recover gracefully.
1089 selfTy = Context.getObjCInterfaceType(OID);
1090 selfTy = Context.getObjCObjectPointerType(selfTy);
1092 selfTy = Context.getObjCIdType();
1094 } else // we have a factory method.
1095 selfTy = Context.getObjCClassType();
1097 if (Context.getLangOpts().ObjCAutoRefCount) {
1098 if (isInstanceMethod()) {
1099 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
1101 // 'self' is always __strong. It's actually pseudo-strong except
1102 // in init methods (or methods labeled ns_consumes_self), though.
1104 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1105 selfTy = Context.getQualifiedType(selfTy, qs);
1107 // In addition, 'self' is const unless this is an init method.
1108 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1109 selfTy = selfTy.withConst();
1110 selfIsPseudoStrong = true;
1114 assert(isClassMethod());
1115 // 'self' is always const in class methods.
1116 selfTy = selfTy.withConst();
1117 selfIsPseudoStrong = true;
1123 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1124 const ObjCInterfaceDecl *OID) {
1125 bool selfIsPseudoStrong, selfIsConsumed;
1127 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1128 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1129 &Context.Idents.get("self"), selfTy,
1130 ImplicitParamDecl::ObjCSelf);
1134 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1136 if (selfIsPseudoStrong)
1137 Self->setARCPseudoStrong(true);
1139 setCmdDecl(ImplicitParamDecl::Create(
1140 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1141 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
1144 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1145 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1147 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1148 return CD->getClassInterface();
1149 if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1150 return IMD->getClassInterface();
1151 if (isa<ObjCProtocolDecl>(getDeclContext()))
1153 llvm_unreachable("unknown method context");
1156 SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1157 const auto *TSI = getReturnTypeSourceInfo();
1159 return TSI->getTypeLoc().getSourceRange();
1160 return SourceRange();
1163 QualType ObjCMethodDecl::getSendResultType() const {
1164 ASTContext &Ctx = getASTContext();
1165 return getReturnType().getNonLValueExprType(Ctx)
1166 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1169 QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1170 // FIXME: Handle related result types here.
1172 return getReturnType().getNonLValueExprType(getASTContext())
1173 .substObjCMemberType(receiverType, getDeclContext(),
1174 ObjCSubstitutionContext::Result);
1177 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1178 const ObjCMethodDecl *Method,
1179 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1180 bool MovedToSuper) {
1184 // In categories look for overridden methods from protocols. A method from
1185 // category is not "overridden" since it is considered as the "same" method
1186 // (same USR) as the one from the interface.
1187 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1188 // Check whether we have a matching method at this category but only if we
1189 // are at the super class level.
1191 if (ObjCMethodDecl *
1192 Overridden = Container->getMethod(Method->getSelector(),
1193 Method->isInstanceMethod(),
1194 /*AllowHidden=*/true))
1195 if (Method != Overridden) {
1196 // We found an override at this category; there is no need to look
1197 // into its protocols.
1198 Methods.push_back(Overridden);
1202 for (const auto *P : Category->protocols())
1203 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1207 // Check whether we have a matching method at this level.
1208 if (const ObjCMethodDecl *
1209 Overridden = Container->getMethod(Method->getSelector(),
1210 Method->isInstanceMethod(),
1211 /*AllowHidden=*/true))
1212 if (Method != Overridden) {
1213 // We found an override at this level; there is no need to look
1214 // into other protocols or categories.
1215 Methods.push_back(Overridden);
1219 if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1220 for (const auto *P : Protocol->protocols())
1221 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1224 if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1225 for (const auto *P : Interface->protocols())
1226 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1228 for (const auto *Cat : Interface->known_categories())
1229 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
1231 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1232 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1233 /*MovedToSuper=*/true);
1237 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1238 const ObjCMethodDecl *Method,
1239 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1240 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1241 /*MovedToSuper=*/false);
1244 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1245 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1246 assert(Method->isOverriding());
1248 if (const auto *ProtD =
1249 dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1250 CollectOverriddenMethods(ProtD, Method, overridden);
1252 } else if (const auto *IMD =
1253 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1254 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1257 // Start searching for overridden methods using the method from the
1258 // interface as starting point.
1259 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1260 Method->isInstanceMethod(),
1261 /*AllowHidden=*/true))
1263 CollectOverriddenMethods(ID, Method, overridden);
1265 } else if (const auto *CatD =
1266 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1267 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1270 // Start searching for overridden methods using the method from the
1271 // interface as starting point.
1272 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1273 Method->isInstanceMethod(),
1274 /*AllowHidden=*/true))
1276 CollectOverriddenMethods(ID, Method, overridden);
1279 CollectOverriddenMethods(
1280 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1281 Method, overridden);
1285 void ObjCMethodDecl::getOverriddenMethods(
1286 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1287 const ObjCMethodDecl *Method = this;
1289 if (Method->isRedeclaration()) {
1290 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1291 getMethod(Method->getSelector(), Method->isInstanceMethod());
1294 if (Method->isOverriding()) {
1295 collectOverriddenMethodsSlow(Method, Overridden);
1296 assert(!Overridden.empty() &&
1297 "ObjCMethodDecl's overriding bit is not as expected");
1301 const ObjCPropertyDecl *
1302 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1303 Selector Sel = getSelector();
1304 unsigned NumArgs = Sel.getNumArgs();
1308 if (isPropertyAccessor()) {
1309 const auto *Container = cast<ObjCContainerDecl>(getParent());
1310 bool IsGetter = (NumArgs == 0);
1311 bool IsInstance = isInstanceMethod();
1313 /// Local function that attempts to find a matching property within the
1314 /// given Objective-C container.
1315 auto findMatchingProperty =
1316 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
1318 for (const auto *I : Container->instance_properties()) {
1319 Selector NextSel = IsGetter ? I->getGetterName()
1320 : I->getSetterName();
1325 for (const auto *I : Container->class_properties()) {
1326 Selector NextSel = IsGetter ? I->getGetterName()
1327 : I->getSetterName();
1336 // Look in the container we were given.
1337 if (const auto *Found = findMatchingProperty(Container))
1340 // If we're in a category or extension, look in the main class.
1341 const ObjCInterfaceDecl *ClassDecl = nullptr;
1342 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1343 ClassDecl = Category->getClassInterface();
1344 if (const auto *Found = findMatchingProperty(ClassDecl))
1347 // Determine whether the container is a class.
1348 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1351 // If we have a class, check its visible extensions.
1353 for (const auto *Ext : ClassDecl->visible_extensions()) {
1354 if (Ext == Container)
1357 if (const auto *Found = findMatchingProperty(Ext))
1362 llvm_unreachable("Marked as a property accessor but no property found!");
1365 if (!CheckOverrides)
1368 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1370 OverridesTy Overrides;
1371 getOverriddenMethods(Overrides);
1372 for (const auto *Override : Overrides)
1373 if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
1379 //===----------------------------------------------------------------------===//
1380 // ObjCTypeParamDecl
1381 //===----------------------------------------------------------------------===//
1383 void ObjCTypeParamDecl::anchor() {}
1385 ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1386 ObjCTypeParamVariance variance,
1387 SourceLocation varianceLoc,
1389 SourceLocation nameLoc,
1390 IdentifierInfo *name,
1391 SourceLocation colonLoc,
1392 TypeSourceInfo *boundInfo) {
1394 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1395 nameLoc, name, colonLoc, boundInfo);
1396 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1397 TPDecl->setTypeForDecl(TPType.getTypePtr());
1401 ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1403 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1404 ObjCTypeParamVariance::Invariant,
1405 SourceLocation(), 0, SourceLocation(),
1406 nullptr, SourceLocation(), nullptr);
1409 SourceRange ObjCTypeParamDecl::getSourceRange() const {
1410 SourceLocation startLoc = VarianceLoc;
1411 if (startLoc.isInvalid())
1412 startLoc = getLocation();
1414 if (hasExplicitBound()) {
1415 return SourceRange(startLoc,
1416 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1419 return SourceRange(startLoc);
1422 //===----------------------------------------------------------------------===//
1423 // ObjCTypeParamList
1424 //===----------------------------------------------------------------------===//
1425 ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1426 ArrayRef<ObjCTypeParamDecl *> typeParams,
1427 SourceLocation rAngleLoc)
1428 : NumParams(typeParams.size()) {
1429 Brackets.Begin = lAngleLoc.getRawEncoding();
1430 Brackets.End = rAngleLoc.getRawEncoding();
1431 std::copy(typeParams.begin(), typeParams.end(), begin());
1434 ObjCTypeParamList *ObjCTypeParamList::create(
1436 SourceLocation lAngleLoc,
1437 ArrayRef<ObjCTypeParamDecl *> typeParams,
1438 SourceLocation rAngleLoc) {
1440 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1441 alignof(ObjCTypeParamList));
1442 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1445 void ObjCTypeParamList::gatherDefaultTypeArgs(
1446 SmallVectorImpl<QualType> &typeArgs) const {
1447 typeArgs.reserve(size());
1448 for (auto typeParam : *this)
1449 typeArgs.push_back(typeParam->getUnderlyingType());
1452 //===----------------------------------------------------------------------===//
1453 // ObjCInterfaceDecl
1454 //===----------------------------------------------------------------------===//
1456 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1458 SourceLocation atLoc,
1460 ObjCTypeParamList *typeParamList,
1461 ObjCInterfaceDecl *PrevDecl,
1462 SourceLocation ClassLoc,
1464 auto *Result = new (C, DC)
1465 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1467 Result->Data.setInt(!C.getLangOpts().Modules);
1468 C.getObjCInterfaceType(Result, PrevDecl);
1472 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1474 auto *Result = new (C, ID)
1475 ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1476 SourceLocation(), nullptr, false);
1477 Result->Data.setInt(!C.getLangOpts().Modules);
1481 ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1482 SourceLocation AtLoc, IdentifierInfo *Id,
1483 ObjCTypeParamList *typeParamList,
1484 SourceLocation CLoc,
1485 ObjCInterfaceDecl *PrevDecl,
1487 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1488 redeclarable_base(C) {
1489 setPreviousDecl(PrevDecl);
1491 // Copy the 'data' pointer over.
1493 Data = PrevDecl->Data;
1495 setImplicit(IsInternal);
1497 setTypeParamList(typeParamList);
1500 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1501 assert(data().ExternallyCompleted && "Class is not externally completed");
1502 data().ExternallyCompleted = false;
1503 getASTContext().getExternalSource()->CompleteType(
1504 const_cast<ObjCInterfaceDecl *>(this));
1507 void ObjCInterfaceDecl::setExternallyCompleted() {
1508 assert(getASTContext().getExternalSource() &&
1509 "Class can't be externally completed without an external source");
1510 assert(hasDefinition() &&
1511 "Forward declarations can't be externally completed");
1512 data().ExternallyCompleted = true;
1515 void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1516 // Check for a complete definition and recover if not so.
1517 if (!isThisDeclarationADefinition())
1519 data().HasDesignatedInitializers = true;
1522 bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1523 // Check for a complete definition and recover if not so.
1524 if (!isThisDeclarationADefinition())
1526 if (data().ExternallyCompleted)
1527 LoadExternalDefinition();
1529 return data().HasDesignatedInitializers;
1533 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1534 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1535 return ObjCRTName->getMetadataName();
1541 ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1542 if (ObjCInterfaceDecl *ID =
1543 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1544 return ID->getObjCRuntimeNameAsString();
1549 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1550 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1551 if (data().ExternallyCompleted)
1552 LoadExternalDefinition();
1554 return getASTContext().getObjCImplementation(
1555 const_cast<ObjCInterfaceDecl*>(Def));
1558 // FIXME: Should make sure no callers ever do this.
1562 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1563 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1568 struct SynthesizeIvarChunk {
1572 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1573 : Size(size), Ivar(ivar) {}
1576 bool operator<(const SynthesizeIvarChunk & LHS,
1577 const SynthesizeIvarChunk &RHS) {
1578 return LHS.Size < RHS.Size;
1583 /// all_declared_ivar_begin - return first ivar declared in this class,
1584 /// its extensions and its implementation. Lazily build the list on first
1587 /// Caveat: The list returned by this method reflects the current
1588 /// state of the parser. The cache will be updated for every ivar
1589 /// added by an extension or the implementation when they are
1591 /// See also ObjCIvarDecl::Create().
1592 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1593 // FIXME: Should make sure no callers ever do this.
1594 if (!hasDefinition())
1597 ObjCIvarDecl *curIvar = nullptr;
1598 if (!data().IvarList) {
1599 if (!ivar_empty()) {
1600 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1601 data().IvarList = *I; ++I;
1602 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1603 curIvar->setNextIvar(*I);
1606 for (const auto *Ext : known_extensions()) {
1607 if (!Ext->ivar_empty()) {
1608 ObjCCategoryDecl::ivar_iterator
1609 I = Ext->ivar_begin(),
1610 E = Ext->ivar_end();
1611 if (!data().IvarList) {
1612 data().IvarList = *I; ++I;
1613 curIvar = data().IvarList;
1615 for ( ;I != E; curIvar = *I, ++I)
1616 curIvar->setNextIvar(*I);
1619 data().IvarListMissingImplementation = true;
1622 // cached and complete!
1623 if (!data().IvarListMissingImplementation)
1624 return data().IvarList;
1626 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1627 data().IvarListMissingImplementation = false;
1628 if (!ImplDecl->ivar_empty()) {
1629 SmallVector<SynthesizeIvarChunk, 16> layout;
1630 for (auto *IV : ImplDecl->ivars()) {
1631 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1632 layout.push_back(SynthesizeIvarChunk(
1633 IV->getASTContext().getTypeSize(IV->getType()), IV));
1636 if (!data().IvarList)
1637 data().IvarList = IV;
1639 curIvar->setNextIvar(IV);
1643 if (!layout.empty()) {
1644 // Order synthesized ivars by their size.
1645 std::stable_sort(layout.begin(), layout.end());
1646 unsigned Ix = 0, EIx = layout.size();
1647 if (!data().IvarList) {
1648 data().IvarList = layout[0].Ivar; Ix++;
1649 curIvar = data().IvarList;
1651 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1652 curIvar->setNextIvar(layout[Ix].Ivar);
1656 return data().IvarList;
1659 /// FindCategoryDeclaration - Finds category declaration in the list of
1660 /// categories for this class and returns it. Name of the category is passed
1661 /// in 'CategoryId'. If category not found, return 0;
1664 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1665 // FIXME: Should make sure no callers ever do this.
1666 if (!hasDefinition())
1669 if (data().ExternallyCompleted)
1670 LoadExternalDefinition();
1672 for (auto *Cat : visible_categories())
1673 if (Cat->getIdentifier() == CategoryId)
1680 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1681 for (const auto *Cat : visible_categories()) {
1682 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1683 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1690 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1691 for (const auto *Cat : visible_categories()) {
1692 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1693 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1700 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1701 /// has been implemented in IDecl class, its super class or categories (if
1702 /// lookupCategory is true).
1703 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1704 bool lookupCategory,
1705 bool RHSIsQualifiedID) {
1706 if (!hasDefinition())
1709 ObjCInterfaceDecl *IDecl = this;
1710 // 1st, look up the class.
1711 for (auto *PI : IDecl->protocols()){
1712 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1714 // This is dubious and is added to be compatible with gcc. In gcc, it is
1715 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1716 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1717 // object. This IMO, should be a bug.
1718 // FIXME: Treat this as an extension, and flag this as an error when GCC
1719 // extensions are not enabled.
1720 if (RHSIsQualifiedID &&
1721 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
1725 // 2nd, look up the category.
1727 for (const auto *Cat : visible_categories()) {
1728 for (auto *PI : Cat->protocols())
1729 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1733 // 3rd, look up the super class(s)
1734 if (IDecl->getSuperClass())
1736 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1742 //===----------------------------------------------------------------------===//
1744 //===----------------------------------------------------------------------===//
1746 void ObjCIvarDecl::anchor() {}
1748 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1749 SourceLocation StartLoc,
1750 SourceLocation IdLoc, IdentifierInfo *Id,
1751 QualType T, TypeSourceInfo *TInfo,
1752 AccessControl ac, Expr *BW,
1755 // Ivar's can only appear in interfaces, implementations (via synthesized
1756 // properties), and class extensions (via direct declaration, or synthesized
1759 // FIXME: This should really be asserting this:
1760 // (isa<ObjCCategoryDecl>(DC) &&
1761 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1762 // but unfortunately we sometimes place ivars into non-class extension
1763 // categories on error. This breaks an AST invariant, and should not be
1765 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1766 isa<ObjCCategoryDecl>(DC)) &&
1767 "Invalid ivar decl context!");
1768 // Once a new ivar is created in any of class/class-extension/implementation
1769 // decl contexts, the previously built IvarList must be rebuilt.
1770 auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1772 if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
1773 ID = IM->getClassInterface();
1775 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1777 ID->setIvarList(nullptr);
1780 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1784 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1785 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1786 nullptr, QualType(), nullptr,
1787 ObjCIvarDecl::None, nullptr, false);
1790 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1791 const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
1793 switch (DC->getKind()) {
1795 case ObjCCategoryImpl:
1797 llvm_unreachable("invalid ivar container!");
1799 // Ivars can only appear in class extension categories.
1800 case ObjCCategory: {
1801 const auto *CD = cast<ObjCCategoryDecl>(DC);
1802 assert(CD->IsClassExtension() && "invalid container for ivar!");
1803 return CD->getClassInterface();
1806 case ObjCImplementation:
1807 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1810 return cast<ObjCInterfaceDecl>(DC);
1814 QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1815 return getType().substObjCMemberType(objectType, getDeclContext(),
1816 ObjCSubstitutionContext::Property);
1819 //===----------------------------------------------------------------------===//
1820 // ObjCAtDefsFieldDecl
1821 //===----------------------------------------------------------------------===//
1823 void ObjCAtDefsFieldDecl::anchor() {}
1826 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1827 SourceLocation StartLoc, SourceLocation IdLoc,
1828 IdentifierInfo *Id, QualType T, Expr *BW) {
1829 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1832 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1834 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1835 SourceLocation(), nullptr, QualType(),
1839 //===----------------------------------------------------------------------===//
1841 //===----------------------------------------------------------------------===//
1843 void ObjCProtocolDecl::anchor() {}
1845 ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1846 IdentifierInfo *Id, SourceLocation nameLoc,
1847 SourceLocation atStartLoc,
1848 ObjCProtocolDecl *PrevDecl)
1849 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1850 redeclarable_base(C) {
1851 setPreviousDecl(PrevDecl);
1853 Data = PrevDecl->Data;
1856 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1858 SourceLocation nameLoc,
1859 SourceLocation atStartLoc,
1860 ObjCProtocolDecl *PrevDecl) {
1862 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1863 Result->Data.setInt(!C.getLangOpts().Modules);
1867 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1869 ObjCProtocolDecl *Result =
1870 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1871 SourceLocation(), nullptr);
1872 Result->Data.setInt(!C.getLangOpts().Modules);
1876 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1877 ObjCProtocolDecl *PDecl = this;
1879 if (Name == getIdentifier())
1882 for (auto *I : protocols())
1883 if ((PDecl = I->lookupProtocolNamed(Name)))
1889 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1891 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1892 bool isInstance) const {
1893 ObjCMethodDecl *MethodDecl = nullptr;
1895 // If there is no definition or the definition is hidden, we don't find
1897 const ObjCProtocolDecl *Def = getDefinition();
1898 if (!Def || Def->isHidden())
1901 if ((MethodDecl = getMethod(Sel, isInstance)))
1904 for (const auto *I : protocols())
1905 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1910 void ObjCProtocolDecl::allocateDefinitionData() {
1911 assert(!Data.getPointer() && "Protocol already has a definition!");
1912 Data.setPointer(new (getASTContext()) DefinitionData);
1913 Data.getPointer()->Definition = this;
1916 void ObjCProtocolDecl::startDefinition() {
1917 allocateDefinitionData();
1919 // Update all of the declarations with a pointer to the definition.
1920 for (auto *RD : redecls())
1921 RD->Data = this->Data;
1924 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1925 PropertyDeclOrder &PO) const {
1926 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1927 for (auto *Prop : PDecl->properties()) {
1928 // Insert into PM if not there already.
1929 PM.insert(std::make_pair(
1930 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1934 // Scan through protocol's protocols.
1935 for (const auto *PI : PDecl->protocols())
1936 PI->collectPropertiesToImplement(PM, PO);
1940 void ObjCProtocolDecl::collectInheritedProtocolProperties(
1941 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1942 PropertyDeclOrder &PO) const {
1943 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1944 if (!PS.insert(PDecl).second)
1946 for (auto *Prop : PDecl->properties()) {
1947 if (Prop == Property)
1949 if (Prop->getIdentifier() == Property->getIdentifier()) {
1954 // Scan through protocol's protocols which did not have a matching property.
1955 for (const auto *PI : PDecl->protocols())
1956 PI->collectInheritedProtocolProperties(Property, PS, PO);
1961 ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1962 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1963 return ObjCRTName->getMetadataName();
1968 //===----------------------------------------------------------------------===//
1970 //===----------------------------------------------------------------------===//
1972 void ObjCCategoryDecl::anchor() {}
1974 ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1975 SourceLocation ClassNameLoc,
1976 SourceLocation CategoryNameLoc,
1977 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1978 ObjCTypeParamList *typeParamList,
1979 SourceLocation IvarLBraceLoc,
1980 SourceLocation IvarRBraceLoc)
1981 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1982 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
1983 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
1984 setTypeParamList(typeParamList);
1987 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1988 SourceLocation AtLoc,
1989 SourceLocation ClassNameLoc,
1990 SourceLocation CategoryNameLoc,
1992 ObjCInterfaceDecl *IDecl,
1993 ObjCTypeParamList *typeParamList,
1994 SourceLocation IvarLBraceLoc,
1995 SourceLocation IvarRBraceLoc) {
1997 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1998 IDecl, typeParamList, IvarLBraceLoc,
2001 // Link this category into its class's category list.
2002 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
2003 if (IDecl->hasDefinition()) {
2004 IDecl->setCategoryListRaw(CatDecl);
2005 if (ASTMutationListener *L = C.getASTMutationListener())
2006 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2013 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
2015 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2016 SourceLocation(), SourceLocation(),
2017 nullptr, nullptr, nullptr);
2020 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2021 return getASTContext().getObjCImplementation(
2022 const_cast<ObjCCategoryDecl*>(this));
2025 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2026 getASTContext().setObjCImplementation(this, ImplD);
2029 void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2030 TypeParamList = TPL;
2033 // Set the declaration context of each of the type parameters.
2034 for (auto *typeParam : *TypeParamList)
2035 typeParam->setDeclContext(this);
2038 //===----------------------------------------------------------------------===//
2039 // ObjCCategoryImplDecl
2040 //===----------------------------------------------------------------------===//
2042 void ObjCCategoryImplDecl::anchor() {}
2044 ObjCCategoryImplDecl *
2045 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
2047 ObjCInterfaceDecl *ClassInterface,
2048 SourceLocation nameLoc,
2049 SourceLocation atStartLoc,
2050 SourceLocation CategoryNameLoc) {
2051 if (ClassInterface && ClassInterface->hasDefinition())
2052 ClassInterface = ClassInterface->getDefinition();
2053 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2054 atStartLoc, CategoryNameLoc);
2057 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2059 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2060 SourceLocation(), SourceLocation(),
2064 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2065 // The class interface might be NULL if we are working with invalid code.
2066 if (const ObjCInterfaceDecl *ID = getClassInterface())
2067 return ID->FindCategoryDeclaration(getIdentifier());
2071 void ObjCImplDecl::anchor() {}
2073 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2074 // FIXME: The context should be correct before we get here.
2075 property->setLexicalDeclContext(this);
2079 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2080 ASTContext &Ctx = getASTContext();
2082 if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2084 Ctx.setObjCImplementation(IFace, ImplD);
2086 } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2087 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2088 Ctx.setObjCImplementation(CD, ImplD);
2091 ClassInterface = IFace;
2094 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2095 /// properties implemented in this \@implementation block and returns
2096 /// the implemented property that uses it.
2097 ObjCPropertyImplDecl *ObjCImplDecl::
2098 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2099 for (auto *PID : property_impls())
2100 if (PID->getPropertyIvarDecl() &&
2101 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2106 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2107 /// added to the list of those properties \@synthesized/\@dynamic in this
2108 /// category \@implementation block.
2109 ObjCPropertyImplDecl *ObjCImplDecl::
2110 FindPropertyImplDecl(IdentifierInfo *Id,
2111 ObjCPropertyQueryKind QueryKind) const {
2112 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2113 for (auto *PID : property_impls())
2114 // If queryKind is unknown, we return the instance property if one
2115 // exists; otherwise we return the class property.
2116 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2117 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2118 !PID->getPropertyDecl()->isClassProperty()) ||
2119 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2120 PID->getPropertyDecl()->isClassProperty()) ||
2121 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2122 !PID->getPropertyDecl()->isClassProperty()))
2125 if (PID->getPropertyDecl()->isClassProperty())
2126 ClassPropImpl = PID;
2129 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2130 // We can't find the instance property, return the class property.
2131 return ClassPropImpl;
2136 raw_ostream &clang::operator<<(raw_ostream &OS,
2137 const ObjCCategoryImplDecl &CID) {
2138 OS << CID.getName();
2142 //===----------------------------------------------------------------------===//
2143 // ObjCImplementationDecl
2144 //===----------------------------------------------------------------------===//
2146 void ObjCImplementationDecl::anchor() {}
2148 ObjCImplementationDecl *
2149 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2150 ObjCInterfaceDecl *ClassInterface,
2151 ObjCInterfaceDecl *SuperDecl,
2152 SourceLocation nameLoc,
2153 SourceLocation atStartLoc,
2154 SourceLocation superLoc,
2155 SourceLocation IvarLBraceLoc,
2156 SourceLocation IvarRBraceLoc) {
2157 if (ClassInterface && ClassInterface->hasDefinition())
2158 ClassInterface = ClassInterface->getDefinition();
2159 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2160 nameLoc, atStartLoc, superLoc,
2161 IvarLBraceLoc, IvarRBraceLoc);
2164 ObjCImplementationDecl *
2165 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2166 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2167 SourceLocation(), SourceLocation());
2170 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2171 CXXCtorInitializer ** initializers,
2172 unsigned numInitializers) {
2173 if (numInitializers > 0) {
2174 NumIvarInitializers = numInitializers;
2175 auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
2176 memcpy(ivarInitializers, initializers,
2177 numInitializers * sizeof(CXXCtorInitializer*));
2178 IvarInitializers = ivarInitializers;
2182 ObjCImplementationDecl::init_const_iterator
2183 ObjCImplementationDecl::init_begin() const {
2184 return IvarInitializers.get(getASTContext().getExternalSource());
2187 raw_ostream &clang::operator<<(raw_ostream &OS,
2188 const ObjCImplementationDecl &ID) {
2193 //===----------------------------------------------------------------------===//
2194 // ObjCCompatibleAliasDecl
2195 //===----------------------------------------------------------------------===//
2197 void ObjCCompatibleAliasDecl::anchor() {}
2199 ObjCCompatibleAliasDecl *
2200 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2203 ObjCInterfaceDecl* AliasedClass) {
2204 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2207 ObjCCompatibleAliasDecl *
2208 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2209 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2213 //===----------------------------------------------------------------------===//
2215 //===----------------------------------------------------------------------===//
2217 void ObjCPropertyDecl::anchor() {}
2219 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2222 SourceLocation AtLoc,
2223 SourceLocation LParenLoc,
2225 TypeSourceInfo *TSI,
2226 PropertyControl propControl) {
2227 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2231 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2233 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2234 SourceLocation(), SourceLocation(),
2235 QualType(), nullptr, None);
2238 QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2239 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2240 ObjCSubstitutionContext::Property);
2243 //===----------------------------------------------------------------------===//
2244 // ObjCPropertyImplDecl
2245 //===----------------------------------------------------------------------===//
2247 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2249 SourceLocation atLoc,
2251 ObjCPropertyDecl *property,
2254 SourceLocation ivarLoc) {
2255 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2259 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2261 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2262 SourceLocation(), nullptr, Dynamic,
2263 nullptr, SourceLocation());
2266 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2267 SourceLocation EndLoc = getLocation();
2268 if (IvarLoc.isValid())
2271 return SourceRange(AtLoc, EndLoc);