1 //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Objective-C related Decl classes.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Stmt.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
29 if (Elts == 0) return; // Setting to an empty list is a noop.
32 List = new (Ctx) void*[Elts];
34 memcpy(List, InList, sizeof(void*)*Elts);
37 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
47 //===----------------------------------------------------------------------===//
49 //===----------------------------------------------------------------------===//
51 void ObjCContainerDecl::anchor() { }
53 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
56 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
57 lookup_const_result R = lookup(Id);
58 for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59 Ivar != IvarEnd; ++Ivar) {
60 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
66 // Get the local instance/class method declared in this interface.
68 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
70 // If this context is a hidden protocol definition, don't find any
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
74 if (Def->isHidden() && !AllowHidden)
78 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
86 lookup_const_result R = lookup(Sel);
87 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88 Meth != MethEnd; ++Meth) {
89 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
90 if (MD && MD->isInstanceMethod() == isInstance)
96 /// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
97 /// method was 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' property.
99 /// This is because, user must provide a setter method for the category's 'readwrite'
102 ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
103 Selector Sel = Property->getSetterName();
104 lookup_const_result R = lookup(Sel);
105 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
115 for (ObjCInterfaceDecl::visible_categories_iterator
116 Cat = ID->visible_categories_begin(),
117 CatEnd = ID->visible_categories_end();
120 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
121 if (!MD->isImplicit())
123 if (Cat->IsClassExtension())
125 // Also search through the categories looking for a 'readwrite' declaration
126 // of this property. If one found, presumably a setter will be provided
127 // (properties declared in categories will not get auto-synthesized).
128 for (ObjCContainerDecl::prop_iterator P = Cat->prop_begin(),
129 E = Cat->prop_end(); P != E; ++P)
130 if (P->getIdentifier() == Property->getIdentifier()) {
131 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
137 // Also look into protocols, for a user declared instance method.
138 for (ObjCInterfaceDecl::all_protocol_iterator P =
139 ID->all_referenced_protocol_begin(),
140 PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
141 ObjCProtocolDecl *Proto = (*P);
142 if (Proto->HasUserDeclaredSetterMethod(Property))
145 // And in its super class.
146 ObjCInterfaceDecl *OSC = ID->getSuperClass();
148 if (OSC->HasUserDeclaredSetterMethod(Property))
150 OSC = OSC->getSuperClass();
153 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
154 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
155 E = PD->protocol_end(); PI != E; ++PI) {
156 if ((*PI)->HasUserDeclaredSetterMethod(Property))
163 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
164 IdentifierInfo *propertyID) {
165 // If this context is a hidden protocol definition, don't find any
167 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
168 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
173 DeclContext::lookup_const_result R = DC->lookup(propertyID);
174 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
176 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
183 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
184 SmallString<128> ivarName;
186 llvm::raw_svector_ostream os(ivarName);
187 os << '_' << getIdentifier()->getName();
189 return &Ctx.Idents.get(ivarName.str());
192 /// FindPropertyDeclaration - Finds declaration of the property given its name
193 /// in 'PropertyId' and returns it. It returns 0, if not found.
195 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
196 // Don't find properties within hidden protocol definitions.
197 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
198 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
203 if (ObjCPropertyDecl *PD =
204 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
210 case Decl::ObjCProtocol: {
211 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
212 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
213 E = PID->protocol_end(); I != E; ++I)
214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
218 case Decl::ObjCInterface: {
219 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
220 // Look through categories (but not extensions).
221 for (ObjCInterfaceDecl::visible_categories_iterator
222 Cat = OID->visible_categories_begin(),
223 CatEnd = OID->visible_categories_end();
224 Cat != CatEnd; ++Cat) {
225 if (!Cat->IsClassExtension())
226 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
230 // Look through protocols.
231 for (ObjCInterfaceDecl::all_protocol_iterator
232 I = OID->all_referenced_protocol_begin(),
233 E = OID->all_referenced_protocol_end(); I != E; ++I)
234 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
237 // Finally, check the super class.
238 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
239 return superClass->FindPropertyDeclaration(PropertyId);
242 case Decl::ObjCCategory: {
243 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
244 // Look through protocols.
245 if (!OCD->IsClassExtension())
246 for (ObjCCategoryDecl::protocol_iterator
247 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
248 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
257 void ObjCInterfaceDecl::anchor() { }
259 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
260 /// with name 'PropertyId' in the primary class; including those in protocols
261 /// (direct or indirect) used by the primary class.
264 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
265 IdentifierInfo *PropertyId) const {
266 // FIXME: Should make sure no callers ever do this.
267 if (!hasDefinition())
270 if (data().ExternallyCompleted)
271 LoadExternalDefinition();
273 if (ObjCPropertyDecl *PD =
274 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
277 // Look through protocols.
278 for (ObjCInterfaceDecl::all_protocol_iterator
279 I = all_referenced_protocol_begin(),
280 E = all_referenced_protocol_end(); I != E; ++I)
281 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
287 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
288 PropertyDeclOrder &PO) const {
289 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
290 E = prop_end(); P != E; ++P) {
291 ObjCPropertyDecl *Prop = *P;
292 PM[Prop->getIdentifier()] = Prop;
295 for (ObjCInterfaceDecl::all_protocol_iterator
296 PI = all_referenced_protocol_begin(),
297 E = all_referenced_protocol_end(); PI != E; ++PI)
298 (*PI)->collectPropertiesToImplement(PM, PO);
299 // Note, the properties declared only in class extensions are still copied
300 // into the main @interface's property list, and therefore we don't
301 // explicitly, have to search class extension properties.
304 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
305 const ObjCInterfaceDecl *Class = this;
307 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
309 Class = Class->getSuperClass();
314 const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
315 const ObjCInterfaceDecl *Class = this;
317 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
319 Class = Class->getSuperClass();
324 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
325 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
328 if (data().ExternallyCompleted)
329 LoadExternalDefinition();
331 if (data().AllReferencedProtocols.empty() &&
332 data().ReferencedProtocols.empty()) {
333 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
337 // Check for duplicate protocol in class's protocol list.
338 // This is O(n*m). But it is extremely rare and number of protocols in
339 // class or its extension are very few.
340 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
341 for (unsigned i = 0; i < ExtNum; i++) {
342 bool protocolExists = false;
343 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
344 for (all_protocol_iterator
345 p = all_referenced_protocol_begin(),
346 e = all_referenced_protocol_end(); p != e; ++p) {
347 ObjCProtocolDecl *Proto = (*p);
348 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
349 protocolExists = true;
353 // Do we want to warn on a protocol in extension class which
354 // already exist in the class? Probably not.
356 ProtocolRefs.push_back(ProtoInExtension);
359 if (ProtocolRefs.empty())
362 // Merge ProtocolRefs into class's protocol list;
363 for (all_protocol_iterator p = all_referenced_protocol_begin(),
364 e = all_referenced_protocol_end(); p != e; ++p) {
365 ProtocolRefs.push_back(*p);
368 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
371 void ObjCInterfaceDecl::allocateDefinitionData() {
372 assert(!hasDefinition() && "ObjC class already has a definition");
373 Data.setPointer(new (getASTContext()) DefinitionData());
374 Data.getPointer()->Definition = this;
376 // Make the type point at the definition, now that we have one.
378 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
381 void ObjCInterfaceDecl::startDefinition() {
382 allocateDefinitionData();
384 // Update all of the declarations with a pointer to the definition.
385 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
392 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
393 ObjCInterfaceDecl *&clsDeclared) {
394 // FIXME: Should make sure no callers ever do this.
395 if (!hasDefinition())
398 if (data().ExternallyCompleted)
399 LoadExternalDefinition();
401 ObjCInterfaceDecl* ClassDecl = this;
402 while (ClassDecl != NULL) {
403 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
404 clsDeclared = ClassDecl;
408 for (ObjCInterfaceDecl::visible_extensions_iterator
409 Ext = ClassDecl->visible_extensions_begin(),
410 ExtEnd = ClassDecl->visible_extensions_end();
411 Ext != ExtEnd; ++Ext) {
412 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
413 clsDeclared = ClassDecl;
418 ClassDecl = ClassDecl->getSuperClass();
423 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
424 /// class whose name is passed as argument. If it is not one of the super classes
425 /// the it returns NULL.
426 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
427 const IdentifierInfo*ICName) {
428 // FIXME: Should make sure no callers ever do this.
429 if (!hasDefinition())
432 if (data().ExternallyCompleted)
433 LoadExternalDefinition();
435 ObjCInterfaceDecl* ClassDecl = this;
436 while (ClassDecl != NULL) {
437 if (ClassDecl->getIdentifier() == ICName)
439 ClassDecl = ClassDecl->getSuperClass();
445 ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
446 for (ObjCInterfaceDecl::all_protocol_iterator P =
447 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
449 if ((*P)->lookupProtocolNamed(Name))
451 ObjCInterfaceDecl *SuperClass = getSuperClass();
452 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
455 /// lookupMethod - This method returns an instance/class method by looking in
456 /// the class, its categories, and its super classes (using a linear search).
457 /// When argument category "C" is specified, any implicit method found
458 /// in this category is ignored.
459 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
461 bool shallowCategoryLookup,
462 const ObjCCategoryDecl *C) const {
463 // FIXME: Should make sure no callers ever do this.
464 if (!hasDefinition())
467 const ObjCInterfaceDecl* ClassDecl = this;
468 ObjCMethodDecl *MethodDecl = 0;
470 if (data().ExternallyCompleted)
471 LoadExternalDefinition();
473 while (ClassDecl != NULL) {
474 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
477 // Didn't find one yet - look through protocols.
478 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
479 E = ClassDecl->protocol_end();
481 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
484 // Didn't find one yet - now look through categories.
485 for (ObjCInterfaceDecl::visible_categories_iterator
486 Cat = ClassDecl->visible_categories_begin(),
487 CatEnd = ClassDecl->visible_categories_end();
488 Cat != CatEnd; ++Cat) {
489 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
490 if (C != (*Cat) || !MethodDecl->isImplicit())
493 if (!shallowCategoryLookup) {
494 // Didn't find one yet - look through protocols.
495 const ObjCList<ObjCProtocolDecl> &Protocols =
496 Cat->getReferencedProtocols();
497 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
498 E = Protocols.end(); I != E; ++I)
499 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
500 if (C != (*Cat) || !MethodDecl->isImplicit())
505 ClassDecl = ClassDecl->getSuperClass();
510 // Will search "local" class/category implementations for a method decl.
511 // If failed, then we search in class's root for an instance method.
512 // Returns 0 if no method is found.
513 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
515 bool Instance) const {
516 // FIXME: Should make sure no callers ever do this.
517 if (!hasDefinition())
520 if (data().ExternallyCompleted)
521 LoadExternalDefinition();
523 ObjCMethodDecl *Method = 0;
524 if (ObjCImplementationDecl *ImpDecl = getImplementation())
525 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
526 : ImpDecl->getClassMethod(Sel);
528 // Look through local category implementations associated with the class.
530 Method = Instance ? getCategoryInstanceMethod(Sel)
531 : getCategoryClassMethod(Sel);
533 // Before we give up, check if the selector is an instance method.
534 // But only in the root. This matches gcc's behavior and what the
536 if (!Instance && !Method && !getSuperClass()) {
537 Method = lookupInstanceMethod(Sel);
538 // Look through local category implementations associated
539 // with the root class.
541 Method = lookupPrivateMethod(Sel, true);
544 if (!Method && getSuperClass())
545 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
549 //===----------------------------------------------------------------------===//
551 //===----------------------------------------------------------------------===//
553 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
554 SourceLocation beginLoc,
555 SourceLocation endLoc,
556 Selector SelInfo, QualType T,
557 TypeSourceInfo *ResultTInfo,
558 DeclContext *contextDecl,
561 bool isPropertyAccessor,
562 bool isImplicitlyDeclared,
564 ImplementationControl impControl,
565 bool HasRelatedResultType) {
566 return new (C) ObjCMethodDecl(beginLoc, endLoc,
567 SelInfo, T, ResultTInfo, contextDecl,
568 isInstance, isVariadic, isPropertyAccessor,
569 isImplicitlyDeclared, isDefined,
571 HasRelatedResultType);
574 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
575 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
576 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
577 Selector(), QualType(), 0, 0);
580 Stmt *ObjCMethodDecl::getBody() const {
581 return Body.get(getASTContext().getExternalSource());
584 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
586 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
587 IsRedeclaration = true;
588 PrevMethod->HasRedeclaration = true;
591 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
592 ArrayRef<ParmVarDecl*> Params,
593 ArrayRef<SourceLocation> SelLocs) {
594 ParamsAndSelLocs = 0;
595 NumParams = Params.size();
596 if (Params.empty() && SelLocs.empty())
599 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
600 sizeof(SourceLocation) * SelLocs.size();
601 ParamsAndSelLocs = C.Allocate(Size);
602 std::copy(Params.begin(), Params.end(), getParams());
603 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
606 void ObjCMethodDecl::getSelectorLocs(
607 SmallVectorImpl<SourceLocation> &SelLocs) const {
608 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
609 SelLocs.push_back(getSelectorLoc(i));
612 void ObjCMethodDecl::setMethodParams(ASTContext &C,
613 ArrayRef<ParmVarDecl*> Params,
614 ArrayRef<SourceLocation> SelLocs) {
615 assert((!SelLocs.empty() || isImplicit()) &&
616 "No selector locs for non-implicit method");
618 return setParamsAndSelLocs(C, Params, llvm::None);
620 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
622 if (SelLocsKind != SelLoc_NonStandard)
623 return setParamsAndSelLocs(C, Params, llvm::None);
625 setParamsAndSelLocs(C, Params, SelLocs);
628 /// \brief A definition will return its interface declaration.
629 /// An interface declaration will return its definition.
630 /// Otherwise it will return itself.
631 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
632 ASTContext &Ctx = getASTContext();
633 ObjCMethodDecl *Redecl = 0;
634 if (HasRedeclaration)
635 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
639 Decl *CtxD = cast<Decl>(getDeclContext());
641 if (!CtxD->isInvalidDecl()) {
642 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
643 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
644 if (!ImplD->isInvalidDecl())
645 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
647 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
648 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
649 if (!ImplD->isInvalidDecl())
650 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
652 } else if (ObjCImplementationDecl *ImplD =
653 dyn_cast<ObjCImplementationDecl>(CtxD)) {
654 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
655 if (!IFD->isInvalidDecl())
656 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
658 } else if (ObjCCategoryImplDecl *CImplD =
659 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
660 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
661 if (!CatD->isInvalidDecl())
662 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
666 if (!Redecl && isRedeclaration()) {
667 // This is the last redeclaration, go back to the first method.
668 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
672 return Redecl ? Redecl : this;
675 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
676 Decl *CtxD = cast<Decl>(getDeclContext());
678 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
679 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
680 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
684 } else if (ObjCCategoryImplDecl *CImplD =
685 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
686 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
687 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
692 if (isRedeclaration())
693 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
699 SourceLocation ObjCMethodDecl::getLocEnd() const {
700 if (Stmt *Body = getBody())
701 return Body->getLocEnd();
705 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
706 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
707 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
710 // Check for an explicit attribute.
711 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
712 // The unfortunate necessity of mapping between enums here is due
713 // to the attributes framework.
714 switch (attr->getFamily()) {
715 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
716 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
717 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
718 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
719 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
720 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
722 Family = static_cast<unsigned>(family);
726 family = getSelector().getMethodFamily();
728 case OMF_None: break;
730 // init only has a conventional meaning for an instance method, and
731 // it has to return an object.
733 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
737 // alloc/copy/new have a conventional meaning for both class and
738 // instance methods, but they require an object return.
741 case OMF_mutableCopy:
743 if (!getResultType()->isObjCObjectPointerType())
747 // These selectors have a conventional meaning only for instance methods.
752 case OMF_autorelease:
753 case OMF_retainCount:
755 if (!isInstanceMethod())
759 case OMF_performSelector:
760 if (!isInstanceMethod() ||
761 !getResultType()->isObjCIdType())
764 unsigned noParams = param_size();
765 if (noParams < 1 || noParams > 3)
768 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
769 QualType ArgT = (*it);
770 if (!ArgT->isObjCSelType()) {
777 if (!ArgT->isObjCIdType()) {
789 Family = static_cast<unsigned>(family);
793 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
794 const ObjCInterfaceDecl *OID) {
796 if (isInstanceMethod()) {
797 // There may be no interface context due to error in declaration
798 // of the interface (which has been reported). Recover gracefully.
800 selfTy = Context.getObjCInterfaceType(OID);
801 selfTy = Context.getObjCObjectPointerType(selfTy);
803 selfTy = Context.getObjCIdType();
805 } else // we have a factory method.
806 selfTy = Context.getObjCClassType();
808 bool selfIsPseudoStrong = false;
809 bool selfIsConsumed = false;
811 if (Context.getLangOpts().ObjCAutoRefCount) {
812 if (isInstanceMethod()) {
813 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
815 // 'self' is always __strong. It's actually pseudo-strong except
816 // in init methods (or methods labeled ns_consumes_self), though.
818 qs.setObjCLifetime(Qualifiers::OCL_Strong);
819 selfTy = Context.getQualifiedType(selfTy, qs);
821 // In addition, 'self' is const unless this is an init method.
822 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
823 selfTy = selfTy.withConst();
824 selfIsPseudoStrong = true;
828 assert(isClassMethod());
829 // 'self' is always const in class methods.
830 selfTy = selfTy.withConst();
831 selfIsPseudoStrong = true;
835 ImplicitParamDecl *self
836 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
837 &Context.Idents.get("self"), selfTy);
841 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
843 if (selfIsPseudoStrong)
844 self->setARCPseudoStrong(true);
846 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
847 &Context.Idents.get("_cmd"),
848 Context.getObjCSelType()));
851 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
852 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
854 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
855 return CD->getClassInterface();
856 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
857 return IMD->getClassInterface();
859 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
860 llvm_unreachable("unknown method context");
863 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
864 const ObjCMethodDecl *Method,
865 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
870 // In categories look for overriden methods from protocols. A method from
871 // category is not "overriden" since it is considered as the "same" method
872 // (same USR) as the one from the interface.
873 if (const ObjCCategoryDecl *
874 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
875 // Check whether we have a matching method at this category but only if we
876 // are at the super class level.
879 Overridden = Container->getMethod(Method->getSelector(),
880 Method->isInstanceMethod(),
881 /*AllowHidden=*/true))
882 if (Method != Overridden) {
883 // We found an override at this category; there is no need to look
884 // into its protocols.
885 Methods.push_back(Overridden);
889 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
890 PEnd = Category->protocol_end();
892 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
896 // Check whether we have a matching method at this level.
897 if (const ObjCMethodDecl *
898 Overridden = Container->getMethod(Method->getSelector(),
899 Method->isInstanceMethod(),
900 /*AllowHidden=*/true))
901 if (Method != Overridden) {
902 // We found an override at this level; there is no need to look
903 // into other protocols or categories.
904 Methods.push_back(Overridden);
908 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
909 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
910 PEnd = Protocol->protocol_end();
912 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
915 if (const ObjCInterfaceDecl *
916 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
917 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
918 PEnd = Interface->protocol_end();
920 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
922 for (ObjCInterfaceDecl::known_categories_iterator
923 Cat = Interface->known_categories_begin(),
924 CatEnd = Interface->known_categories_end();
925 Cat != CatEnd; ++Cat) {
926 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
930 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
931 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
932 /*MovedToSuper=*/true);
936 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
937 const ObjCMethodDecl *Method,
938 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
939 CollectOverriddenMethodsRecurse(Container, Method, Methods,
940 /*MovedToSuper=*/false);
943 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
944 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
945 assert(Method->isOverriding());
947 if (const ObjCProtocolDecl *
948 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
949 CollectOverriddenMethods(ProtD, Method, overridden);
951 } else if (const ObjCImplDecl *
952 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
953 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
956 // Start searching for overridden methods using the method from the
957 // interface as starting point.
958 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
959 Method->isInstanceMethod(),
960 /*AllowHidden=*/true))
962 CollectOverriddenMethods(ID, Method, overridden);
964 } else if (const ObjCCategoryDecl *
965 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
966 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
969 // Start searching for overridden methods using the method from the
970 // interface as starting point.
971 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
972 Method->isInstanceMethod(),
973 /*AllowHidden=*/true))
975 CollectOverriddenMethods(ID, Method, overridden);
978 CollectOverriddenMethods(
979 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
984 void ObjCMethodDecl::getOverriddenMethods(
985 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
986 const ObjCMethodDecl *Method = this;
988 if (Method->isRedeclaration()) {
989 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
990 getMethod(Method->getSelector(), Method->isInstanceMethod());
993 if (Method->isOverriding()) {
994 collectOverriddenMethodsSlow(Method, Overridden);
995 assert(!Overridden.empty() &&
996 "ObjCMethodDecl's overriding bit is not as expected");
1000 const ObjCPropertyDecl *
1001 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1002 Selector Sel = getSelector();
1003 unsigned NumArgs = Sel.getNumArgs();
1007 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
1010 if (isPropertyAccessor()) {
1011 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
1012 // If container is class extension, find its primary class.
1013 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1014 if (CatDecl->IsClassExtension())
1015 Container = CatDecl->getClassInterface();
1017 bool IsGetter = (NumArgs == 0);
1019 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1020 E = Container->prop_end();
1022 Selector NextSel = IsGetter ? (*I)->getGetterName()
1023 : (*I)->getSetterName();
1028 llvm_unreachable("Marked as a property accessor but no property found!");
1031 if (!CheckOverrides)
1034 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1035 OverridesTy Overrides;
1036 getOverriddenMethods(Overrides);
1037 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1039 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1047 //===----------------------------------------------------------------------===//
1048 // ObjCInterfaceDecl
1049 //===----------------------------------------------------------------------===//
1051 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1053 SourceLocation atLoc,
1055 ObjCInterfaceDecl *PrevDecl,
1056 SourceLocation ClassLoc,
1058 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
1059 PrevDecl, isInternal);
1060 Result->Data.setInt(!C.getLangOpts().Modules);
1061 C.getObjCInterfaceType(Result, PrevDecl);
1065 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1067 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1068 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1069 0, SourceLocation(),
1071 Result->Data.setInt(!C.getLangOpts().Modules);
1076 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
1077 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1079 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
1080 TypeForDecl(0), Data()
1082 setPreviousDecl(PrevDecl);
1084 // Copy the 'data' pointer over.
1086 Data = PrevDecl->Data;
1088 setImplicit(isInternal);
1091 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1092 assert(data().ExternallyCompleted && "Class is not externally completed");
1093 data().ExternallyCompleted = false;
1094 getASTContext().getExternalSource()->CompleteType(
1095 const_cast<ObjCInterfaceDecl *>(this));
1098 void ObjCInterfaceDecl::setExternallyCompleted() {
1099 assert(getASTContext().getExternalSource() &&
1100 "Class can't be externally completed without an external source");
1101 assert(hasDefinition() &&
1102 "Forward declarations can't be externally completed");
1103 data().ExternallyCompleted = true;
1106 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1107 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1108 if (data().ExternallyCompleted)
1109 LoadExternalDefinition();
1111 return getASTContext().getObjCImplementation(
1112 const_cast<ObjCInterfaceDecl*>(Def));
1115 // FIXME: Should make sure no callers ever do this.
1119 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1120 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1124 struct SynthesizeIvarChunk {
1127 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1128 : Size(size), Ivar(ivar) {}
1131 bool operator<(const SynthesizeIvarChunk & LHS,
1132 const SynthesizeIvarChunk &RHS) {
1133 return LHS.Size < RHS.Size;
1137 /// all_declared_ivar_begin - return first ivar declared in this class,
1138 /// its extensions and its implementation. Lazily build the list on first
1141 /// Caveat: The list returned by this method reflects the current
1142 /// state of the parser. The cache will be updated for every ivar
1143 /// added by an extension or the implementation when they are
1145 /// See also ObjCIvarDecl::Create().
1146 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1147 // FIXME: Should make sure no callers ever do this.
1148 if (!hasDefinition())
1151 ObjCIvarDecl *curIvar = 0;
1152 if (!data().IvarList) {
1153 if (!ivar_empty()) {
1154 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1155 data().IvarList = *I; ++I;
1156 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1157 curIvar->setNextIvar(*I);
1160 for (ObjCInterfaceDecl::known_extensions_iterator
1161 Ext = known_extensions_begin(),
1162 ExtEnd = known_extensions_end();
1163 Ext != ExtEnd; ++Ext) {
1164 if (!Ext->ivar_empty()) {
1165 ObjCCategoryDecl::ivar_iterator
1166 I = Ext->ivar_begin(),
1167 E = Ext->ivar_end();
1168 if (!data().IvarList) {
1169 data().IvarList = *I; ++I;
1170 curIvar = data().IvarList;
1172 for ( ;I != E; curIvar = *I, ++I)
1173 curIvar->setNextIvar(*I);
1176 data().IvarListMissingImplementation = true;
1179 // cached and complete!
1180 if (!data().IvarListMissingImplementation)
1181 return data().IvarList;
1183 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1184 data().IvarListMissingImplementation = false;
1185 if (!ImplDecl->ivar_empty()) {
1186 SmallVector<SynthesizeIvarChunk, 16> layout;
1187 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1188 E = ImplDecl->ivar_end(); I != E; ++I) {
1189 ObjCIvarDecl *IV = *I;
1190 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1191 layout.push_back(SynthesizeIvarChunk(
1192 IV->getASTContext().getTypeSize(IV->getType()), IV));
1195 if (!data().IvarList)
1196 data().IvarList = *I;
1198 curIvar->setNextIvar(*I);
1202 if (!layout.empty()) {
1203 // Order synthesized ivars by their size.
1204 std::stable_sort(layout.begin(), layout.end());
1205 unsigned Ix = 0, EIx = layout.size();
1206 if (!data().IvarList) {
1207 data().IvarList = layout[0].Ivar; Ix++;
1208 curIvar = data().IvarList;
1210 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1211 curIvar->setNextIvar(layout[Ix].Ivar);
1215 return data().IvarList;
1218 /// FindCategoryDeclaration - Finds category declaration in the list of
1219 /// categories for this class and returns it. Name of the category is passed
1220 /// in 'CategoryId'. If category not found, return 0;
1223 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1224 // FIXME: Should make sure no callers ever do this.
1225 if (!hasDefinition())
1228 if (data().ExternallyCompleted)
1229 LoadExternalDefinition();
1231 for (visible_categories_iterator Cat = visible_categories_begin(),
1232 CatEnd = visible_categories_end();
1235 if (Cat->getIdentifier() == CategoryId)
1243 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1244 for (visible_categories_iterator Cat = visible_categories_begin(),
1245 CatEnd = visible_categories_end();
1248 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1249 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1256 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1257 for (visible_categories_iterator Cat = visible_categories_begin(),
1258 CatEnd = visible_categories_end();
1261 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1262 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1269 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1270 /// has been implemented in IDecl class, its super class or categories (if
1271 /// lookupCategory is true).
1272 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1273 bool lookupCategory,
1274 bool RHSIsQualifiedID) {
1275 if (!hasDefinition())
1278 ObjCInterfaceDecl *IDecl = this;
1279 // 1st, look up the class.
1280 for (ObjCInterfaceDecl::protocol_iterator
1281 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
1282 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1284 // This is dubious and is added to be compatible with gcc. In gcc, it is
1285 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1286 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1287 // object. This IMO, should be a bug.
1288 // FIXME: Treat this as an extension, and flag this as an error when GCC
1289 // extensions are not enabled.
1290 if (RHSIsQualifiedID &&
1291 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1295 // 2nd, look up the category.
1297 for (visible_categories_iterator Cat = visible_categories_begin(),
1298 CatEnd = visible_categories_end();
1301 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1302 E = Cat->protocol_end();
1304 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1308 // 3rd, look up the super class(s)
1309 if (IDecl->getSuperClass())
1311 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1317 //===----------------------------------------------------------------------===//
1319 //===----------------------------------------------------------------------===//
1321 void ObjCIvarDecl::anchor() { }
1323 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1324 SourceLocation StartLoc,
1325 SourceLocation IdLoc, IdentifierInfo *Id,
1326 QualType T, TypeSourceInfo *TInfo,
1327 AccessControl ac, Expr *BW,
1329 bool backingIvarReferencedInAccessor) {
1331 // Ivar's can only appear in interfaces, implementations (via synthesized
1332 // properties), and class extensions (via direct declaration, or synthesized
1335 // FIXME: This should really be asserting this:
1336 // (isa<ObjCCategoryDecl>(DC) &&
1337 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1338 // but unfortunately we sometimes place ivars into non-class extension
1339 // categories on error. This breaks an AST invariant, and should not be
1341 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1342 isa<ObjCCategoryDecl>(DC)) &&
1343 "Invalid ivar decl context!");
1344 // Once a new ivar is created in any of class/class-extension/implementation
1345 // decl contexts, the previously built IvarList must be rebuilt.
1346 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1348 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1349 ID = IM->getClassInterface();
1351 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1356 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1357 ac, BW, synthesized, backingIvarReferencedInAccessor);
1360 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1361 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1362 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1363 QualType(), 0, ObjCIvarDecl::None, 0, false, false);
1366 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1367 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1369 switch (DC->getKind()) {
1371 case ObjCCategoryImpl:
1373 llvm_unreachable("invalid ivar container!");
1375 // Ivars can only appear in class extension categories.
1376 case ObjCCategory: {
1377 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1378 assert(CD->IsClassExtension() && "invalid container for ivar!");
1379 return CD->getClassInterface();
1382 case ObjCImplementation:
1383 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1386 return cast<ObjCInterfaceDecl>(DC);
1390 //===----------------------------------------------------------------------===//
1391 // ObjCAtDefsFieldDecl
1392 //===----------------------------------------------------------------------===//
1394 void ObjCAtDefsFieldDecl::anchor() { }
1397 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1398 SourceLocation StartLoc, SourceLocation IdLoc,
1399 IdentifierInfo *Id, QualType T, Expr *BW) {
1400 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1403 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1405 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1406 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1410 //===----------------------------------------------------------------------===//
1412 //===----------------------------------------------------------------------===//
1414 void ObjCProtocolDecl::anchor() { }
1416 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1417 SourceLocation nameLoc,
1418 SourceLocation atStartLoc,
1419 ObjCProtocolDecl *PrevDecl)
1420 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1422 setPreviousDecl(PrevDecl);
1424 Data = PrevDecl->Data;
1427 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1429 SourceLocation nameLoc,
1430 SourceLocation atStartLoc,
1431 ObjCProtocolDecl *PrevDecl) {
1432 ObjCProtocolDecl *Result
1433 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1434 Result->Data.setInt(!C.getLangOpts().Modules);
1438 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1440 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1441 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1442 SourceLocation(), 0);
1443 Result->Data.setInt(!C.getLangOpts().Modules);
1447 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1448 ObjCProtocolDecl *PDecl = this;
1450 if (Name == getIdentifier())
1453 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1454 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1460 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1462 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1463 bool isInstance) const {
1464 ObjCMethodDecl *MethodDecl = NULL;
1466 // If there is no definition or the definition is hidden, we don't find
1468 const ObjCProtocolDecl *Def = getDefinition();
1469 if (!Def || Def->isHidden())
1472 if ((MethodDecl = getMethod(Sel, isInstance)))
1475 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1476 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1481 void ObjCProtocolDecl::allocateDefinitionData() {
1482 assert(!Data.getPointer() && "Protocol already has a definition!");
1483 Data.setPointer(new (getASTContext()) DefinitionData);
1484 Data.getPointer()->Definition = this;
1487 void ObjCProtocolDecl::startDefinition() {
1488 allocateDefinitionData();
1490 // Update all of the declarations with a pointer to the definition.
1491 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1493 RD->Data = this->Data;
1496 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1497 PropertyDeclOrder &PO) const {
1499 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1500 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1501 E = PDecl->prop_end(); P != E; ++P) {
1502 ObjCPropertyDecl *Prop = *P;
1503 // Insert into PM if not there already.
1504 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1507 // Scan through protocol's protocols.
1508 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1509 E = PDecl->protocol_end(); PI != E; ++PI)
1510 (*PI)->collectPropertiesToImplement(PM, PO);
1515 void ObjCProtocolDecl::collectInheritedProtocolProperties(
1516 const ObjCPropertyDecl *Property,
1517 ProtocolPropertyMap &PM) const {
1518 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1519 bool MatchFound = false;
1520 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1521 E = PDecl->prop_end(); P != E; ++P) {
1522 ObjCPropertyDecl *Prop = *P;
1523 if (Prop == Property)
1525 if (Prop->getIdentifier() == Property->getIdentifier()) {
1531 // Scan through protocol's protocols which did not have a matching property.
1533 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1534 E = PDecl->protocol_end(); PI != E; ++PI)
1535 (*PI)->collectInheritedProtocolProperties(Property, PM);
1539 //===----------------------------------------------------------------------===//
1541 //===----------------------------------------------------------------------===//
1543 void ObjCCategoryDecl::anchor() { }
1545 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1546 SourceLocation AtLoc,
1547 SourceLocation ClassNameLoc,
1548 SourceLocation CategoryNameLoc,
1550 ObjCInterfaceDecl *IDecl,
1551 SourceLocation IvarLBraceLoc,
1552 SourceLocation IvarRBraceLoc) {
1553 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1554 CategoryNameLoc, Id,
1556 IvarLBraceLoc, IvarRBraceLoc);
1558 // Link this category into its class's category list.
1559 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1560 if (IDecl->hasDefinition()) {
1561 IDecl->setCategoryListRaw(CatDecl);
1562 if (ASTMutationListener *L = C.getASTMutationListener())
1563 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1570 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1572 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1573 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1574 SourceLocation(), 0, 0);
1577 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1578 return getASTContext().getObjCImplementation(
1579 const_cast<ObjCCategoryDecl*>(this));
1582 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1583 getASTContext().setObjCImplementation(this, ImplD);
1587 //===----------------------------------------------------------------------===//
1588 // ObjCCategoryImplDecl
1589 //===----------------------------------------------------------------------===//
1591 void ObjCCategoryImplDecl::anchor() { }
1593 ObjCCategoryImplDecl *
1594 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1596 ObjCInterfaceDecl *ClassInterface,
1597 SourceLocation nameLoc,
1598 SourceLocation atStartLoc,
1599 SourceLocation CategoryNameLoc) {
1600 if (ClassInterface && ClassInterface->hasDefinition())
1601 ClassInterface = ClassInterface->getDefinition();
1602 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1603 nameLoc, atStartLoc, CategoryNameLoc);
1606 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1608 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1609 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1610 SourceLocation(), SourceLocation());
1613 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1614 // The class interface might be NULL if we are working with invalid code.
1615 if (const ObjCInterfaceDecl *ID = getClassInterface())
1616 return ID->FindCategoryDeclaration(getIdentifier());
1621 void ObjCImplDecl::anchor() { }
1623 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1624 // FIXME: The context should be correct before we get here.
1625 property->setLexicalDeclContext(this);
1629 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1630 ASTContext &Ctx = getASTContext();
1632 if (ObjCImplementationDecl *ImplD
1633 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1635 Ctx.setObjCImplementation(IFace, ImplD);
1637 } else if (ObjCCategoryImplDecl *ImplD =
1638 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1639 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1640 Ctx.setObjCImplementation(CD, ImplD);
1643 ClassInterface = IFace;
1646 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1647 /// properties implemented in this \@implementation block and returns
1648 /// the implemented property that uses it.
1650 ObjCPropertyImplDecl *ObjCImplDecl::
1651 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1652 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1653 ObjCPropertyImplDecl *PID = *i;
1654 if (PID->getPropertyIvarDecl() &&
1655 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1661 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1662 /// added to the list of those properties \@synthesized/\@dynamic in this
1663 /// category \@implementation block.
1665 ObjCPropertyImplDecl *ObjCImplDecl::
1666 FindPropertyImplDecl(IdentifierInfo *Id) const {
1667 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1668 ObjCPropertyImplDecl *PID = *i;
1669 if (PID->getPropertyDecl()->getIdentifier() == Id)
1675 raw_ostream &clang::operator<<(raw_ostream &OS,
1676 const ObjCCategoryImplDecl &CID) {
1677 OS << CID.getName();
1681 //===----------------------------------------------------------------------===//
1682 // ObjCImplementationDecl
1683 //===----------------------------------------------------------------------===//
1685 void ObjCImplementationDecl::anchor() { }
1687 ObjCImplementationDecl *
1688 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1689 ObjCInterfaceDecl *ClassInterface,
1690 ObjCInterfaceDecl *SuperDecl,
1691 SourceLocation nameLoc,
1692 SourceLocation atStartLoc,
1693 SourceLocation superLoc,
1694 SourceLocation IvarLBraceLoc,
1695 SourceLocation IvarRBraceLoc) {
1696 if (ClassInterface && ClassInterface->hasDefinition())
1697 ClassInterface = ClassInterface->getDefinition();
1698 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1699 nameLoc, atStartLoc, superLoc,
1700 IvarLBraceLoc, IvarRBraceLoc);
1703 ObjCImplementationDecl *
1704 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1705 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1706 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1710 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1711 CXXCtorInitializer ** initializers,
1712 unsigned numInitializers) {
1713 if (numInitializers > 0) {
1714 NumIvarInitializers = numInitializers;
1715 CXXCtorInitializer **ivarInitializers =
1716 new (C) CXXCtorInitializer*[NumIvarInitializers];
1717 memcpy(ivarInitializers, initializers,
1718 numInitializers * sizeof(CXXCtorInitializer*));
1719 IvarInitializers = ivarInitializers;
1723 raw_ostream &clang::operator<<(raw_ostream &OS,
1724 const ObjCImplementationDecl &ID) {
1729 //===----------------------------------------------------------------------===//
1730 // ObjCCompatibleAliasDecl
1731 //===----------------------------------------------------------------------===//
1733 void ObjCCompatibleAliasDecl::anchor() { }
1735 ObjCCompatibleAliasDecl *
1736 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1739 ObjCInterfaceDecl* AliasedClass) {
1740 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1743 ObjCCompatibleAliasDecl *
1744 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1745 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1746 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1749 //===----------------------------------------------------------------------===//
1751 //===----------------------------------------------------------------------===//
1753 void ObjCPropertyDecl::anchor() { }
1755 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1758 SourceLocation AtLoc,
1759 SourceLocation LParenLoc,
1761 PropertyControl propControl) {
1762 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1765 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1767 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1768 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1773 //===----------------------------------------------------------------------===//
1774 // ObjCPropertyImplDecl
1775 //===----------------------------------------------------------------------===//
1777 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1779 SourceLocation atLoc,
1781 ObjCPropertyDecl *property,
1784 SourceLocation ivarLoc) {
1785 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1789 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1791 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1792 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1793 0, Dynamic, 0, SourceLocation());
1796 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1797 SourceLocation EndLoc = getLocation();
1798 if (IvarLoc.isValid())
1801 return SourceRange(AtLoc, EndLoc);