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();
444 /// lookupMethod - This method returns an instance/class method by looking in
445 /// the class, its categories, and its super classes (using a linear search).
446 /// When argument category "C" is specified, any implicit method found
447 /// in this category is ignored.
448 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
450 bool shallowCategoryLookup,
451 const ObjCCategoryDecl *C) const {
452 // FIXME: Should make sure no callers ever do this.
453 if (!hasDefinition())
456 const ObjCInterfaceDecl* ClassDecl = this;
457 ObjCMethodDecl *MethodDecl = 0;
459 if (data().ExternallyCompleted)
460 LoadExternalDefinition();
462 while (ClassDecl != NULL) {
463 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
466 // Didn't find one yet - look through protocols.
467 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
468 E = ClassDecl->protocol_end();
470 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
473 // Didn't find one yet - now look through categories.
474 for (ObjCInterfaceDecl::visible_categories_iterator
475 Cat = ClassDecl->visible_categories_begin(),
476 CatEnd = ClassDecl->visible_categories_end();
477 Cat != CatEnd; ++Cat) {
478 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
479 if (C != (*Cat) || !MethodDecl->isImplicit())
482 if (!shallowCategoryLookup) {
483 // Didn't find one yet - look through protocols.
484 const ObjCList<ObjCProtocolDecl> &Protocols =
485 Cat->getReferencedProtocols();
486 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
487 E = Protocols.end(); I != E; ++I)
488 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
489 if (C != (*Cat) || !MethodDecl->isImplicit())
494 ClassDecl = ClassDecl->getSuperClass();
499 // Will search "local" class/category implementations for a method decl.
500 // If failed, then we search in class's root for an instance method.
501 // Returns 0 if no method is found.
502 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
504 bool Instance) const {
505 // FIXME: Should make sure no callers ever do this.
506 if (!hasDefinition())
509 if (data().ExternallyCompleted)
510 LoadExternalDefinition();
512 ObjCMethodDecl *Method = 0;
513 if (ObjCImplementationDecl *ImpDecl = getImplementation())
514 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
515 : ImpDecl->getClassMethod(Sel);
517 // Look through local category implementations associated with the class.
519 Method = Instance ? getCategoryInstanceMethod(Sel)
520 : getCategoryClassMethod(Sel);
522 // Before we give up, check if the selector is an instance method.
523 // But only in the root. This matches gcc's behavior and what the
525 if (!Instance && !Method && !getSuperClass()) {
526 Method = lookupInstanceMethod(Sel);
527 // Look through local category implementations associated
528 // with the root class.
530 Method = lookupPrivateMethod(Sel, true);
533 if (!Method && getSuperClass())
534 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
538 //===----------------------------------------------------------------------===//
540 //===----------------------------------------------------------------------===//
542 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
543 SourceLocation beginLoc,
544 SourceLocation endLoc,
545 Selector SelInfo, QualType T,
546 TypeSourceInfo *ResultTInfo,
547 DeclContext *contextDecl,
550 bool isPropertyAccessor,
551 bool isImplicitlyDeclared,
553 ImplementationControl impControl,
554 bool HasRelatedResultType) {
555 return new (C) ObjCMethodDecl(beginLoc, endLoc,
556 SelInfo, T, ResultTInfo, contextDecl,
557 isInstance, isVariadic, isPropertyAccessor,
558 isImplicitlyDeclared, isDefined,
560 HasRelatedResultType);
563 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
564 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
565 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
566 Selector(), QualType(), 0, 0);
569 Stmt *ObjCMethodDecl::getBody() const {
570 return Body.get(getASTContext().getExternalSource());
573 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
575 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
576 IsRedeclaration = true;
577 PrevMethod->HasRedeclaration = true;
580 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
581 ArrayRef<ParmVarDecl*> Params,
582 ArrayRef<SourceLocation> SelLocs) {
583 ParamsAndSelLocs = 0;
584 NumParams = Params.size();
585 if (Params.empty() && SelLocs.empty())
588 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
589 sizeof(SourceLocation) * SelLocs.size();
590 ParamsAndSelLocs = C.Allocate(Size);
591 std::copy(Params.begin(), Params.end(), getParams());
592 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
595 void ObjCMethodDecl::getSelectorLocs(
596 SmallVectorImpl<SourceLocation> &SelLocs) const {
597 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
598 SelLocs.push_back(getSelectorLoc(i));
601 void ObjCMethodDecl::setMethodParams(ASTContext &C,
602 ArrayRef<ParmVarDecl*> Params,
603 ArrayRef<SourceLocation> SelLocs) {
604 assert((!SelLocs.empty() || isImplicit()) &&
605 "No selector locs for non-implicit method");
607 return setParamsAndSelLocs(C, Params, llvm::None);
609 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
611 if (SelLocsKind != SelLoc_NonStandard)
612 return setParamsAndSelLocs(C, Params, llvm::None);
614 setParamsAndSelLocs(C, Params, SelLocs);
617 /// \brief A definition will return its interface declaration.
618 /// An interface declaration will return its definition.
619 /// Otherwise it will return itself.
620 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
621 ASTContext &Ctx = getASTContext();
622 ObjCMethodDecl *Redecl = 0;
623 if (HasRedeclaration)
624 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
628 Decl *CtxD = cast<Decl>(getDeclContext());
630 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
631 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
632 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
634 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
635 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
636 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
638 } else if (ObjCImplementationDecl *ImplD =
639 dyn_cast<ObjCImplementationDecl>(CtxD)) {
640 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
641 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
643 } else if (ObjCCategoryImplDecl *CImplD =
644 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
645 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
646 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
649 if (!Redecl && isRedeclaration()) {
650 // This is the last redeclaration, go back to the first method.
651 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
655 return Redecl ? Redecl : this;
658 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
659 Decl *CtxD = cast<Decl>(getDeclContext());
661 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
662 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
663 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
667 } else if (ObjCCategoryImplDecl *CImplD =
668 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
669 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
670 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
675 if (isRedeclaration())
676 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
682 SourceLocation ObjCMethodDecl::getLocEnd() const {
683 if (Stmt *Body = getBody())
684 return Body->getLocEnd();
688 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
689 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
690 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
693 // Check for an explicit attribute.
694 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
695 // The unfortunate necessity of mapping between enums here is due
696 // to the attributes framework.
697 switch (attr->getFamily()) {
698 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
699 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
700 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
701 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
702 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
703 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
705 Family = static_cast<unsigned>(family);
709 family = getSelector().getMethodFamily();
711 case OMF_None: break;
713 // init only has a conventional meaning for an instance method, and
714 // it has to return an object.
716 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
720 // alloc/copy/new have a conventional meaning for both class and
721 // instance methods, but they require an object return.
724 case OMF_mutableCopy:
726 if (!getResultType()->isObjCObjectPointerType())
730 // These selectors have a conventional meaning only for instance methods.
735 case OMF_autorelease:
736 case OMF_retainCount:
738 if (!isInstanceMethod())
742 case OMF_performSelector:
743 if (!isInstanceMethod() ||
744 !getResultType()->isObjCIdType())
747 unsigned noParams = param_size();
748 if (noParams < 1 || noParams > 3)
751 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
752 QualType ArgT = (*it);
753 if (!ArgT->isObjCSelType()) {
760 if (!ArgT->isObjCIdType()) {
772 Family = static_cast<unsigned>(family);
776 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
777 const ObjCInterfaceDecl *OID) {
779 if (isInstanceMethod()) {
780 // There may be no interface context due to error in declaration
781 // of the interface (which has been reported). Recover gracefully.
783 selfTy = Context.getObjCInterfaceType(OID);
784 selfTy = Context.getObjCObjectPointerType(selfTy);
786 selfTy = Context.getObjCIdType();
788 } else // we have a factory method.
789 selfTy = Context.getObjCClassType();
791 bool selfIsPseudoStrong = false;
792 bool selfIsConsumed = false;
794 if (Context.getLangOpts().ObjCAutoRefCount) {
795 if (isInstanceMethod()) {
796 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
798 // 'self' is always __strong. It's actually pseudo-strong except
799 // in init methods (or methods labeled ns_consumes_self), though.
801 qs.setObjCLifetime(Qualifiers::OCL_Strong);
802 selfTy = Context.getQualifiedType(selfTy, qs);
804 // In addition, 'self' is const unless this is an init method.
805 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
806 selfTy = selfTy.withConst();
807 selfIsPseudoStrong = true;
811 assert(isClassMethod());
812 // 'self' is always const in class methods.
813 selfTy = selfTy.withConst();
814 selfIsPseudoStrong = true;
818 ImplicitParamDecl *self
819 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
820 &Context.Idents.get("self"), selfTy);
824 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
826 if (selfIsPseudoStrong)
827 self->setARCPseudoStrong(true);
829 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
830 &Context.Idents.get("_cmd"),
831 Context.getObjCSelType()));
834 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
835 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
837 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
838 return CD->getClassInterface();
839 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
840 return IMD->getClassInterface();
842 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
843 llvm_unreachable("unknown method context");
846 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
847 const ObjCMethodDecl *Method,
848 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
853 // In categories look for overriden methods from protocols. A method from
854 // category is not "overriden" since it is considered as the "same" method
855 // (same USR) as the one from the interface.
856 if (const ObjCCategoryDecl *
857 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
858 // Check whether we have a matching method at this category but only if we
859 // are at the super class level.
862 Overridden = Container->getMethod(Method->getSelector(),
863 Method->isInstanceMethod(),
864 /*AllowHidden=*/true))
865 if (Method != Overridden) {
866 // We found an override at this category; there is no need to look
867 // into its protocols.
868 Methods.push_back(Overridden);
872 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
873 PEnd = Category->protocol_end();
875 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
879 // Check whether we have a matching method at this level.
880 if (const ObjCMethodDecl *
881 Overridden = Container->getMethod(Method->getSelector(),
882 Method->isInstanceMethod(),
883 /*AllowHidden=*/true))
884 if (Method != Overridden) {
885 // We found an override at this level; there is no need to look
886 // into other protocols or categories.
887 Methods.push_back(Overridden);
891 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
892 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
893 PEnd = Protocol->protocol_end();
895 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
898 if (const ObjCInterfaceDecl *
899 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
900 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
901 PEnd = Interface->protocol_end();
903 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
905 for (ObjCInterfaceDecl::known_categories_iterator
906 Cat = Interface->known_categories_begin(),
907 CatEnd = Interface->known_categories_end();
908 Cat != CatEnd; ++Cat) {
909 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
913 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
914 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
915 /*MovedToSuper=*/true);
919 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
920 const ObjCMethodDecl *Method,
921 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
922 CollectOverriddenMethodsRecurse(Container, Method, Methods,
923 /*MovedToSuper=*/false);
926 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
927 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
928 assert(Method->isOverriding());
930 if (const ObjCProtocolDecl *
931 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
932 CollectOverriddenMethods(ProtD, Method, overridden);
934 } else if (const ObjCImplDecl *
935 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
936 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
939 // Start searching for overridden methods using the method from the
940 // interface as starting point.
941 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
942 Method->isInstanceMethod(),
943 /*AllowHidden=*/true))
945 CollectOverriddenMethods(ID, Method, overridden);
947 } else if (const ObjCCategoryDecl *
948 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
949 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
952 // Start searching for overridden methods using the method from the
953 // interface as starting point.
954 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
955 Method->isInstanceMethod(),
956 /*AllowHidden=*/true))
958 CollectOverriddenMethods(ID, Method, overridden);
961 CollectOverriddenMethods(
962 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
967 void ObjCMethodDecl::getOverriddenMethods(
968 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
969 const ObjCMethodDecl *Method = this;
971 if (Method->isRedeclaration()) {
972 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
973 getMethod(Method->getSelector(), Method->isInstanceMethod());
976 if (Method->isOverriding()) {
977 collectOverriddenMethodsSlow(Method, Overridden);
978 assert(!Overridden.empty() &&
979 "ObjCMethodDecl's overriding bit is not as expected");
983 const ObjCPropertyDecl *
984 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
985 Selector Sel = getSelector();
986 unsigned NumArgs = Sel.getNumArgs();
990 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
993 if (isPropertyAccessor()) {
994 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
995 // If container is class extension, find its primary class.
996 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
997 if (CatDecl->IsClassExtension())
998 Container = CatDecl->getClassInterface();
1000 bool IsGetter = (NumArgs == 0);
1002 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1003 E = Container->prop_end();
1005 Selector NextSel = IsGetter ? (*I)->getGetterName()
1006 : (*I)->getSetterName();
1011 llvm_unreachable("Marked as a property accessor but no property found!");
1014 if (!CheckOverrides)
1017 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1018 OverridesTy Overrides;
1019 getOverriddenMethods(Overrides);
1020 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1022 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1030 //===----------------------------------------------------------------------===//
1031 // ObjCInterfaceDecl
1032 //===----------------------------------------------------------------------===//
1034 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1036 SourceLocation atLoc,
1038 ObjCInterfaceDecl *PrevDecl,
1039 SourceLocation ClassLoc,
1041 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
1042 PrevDecl, isInternal);
1043 Result->Data.setInt(!C.getLangOpts().Modules);
1044 C.getObjCInterfaceType(Result, PrevDecl);
1048 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1050 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1051 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1052 0, SourceLocation(),
1054 Result->Data.setInt(!C.getLangOpts().Modules);
1059 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
1060 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1062 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
1063 TypeForDecl(0), Data()
1065 setPreviousDeclaration(PrevDecl);
1067 // Copy the 'data' pointer over.
1069 Data = PrevDecl->Data;
1071 setImplicit(isInternal);
1074 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1075 assert(data().ExternallyCompleted && "Class is not externally completed");
1076 data().ExternallyCompleted = false;
1077 getASTContext().getExternalSource()->CompleteType(
1078 const_cast<ObjCInterfaceDecl *>(this));
1081 void ObjCInterfaceDecl::setExternallyCompleted() {
1082 assert(getASTContext().getExternalSource() &&
1083 "Class can't be externally completed without an external source");
1084 assert(hasDefinition() &&
1085 "Forward declarations can't be externally completed");
1086 data().ExternallyCompleted = true;
1089 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1090 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1091 if (data().ExternallyCompleted)
1092 LoadExternalDefinition();
1094 return getASTContext().getObjCImplementation(
1095 const_cast<ObjCInterfaceDecl*>(Def));
1098 // FIXME: Should make sure no callers ever do this.
1102 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1103 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1107 struct SynthesizeIvarChunk {
1110 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1111 : Size(size), Ivar(ivar) {}
1114 bool operator<(const SynthesizeIvarChunk & LHS,
1115 const SynthesizeIvarChunk &RHS) {
1116 return LHS.Size < RHS.Size;
1120 /// all_declared_ivar_begin - return first ivar declared in this class,
1121 /// its extensions and its implementation. Lazily build the list on first
1124 /// Caveat: The list returned by this method reflects the current
1125 /// state of the parser. The cache will be updated for every ivar
1126 /// added by an extension or the implementation when they are
1128 /// See also ObjCIvarDecl::Create().
1129 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1130 // FIXME: Should make sure no callers ever do this.
1131 if (!hasDefinition())
1134 ObjCIvarDecl *curIvar = 0;
1135 if (!data().IvarList) {
1136 if (!ivar_empty()) {
1137 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1138 data().IvarList = *I; ++I;
1139 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1140 curIvar->setNextIvar(*I);
1143 for (ObjCInterfaceDecl::known_extensions_iterator
1144 Ext = known_extensions_begin(),
1145 ExtEnd = known_extensions_end();
1146 Ext != ExtEnd; ++Ext) {
1147 if (!Ext->ivar_empty()) {
1148 ObjCCategoryDecl::ivar_iterator
1149 I = Ext->ivar_begin(),
1150 E = Ext->ivar_end();
1151 if (!data().IvarList) {
1152 data().IvarList = *I; ++I;
1153 curIvar = data().IvarList;
1155 for ( ;I != E; curIvar = *I, ++I)
1156 curIvar->setNextIvar(*I);
1159 data().IvarListMissingImplementation = true;
1162 // cached and complete!
1163 if (!data().IvarListMissingImplementation)
1164 return data().IvarList;
1166 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1167 data().IvarListMissingImplementation = false;
1168 if (!ImplDecl->ivar_empty()) {
1169 SmallVector<SynthesizeIvarChunk, 16> layout;
1170 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1171 E = ImplDecl->ivar_end(); I != E; ++I) {
1172 ObjCIvarDecl *IV = *I;
1173 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1174 layout.push_back(SynthesizeIvarChunk(
1175 IV->getASTContext().getTypeSize(IV->getType()), IV));
1178 if (!data().IvarList)
1179 data().IvarList = *I;
1181 curIvar->setNextIvar(*I);
1185 if (!layout.empty()) {
1186 // Order synthesized ivars by their size.
1187 std::stable_sort(layout.begin(), layout.end());
1188 unsigned Ix = 0, EIx = layout.size();
1189 if (!data().IvarList) {
1190 data().IvarList = layout[0].Ivar; Ix++;
1191 curIvar = data().IvarList;
1193 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1194 curIvar->setNextIvar(layout[Ix].Ivar);
1198 return data().IvarList;
1201 /// FindCategoryDeclaration - Finds category declaration in the list of
1202 /// categories for this class and returns it. Name of the category is passed
1203 /// in 'CategoryId'. If category not found, return 0;
1206 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1207 // FIXME: Should make sure no callers ever do this.
1208 if (!hasDefinition())
1211 if (data().ExternallyCompleted)
1212 LoadExternalDefinition();
1214 for (visible_categories_iterator Cat = visible_categories_begin(),
1215 CatEnd = visible_categories_end();
1218 if (Cat->getIdentifier() == CategoryId)
1226 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1227 for (visible_categories_iterator Cat = visible_categories_begin(),
1228 CatEnd = visible_categories_end();
1231 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1232 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1239 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1240 for (visible_categories_iterator Cat = visible_categories_begin(),
1241 CatEnd = visible_categories_end();
1244 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1245 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1252 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1253 /// has been implemented in IDecl class, its super class or categories (if
1254 /// lookupCategory is true).
1255 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1256 bool lookupCategory,
1257 bool RHSIsQualifiedID) {
1258 if (!hasDefinition())
1261 ObjCInterfaceDecl *IDecl = this;
1262 // 1st, look up the class.
1263 for (ObjCInterfaceDecl::protocol_iterator
1264 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
1265 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1267 // This is dubious and is added to be compatible with gcc. In gcc, it is
1268 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1269 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1270 // object. This IMO, should be a bug.
1271 // FIXME: Treat this as an extension, and flag this as an error when GCC
1272 // extensions are not enabled.
1273 if (RHSIsQualifiedID &&
1274 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1278 // 2nd, look up the category.
1280 for (visible_categories_iterator Cat = visible_categories_begin(),
1281 CatEnd = visible_categories_end();
1284 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1285 E = Cat->protocol_end();
1287 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1291 // 3rd, look up the super class(s)
1292 if (IDecl->getSuperClass())
1294 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1300 //===----------------------------------------------------------------------===//
1302 //===----------------------------------------------------------------------===//
1304 void ObjCIvarDecl::anchor() { }
1306 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1307 SourceLocation StartLoc,
1308 SourceLocation IdLoc, IdentifierInfo *Id,
1309 QualType T, TypeSourceInfo *TInfo,
1310 AccessControl ac, Expr *BW,
1313 // Ivar's can only appear in interfaces, implementations (via synthesized
1314 // properties), and class extensions (via direct declaration, or synthesized
1317 // FIXME: This should really be asserting this:
1318 // (isa<ObjCCategoryDecl>(DC) &&
1319 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1320 // but unfortunately we sometimes place ivars into non-class extension
1321 // categories on error. This breaks an AST invariant, and should not be
1323 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1324 isa<ObjCCategoryDecl>(DC)) &&
1325 "Invalid ivar decl context!");
1326 // Once a new ivar is created in any of class/class-extension/implementation
1327 // decl contexts, the previously built IvarList must be rebuilt.
1328 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1330 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1331 ID = IM->getClassInterface();
1333 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1338 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1339 ac, BW, synthesized);
1342 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1343 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1344 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1345 QualType(), 0, ObjCIvarDecl::None, 0, false);
1348 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1349 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1351 switch (DC->getKind()) {
1353 case ObjCCategoryImpl:
1355 llvm_unreachable("invalid ivar container!");
1357 // Ivars can only appear in class extension categories.
1358 case ObjCCategory: {
1359 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1360 assert(CD->IsClassExtension() && "invalid container for ivar!");
1361 return CD->getClassInterface();
1364 case ObjCImplementation:
1365 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1368 return cast<ObjCInterfaceDecl>(DC);
1372 //===----------------------------------------------------------------------===//
1373 // ObjCAtDefsFieldDecl
1374 //===----------------------------------------------------------------------===//
1376 void ObjCAtDefsFieldDecl::anchor() { }
1379 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1380 SourceLocation StartLoc, SourceLocation IdLoc,
1381 IdentifierInfo *Id, QualType T, Expr *BW) {
1382 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1385 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1387 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1388 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1392 //===----------------------------------------------------------------------===//
1394 //===----------------------------------------------------------------------===//
1396 void ObjCProtocolDecl::anchor() { }
1398 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1399 SourceLocation nameLoc,
1400 SourceLocation atStartLoc,
1401 ObjCProtocolDecl *PrevDecl)
1402 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1404 setPreviousDeclaration(PrevDecl);
1406 Data = PrevDecl->Data;
1409 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1411 SourceLocation nameLoc,
1412 SourceLocation atStartLoc,
1413 ObjCProtocolDecl *PrevDecl) {
1414 ObjCProtocolDecl *Result
1415 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1416 Result->Data.setInt(!C.getLangOpts().Modules);
1420 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1422 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1423 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1424 SourceLocation(), 0);
1425 Result->Data.setInt(!C.getLangOpts().Modules);
1429 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1430 ObjCProtocolDecl *PDecl = this;
1432 if (Name == getIdentifier())
1435 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1436 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1442 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1444 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1445 bool isInstance) const {
1446 ObjCMethodDecl *MethodDecl = NULL;
1448 // If there is no definition or the definition is hidden, we don't find
1450 const ObjCProtocolDecl *Def = getDefinition();
1451 if (!Def || Def->isHidden())
1454 if ((MethodDecl = getMethod(Sel, isInstance)))
1457 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1458 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1463 void ObjCProtocolDecl::allocateDefinitionData() {
1464 assert(!Data.getPointer() && "Protocol already has a definition!");
1465 Data.setPointer(new (getASTContext()) DefinitionData);
1466 Data.getPointer()->Definition = this;
1469 void ObjCProtocolDecl::startDefinition() {
1470 allocateDefinitionData();
1472 // Update all of the declarations with a pointer to the definition.
1473 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1475 RD->Data = this->Data;
1478 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1479 PropertyDeclOrder &PO) const {
1481 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1482 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1483 E = PDecl->prop_end(); P != E; ++P) {
1484 ObjCPropertyDecl *Prop = *P;
1485 // Insert into PM if not there already.
1486 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1489 // Scan through protocol's protocols.
1490 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1491 E = PDecl->protocol_end(); PI != E; ++PI)
1492 (*PI)->collectPropertiesToImplement(PM, PO);
1497 //===----------------------------------------------------------------------===//
1499 //===----------------------------------------------------------------------===//
1501 void ObjCCategoryDecl::anchor() { }
1503 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1504 SourceLocation AtLoc,
1505 SourceLocation ClassNameLoc,
1506 SourceLocation CategoryNameLoc,
1508 ObjCInterfaceDecl *IDecl,
1509 SourceLocation IvarLBraceLoc,
1510 SourceLocation IvarRBraceLoc) {
1511 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1512 CategoryNameLoc, Id,
1514 IvarLBraceLoc, IvarRBraceLoc);
1516 // Link this category into its class's category list.
1517 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1518 if (IDecl->hasDefinition()) {
1519 IDecl->setCategoryListRaw(CatDecl);
1520 if (ASTMutationListener *L = C.getASTMutationListener())
1521 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1528 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1530 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1531 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1532 SourceLocation(), 0, 0);
1535 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1536 return getASTContext().getObjCImplementation(
1537 const_cast<ObjCCategoryDecl*>(this));
1540 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1541 getASTContext().setObjCImplementation(this, ImplD);
1545 //===----------------------------------------------------------------------===//
1546 // ObjCCategoryImplDecl
1547 //===----------------------------------------------------------------------===//
1549 void ObjCCategoryImplDecl::anchor() { }
1551 ObjCCategoryImplDecl *
1552 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1554 ObjCInterfaceDecl *ClassInterface,
1555 SourceLocation nameLoc,
1556 SourceLocation atStartLoc,
1557 SourceLocation CategoryNameLoc) {
1558 if (ClassInterface && ClassInterface->hasDefinition())
1559 ClassInterface = ClassInterface->getDefinition();
1560 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1561 nameLoc, atStartLoc, CategoryNameLoc);
1564 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1566 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1567 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1568 SourceLocation(), SourceLocation());
1571 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1572 // The class interface might be NULL if we are working with invalid code.
1573 if (const ObjCInterfaceDecl *ID = getClassInterface())
1574 return ID->FindCategoryDeclaration(getIdentifier());
1579 void ObjCImplDecl::anchor() { }
1581 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1582 // FIXME: The context should be correct before we get here.
1583 property->setLexicalDeclContext(this);
1587 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1588 ASTContext &Ctx = getASTContext();
1590 if (ObjCImplementationDecl *ImplD
1591 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1593 Ctx.setObjCImplementation(IFace, ImplD);
1595 } else if (ObjCCategoryImplDecl *ImplD =
1596 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1597 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1598 Ctx.setObjCImplementation(CD, ImplD);
1601 ClassInterface = IFace;
1604 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1605 /// properties implemented in this \@implementation block and returns
1606 /// the implemented property that uses it.
1608 ObjCPropertyImplDecl *ObjCImplDecl::
1609 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1610 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1611 ObjCPropertyImplDecl *PID = *i;
1612 if (PID->getPropertyIvarDecl() &&
1613 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1619 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1620 /// added to the list of those properties \@synthesized/\@dynamic in this
1621 /// category \@implementation block.
1623 ObjCPropertyImplDecl *ObjCImplDecl::
1624 FindPropertyImplDecl(IdentifierInfo *Id) const {
1625 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1626 ObjCPropertyImplDecl *PID = *i;
1627 if (PID->getPropertyDecl()->getIdentifier() == Id)
1633 raw_ostream &clang::operator<<(raw_ostream &OS,
1634 const ObjCCategoryImplDecl &CID) {
1635 OS << CID.getName();
1639 //===----------------------------------------------------------------------===//
1640 // ObjCImplementationDecl
1641 //===----------------------------------------------------------------------===//
1643 void ObjCImplementationDecl::anchor() { }
1645 ObjCImplementationDecl *
1646 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1647 ObjCInterfaceDecl *ClassInterface,
1648 ObjCInterfaceDecl *SuperDecl,
1649 SourceLocation nameLoc,
1650 SourceLocation atStartLoc,
1651 SourceLocation superLoc,
1652 SourceLocation IvarLBraceLoc,
1653 SourceLocation IvarRBraceLoc) {
1654 if (ClassInterface && ClassInterface->hasDefinition())
1655 ClassInterface = ClassInterface->getDefinition();
1656 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1657 nameLoc, atStartLoc, superLoc,
1658 IvarLBraceLoc, IvarRBraceLoc);
1661 ObjCImplementationDecl *
1662 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1663 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1664 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1668 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1669 CXXCtorInitializer ** initializers,
1670 unsigned numInitializers) {
1671 if (numInitializers > 0) {
1672 NumIvarInitializers = numInitializers;
1673 CXXCtorInitializer **ivarInitializers =
1674 new (C) CXXCtorInitializer*[NumIvarInitializers];
1675 memcpy(ivarInitializers, initializers,
1676 numInitializers * sizeof(CXXCtorInitializer*));
1677 IvarInitializers = ivarInitializers;
1681 raw_ostream &clang::operator<<(raw_ostream &OS,
1682 const ObjCImplementationDecl &ID) {
1687 //===----------------------------------------------------------------------===//
1688 // ObjCCompatibleAliasDecl
1689 //===----------------------------------------------------------------------===//
1691 void ObjCCompatibleAliasDecl::anchor() { }
1693 ObjCCompatibleAliasDecl *
1694 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1697 ObjCInterfaceDecl* AliasedClass) {
1698 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1701 ObjCCompatibleAliasDecl *
1702 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1703 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1704 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1707 //===----------------------------------------------------------------------===//
1709 //===----------------------------------------------------------------------===//
1711 void ObjCPropertyDecl::anchor() { }
1713 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1716 SourceLocation AtLoc,
1717 SourceLocation LParenLoc,
1719 PropertyControl propControl) {
1720 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1723 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1725 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1726 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1731 //===----------------------------------------------------------------------===//
1732 // ObjCPropertyImplDecl
1733 //===----------------------------------------------------------------------===//
1735 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1737 SourceLocation atLoc,
1739 ObjCPropertyDecl *property,
1742 SourceLocation ivarLoc) {
1743 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1747 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1749 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1750 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1751 0, Dynamic, 0, SourceLocation());
1754 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1755 SourceLocation EndLoc = getLocation();
1756 if (IvarLoc.isValid())
1759 return SourceRange(AtLoc, EndLoc);