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 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->known_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(llvm::AlignOf<ParmVarDecl *>::Alignment >=
804 llvm::AlignOf<SourceLocation>::Alignment,
805 "Alignment not sufficient for SourceLocation");
807 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
808 sizeof(SourceLocation) * SelLocs.size();
809 ParamsAndSelLocs = C.Allocate(Size);
810 std::copy(Params.begin(), Params.end(), getParams());
811 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
814 void ObjCMethodDecl::getSelectorLocs(
815 SmallVectorImpl<SourceLocation> &SelLocs) const {
816 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
817 SelLocs.push_back(getSelectorLoc(i));
820 void ObjCMethodDecl::setMethodParams(ASTContext &C,
821 ArrayRef<ParmVarDecl*> Params,
822 ArrayRef<SourceLocation> SelLocs) {
823 assert((!SelLocs.empty() || isImplicit()) &&
824 "No selector locs for non-implicit method");
826 return setParamsAndSelLocs(C, Params, llvm::None);
828 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
830 if (SelLocsKind != SelLoc_NonStandard)
831 return setParamsAndSelLocs(C, Params, llvm::None);
833 setParamsAndSelLocs(C, Params, SelLocs);
836 /// \brief A definition will return its interface declaration.
837 /// An interface declaration will return its definition.
838 /// Otherwise it will return itself.
839 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
840 ASTContext &Ctx = getASTContext();
841 ObjCMethodDecl *Redecl = nullptr;
842 if (HasRedeclaration)
843 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
847 Decl *CtxD = cast<Decl>(getDeclContext());
849 if (!CtxD->isInvalidDecl()) {
850 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
851 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
852 if (!ImplD->isInvalidDecl())
853 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
855 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
856 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
857 if (!ImplD->isInvalidDecl())
858 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
860 } else if (ObjCImplementationDecl *ImplD =
861 dyn_cast<ObjCImplementationDecl>(CtxD)) {
862 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
863 if (!IFD->isInvalidDecl())
864 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
866 } else if (ObjCCategoryImplDecl *CImplD =
867 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
868 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
869 if (!CatD->isInvalidDecl())
870 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
874 if (!Redecl && isRedeclaration()) {
875 // This is the last redeclaration, go back to the first method.
876 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
880 return Redecl ? Redecl : this;
883 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
884 Decl *CtxD = cast<Decl>(getDeclContext());
886 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
887 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
888 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
892 } else if (ObjCCategoryImplDecl *CImplD =
893 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
894 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
895 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
900 if (isRedeclaration())
901 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
907 SourceLocation ObjCMethodDecl::getLocEnd() const {
908 if (Stmt *Body = getBody())
909 return Body->getLocEnd();
913 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
914 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
915 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
918 // Check for an explicit attribute.
919 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
920 // The unfortunate necessity of mapping between enums here is due
921 // to the attributes framework.
922 switch (attr->getFamily()) {
923 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
924 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
925 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
926 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
927 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
928 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
930 Family = static_cast<unsigned>(family);
934 family = getSelector().getMethodFamily();
936 case OMF_None: break;
938 // init only has a conventional meaning for an instance method, and
939 // it has to return an object.
941 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
945 // alloc/copy/new have a conventional meaning for both class and
946 // instance methods, but they require an object return.
949 case OMF_mutableCopy:
951 if (!getReturnType()->isObjCObjectPointerType())
955 // These selectors have a conventional meaning only for instance methods.
960 case OMF_autorelease:
961 case OMF_retainCount:
963 if (!isInstanceMethod())
968 if (isInstanceMethod() || !getReturnType()->isVoidType())
972 case OMF_performSelector:
973 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
976 unsigned noParams = param_size();
977 if (noParams < 1 || noParams > 3)
980 ObjCMethodDecl::param_type_iterator it = param_type_begin();
981 QualType ArgT = (*it);
982 if (!ArgT->isObjCSelType()) {
989 if (!ArgT->isObjCIdType()) {
1000 // Cache the result.
1001 Family = static_cast<unsigned>(family);
1005 QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1006 const ObjCInterfaceDecl *OID,
1007 bool &selfIsPseudoStrong,
1008 bool &selfIsConsumed) {
1010 selfIsPseudoStrong = false;
1011 selfIsConsumed = false;
1012 if (isInstanceMethod()) {
1013 // There may be no interface context due to error in declaration
1014 // of the interface (which has been reported). Recover gracefully.
1016 selfTy = Context.getObjCInterfaceType(OID);
1017 selfTy = Context.getObjCObjectPointerType(selfTy);
1019 selfTy = Context.getObjCIdType();
1021 } else // we have a factory method.
1022 selfTy = Context.getObjCClassType();
1024 if (Context.getLangOpts().ObjCAutoRefCount) {
1025 if (isInstanceMethod()) {
1026 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
1028 // 'self' is always __strong. It's actually pseudo-strong except
1029 // in init methods (or methods labeled ns_consumes_self), though.
1031 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1032 selfTy = Context.getQualifiedType(selfTy, qs);
1034 // In addition, 'self' is const unless this is an init method.
1035 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1036 selfTy = selfTy.withConst();
1037 selfIsPseudoStrong = true;
1041 assert(isClassMethod());
1042 // 'self' is always const in class methods.
1043 selfTy = selfTy.withConst();
1044 selfIsPseudoStrong = true;
1050 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1051 const ObjCInterfaceDecl *OID) {
1052 bool selfIsPseudoStrong, selfIsConsumed;
1054 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1055 ImplicitParamDecl *self
1056 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1057 &Context.Idents.get("self"), selfTy);
1061 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1063 if (selfIsPseudoStrong)
1064 self->setARCPseudoStrong(true);
1066 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
1067 &Context.Idents.get("_cmd"),
1068 Context.getObjCSelType()));
1071 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1072 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1074 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1075 return CD->getClassInterface();
1076 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1077 return IMD->getClassInterface();
1078 if (isa<ObjCProtocolDecl>(getDeclContext()))
1080 llvm_unreachable("unknown method context");
1083 SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1084 const auto *TSI = getReturnTypeSourceInfo();
1086 return TSI->getTypeLoc().getSourceRange();
1087 return SourceRange();
1090 QualType ObjCMethodDecl::getSendResultType() const {
1091 ASTContext &Ctx = getASTContext();
1092 return getReturnType().getNonLValueExprType(Ctx)
1093 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1096 QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1097 // FIXME: Handle related result types here.
1099 return getReturnType().getNonLValueExprType(getASTContext())
1100 .substObjCMemberType(receiverType, getDeclContext(),
1101 ObjCSubstitutionContext::Result);
1104 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1105 const ObjCMethodDecl *Method,
1106 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1107 bool MovedToSuper) {
1111 // In categories look for overriden methods from protocols. A method from
1112 // category is not "overriden" since it is considered as the "same" method
1113 // (same USR) as the one from the interface.
1114 if (const ObjCCategoryDecl *
1115 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1116 // Check whether we have a matching method at this category but only if we
1117 // are at the super class level.
1119 if (ObjCMethodDecl *
1120 Overridden = Container->getMethod(Method->getSelector(),
1121 Method->isInstanceMethod(),
1122 /*AllowHidden=*/true))
1123 if (Method != Overridden) {
1124 // We found an override at this category; there is no need to look
1125 // into its protocols.
1126 Methods.push_back(Overridden);
1130 for (const auto *P : Category->protocols())
1131 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1135 // Check whether we have a matching method at this level.
1136 if (const ObjCMethodDecl *
1137 Overridden = Container->getMethod(Method->getSelector(),
1138 Method->isInstanceMethod(),
1139 /*AllowHidden=*/true))
1140 if (Method != Overridden) {
1141 // We found an override at this level; there is no need to look
1142 // into other protocols or categories.
1143 Methods.push_back(Overridden);
1147 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1148 for (const auto *P : Protocol->protocols())
1149 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1152 if (const ObjCInterfaceDecl *
1153 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1154 for (const auto *P : Interface->protocols())
1155 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1157 for (const auto *Cat : Interface->known_categories())
1158 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
1160 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1161 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1162 /*MovedToSuper=*/true);
1166 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1167 const ObjCMethodDecl *Method,
1168 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1169 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1170 /*MovedToSuper=*/false);
1173 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1174 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1175 assert(Method->isOverriding());
1177 if (const ObjCProtocolDecl *
1178 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1179 CollectOverriddenMethods(ProtD, Method, overridden);
1181 } else if (const ObjCImplDecl *
1182 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1183 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1186 // Start searching for overridden methods using the method from the
1187 // interface as starting point.
1188 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1189 Method->isInstanceMethod(),
1190 /*AllowHidden=*/true))
1192 CollectOverriddenMethods(ID, Method, overridden);
1194 } else if (const ObjCCategoryDecl *
1195 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1196 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1199 // Start searching for overridden methods using the method from the
1200 // interface as starting point.
1201 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1202 Method->isInstanceMethod(),
1203 /*AllowHidden=*/true))
1205 CollectOverriddenMethods(ID, Method, overridden);
1208 CollectOverriddenMethods(
1209 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1210 Method, overridden);
1214 void ObjCMethodDecl::getOverriddenMethods(
1215 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1216 const ObjCMethodDecl *Method = this;
1218 if (Method->isRedeclaration()) {
1219 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1220 getMethod(Method->getSelector(), Method->isInstanceMethod());
1223 if (Method->isOverriding()) {
1224 collectOverriddenMethodsSlow(Method, Overridden);
1225 assert(!Overridden.empty() &&
1226 "ObjCMethodDecl's overriding bit is not as expected");
1230 const ObjCPropertyDecl *
1231 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1232 Selector Sel = getSelector();
1233 unsigned NumArgs = Sel.getNumArgs();
1237 if (isPropertyAccessor()) {
1238 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
1239 bool IsGetter = (NumArgs == 0);
1240 bool IsInstance = isInstanceMethod();
1242 /// Local function that attempts to find a matching property within the
1243 /// given Objective-C container.
1244 auto findMatchingProperty =
1245 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
1247 for (const auto *I : Container->instance_properties()) {
1248 Selector NextSel = IsGetter ? I->getGetterName()
1249 : I->getSetterName();
1254 for (const auto *I : Container->class_properties()) {
1255 Selector NextSel = IsGetter ? I->getGetterName()
1256 : I->getSetterName();
1265 // Look in the container we were given.
1266 if (const auto *Found = findMatchingProperty(Container))
1269 // If we're in a category or extension, look in the main class.
1270 const ObjCInterfaceDecl *ClassDecl = nullptr;
1271 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1272 ClassDecl = Category->getClassInterface();
1273 if (const auto *Found = findMatchingProperty(ClassDecl))
1276 // Determine whether the container is a class.
1277 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1280 // If we have a class, check its visible extensions.
1282 for (const auto *Ext : ClassDecl->visible_extensions()) {
1283 if (Ext == Container)
1286 if (const auto *Found = findMatchingProperty(Ext))
1291 llvm_unreachable("Marked as a property accessor but no property found!");
1294 if (!CheckOverrides)
1297 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1298 OverridesTy Overrides;
1299 getOverriddenMethods(Overrides);
1300 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1302 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1309 //===----------------------------------------------------------------------===//
1310 // ObjCTypeParamDecl
1311 //===----------------------------------------------------------------------===//
1313 void ObjCTypeParamDecl::anchor() { }
1315 ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1316 ObjCTypeParamVariance variance,
1317 SourceLocation varianceLoc,
1319 SourceLocation nameLoc,
1320 IdentifierInfo *name,
1321 SourceLocation colonLoc,
1322 TypeSourceInfo *boundInfo) {
1323 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1324 nameLoc, name, colonLoc, boundInfo);
1327 ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1329 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1330 ObjCTypeParamVariance::Invariant,
1331 SourceLocation(), 0, SourceLocation(),
1332 nullptr, SourceLocation(), nullptr);
1335 SourceRange ObjCTypeParamDecl::getSourceRange() const {
1336 SourceLocation startLoc = VarianceLoc;
1337 if (startLoc.isInvalid())
1338 startLoc = getLocation();
1340 if (hasExplicitBound()) {
1341 return SourceRange(startLoc,
1342 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1345 return SourceRange(startLoc);
1348 //===----------------------------------------------------------------------===//
1349 // ObjCTypeParamList
1350 //===----------------------------------------------------------------------===//
1351 ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1352 ArrayRef<ObjCTypeParamDecl *> typeParams,
1353 SourceLocation rAngleLoc)
1354 : NumParams(typeParams.size())
1356 Brackets.Begin = lAngleLoc.getRawEncoding();
1357 Brackets.End = rAngleLoc.getRawEncoding();
1358 std::copy(typeParams.begin(), typeParams.end(), begin());
1362 ObjCTypeParamList *ObjCTypeParamList::create(
1364 SourceLocation lAngleLoc,
1365 ArrayRef<ObjCTypeParamDecl *> typeParams,
1366 SourceLocation rAngleLoc) {
1368 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1369 llvm::alignOf<ObjCTypeParamList>());
1370 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1373 void ObjCTypeParamList::gatherDefaultTypeArgs(
1374 SmallVectorImpl<QualType> &typeArgs) const {
1375 typeArgs.reserve(size());
1376 for (auto typeParam : *this)
1377 typeArgs.push_back(typeParam->getUnderlyingType());
1380 //===----------------------------------------------------------------------===//
1381 // ObjCInterfaceDecl
1382 //===----------------------------------------------------------------------===//
1384 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1386 SourceLocation atLoc,
1388 ObjCTypeParamList *typeParamList,
1389 ObjCInterfaceDecl *PrevDecl,
1390 SourceLocation ClassLoc,
1392 ObjCInterfaceDecl *Result = new (C, DC)
1393 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1395 Result->Data.setInt(!C.getLangOpts().Modules);
1396 C.getObjCInterfaceType(Result, PrevDecl);
1400 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1402 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
1408 Result->Data.setInt(!C.getLangOpts().Modules);
1412 ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1413 SourceLocation AtLoc, IdentifierInfo *Id,
1414 ObjCTypeParamList *typeParamList,
1415 SourceLocation CLoc,
1416 ObjCInterfaceDecl *PrevDecl,
1418 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1419 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(nullptr),
1421 setPreviousDecl(PrevDecl);
1423 // Copy the 'data' pointer over.
1425 Data = PrevDecl->Data;
1427 setImplicit(IsInternal);
1429 setTypeParamList(typeParamList);
1432 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1433 assert(data().ExternallyCompleted && "Class is not externally completed");
1434 data().ExternallyCompleted = false;
1435 getASTContext().getExternalSource()->CompleteType(
1436 const_cast<ObjCInterfaceDecl *>(this));
1439 void ObjCInterfaceDecl::setExternallyCompleted() {
1440 assert(getASTContext().getExternalSource() &&
1441 "Class can't be externally completed without an external source");
1442 assert(hasDefinition() &&
1443 "Forward declarations can't be externally completed");
1444 data().ExternallyCompleted = true;
1447 void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1448 // Check for a complete definition and recover if not so.
1449 if (!isThisDeclarationADefinition())
1451 data().HasDesignatedInitializers = true;
1454 bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1455 // Check for a complete definition and recover if not so.
1456 if (!isThisDeclarationADefinition())
1458 if (data().ExternallyCompleted)
1459 LoadExternalDefinition();
1461 return data().HasDesignatedInitializers;
1465 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1466 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1467 return ObjCRTName->getMetadataName();
1473 ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1474 if (ObjCInterfaceDecl *ID =
1475 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1476 return ID->getObjCRuntimeNameAsString();
1481 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1482 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1483 if (data().ExternallyCompleted)
1484 LoadExternalDefinition();
1486 return getASTContext().getObjCImplementation(
1487 const_cast<ObjCInterfaceDecl*>(Def));
1490 // FIXME: Should make sure no callers ever do this.
1494 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1495 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1499 struct SynthesizeIvarChunk {
1502 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1503 : Size(size), Ivar(ivar) {}
1506 bool operator<(const SynthesizeIvarChunk & LHS,
1507 const SynthesizeIvarChunk &RHS) {
1508 return LHS.Size < RHS.Size;
1512 /// all_declared_ivar_begin - return first ivar declared in this class,
1513 /// its extensions and its implementation. Lazily build the list on first
1516 /// Caveat: The list returned by this method reflects the current
1517 /// state of the parser. The cache will be updated for every ivar
1518 /// added by an extension or the implementation when they are
1520 /// See also ObjCIvarDecl::Create().
1521 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1522 // FIXME: Should make sure no callers ever do this.
1523 if (!hasDefinition())
1526 ObjCIvarDecl *curIvar = nullptr;
1527 if (!data().IvarList) {
1528 if (!ivar_empty()) {
1529 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1530 data().IvarList = *I; ++I;
1531 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1532 curIvar->setNextIvar(*I);
1535 for (const auto *Ext : known_extensions()) {
1536 if (!Ext->ivar_empty()) {
1537 ObjCCategoryDecl::ivar_iterator
1538 I = Ext->ivar_begin(),
1539 E = Ext->ivar_end();
1540 if (!data().IvarList) {
1541 data().IvarList = *I; ++I;
1542 curIvar = data().IvarList;
1544 for ( ;I != E; curIvar = *I, ++I)
1545 curIvar->setNextIvar(*I);
1548 data().IvarListMissingImplementation = true;
1551 // cached and complete!
1552 if (!data().IvarListMissingImplementation)
1553 return data().IvarList;
1555 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1556 data().IvarListMissingImplementation = false;
1557 if (!ImplDecl->ivar_empty()) {
1558 SmallVector<SynthesizeIvarChunk, 16> layout;
1559 for (auto *IV : ImplDecl->ivars()) {
1560 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1561 layout.push_back(SynthesizeIvarChunk(
1562 IV->getASTContext().getTypeSize(IV->getType()), IV));
1565 if (!data().IvarList)
1566 data().IvarList = IV;
1568 curIvar->setNextIvar(IV);
1572 if (!layout.empty()) {
1573 // Order synthesized ivars by their size.
1574 std::stable_sort(layout.begin(), layout.end());
1575 unsigned Ix = 0, EIx = layout.size();
1576 if (!data().IvarList) {
1577 data().IvarList = layout[0].Ivar; Ix++;
1578 curIvar = data().IvarList;
1580 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1581 curIvar->setNextIvar(layout[Ix].Ivar);
1585 return data().IvarList;
1588 /// FindCategoryDeclaration - Finds category declaration in the list of
1589 /// categories for this class and returns it. Name of the category is passed
1590 /// in 'CategoryId'. If category not found, return 0;
1593 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1594 // FIXME: Should make sure no callers ever do this.
1595 if (!hasDefinition())
1598 if (data().ExternallyCompleted)
1599 LoadExternalDefinition();
1601 for (auto *Cat : visible_categories())
1602 if (Cat->getIdentifier() == CategoryId)
1609 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1610 for (const auto *Cat : visible_categories()) {
1611 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1612 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1619 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1620 for (const auto *Cat : visible_categories()) {
1621 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1622 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1629 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1630 /// has been implemented in IDecl class, its super class or categories (if
1631 /// lookupCategory is true).
1632 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1633 bool lookupCategory,
1634 bool RHSIsQualifiedID) {
1635 if (!hasDefinition())
1638 ObjCInterfaceDecl *IDecl = this;
1639 // 1st, look up the class.
1640 for (auto *PI : IDecl->protocols()){
1641 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1643 // This is dubious and is added to be compatible with gcc. In gcc, it is
1644 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1645 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1646 // object. This IMO, should be a bug.
1647 // FIXME: Treat this as an extension, and flag this as an error when GCC
1648 // extensions are not enabled.
1649 if (RHSIsQualifiedID &&
1650 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
1654 // 2nd, look up the category.
1656 for (const auto *Cat : visible_categories()) {
1657 for (auto *PI : Cat->protocols())
1658 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1662 // 3rd, look up the super class(s)
1663 if (IDecl->getSuperClass())
1665 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1671 //===----------------------------------------------------------------------===//
1673 //===----------------------------------------------------------------------===//
1675 void ObjCIvarDecl::anchor() { }
1677 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1678 SourceLocation StartLoc,
1679 SourceLocation IdLoc, IdentifierInfo *Id,
1680 QualType T, TypeSourceInfo *TInfo,
1681 AccessControl ac, Expr *BW,
1684 // Ivar's can only appear in interfaces, implementations (via synthesized
1685 // properties), and class extensions (via direct declaration, or synthesized
1688 // FIXME: This should really be asserting this:
1689 // (isa<ObjCCategoryDecl>(DC) &&
1690 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1691 // but unfortunately we sometimes place ivars into non-class extension
1692 // categories on error. This breaks an AST invariant, and should not be
1694 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1695 isa<ObjCCategoryDecl>(DC)) &&
1696 "Invalid ivar decl context!");
1697 // Once a new ivar is created in any of class/class-extension/implementation
1698 // decl contexts, the previously built IvarList must be rebuilt.
1699 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1701 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1702 ID = IM->getClassInterface();
1704 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1706 ID->setIvarList(nullptr);
1709 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1713 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1714 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1715 nullptr, QualType(), nullptr,
1716 ObjCIvarDecl::None, nullptr, false);
1719 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1720 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1722 switch (DC->getKind()) {
1724 case ObjCCategoryImpl:
1726 llvm_unreachable("invalid ivar container!");
1728 // Ivars can only appear in class extension categories.
1729 case ObjCCategory: {
1730 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1731 assert(CD->IsClassExtension() && "invalid container for ivar!");
1732 return CD->getClassInterface();
1735 case ObjCImplementation:
1736 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1739 return cast<ObjCInterfaceDecl>(DC);
1743 QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1744 return getType().substObjCMemberType(objectType, getDeclContext(),
1745 ObjCSubstitutionContext::Property);
1748 //===----------------------------------------------------------------------===//
1749 // ObjCAtDefsFieldDecl
1750 //===----------------------------------------------------------------------===//
1752 void ObjCAtDefsFieldDecl::anchor() { }
1755 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1756 SourceLocation StartLoc, SourceLocation IdLoc,
1757 IdentifierInfo *Id, QualType T, Expr *BW) {
1758 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1761 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1763 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1764 SourceLocation(), nullptr, QualType(),
1768 //===----------------------------------------------------------------------===//
1770 //===----------------------------------------------------------------------===//
1772 void ObjCProtocolDecl::anchor() { }
1774 ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1775 IdentifierInfo *Id, SourceLocation nameLoc,
1776 SourceLocation atStartLoc,
1777 ObjCProtocolDecl *PrevDecl)
1778 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1779 redeclarable_base(C), Data() {
1780 setPreviousDecl(PrevDecl);
1782 Data = PrevDecl->Data;
1785 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1787 SourceLocation nameLoc,
1788 SourceLocation atStartLoc,
1789 ObjCProtocolDecl *PrevDecl) {
1790 ObjCProtocolDecl *Result =
1791 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1792 Result->Data.setInt(!C.getLangOpts().Modules);
1796 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1798 ObjCProtocolDecl *Result =
1799 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1800 SourceLocation(), nullptr);
1801 Result->Data.setInt(!C.getLangOpts().Modules);
1805 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1806 ObjCProtocolDecl *PDecl = this;
1808 if (Name == getIdentifier())
1811 for (auto *I : protocols())
1812 if ((PDecl = I->lookupProtocolNamed(Name)))
1818 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1820 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1821 bool isInstance) const {
1822 ObjCMethodDecl *MethodDecl = nullptr;
1824 // If there is no definition or the definition is hidden, we don't find
1826 const ObjCProtocolDecl *Def = getDefinition();
1827 if (!Def || Def->isHidden())
1830 if ((MethodDecl = getMethod(Sel, isInstance)))
1833 for (const auto *I : protocols())
1834 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1839 void ObjCProtocolDecl::allocateDefinitionData() {
1840 assert(!Data.getPointer() && "Protocol already has a definition!");
1841 Data.setPointer(new (getASTContext()) DefinitionData);
1842 Data.getPointer()->Definition = this;
1845 void ObjCProtocolDecl::startDefinition() {
1846 allocateDefinitionData();
1848 // Update all of the declarations with a pointer to the definition.
1849 for (auto RD : redecls())
1850 RD->Data = this->Data;
1853 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1854 PropertyDeclOrder &PO) const {
1856 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1857 for (auto *Prop : PDecl->properties()) {
1858 // Insert into PM if not there already.
1859 PM.insert(std::make_pair(
1860 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1864 // Scan through protocol's protocols.
1865 for (const auto *PI : PDecl->protocols())
1866 PI->collectPropertiesToImplement(PM, PO);
1871 void ObjCProtocolDecl::collectInheritedProtocolProperties(
1872 const ObjCPropertyDecl *Property,
1873 ProtocolPropertyMap &PM) const {
1874 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1875 bool MatchFound = false;
1876 for (auto *Prop : PDecl->properties()) {
1877 if (Prop == Property)
1879 if (Prop->getIdentifier() == Property->getIdentifier()) {
1885 // Scan through protocol's protocols which did not have a matching property.
1887 for (const auto *PI : PDecl->protocols())
1888 PI->collectInheritedProtocolProperties(Property, PM);
1893 ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1894 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1895 return ObjCRTName->getMetadataName();
1900 //===----------------------------------------------------------------------===//
1902 //===----------------------------------------------------------------------===//
1904 void ObjCCategoryDecl::anchor() { }
1906 ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1907 SourceLocation ClassNameLoc,
1908 SourceLocation CategoryNameLoc,
1909 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1910 ObjCTypeParamList *typeParamList,
1911 SourceLocation IvarLBraceLoc,
1912 SourceLocation IvarRBraceLoc)
1913 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1914 ClassInterface(IDecl), TypeParamList(nullptr),
1915 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1916 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1918 setTypeParamList(typeParamList);
1921 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1922 SourceLocation AtLoc,
1923 SourceLocation ClassNameLoc,
1924 SourceLocation CategoryNameLoc,
1926 ObjCInterfaceDecl *IDecl,
1927 ObjCTypeParamList *typeParamList,
1928 SourceLocation IvarLBraceLoc,
1929 SourceLocation IvarRBraceLoc) {
1930 ObjCCategoryDecl *CatDecl =
1931 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1932 IDecl, typeParamList, IvarLBraceLoc,
1935 // Link this category into its class's category list.
1936 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1937 if (IDecl->hasDefinition()) {
1938 IDecl->setCategoryListRaw(CatDecl);
1939 if (ASTMutationListener *L = C.getASTMutationListener())
1940 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1947 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1949 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1950 SourceLocation(), SourceLocation(),
1951 nullptr, nullptr, nullptr);
1954 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1955 return getASTContext().getObjCImplementation(
1956 const_cast<ObjCCategoryDecl*>(this));
1959 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1960 getASTContext().setObjCImplementation(this, ImplD);
1963 void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1964 TypeParamList = TPL;
1967 // Set the declaration context of each of the type parameters.
1968 for (auto typeParam : *TypeParamList)
1969 typeParam->setDeclContext(this);
1973 //===----------------------------------------------------------------------===//
1974 // ObjCCategoryImplDecl
1975 //===----------------------------------------------------------------------===//
1977 void ObjCCategoryImplDecl::anchor() { }
1979 ObjCCategoryImplDecl *
1980 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1982 ObjCInterfaceDecl *ClassInterface,
1983 SourceLocation nameLoc,
1984 SourceLocation atStartLoc,
1985 SourceLocation CategoryNameLoc) {
1986 if (ClassInterface && ClassInterface->hasDefinition())
1987 ClassInterface = ClassInterface->getDefinition();
1988 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1989 atStartLoc, CategoryNameLoc);
1992 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1994 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1995 SourceLocation(), SourceLocation(),
1999 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2000 // The class interface might be NULL if we are working with invalid code.
2001 if (const ObjCInterfaceDecl *ID = getClassInterface())
2002 return ID->FindCategoryDeclaration(getIdentifier());
2007 void ObjCImplDecl::anchor() { }
2009 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2010 // FIXME: The context should be correct before we get here.
2011 property->setLexicalDeclContext(this);
2015 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2016 ASTContext &Ctx = getASTContext();
2018 if (ObjCImplementationDecl *ImplD
2019 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2021 Ctx.setObjCImplementation(IFace, ImplD);
2023 } else if (ObjCCategoryImplDecl *ImplD =
2024 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2025 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2026 Ctx.setObjCImplementation(CD, ImplD);
2029 ClassInterface = IFace;
2032 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2033 /// properties implemented in this \@implementation block and returns
2034 /// the implemented property that uses it.
2036 ObjCPropertyImplDecl *ObjCImplDecl::
2037 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2038 for (auto *PID : property_impls())
2039 if (PID->getPropertyIvarDecl() &&
2040 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2045 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2046 /// added to the list of those properties \@synthesized/\@dynamic in this
2047 /// category \@implementation block.
2049 ObjCPropertyImplDecl *ObjCImplDecl::
2050 FindPropertyImplDecl(IdentifierInfo *Id,
2051 ObjCPropertyQueryKind QueryKind) const {
2052 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2053 for (auto *PID : property_impls())
2054 // If queryKind is unknown, we return the instance property if one
2055 // exists; otherwise we return the class property.
2056 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2057 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2058 !PID->getPropertyDecl()->isClassProperty()) ||
2059 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2060 PID->getPropertyDecl()->isClassProperty()) ||
2061 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2062 !PID->getPropertyDecl()->isClassProperty()))
2065 if (PID->getPropertyDecl()->isClassProperty())
2066 ClassPropImpl = PID;
2069 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2070 // We can't find the instance property, return the class property.
2071 return ClassPropImpl;
2076 raw_ostream &clang::operator<<(raw_ostream &OS,
2077 const ObjCCategoryImplDecl &CID) {
2078 OS << CID.getName();
2082 //===----------------------------------------------------------------------===//
2083 // ObjCImplementationDecl
2084 //===----------------------------------------------------------------------===//
2086 void ObjCImplementationDecl::anchor() { }
2088 ObjCImplementationDecl *
2089 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2090 ObjCInterfaceDecl *ClassInterface,
2091 ObjCInterfaceDecl *SuperDecl,
2092 SourceLocation nameLoc,
2093 SourceLocation atStartLoc,
2094 SourceLocation superLoc,
2095 SourceLocation IvarLBraceLoc,
2096 SourceLocation IvarRBraceLoc) {
2097 if (ClassInterface && ClassInterface->hasDefinition())
2098 ClassInterface = ClassInterface->getDefinition();
2099 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2100 nameLoc, atStartLoc, superLoc,
2101 IvarLBraceLoc, IvarRBraceLoc);
2104 ObjCImplementationDecl *
2105 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2106 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2107 SourceLocation(), SourceLocation());
2110 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2111 CXXCtorInitializer ** initializers,
2112 unsigned numInitializers) {
2113 if (numInitializers > 0) {
2114 NumIvarInitializers = numInitializers;
2115 CXXCtorInitializer **ivarInitializers =
2116 new (C) CXXCtorInitializer*[NumIvarInitializers];
2117 memcpy(ivarInitializers, initializers,
2118 numInitializers * sizeof(CXXCtorInitializer*));
2119 IvarInitializers = ivarInitializers;
2123 ObjCImplementationDecl::init_const_iterator
2124 ObjCImplementationDecl::init_begin() const {
2125 return IvarInitializers.get(getASTContext().getExternalSource());
2128 raw_ostream &clang::operator<<(raw_ostream &OS,
2129 const ObjCImplementationDecl &ID) {
2134 //===----------------------------------------------------------------------===//
2135 // ObjCCompatibleAliasDecl
2136 //===----------------------------------------------------------------------===//
2138 void ObjCCompatibleAliasDecl::anchor() { }
2140 ObjCCompatibleAliasDecl *
2141 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2144 ObjCInterfaceDecl* AliasedClass) {
2145 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2148 ObjCCompatibleAliasDecl *
2149 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2150 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2154 //===----------------------------------------------------------------------===//
2156 //===----------------------------------------------------------------------===//
2158 void ObjCPropertyDecl::anchor() { }
2160 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2163 SourceLocation AtLoc,
2164 SourceLocation LParenLoc,
2166 TypeSourceInfo *TSI,
2167 PropertyControl propControl) {
2168 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2172 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2174 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2175 SourceLocation(), SourceLocation(),
2176 QualType(), nullptr, None);
2179 QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2180 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2181 ObjCSubstitutionContext::Property);
2184 //===----------------------------------------------------------------------===//
2185 // ObjCPropertyImplDecl
2186 //===----------------------------------------------------------------------===//
2188 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2190 SourceLocation atLoc,
2192 ObjCPropertyDecl *property,
2195 SourceLocation ivarLoc) {
2196 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2200 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2202 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2203 SourceLocation(), nullptr, Dynamic,
2204 nullptr, SourceLocation());
2207 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2208 SourceLocation EndLoc = getLocation();
2209 if (IvarLoc.isValid())
2212 return SourceRange(AtLoc, EndLoc);