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_result R = lookup(Id);
58 for (lookup_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_result R = lookup(Sel);
87 for (lookup_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_result R = lookup(Sel);
105 for (lookup_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 ObjCPropertyQueryKind queryKind) {
157 // If this context is a hidden protocol definition, don't find any
159 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
160 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
165 // If context is class, then lookup property in its visible extensions.
166 // This comes before property is looked up in primary class.
167 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
168 for (const auto *Ext : IDecl->visible_extensions())
169 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
175 DeclContext::lookup_result R = DC->lookup(propertyID);
176 ObjCPropertyDecl *classProp = nullptr;
177 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
179 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
180 // If queryKind is unknown, we return the instance property if one
181 // exists; otherwise we return the class property.
182 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
183 !PD->isClassProperty()) ||
184 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
185 PD->isClassProperty()) ||
186 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
187 !PD->isClassProperty()))
190 if (PD->isClassProperty())
194 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
195 // We can't find the instance property, return the class property.
202 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
203 SmallString<128> ivarName;
205 llvm::raw_svector_ostream os(ivarName);
206 os << '_' << getIdentifier()->getName();
208 return &Ctx.Idents.get(ivarName.str());
211 /// FindPropertyDeclaration - Finds declaration of the property given its name
212 /// in 'PropertyId' and returns it. It returns 0, if not found.
213 ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
214 const IdentifierInfo *PropertyId,
215 ObjCPropertyQueryKind QueryKind) const {
216 // Don't find properties within hidden protocol definitions.
217 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
218 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
223 // Search the extensions of a class first; they override what's in
225 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
226 for (const auto *Ext : ClassDecl->visible_extensions()) {
227 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
232 if (ObjCPropertyDecl *PD =
233 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
240 case Decl::ObjCProtocol: {
241 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
242 for (const auto *I : PID->protocols())
243 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
248 case Decl::ObjCInterface: {
249 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
250 // Look through categories (but not extensions; they were handled above).
251 for (const auto *Cat : OID->visible_categories()) {
252 if (!Cat->IsClassExtension())
253 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
254 PropertyId, QueryKind))
258 // Look through protocols.
259 for (const auto *I : OID->all_referenced_protocols())
260 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
264 // Finally, check the super class.
265 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
266 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
269 case Decl::ObjCCategory: {
270 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
271 // Look through protocols.
272 if (!OCD->IsClassExtension())
273 for (const auto *I : OCD->protocols())
274 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
283 void ObjCInterfaceDecl::anchor() { }
285 ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
286 // If this particular declaration has a type parameter list, return it.
287 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
290 // If there is a definition, return its type parameter list.
291 if (const ObjCInterfaceDecl *def = getDefinition())
292 return def->getTypeParamListAsWritten();
294 // Otherwise, look at previous declarations to determine whether any
295 // of them has a type parameter list, skipping over those
296 // declarations that do not.
297 for (auto decl = getMostRecentDecl(); decl; decl = decl->getPreviousDecl()) {
298 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
305 void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
309 // Set the declaration context of each of the type parameters.
310 for (auto typeParam : *TypeParamList)
311 typeParam->setDeclContext(this);
314 ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
315 // FIXME: Should make sure no callers ever do this.
316 if (!hasDefinition())
319 if (data().ExternallyCompleted)
320 LoadExternalDefinition();
322 if (const ObjCObjectType *superType = getSuperClassType()) {
323 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
324 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
334 SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
335 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
336 return superTInfo->getTypeLoc().getLocStart();
338 return SourceLocation();
341 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
342 /// with name 'PropertyId' in the primary class; including those in protocols
343 /// (direct or indirect) used by the primary class.
346 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
347 IdentifierInfo *PropertyId,
348 ObjCPropertyQueryKind QueryKind) const {
349 // FIXME: Should make sure no callers ever do this.
350 if (!hasDefinition())
353 if (data().ExternallyCompleted)
354 LoadExternalDefinition();
356 if (ObjCPropertyDecl *PD =
357 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
361 // Look through protocols.
362 for (const auto *I : all_referenced_protocols())
363 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
370 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
371 PropertyDeclOrder &PO) const {
372 for (auto *Prop : properties()) {
373 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
376 for (const auto *Ext : known_extensions()) {
377 const ObjCCategoryDecl *ClassExt = Ext;
378 for (auto *Prop : ClassExt->properties()) {
379 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
383 for (const auto *PI : all_referenced_protocols())
384 PI->collectPropertiesToImplement(PM, PO);
385 // Note, the properties declared only in class extensions are still copied
386 // into the main @interface's property list, and therefore we don't
387 // explicitly, have to search class extension properties.
390 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
391 const ObjCInterfaceDecl *Class = this;
393 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
395 Class = Class->getSuperClass();
400 const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
401 const ObjCInterfaceDecl *Class = this;
403 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
405 Class = Class->getSuperClass();
410 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
411 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
414 if (data().ExternallyCompleted)
415 LoadExternalDefinition();
417 if (data().AllReferencedProtocols.empty() &&
418 data().ReferencedProtocols.empty()) {
419 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
423 // Check for duplicate protocol in class's protocol list.
424 // This is O(n*m). But it is extremely rare and number of protocols in
425 // class or its extension are very few.
426 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
427 for (unsigned i = 0; i < ExtNum; i++) {
428 bool protocolExists = false;
429 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
430 for (auto *Proto : all_referenced_protocols()) {
431 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
432 protocolExists = true;
436 // Do we want to warn on a protocol in extension class which
437 // already exist in the class? Probably not.
439 ProtocolRefs.push_back(ProtoInExtension);
442 if (ProtocolRefs.empty())
445 // Merge ProtocolRefs into class's protocol list;
446 ProtocolRefs.append(all_referenced_protocol_begin(),
447 all_referenced_protocol_end());
449 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
452 const ObjCInterfaceDecl *
453 ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
454 const ObjCInterfaceDecl *IFace = this;
456 if (IFace->hasDesignatedInitializers())
458 if (!IFace->inheritsDesignatedInitializers())
460 IFace = IFace->getSuperClass();
465 static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
466 for (const auto *MD : D->instance_methods()) {
467 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
470 for (const auto *Ext : D->visible_extensions()) {
471 for (const auto *MD : Ext->instance_methods()) {
472 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
476 if (const auto *ImplD = D->getImplementation()) {
477 for (const auto *MD : ImplD->instance_methods()) {
478 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
485 bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
486 switch (data().InheritedDesignatedInitializers) {
487 case DefinitionData::IDI_Inherited:
489 case DefinitionData::IDI_NotInherited:
491 case DefinitionData::IDI_Unknown: {
492 // If the class introduced initializers we conservatively assume that we
493 // don't know if any of them is a designated initializer to avoid possible
494 // misleading warnings.
495 if (isIntroducingInitializers(this)) {
496 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
498 if (auto SuperD = getSuperClass()) {
499 data().InheritedDesignatedInitializers =
500 SuperD->declaresOrInheritsDesignatedInitializers() ?
501 DefinitionData::IDI_Inherited :
502 DefinitionData::IDI_NotInherited;
504 data().InheritedDesignatedInitializers =
505 DefinitionData::IDI_NotInherited;
508 assert(data().InheritedDesignatedInitializers
509 != DefinitionData::IDI_Unknown);
510 return data().InheritedDesignatedInitializers ==
511 DefinitionData::IDI_Inherited;
515 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
518 void ObjCInterfaceDecl::getDesignatedInitializers(
519 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
520 // Check for a complete definition and recover if not so.
521 if (!isThisDeclarationADefinition())
523 if (data().ExternallyCompleted)
524 LoadExternalDefinition();
526 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
530 for (const auto *MD : IFace->instance_methods())
531 if (MD->isThisDeclarationADesignatedInitializer())
532 Methods.push_back(MD);
533 for (const auto *Ext : IFace->visible_extensions()) {
534 for (const auto *MD : Ext->instance_methods())
535 if (MD->isThisDeclarationADesignatedInitializer())
536 Methods.push_back(MD);
540 bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
541 const ObjCMethodDecl **InitMethod) const {
542 // Check for a complete definition and recover if not so.
543 if (!isThisDeclarationADefinition())
545 if (data().ExternallyCompleted)
546 LoadExternalDefinition();
548 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
552 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
553 if (MD->isThisDeclarationADesignatedInitializer()) {
559 for (const auto *Ext : IFace->visible_extensions()) {
560 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
561 if (MD->isThisDeclarationADesignatedInitializer()) {
571 void ObjCInterfaceDecl::allocateDefinitionData() {
572 assert(!hasDefinition() && "ObjC class already has a definition");
573 Data.setPointer(new (getASTContext()) DefinitionData());
574 Data.getPointer()->Definition = this;
576 // Make the type point at the definition, now that we have one.
578 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
581 void ObjCInterfaceDecl::startDefinition() {
582 allocateDefinitionData();
584 // Update all of the declarations with a pointer to the definition.
585 for (auto RD : redecls()) {
591 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
592 ObjCInterfaceDecl *&clsDeclared) {
593 // FIXME: Should make sure no callers ever do this.
594 if (!hasDefinition())
597 if (data().ExternallyCompleted)
598 LoadExternalDefinition();
600 ObjCInterfaceDecl* ClassDecl = this;
601 while (ClassDecl != nullptr) {
602 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
603 clsDeclared = ClassDecl;
607 for (const auto *Ext : ClassDecl->visible_extensions()) {
608 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
609 clsDeclared = ClassDecl;
614 ClassDecl = ClassDecl->getSuperClass();
619 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
620 /// class whose name is passed as argument. If it is not one of the super classes
621 /// the it returns NULL.
622 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
623 const IdentifierInfo*ICName) {
624 // FIXME: Should make sure no callers ever do this.
625 if (!hasDefinition())
628 if (data().ExternallyCompleted)
629 LoadExternalDefinition();
631 ObjCInterfaceDecl* ClassDecl = this;
632 while (ClassDecl != nullptr) {
633 if (ClassDecl->getIdentifier() == ICName)
635 ClassDecl = ClassDecl->getSuperClass();
641 ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
642 for (auto *P : all_referenced_protocols())
643 if (P->lookupProtocolNamed(Name))
645 ObjCInterfaceDecl *SuperClass = getSuperClass();
646 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
649 /// lookupMethod - This method returns an instance/class method by looking in
650 /// the class, its categories, and its super classes (using a linear search).
651 /// When argument category "C" is specified, any implicit method found
652 /// in this category is ignored.
653 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
655 bool shallowCategoryLookup,
657 const ObjCCategoryDecl *C) const
659 // FIXME: Should make sure no callers ever do this.
660 if (!hasDefinition())
663 const ObjCInterfaceDecl* ClassDecl = this;
664 ObjCMethodDecl *MethodDecl = nullptr;
666 if (data().ExternallyCompleted)
667 LoadExternalDefinition();
670 // 1. Look through primary class.
671 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
674 // 2. Didn't find one yet - now look through categories.
675 for (const auto *Cat : ClassDecl->visible_categories())
676 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
677 if (C != Cat || !MethodDecl->isImplicit())
680 // 3. Didn't find one yet - look through primary class's protocols.
681 for (const auto *I : ClassDecl->protocols())
682 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
685 // 4. Didn't find one yet - now look through categories' protocols
686 if (!shallowCategoryLookup)
687 for (const auto *Cat : ClassDecl->visible_categories()) {
688 // Didn't find one yet - look through protocols.
689 const ObjCList<ObjCProtocolDecl> &Protocols =
690 Cat->getReferencedProtocols();
691 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
692 E = Protocols.end(); I != E; ++I)
693 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
694 if (C != Cat || !MethodDecl->isImplicit())
702 // 5. Get to the super class (if any).
703 ClassDecl = ClassDecl->getSuperClass();
708 // Will search "local" class/category implementations for a method decl.
709 // If failed, then we search in class's root for an instance method.
710 // Returns 0 if no method is found.
711 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
713 bool Instance) const {
714 // FIXME: Should make sure no callers ever do this.
715 if (!hasDefinition())
718 if (data().ExternallyCompleted)
719 LoadExternalDefinition();
721 ObjCMethodDecl *Method = nullptr;
722 if (ObjCImplementationDecl *ImpDecl = getImplementation())
723 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
724 : ImpDecl->getClassMethod(Sel);
726 // Look through local category implementations associated with the class.
728 Method = getCategoryMethod(Sel, Instance);
730 // Before we give up, check if the selector is an instance method.
731 // But only in the root. This matches gcc's behavior and what the
733 if (!Instance && !Method && !getSuperClass()) {
734 Method = lookupInstanceMethod(Sel);
735 // Look through local category implementations associated
736 // with the root class.
738 Method = lookupPrivateMethod(Sel, true);
741 if (!Method && getSuperClass())
742 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
746 //===----------------------------------------------------------------------===//
748 //===----------------------------------------------------------------------===//
750 ObjCMethodDecl *ObjCMethodDecl::Create(
751 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
752 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
753 DeclContext *contextDecl, bool isInstance, bool isVariadic,
754 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
755 ImplementationControl impControl, bool HasRelatedResultType) {
756 return new (C, contextDecl) ObjCMethodDecl(
757 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
758 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
759 impControl, HasRelatedResultType);
762 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
763 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
764 Selector(), QualType(), nullptr, nullptr);
767 bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
768 return getMethodFamily() == OMF_init &&
769 hasAttr<ObjCDesignatedInitializerAttr>();
772 bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
773 const ObjCMethodDecl **InitMethod) const {
774 if (getMethodFamily() != OMF_init)
776 const DeclContext *DC = getDeclContext();
777 if (isa<ObjCProtocolDecl>(DC))
779 if (const ObjCInterfaceDecl *ID = getClassInterface())
780 return ID->isDesignatedInitializer(getSelector(), InitMethod);
784 Stmt *ObjCMethodDecl::getBody() const {
785 return Body.get(getASTContext().getExternalSource());
788 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
790 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
791 IsRedeclaration = true;
792 PrevMethod->HasRedeclaration = true;
795 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
796 ArrayRef<ParmVarDecl*> Params,
797 ArrayRef<SourceLocation> SelLocs) {
798 ParamsAndSelLocs = nullptr;
799 NumParams = Params.size();
800 if (Params.empty() && SelLocs.empty())
803 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
804 "Alignment not sufficient for SourceLocation");
806 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
807 sizeof(SourceLocation) * SelLocs.size();
808 ParamsAndSelLocs = C.Allocate(Size);
809 std::copy(Params.begin(), Params.end(), getParams());
810 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
813 void ObjCMethodDecl::getSelectorLocs(
814 SmallVectorImpl<SourceLocation> &SelLocs) const {
815 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
816 SelLocs.push_back(getSelectorLoc(i));
819 void ObjCMethodDecl::setMethodParams(ASTContext &C,
820 ArrayRef<ParmVarDecl*> Params,
821 ArrayRef<SourceLocation> SelLocs) {
822 assert((!SelLocs.empty() || isImplicit()) &&
823 "No selector locs for non-implicit method");
825 return setParamsAndSelLocs(C, Params, llvm::None);
827 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
829 if (SelLocsKind != SelLoc_NonStandard)
830 return setParamsAndSelLocs(C, Params, llvm::None);
832 setParamsAndSelLocs(C, Params, SelLocs);
835 /// \brief A definition will return its interface declaration.
836 /// An interface declaration will return its definition.
837 /// Otherwise it will return itself.
838 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
839 ASTContext &Ctx = getASTContext();
840 ObjCMethodDecl *Redecl = nullptr;
841 if (HasRedeclaration)
842 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
846 Decl *CtxD = cast<Decl>(getDeclContext());
848 if (!CtxD->isInvalidDecl()) {
849 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
850 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
851 if (!ImplD->isInvalidDecl())
852 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
854 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
855 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
856 if (!ImplD->isInvalidDecl())
857 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
859 } else if (ObjCImplementationDecl *ImplD =
860 dyn_cast<ObjCImplementationDecl>(CtxD)) {
861 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
862 if (!IFD->isInvalidDecl())
863 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
865 } else if (ObjCCategoryImplDecl *CImplD =
866 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
867 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
868 if (!CatD->isInvalidDecl())
869 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
873 // Ensure that the discovered method redeclaration has a valid declaration
874 // context. Used to prevent infinite loops when iterating redeclarations in
875 // a partially invalid AST.
876 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
879 if (!Redecl && isRedeclaration()) {
880 // This is the last redeclaration, go back to the first method.
881 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
885 return Redecl ? Redecl : this;
888 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
889 Decl *CtxD = cast<Decl>(getDeclContext());
891 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
892 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
893 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
897 } else if (ObjCCategoryImplDecl *CImplD =
898 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
899 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
900 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
905 if (isRedeclaration()) {
906 // It is possible that we have not done deserializing the ObjCMethod yet.
908 cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
910 return MD ? MD : this;
916 SourceLocation ObjCMethodDecl::getLocEnd() const {
917 if (Stmt *Body = getBody())
918 return Body->getLocEnd();
922 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
923 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
924 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
927 // Check for an explicit attribute.
928 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
929 // The unfortunate necessity of mapping between enums here is due
930 // to the attributes framework.
931 switch (attr->getFamily()) {
932 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
933 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
934 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
935 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
936 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
937 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
939 Family = static_cast<unsigned>(family);
943 family = getSelector().getMethodFamily();
945 case OMF_None: break;
947 // init only has a conventional meaning for an instance method, and
948 // it has to return an object.
950 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
954 // alloc/copy/new have a conventional meaning for both class and
955 // instance methods, but they require an object return.
958 case OMF_mutableCopy:
960 if (!getReturnType()->isObjCObjectPointerType())
964 // These selectors have a conventional meaning only for instance methods.
969 case OMF_autorelease:
970 case OMF_retainCount:
972 if (!isInstanceMethod())
977 if (isInstanceMethod() || !getReturnType()->isVoidType())
981 case OMF_performSelector:
982 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
985 unsigned noParams = param_size();
986 if (noParams < 1 || noParams > 3)
989 ObjCMethodDecl::param_type_iterator it = param_type_begin();
990 QualType ArgT = (*it);
991 if (!ArgT->isObjCSelType()) {
998 if (!ArgT->isObjCIdType()) {
1009 // Cache the result.
1010 Family = static_cast<unsigned>(family);
1014 QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1015 const ObjCInterfaceDecl *OID,
1016 bool &selfIsPseudoStrong,
1017 bool &selfIsConsumed) {
1019 selfIsPseudoStrong = false;
1020 selfIsConsumed = false;
1021 if (isInstanceMethod()) {
1022 // There may be no interface context due to error in declaration
1023 // of the interface (which has been reported). Recover gracefully.
1025 selfTy = Context.getObjCInterfaceType(OID);
1026 selfTy = Context.getObjCObjectPointerType(selfTy);
1028 selfTy = Context.getObjCIdType();
1030 } else // we have a factory method.
1031 selfTy = Context.getObjCClassType();
1033 if (Context.getLangOpts().ObjCAutoRefCount) {
1034 if (isInstanceMethod()) {
1035 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
1037 // 'self' is always __strong. It's actually pseudo-strong except
1038 // in init methods (or methods labeled ns_consumes_self), though.
1040 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1041 selfTy = Context.getQualifiedType(selfTy, qs);
1043 // In addition, 'self' is const unless this is an init method.
1044 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1045 selfTy = selfTy.withConst();
1046 selfIsPseudoStrong = true;
1050 assert(isClassMethod());
1051 // 'self' is always const in class methods.
1052 selfTy = selfTy.withConst();
1053 selfIsPseudoStrong = true;
1059 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1060 const ObjCInterfaceDecl *OID) {
1061 bool selfIsPseudoStrong, selfIsConsumed;
1063 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1064 ImplicitParamDecl *self
1065 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1066 &Context.Idents.get("self"), selfTy);
1070 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1072 if (selfIsPseudoStrong)
1073 self->setARCPseudoStrong(true);
1075 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
1076 &Context.Idents.get("_cmd"),
1077 Context.getObjCSelType()));
1080 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1081 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1083 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1084 return CD->getClassInterface();
1085 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1086 return IMD->getClassInterface();
1087 if (isa<ObjCProtocolDecl>(getDeclContext()))
1089 llvm_unreachable("unknown method context");
1092 SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1093 const auto *TSI = getReturnTypeSourceInfo();
1095 return TSI->getTypeLoc().getSourceRange();
1096 return SourceRange();
1099 QualType ObjCMethodDecl::getSendResultType() const {
1100 ASTContext &Ctx = getASTContext();
1101 return getReturnType().getNonLValueExprType(Ctx)
1102 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1105 QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1106 // FIXME: Handle related result types here.
1108 return getReturnType().getNonLValueExprType(getASTContext())
1109 .substObjCMemberType(receiverType, getDeclContext(),
1110 ObjCSubstitutionContext::Result);
1113 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1114 const ObjCMethodDecl *Method,
1115 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1116 bool MovedToSuper) {
1120 // In categories look for overriden methods from protocols. A method from
1121 // category is not "overriden" since it is considered as the "same" method
1122 // (same USR) as the one from the interface.
1123 if (const ObjCCategoryDecl *
1124 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1125 // Check whether we have a matching method at this category but only if we
1126 // are at the super class level.
1128 if (ObjCMethodDecl *
1129 Overridden = Container->getMethod(Method->getSelector(),
1130 Method->isInstanceMethod(),
1131 /*AllowHidden=*/true))
1132 if (Method != Overridden) {
1133 // We found an override at this category; there is no need to look
1134 // into its protocols.
1135 Methods.push_back(Overridden);
1139 for (const auto *P : Category->protocols())
1140 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1144 // Check whether we have a matching method at this level.
1145 if (const ObjCMethodDecl *
1146 Overridden = Container->getMethod(Method->getSelector(),
1147 Method->isInstanceMethod(),
1148 /*AllowHidden=*/true))
1149 if (Method != Overridden) {
1150 // We found an override at this level; there is no need to look
1151 // into other protocols or categories.
1152 Methods.push_back(Overridden);
1156 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1157 for (const auto *P : Protocol->protocols())
1158 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1161 if (const ObjCInterfaceDecl *
1162 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1163 for (const auto *P : Interface->protocols())
1164 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1166 for (const auto *Cat : Interface->known_categories())
1167 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
1169 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1170 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1171 /*MovedToSuper=*/true);
1175 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1176 const ObjCMethodDecl *Method,
1177 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1178 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1179 /*MovedToSuper=*/false);
1182 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1183 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1184 assert(Method->isOverriding());
1186 if (const ObjCProtocolDecl *
1187 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1188 CollectOverriddenMethods(ProtD, Method, overridden);
1190 } else if (const ObjCImplDecl *
1191 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1192 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1195 // Start searching for overridden methods using the method from the
1196 // interface as starting point.
1197 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1198 Method->isInstanceMethod(),
1199 /*AllowHidden=*/true))
1201 CollectOverriddenMethods(ID, Method, overridden);
1203 } else if (const ObjCCategoryDecl *
1204 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1205 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1208 // Start searching for overridden methods using the method from the
1209 // interface as starting point.
1210 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1211 Method->isInstanceMethod(),
1212 /*AllowHidden=*/true))
1214 CollectOverriddenMethods(ID, Method, overridden);
1217 CollectOverriddenMethods(
1218 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1219 Method, overridden);
1223 void ObjCMethodDecl::getOverriddenMethods(
1224 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1225 const ObjCMethodDecl *Method = this;
1227 if (Method->isRedeclaration()) {
1228 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1229 getMethod(Method->getSelector(), Method->isInstanceMethod());
1232 if (Method->isOverriding()) {
1233 collectOverriddenMethodsSlow(Method, Overridden);
1234 assert(!Overridden.empty() &&
1235 "ObjCMethodDecl's overriding bit is not as expected");
1239 const ObjCPropertyDecl *
1240 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1241 Selector Sel = getSelector();
1242 unsigned NumArgs = Sel.getNumArgs();
1246 if (isPropertyAccessor()) {
1247 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
1248 bool IsGetter = (NumArgs == 0);
1249 bool IsInstance = isInstanceMethod();
1251 /// Local function that attempts to find a matching property within the
1252 /// given Objective-C container.
1253 auto findMatchingProperty =
1254 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
1256 for (const auto *I : Container->instance_properties()) {
1257 Selector NextSel = IsGetter ? I->getGetterName()
1258 : I->getSetterName();
1263 for (const auto *I : Container->class_properties()) {
1264 Selector NextSel = IsGetter ? I->getGetterName()
1265 : I->getSetterName();
1274 // Look in the container we were given.
1275 if (const auto *Found = findMatchingProperty(Container))
1278 // If we're in a category or extension, look in the main class.
1279 const ObjCInterfaceDecl *ClassDecl = nullptr;
1280 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1281 ClassDecl = Category->getClassInterface();
1282 if (const auto *Found = findMatchingProperty(ClassDecl))
1285 // Determine whether the container is a class.
1286 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1289 // If we have a class, check its visible extensions.
1291 for (const auto *Ext : ClassDecl->visible_extensions()) {
1292 if (Ext == Container)
1295 if (const auto *Found = findMatchingProperty(Ext))
1300 llvm_unreachable("Marked as a property accessor but no property found!");
1303 if (!CheckOverrides)
1306 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1307 OverridesTy Overrides;
1308 getOverriddenMethods(Overrides);
1309 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1311 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1318 //===----------------------------------------------------------------------===//
1319 // ObjCTypeParamDecl
1320 //===----------------------------------------------------------------------===//
1322 void ObjCTypeParamDecl::anchor() { }
1324 ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1325 ObjCTypeParamVariance variance,
1326 SourceLocation varianceLoc,
1328 SourceLocation nameLoc,
1329 IdentifierInfo *name,
1330 SourceLocation colonLoc,
1331 TypeSourceInfo *boundInfo) {
1333 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1334 nameLoc, name, colonLoc, boundInfo);
1335 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1336 TPDecl->setTypeForDecl(TPType.getTypePtr());
1340 ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1342 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1343 ObjCTypeParamVariance::Invariant,
1344 SourceLocation(), 0, SourceLocation(),
1345 nullptr, SourceLocation(), nullptr);
1348 SourceRange ObjCTypeParamDecl::getSourceRange() const {
1349 SourceLocation startLoc = VarianceLoc;
1350 if (startLoc.isInvalid())
1351 startLoc = getLocation();
1353 if (hasExplicitBound()) {
1354 return SourceRange(startLoc,
1355 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1358 return SourceRange(startLoc);
1361 //===----------------------------------------------------------------------===//
1362 // ObjCTypeParamList
1363 //===----------------------------------------------------------------------===//
1364 ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1365 ArrayRef<ObjCTypeParamDecl *> typeParams,
1366 SourceLocation rAngleLoc)
1367 : NumParams(typeParams.size())
1369 Brackets.Begin = lAngleLoc.getRawEncoding();
1370 Brackets.End = rAngleLoc.getRawEncoding();
1371 std::copy(typeParams.begin(), typeParams.end(), begin());
1375 ObjCTypeParamList *ObjCTypeParamList::create(
1377 SourceLocation lAngleLoc,
1378 ArrayRef<ObjCTypeParamDecl *> typeParams,
1379 SourceLocation rAngleLoc) {
1381 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1382 alignof(ObjCTypeParamList));
1383 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1386 void ObjCTypeParamList::gatherDefaultTypeArgs(
1387 SmallVectorImpl<QualType> &typeArgs) const {
1388 typeArgs.reserve(size());
1389 for (auto typeParam : *this)
1390 typeArgs.push_back(typeParam->getUnderlyingType());
1393 //===----------------------------------------------------------------------===//
1394 // ObjCInterfaceDecl
1395 //===----------------------------------------------------------------------===//
1397 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1399 SourceLocation atLoc,
1401 ObjCTypeParamList *typeParamList,
1402 ObjCInterfaceDecl *PrevDecl,
1403 SourceLocation ClassLoc,
1405 ObjCInterfaceDecl *Result = new (C, DC)
1406 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1408 Result->Data.setInt(!C.getLangOpts().Modules);
1409 C.getObjCInterfaceType(Result, PrevDecl);
1413 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1415 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
1421 Result->Data.setInt(!C.getLangOpts().Modules);
1425 ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1426 SourceLocation AtLoc, IdentifierInfo *Id,
1427 ObjCTypeParamList *typeParamList,
1428 SourceLocation CLoc,
1429 ObjCInterfaceDecl *PrevDecl,
1431 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1432 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(nullptr),
1434 setPreviousDecl(PrevDecl);
1436 // Copy the 'data' pointer over.
1438 Data = PrevDecl->Data;
1440 setImplicit(IsInternal);
1442 setTypeParamList(typeParamList);
1445 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1446 assert(data().ExternallyCompleted && "Class is not externally completed");
1447 data().ExternallyCompleted = false;
1448 getASTContext().getExternalSource()->CompleteType(
1449 const_cast<ObjCInterfaceDecl *>(this));
1452 void ObjCInterfaceDecl::setExternallyCompleted() {
1453 assert(getASTContext().getExternalSource() &&
1454 "Class can't be externally completed without an external source");
1455 assert(hasDefinition() &&
1456 "Forward declarations can't be externally completed");
1457 data().ExternallyCompleted = true;
1460 void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1461 // Check for a complete definition and recover if not so.
1462 if (!isThisDeclarationADefinition())
1464 data().HasDesignatedInitializers = true;
1467 bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1468 // Check for a complete definition and recover if not so.
1469 if (!isThisDeclarationADefinition())
1471 if (data().ExternallyCompleted)
1472 LoadExternalDefinition();
1474 return data().HasDesignatedInitializers;
1478 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1479 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1480 return ObjCRTName->getMetadataName();
1486 ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1487 if (ObjCInterfaceDecl *ID =
1488 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1489 return ID->getObjCRuntimeNameAsString();
1494 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1495 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1496 if (data().ExternallyCompleted)
1497 LoadExternalDefinition();
1499 return getASTContext().getObjCImplementation(
1500 const_cast<ObjCInterfaceDecl*>(Def));
1503 // FIXME: Should make sure no callers ever do this.
1507 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1508 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1512 struct SynthesizeIvarChunk {
1515 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1516 : Size(size), Ivar(ivar) {}
1519 bool operator<(const SynthesizeIvarChunk & LHS,
1520 const SynthesizeIvarChunk &RHS) {
1521 return LHS.Size < RHS.Size;
1525 /// all_declared_ivar_begin - return first ivar declared in this class,
1526 /// its extensions and its implementation. Lazily build the list on first
1529 /// Caveat: The list returned by this method reflects the current
1530 /// state of the parser. The cache will be updated for every ivar
1531 /// added by an extension or the implementation when they are
1533 /// See also ObjCIvarDecl::Create().
1534 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1535 // FIXME: Should make sure no callers ever do this.
1536 if (!hasDefinition())
1539 ObjCIvarDecl *curIvar = nullptr;
1540 if (!data().IvarList) {
1541 if (!ivar_empty()) {
1542 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1543 data().IvarList = *I; ++I;
1544 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1545 curIvar->setNextIvar(*I);
1548 for (const auto *Ext : known_extensions()) {
1549 if (!Ext->ivar_empty()) {
1550 ObjCCategoryDecl::ivar_iterator
1551 I = Ext->ivar_begin(),
1552 E = Ext->ivar_end();
1553 if (!data().IvarList) {
1554 data().IvarList = *I; ++I;
1555 curIvar = data().IvarList;
1557 for ( ;I != E; curIvar = *I, ++I)
1558 curIvar->setNextIvar(*I);
1561 data().IvarListMissingImplementation = true;
1564 // cached and complete!
1565 if (!data().IvarListMissingImplementation)
1566 return data().IvarList;
1568 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1569 data().IvarListMissingImplementation = false;
1570 if (!ImplDecl->ivar_empty()) {
1571 SmallVector<SynthesizeIvarChunk, 16> layout;
1572 for (auto *IV : ImplDecl->ivars()) {
1573 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1574 layout.push_back(SynthesizeIvarChunk(
1575 IV->getASTContext().getTypeSize(IV->getType()), IV));
1578 if (!data().IvarList)
1579 data().IvarList = IV;
1581 curIvar->setNextIvar(IV);
1585 if (!layout.empty()) {
1586 // Order synthesized ivars by their size.
1587 std::stable_sort(layout.begin(), layout.end());
1588 unsigned Ix = 0, EIx = layout.size();
1589 if (!data().IvarList) {
1590 data().IvarList = layout[0].Ivar; Ix++;
1591 curIvar = data().IvarList;
1593 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1594 curIvar->setNextIvar(layout[Ix].Ivar);
1598 return data().IvarList;
1601 /// FindCategoryDeclaration - Finds category declaration in the list of
1602 /// categories for this class and returns it. Name of the category is passed
1603 /// in 'CategoryId'. If category not found, return 0;
1606 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1607 // FIXME: Should make sure no callers ever do this.
1608 if (!hasDefinition())
1611 if (data().ExternallyCompleted)
1612 LoadExternalDefinition();
1614 for (auto *Cat : visible_categories())
1615 if (Cat->getIdentifier() == CategoryId)
1622 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1623 for (const auto *Cat : visible_categories()) {
1624 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1625 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1632 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1633 for (const auto *Cat : visible_categories()) {
1634 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1635 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1642 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1643 /// has been implemented in IDecl class, its super class or categories (if
1644 /// lookupCategory is true).
1645 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1646 bool lookupCategory,
1647 bool RHSIsQualifiedID) {
1648 if (!hasDefinition())
1651 ObjCInterfaceDecl *IDecl = this;
1652 // 1st, look up the class.
1653 for (auto *PI : IDecl->protocols()){
1654 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1656 // This is dubious and is added to be compatible with gcc. In gcc, it is
1657 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1658 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1659 // object. This IMO, should be a bug.
1660 // FIXME: Treat this as an extension, and flag this as an error when GCC
1661 // extensions are not enabled.
1662 if (RHSIsQualifiedID &&
1663 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
1667 // 2nd, look up the category.
1669 for (const auto *Cat : visible_categories()) {
1670 for (auto *PI : Cat->protocols())
1671 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1675 // 3rd, look up the super class(s)
1676 if (IDecl->getSuperClass())
1678 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1684 //===----------------------------------------------------------------------===//
1686 //===----------------------------------------------------------------------===//
1688 void ObjCIvarDecl::anchor() { }
1690 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1691 SourceLocation StartLoc,
1692 SourceLocation IdLoc, IdentifierInfo *Id,
1693 QualType T, TypeSourceInfo *TInfo,
1694 AccessControl ac, Expr *BW,
1697 // Ivar's can only appear in interfaces, implementations (via synthesized
1698 // properties), and class extensions (via direct declaration, or synthesized
1701 // FIXME: This should really be asserting this:
1702 // (isa<ObjCCategoryDecl>(DC) &&
1703 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1704 // but unfortunately we sometimes place ivars into non-class extension
1705 // categories on error. This breaks an AST invariant, and should not be
1707 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1708 isa<ObjCCategoryDecl>(DC)) &&
1709 "Invalid ivar decl context!");
1710 // Once a new ivar is created in any of class/class-extension/implementation
1711 // decl contexts, the previously built IvarList must be rebuilt.
1712 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1714 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1715 ID = IM->getClassInterface();
1717 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1719 ID->setIvarList(nullptr);
1722 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1726 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1727 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1728 nullptr, QualType(), nullptr,
1729 ObjCIvarDecl::None, nullptr, false);
1732 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1733 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1735 switch (DC->getKind()) {
1737 case ObjCCategoryImpl:
1739 llvm_unreachable("invalid ivar container!");
1741 // Ivars can only appear in class extension categories.
1742 case ObjCCategory: {
1743 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1744 assert(CD->IsClassExtension() && "invalid container for ivar!");
1745 return CD->getClassInterface();
1748 case ObjCImplementation:
1749 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1752 return cast<ObjCInterfaceDecl>(DC);
1756 QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1757 return getType().substObjCMemberType(objectType, getDeclContext(),
1758 ObjCSubstitutionContext::Property);
1761 //===----------------------------------------------------------------------===//
1762 // ObjCAtDefsFieldDecl
1763 //===----------------------------------------------------------------------===//
1765 void ObjCAtDefsFieldDecl::anchor() { }
1768 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1769 SourceLocation StartLoc, SourceLocation IdLoc,
1770 IdentifierInfo *Id, QualType T, Expr *BW) {
1771 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1774 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1776 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1777 SourceLocation(), nullptr, QualType(),
1781 //===----------------------------------------------------------------------===//
1783 //===----------------------------------------------------------------------===//
1785 void ObjCProtocolDecl::anchor() { }
1787 ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1788 IdentifierInfo *Id, SourceLocation nameLoc,
1789 SourceLocation atStartLoc,
1790 ObjCProtocolDecl *PrevDecl)
1791 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1792 redeclarable_base(C), Data() {
1793 setPreviousDecl(PrevDecl);
1795 Data = PrevDecl->Data;
1798 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1800 SourceLocation nameLoc,
1801 SourceLocation atStartLoc,
1802 ObjCProtocolDecl *PrevDecl) {
1803 ObjCProtocolDecl *Result =
1804 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1805 Result->Data.setInt(!C.getLangOpts().Modules);
1809 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1811 ObjCProtocolDecl *Result =
1812 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1813 SourceLocation(), nullptr);
1814 Result->Data.setInt(!C.getLangOpts().Modules);
1818 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1819 ObjCProtocolDecl *PDecl = this;
1821 if (Name == getIdentifier())
1824 for (auto *I : protocols())
1825 if ((PDecl = I->lookupProtocolNamed(Name)))
1831 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1833 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1834 bool isInstance) const {
1835 ObjCMethodDecl *MethodDecl = nullptr;
1837 // If there is no definition or the definition is hidden, we don't find
1839 const ObjCProtocolDecl *Def = getDefinition();
1840 if (!Def || Def->isHidden())
1843 if ((MethodDecl = getMethod(Sel, isInstance)))
1846 for (const auto *I : protocols())
1847 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1852 void ObjCProtocolDecl::allocateDefinitionData() {
1853 assert(!Data.getPointer() && "Protocol already has a definition!");
1854 Data.setPointer(new (getASTContext()) DefinitionData);
1855 Data.getPointer()->Definition = this;
1858 void ObjCProtocolDecl::startDefinition() {
1859 allocateDefinitionData();
1861 // Update all of the declarations with a pointer to the definition.
1862 for (auto RD : redecls())
1863 RD->Data = this->Data;
1866 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1867 PropertyDeclOrder &PO) const {
1869 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1870 for (auto *Prop : PDecl->properties()) {
1871 // Insert into PM if not there already.
1872 PM.insert(std::make_pair(
1873 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1877 // Scan through protocol's protocols.
1878 for (const auto *PI : PDecl->protocols())
1879 PI->collectPropertiesToImplement(PM, PO);
1884 void ObjCProtocolDecl::collectInheritedProtocolProperties(
1885 const ObjCPropertyDecl *Property,
1886 ProtocolPropertyMap &PM) const {
1887 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1888 bool MatchFound = false;
1889 for (auto *Prop : PDecl->properties()) {
1890 if (Prop == Property)
1892 if (Prop->getIdentifier() == Property->getIdentifier()) {
1898 // Scan through protocol's protocols which did not have a matching property.
1900 for (const auto *PI : PDecl->protocols())
1901 PI->collectInheritedProtocolProperties(Property, PM);
1906 ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1907 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1908 return ObjCRTName->getMetadataName();
1913 //===----------------------------------------------------------------------===//
1915 //===----------------------------------------------------------------------===//
1917 void ObjCCategoryDecl::anchor() { }
1919 ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1920 SourceLocation ClassNameLoc,
1921 SourceLocation CategoryNameLoc,
1922 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1923 ObjCTypeParamList *typeParamList,
1924 SourceLocation IvarLBraceLoc,
1925 SourceLocation IvarRBraceLoc)
1926 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1927 ClassInterface(IDecl), TypeParamList(nullptr),
1928 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1929 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1931 setTypeParamList(typeParamList);
1934 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1935 SourceLocation AtLoc,
1936 SourceLocation ClassNameLoc,
1937 SourceLocation CategoryNameLoc,
1939 ObjCInterfaceDecl *IDecl,
1940 ObjCTypeParamList *typeParamList,
1941 SourceLocation IvarLBraceLoc,
1942 SourceLocation IvarRBraceLoc) {
1943 ObjCCategoryDecl *CatDecl =
1944 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1945 IDecl, typeParamList, IvarLBraceLoc,
1948 // Link this category into its class's category list.
1949 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1950 if (IDecl->hasDefinition()) {
1951 IDecl->setCategoryListRaw(CatDecl);
1952 if (ASTMutationListener *L = C.getASTMutationListener())
1953 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1960 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1962 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1963 SourceLocation(), SourceLocation(),
1964 nullptr, nullptr, nullptr);
1967 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1968 return getASTContext().getObjCImplementation(
1969 const_cast<ObjCCategoryDecl*>(this));
1972 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1973 getASTContext().setObjCImplementation(this, ImplD);
1976 void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1977 TypeParamList = TPL;
1980 // Set the declaration context of each of the type parameters.
1981 for (auto typeParam : *TypeParamList)
1982 typeParam->setDeclContext(this);
1986 //===----------------------------------------------------------------------===//
1987 // ObjCCategoryImplDecl
1988 //===----------------------------------------------------------------------===//
1990 void ObjCCategoryImplDecl::anchor() { }
1992 ObjCCategoryImplDecl *
1993 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1995 ObjCInterfaceDecl *ClassInterface,
1996 SourceLocation nameLoc,
1997 SourceLocation atStartLoc,
1998 SourceLocation CategoryNameLoc) {
1999 if (ClassInterface && ClassInterface->hasDefinition())
2000 ClassInterface = ClassInterface->getDefinition();
2001 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2002 atStartLoc, CategoryNameLoc);
2005 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2007 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2008 SourceLocation(), SourceLocation(),
2012 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2013 // The class interface might be NULL if we are working with invalid code.
2014 if (const ObjCInterfaceDecl *ID = getClassInterface())
2015 return ID->FindCategoryDeclaration(getIdentifier());
2020 void ObjCImplDecl::anchor() { }
2022 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2023 // FIXME: The context should be correct before we get here.
2024 property->setLexicalDeclContext(this);
2028 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2029 ASTContext &Ctx = getASTContext();
2031 if (ObjCImplementationDecl *ImplD
2032 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2034 Ctx.setObjCImplementation(IFace, ImplD);
2036 } else if (ObjCCategoryImplDecl *ImplD =
2037 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2038 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2039 Ctx.setObjCImplementation(CD, ImplD);
2042 ClassInterface = IFace;
2045 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2046 /// properties implemented in this \@implementation block and returns
2047 /// the implemented property that uses it.
2049 ObjCPropertyImplDecl *ObjCImplDecl::
2050 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2051 for (auto *PID : property_impls())
2052 if (PID->getPropertyIvarDecl() &&
2053 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2058 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2059 /// added to the list of those properties \@synthesized/\@dynamic in this
2060 /// category \@implementation block.
2062 ObjCPropertyImplDecl *ObjCImplDecl::
2063 FindPropertyImplDecl(IdentifierInfo *Id,
2064 ObjCPropertyQueryKind QueryKind) const {
2065 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2066 for (auto *PID : property_impls())
2067 // If queryKind is unknown, we return the instance property if one
2068 // exists; otherwise we return the class property.
2069 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2070 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2071 !PID->getPropertyDecl()->isClassProperty()) ||
2072 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2073 PID->getPropertyDecl()->isClassProperty()) ||
2074 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2075 !PID->getPropertyDecl()->isClassProperty()))
2078 if (PID->getPropertyDecl()->isClassProperty())
2079 ClassPropImpl = PID;
2082 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2083 // We can't find the instance property, return the class property.
2084 return ClassPropImpl;
2089 raw_ostream &clang::operator<<(raw_ostream &OS,
2090 const ObjCCategoryImplDecl &CID) {
2091 OS << CID.getName();
2095 //===----------------------------------------------------------------------===//
2096 // ObjCImplementationDecl
2097 //===----------------------------------------------------------------------===//
2099 void ObjCImplementationDecl::anchor() { }
2101 ObjCImplementationDecl *
2102 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2103 ObjCInterfaceDecl *ClassInterface,
2104 ObjCInterfaceDecl *SuperDecl,
2105 SourceLocation nameLoc,
2106 SourceLocation atStartLoc,
2107 SourceLocation superLoc,
2108 SourceLocation IvarLBraceLoc,
2109 SourceLocation IvarRBraceLoc) {
2110 if (ClassInterface && ClassInterface->hasDefinition())
2111 ClassInterface = ClassInterface->getDefinition();
2112 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2113 nameLoc, atStartLoc, superLoc,
2114 IvarLBraceLoc, IvarRBraceLoc);
2117 ObjCImplementationDecl *
2118 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2119 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2120 SourceLocation(), SourceLocation());
2123 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2124 CXXCtorInitializer ** initializers,
2125 unsigned numInitializers) {
2126 if (numInitializers > 0) {
2127 NumIvarInitializers = numInitializers;
2128 CXXCtorInitializer **ivarInitializers =
2129 new (C) CXXCtorInitializer*[NumIvarInitializers];
2130 memcpy(ivarInitializers, initializers,
2131 numInitializers * sizeof(CXXCtorInitializer*));
2132 IvarInitializers = ivarInitializers;
2136 ObjCImplementationDecl::init_const_iterator
2137 ObjCImplementationDecl::init_begin() const {
2138 return IvarInitializers.get(getASTContext().getExternalSource());
2141 raw_ostream &clang::operator<<(raw_ostream &OS,
2142 const ObjCImplementationDecl &ID) {
2147 //===----------------------------------------------------------------------===//
2148 // ObjCCompatibleAliasDecl
2149 //===----------------------------------------------------------------------===//
2151 void ObjCCompatibleAliasDecl::anchor() { }
2153 ObjCCompatibleAliasDecl *
2154 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2157 ObjCInterfaceDecl* AliasedClass) {
2158 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2161 ObjCCompatibleAliasDecl *
2162 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2163 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2167 //===----------------------------------------------------------------------===//
2169 //===----------------------------------------------------------------------===//
2171 void ObjCPropertyDecl::anchor() { }
2173 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2176 SourceLocation AtLoc,
2177 SourceLocation LParenLoc,
2179 TypeSourceInfo *TSI,
2180 PropertyControl propControl) {
2181 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2185 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2187 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2188 SourceLocation(), SourceLocation(),
2189 QualType(), nullptr, None);
2192 QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2193 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2194 ObjCSubstitutionContext::Property);
2197 //===----------------------------------------------------------------------===//
2198 // ObjCPropertyImplDecl
2199 //===----------------------------------------------------------------------===//
2201 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2203 SourceLocation atLoc,
2205 ObjCPropertyDecl *property,
2208 SourceLocation ivarLoc) {
2209 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2213 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2215 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2216 SourceLocation(), nullptr, Dynamic,
2217 nullptr, SourceLocation());
2220 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2221 SourceLocation EndLoc = getLocation();
2222 if (IvarLoc.isValid())
2225 return SourceRange(AtLoc, EndLoc);