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/Stmt.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "llvm/ADT/STLExtras.h"
19 using namespace clang;
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
27 if (Elts == 0) return; // Setting to an empty list is a noop.
30 List = new (Ctx) void*[Elts];
32 memcpy(List, InList, sizeof(void*)*Elts);
35 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
36 const SourceLocation *Locs, ASTContext &Ctx) {
40 Locations = new (Ctx) SourceLocation[Elts];
41 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
42 set(InList, Elts, Ctx);
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 void ObjCContainerDecl::anchor() { }
51 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
54 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
55 lookup_const_iterator Ivar, IvarEnd;
56 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
57 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
63 // Get the local instance/class method declared in this interface.
65 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
66 // Since instance & class methods can have the same name, the loop below
67 // ensures we get the correct method.
69 // @interface Whatever
70 // - (int) class_method;
71 // + (float) class_method;
74 lookup_const_iterator Meth, MethEnd;
75 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
76 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
77 if (MD && MD->isInstanceMethod() == isInstance)
84 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
85 IdentifierInfo *propertyID) {
87 DeclContext::lookup_const_iterator I, E;
88 llvm::tie(I, E) = DC->lookup(propertyID);
90 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
96 /// FindPropertyDeclaration - Finds declaration of the property given its name
97 /// in 'PropertyId' and returns it. It returns 0, if not found.
99 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
101 if (ObjCPropertyDecl *PD =
102 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
108 case Decl::ObjCProtocol: {
109 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
110 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
111 E = PID->protocol_end(); I != E; ++I)
112 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
116 case Decl::ObjCInterface: {
117 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
118 // Look through categories.
119 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
120 Cat; Cat = Cat->getNextClassCategory())
121 if (!Cat->IsClassExtension())
122 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
125 // Look through protocols.
126 for (ObjCInterfaceDecl::all_protocol_iterator
127 I = OID->all_referenced_protocol_begin(),
128 E = OID->all_referenced_protocol_end(); I != E; ++I)
129 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
132 // Finally, check the super class.
133 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
134 return superClass->FindPropertyDeclaration(PropertyId);
137 case Decl::ObjCCategory: {
138 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
139 // Look through protocols.
140 if (!OCD->IsClassExtension())
141 for (ObjCCategoryDecl::protocol_iterator
142 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
143 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
152 void ObjCInterfaceDecl::anchor() { }
154 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
155 /// with name 'PropertyId' in the primary class; including those in protocols
156 /// (direct or indirect) used by the primary class.
159 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
160 IdentifierInfo *PropertyId) const {
161 // FIXME: Should make sure no callers ever do this.
162 if (!hasDefinition())
165 if (data().ExternallyCompleted)
166 LoadExternalDefinition();
168 if (ObjCPropertyDecl *PD =
169 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
172 // Look through protocols.
173 for (ObjCInterfaceDecl::all_protocol_iterator
174 I = all_referenced_protocol_begin(),
175 E = all_referenced_protocol_end(); I != E; ++I)
176 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
182 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
183 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
186 if (data().ExternallyCompleted)
187 LoadExternalDefinition();
189 if (data().AllReferencedProtocols.empty() &&
190 data().ReferencedProtocols.empty()) {
191 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
195 // Check for duplicate protocol in class's protocol list.
196 // This is O(n*m). But it is extremely rare and number of protocols in
197 // class or its extension are very few.
198 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
199 for (unsigned i = 0; i < ExtNum; i++) {
200 bool protocolExists = false;
201 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
202 for (all_protocol_iterator
203 p = all_referenced_protocol_begin(),
204 e = all_referenced_protocol_end(); p != e; ++p) {
205 ObjCProtocolDecl *Proto = (*p);
206 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
207 protocolExists = true;
211 // Do we want to warn on a protocol in extension class which
212 // already exist in the class? Probably not.
214 ProtocolRefs.push_back(ProtoInExtension);
217 if (ProtocolRefs.empty())
220 // Merge ProtocolRefs into class's protocol list;
221 for (all_protocol_iterator p = all_referenced_protocol_begin(),
222 e = all_referenced_protocol_end(); p != e; ++p) {
223 ProtocolRefs.push_back(*p);
226 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
229 void ObjCInterfaceDecl::allocateDefinitionData() {
230 assert(!hasDefinition() && "ObjC class already has a definition");
231 Data = new (getASTContext()) DefinitionData();
232 Data->Definition = this;
234 // Make the type point at the definition, now that we have one.
236 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
239 void ObjCInterfaceDecl::startDefinition() {
240 allocateDefinitionData();
242 // Update all of the declarations with a pointer to the definition.
243 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
250 /// getFirstClassExtension - Find first class extension of the given class.
251 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
252 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
253 CDecl = CDecl->getNextClassCategory())
254 if (CDecl->IsClassExtension())
259 /// getNextClassCategory - Find next class extension in list of categories.
260 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
261 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
262 CDecl = CDecl->getNextClassCategory())
263 if (CDecl->IsClassExtension())
268 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
269 ObjCInterfaceDecl *&clsDeclared) {
270 // FIXME: Should make sure no callers ever do this.
271 if (!hasDefinition())
274 if (data().ExternallyCompleted)
275 LoadExternalDefinition();
277 ObjCInterfaceDecl* ClassDecl = this;
278 while (ClassDecl != NULL) {
279 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
280 clsDeclared = ClassDecl;
283 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
284 CDecl; CDecl = CDecl->getNextClassExtension()) {
285 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
286 clsDeclared = ClassDecl;
291 ClassDecl = ClassDecl->getSuperClass();
296 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
297 /// class whose name is passed as argument. If it is not one of the super classes
298 /// the it returns NULL.
299 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
300 const IdentifierInfo*ICName) {
301 // FIXME: Should make sure no callers ever do this.
302 if (!hasDefinition())
305 if (data().ExternallyCompleted)
306 LoadExternalDefinition();
308 ObjCInterfaceDecl* ClassDecl = this;
309 while (ClassDecl != NULL) {
310 if (ClassDecl->getIdentifier() == ICName)
312 ClassDecl = ClassDecl->getSuperClass();
317 /// lookupMethod - This method returns an instance/class method by looking in
318 /// the class, its categories, and its super classes (using a linear search).
319 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
321 bool shallowCategoryLookup) const {
322 // FIXME: Should make sure no callers ever do this.
323 if (!hasDefinition())
326 const ObjCInterfaceDecl* ClassDecl = this;
327 ObjCMethodDecl *MethodDecl = 0;
329 if (data().ExternallyCompleted)
330 LoadExternalDefinition();
332 while (ClassDecl != NULL) {
333 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
336 // Didn't find one yet - look through protocols.
337 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
338 E = ClassDecl->protocol_end();
340 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
343 // Didn't find one yet - now look through categories.
344 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
346 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
349 if (!shallowCategoryLookup) {
350 // Didn't find one yet - look through protocols.
351 const ObjCList<ObjCProtocolDecl> &Protocols =
352 CatDecl->getReferencedProtocols();
353 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
354 E = Protocols.end(); I != E; ++I)
355 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
358 CatDecl = CatDecl->getNextClassCategory();
361 ClassDecl = ClassDecl->getSuperClass();
366 // Will search "local" class/category implementations for a method decl.
367 // If failed, then we search in class's root for an instance method.
368 // Returns 0 if no method is found.
369 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
371 bool Instance) const {
372 // FIXME: Should make sure no callers ever do this.
373 if (!hasDefinition())
376 if (data().ExternallyCompleted)
377 LoadExternalDefinition();
379 ObjCMethodDecl *Method = 0;
380 if (ObjCImplementationDecl *ImpDecl = getImplementation())
381 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
382 : ImpDecl->getClassMethod(Sel);
384 // Look through local category implementations associated with the class.
386 Method = Instance ? getCategoryInstanceMethod(Sel)
387 : getCategoryClassMethod(Sel);
389 // Before we give up, check if the selector is an instance method.
390 // But only in the root. This matches gcc's behavior and what the
392 if (!Instance && !Method && !getSuperClass()) {
393 Method = lookupInstanceMethod(Sel);
394 // Look through local category implementations associated
395 // with the root class.
397 Method = lookupPrivateMethod(Sel, true);
400 if (!Method && getSuperClass())
401 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
405 //===----------------------------------------------------------------------===//
407 //===----------------------------------------------------------------------===//
409 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
410 SourceLocation beginLoc,
411 SourceLocation endLoc,
412 Selector SelInfo, QualType T,
413 TypeSourceInfo *ResultTInfo,
414 DeclContext *contextDecl,
418 bool isImplicitlyDeclared,
420 ImplementationControl impControl,
421 bool HasRelatedResultType) {
422 return new (C) ObjCMethodDecl(beginLoc, endLoc,
423 SelInfo, T, ResultTInfo, contextDecl,
425 isVariadic, isSynthesized, isImplicitlyDeclared,
428 HasRelatedResultType);
431 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
432 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
433 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
434 Selector(), QualType(), 0, 0);
437 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
439 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
440 IsRedeclaration = true;
441 PrevMethod->HasRedeclaration = true;
444 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
445 ArrayRef<ParmVarDecl*> Params,
446 ArrayRef<SourceLocation> SelLocs) {
447 ParamsAndSelLocs = 0;
448 NumParams = Params.size();
449 if (Params.empty() && SelLocs.empty())
452 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
453 sizeof(SourceLocation) * SelLocs.size();
454 ParamsAndSelLocs = C.Allocate(Size);
455 std::copy(Params.begin(), Params.end(), getParams());
456 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
459 void ObjCMethodDecl::getSelectorLocs(
460 SmallVectorImpl<SourceLocation> &SelLocs) const {
461 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
462 SelLocs.push_back(getSelectorLoc(i));
465 void ObjCMethodDecl::setMethodParams(ASTContext &C,
466 ArrayRef<ParmVarDecl*> Params,
467 ArrayRef<SourceLocation> SelLocs) {
468 assert((!SelLocs.empty() || isImplicit()) &&
469 "No selector locs for non-implicit method");
471 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
473 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
475 if (SelLocsKind != SelLoc_NonStandard)
476 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
478 setParamsAndSelLocs(C, Params, SelLocs);
481 /// \brief A definition will return its interface declaration.
482 /// An interface declaration will return its definition.
483 /// Otherwise it will return itself.
484 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
485 ASTContext &Ctx = getASTContext();
486 ObjCMethodDecl *Redecl = 0;
487 if (HasRedeclaration)
488 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
492 Decl *CtxD = cast<Decl>(getDeclContext());
494 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
495 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
496 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
498 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
499 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
500 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
502 } else if (ObjCImplementationDecl *ImplD =
503 dyn_cast<ObjCImplementationDecl>(CtxD)) {
504 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
505 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
507 } else if (ObjCCategoryImplDecl *CImplD =
508 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
509 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
510 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
513 if (!Redecl && isRedeclaration()) {
514 // This is the last redeclaration, go back to the first method.
515 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
519 return Redecl ? Redecl : this;
522 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
523 Decl *CtxD = cast<Decl>(getDeclContext());
525 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
526 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
527 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
531 } else if (ObjCCategoryImplDecl *CImplD =
532 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
533 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
534 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
539 if (isRedeclaration())
540 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
546 SourceLocation ObjCMethodDecl::getLocEnd() const {
547 if (Stmt *Body = getBody())
548 return Body->getLocEnd();
552 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
553 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
554 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
557 // Check for an explicit attribute.
558 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
559 // The unfortunate necessity of mapping between enums here is due
560 // to the attributes framework.
561 switch (attr->getFamily()) {
562 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
563 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
564 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
565 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
566 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
567 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
569 Family = static_cast<unsigned>(family);
573 family = getSelector().getMethodFamily();
575 case OMF_None: break;
577 // init only has a conventional meaning for an instance method, and
578 // it has to return an object.
580 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
584 // alloc/copy/new have a conventional meaning for both class and
585 // instance methods, but they require an object return.
588 case OMF_mutableCopy:
590 if (!getResultType()->isObjCObjectPointerType())
594 // These selectors have a conventional meaning only for instance methods.
599 case OMF_autorelease:
600 case OMF_retainCount:
602 if (!isInstanceMethod())
606 case OMF_performSelector:
607 if (!isInstanceMethod() ||
608 !getResultType()->isObjCIdType())
611 unsigned noParams = param_size();
612 if (noParams < 1 || noParams > 3)
615 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
616 QualType ArgT = (*it);
617 if (!ArgT->isObjCSelType()) {
624 if (!ArgT->isObjCIdType()) {
636 Family = static_cast<unsigned>(family);
640 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
641 const ObjCInterfaceDecl *OID) {
643 if (isInstanceMethod()) {
644 // There may be no interface context due to error in declaration
645 // of the interface (which has been reported). Recover gracefully.
647 selfTy = Context.getObjCInterfaceType(OID);
648 selfTy = Context.getObjCObjectPointerType(selfTy);
650 selfTy = Context.getObjCIdType();
652 } else // we have a factory method.
653 selfTy = Context.getObjCClassType();
655 bool selfIsPseudoStrong = false;
656 bool selfIsConsumed = false;
658 if (Context.getLangOpts().ObjCAutoRefCount) {
659 if (isInstanceMethod()) {
660 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
662 // 'self' is always __strong. It's actually pseudo-strong except
663 // in init methods (or methods labeled ns_consumes_self), though.
665 qs.setObjCLifetime(Qualifiers::OCL_Strong);
666 selfTy = Context.getQualifiedType(selfTy, qs);
668 // In addition, 'self' is const unless this is an init method.
669 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
670 selfTy = selfTy.withConst();
671 selfIsPseudoStrong = true;
675 assert(isClassMethod());
676 // 'self' is always const in class methods.
677 selfTy = selfTy.withConst();
678 selfIsPseudoStrong = true;
682 ImplicitParamDecl *self
683 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
684 &Context.Idents.get("self"), selfTy);
688 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
690 if (selfIsPseudoStrong)
691 self->setARCPseudoStrong(true);
693 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
694 &Context.Idents.get("_cmd"),
695 Context.getObjCSelType()));
698 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
699 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
701 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
702 return CD->getClassInterface();
703 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
704 return IMD->getClassInterface();
706 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
707 llvm_unreachable("unknown method context");
710 //===----------------------------------------------------------------------===//
712 //===----------------------------------------------------------------------===//
714 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
716 SourceLocation atLoc,
718 ObjCInterfaceDecl *PrevDecl,
719 SourceLocation ClassLoc,
721 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
722 PrevDecl, isInternal);
723 C.getObjCInterfaceType(Result, PrevDecl);
727 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
729 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
730 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
735 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
736 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
738 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
739 TypeForDecl(0), Data()
741 setPreviousDeclaration(PrevDecl);
743 // Copy the 'data' pointer over.
745 Data = PrevDecl->Data;
747 setImplicit(isInternal);
750 void ObjCInterfaceDecl::LoadExternalDefinition() const {
751 assert(data().ExternallyCompleted && "Class is not externally completed");
752 data().ExternallyCompleted = false;
753 getASTContext().getExternalSource()->CompleteType(
754 const_cast<ObjCInterfaceDecl *>(this));
757 void ObjCInterfaceDecl::setExternallyCompleted() {
758 assert(getASTContext().getExternalSource() &&
759 "Class can't be externally completed without an external source");
760 assert(hasDefinition() &&
761 "Forward declarations can't be externally completed");
762 data().ExternallyCompleted = true;
765 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
766 if (const ObjCInterfaceDecl *Def = getDefinition()) {
767 if (data().ExternallyCompleted)
768 LoadExternalDefinition();
770 return getASTContext().getObjCImplementation(
771 const_cast<ObjCInterfaceDecl*>(Def));
774 // FIXME: Should make sure no callers ever do this.
778 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
779 getASTContext().setObjCImplementation(getDefinition(), ImplD);
782 /// all_declared_ivar_begin - return first ivar declared in this class,
783 /// its extensions and its implementation. Lazily build the list on first
785 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
786 // FIXME: Should make sure no callers ever do this.
787 if (!hasDefinition())
791 return data().IvarList;
793 ObjCIvarDecl *curIvar = 0;
795 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
796 data().IvarList = *I; ++I;
797 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
798 curIvar->setNextIvar(*I);
801 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
802 CDecl = CDecl->getNextClassExtension()) {
803 if (!CDecl->ivar_empty()) {
804 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
805 E = CDecl->ivar_end();
806 if (!data().IvarList) {
807 data().IvarList = *I; ++I;
808 curIvar = data().IvarList;
810 for ( ;I != E; curIvar = *I, ++I)
811 curIvar->setNextIvar(*I);
815 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
816 if (!ImplDecl->ivar_empty()) {
817 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
818 E = ImplDecl->ivar_end();
819 if (!data().IvarList) {
820 data().IvarList = *I; ++I;
821 curIvar = data().IvarList;
823 for ( ;I != E; curIvar = *I, ++I)
824 curIvar->setNextIvar(*I);
827 return data().IvarList;
830 /// FindCategoryDeclaration - Finds category declaration in the list of
831 /// categories for this class and returns it. Name of the category is passed
832 /// in 'CategoryId'. If category not found, return 0;
835 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
836 // FIXME: Should make sure no callers ever do this.
837 if (!hasDefinition())
840 if (data().ExternallyCompleted)
841 LoadExternalDefinition();
843 for (ObjCCategoryDecl *Category = getCategoryList();
844 Category; Category = Category->getNextClassCategory())
845 if (Category->getIdentifier() == CategoryId)
851 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
852 for (ObjCCategoryDecl *Category = getCategoryList();
853 Category; Category = Category->getNextClassCategory())
854 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
855 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
860 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
861 for (ObjCCategoryDecl *Category = getCategoryList();
862 Category; Category = Category->getNextClassCategory())
863 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
864 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
869 /// ClassImplementsProtocol - Checks that 'lProto' protocol
870 /// has been implemented in IDecl class, its super class or categories (if
871 /// lookupCategory is true).
872 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
874 bool RHSIsQualifiedID) {
875 if (!hasDefinition())
878 ObjCInterfaceDecl *IDecl = this;
879 // 1st, look up the class.
880 for (ObjCInterfaceDecl::protocol_iterator
881 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
882 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
884 // This is dubious and is added to be compatible with gcc. In gcc, it is
885 // also allowed assigning a protocol-qualified 'id' type to a LHS object
886 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
887 // object. This IMO, should be a bug.
888 // FIXME: Treat this as an extension, and flag this as an error when GCC
889 // extensions are not enabled.
890 if (RHSIsQualifiedID &&
891 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
895 // 2nd, look up the category.
897 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
898 CDecl = CDecl->getNextClassCategory()) {
899 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
900 E = CDecl->protocol_end(); PI != E; ++PI)
901 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
905 // 3rd, look up the super class(s)
906 if (IDecl->getSuperClass())
908 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
914 //===----------------------------------------------------------------------===//
916 //===----------------------------------------------------------------------===//
918 void ObjCIvarDecl::anchor() { }
920 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
921 SourceLocation StartLoc,
922 SourceLocation IdLoc, IdentifierInfo *Id,
923 QualType T, TypeSourceInfo *TInfo,
924 AccessControl ac, Expr *BW,
927 // Ivar's can only appear in interfaces, implementations (via synthesized
928 // properties), and class extensions (via direct declaration, or synthesized
931 // FIXME: This should really be asserting this:
932 // (isa<ObjCCategoryDecl>(DC) &&
933 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
934 // but unfortunately we sometimes place ivars into non-class extension
935 // categories on error. This breaks an AST invariant, and should not be
937 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
938 isa<ObjCCategoryDecl>(DC)) &&
939 "Invalid ivar decl context!");
940 // Once a new ivar is created in any of class/class-extension/implementation
941 // decl contexts, the previously built IvarList must be rebuilt.
942 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
944 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
945 ID = IM->getClassInterface();
947 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
952 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
953 ac, BW, synthesized);
956 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
957 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
958 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
959 QualType(), 0, ObjCIvarDecl::None, 0, false);
962 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
963 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
965 switch (DC->getKind()) {
967 case ObjCCategoryImpl:
969 llvm_unreachable("invalid ivar container!");
971 // Ivars can only appear in class extension categories.
973 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
974 assert(CD->IsClassExtension() && "invalid container for ivar!");
975 return CD->getClassInterface();
978 case ObjCImplementation:
979 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
982 return cast<ObjCInterfaceDecl>(DC);
986 //===----------------------------------------------------------------------===//
987 // ObjCAtDefsFieldDecl
988 //===----------------------------------------------------------------------===//
990 void ObjCAtDefsFieldDecl::anchor() { }
993 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
994 SourceLocation StartLoc, SourceLocation IdLoc,
995 IdentifierInfo *Id, QualType T, Expr *BW) {
996 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
999 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1001 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1002 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1006 //===----------------------------------------------------------------------===//
1008 //===----------------------------------------------------------------------===//
1010 void ObjCProtocolDecl::anchor() { }
1012 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1013 SourceLocation nameLoc,
1014 SourceLocation atStartLoc,
1015 ObjCProtocolDecl *PrevDecl)
1016 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1018 setPreviousDeclaration(PrevDecl);
1020 Data = PrevDecl->Data;
1023 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1025 SourceLocation nameLoc,
1026 SourceLocation atStartLoc,
1027 ObjCProtocolDecl *PrevDecl) {
1028 ObjCProtocolDecl *Result
1029 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1034 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1036 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1037 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1041 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1042 ObjCProtocolDecl *PDecl = this;
1044 if (Name == getIdentifier())
1047 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1048 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1054 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1056 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1057 bool isInstance) const {
1058 ObjCMethodDecl *MethodDecl = NULL;
1060 if ((MethodDecl = getMethod(Sel, isInstance)))
1063 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1064 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1069 void ObjCProtocolDecl::allocateDefinitionData() {
1070 assert(!Data && "Protocol already has a definition!");
1071 Data = new (getASTContext()) DefinitionData;
1072 Data->Definition = this;
1075 void ObjCProtocolDecl::startDefinition() {
1076 allocateDefinitionData();
1078 // Update all of the declarations with a pointer to the definition.
1079 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1081 RD->Data = this->Data;
1084 //===----------------------------------------------------------------------===//
1086 //===----------------------------------------------------------------------===//
1088 void ObjCCategoryDecl::anchor() { }
1090 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1091 SourceLocation AtLoc,
1092 SourceLocation ClassNameLoc,
1093 SourceLocation CategoryNameLoc,
1095 ObjCInterfaceDecl *IDecl,
1096 SourceLocation IvarLBraceLoc,
1097 SourceLocation IvarRBraceLoc) {
1098 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1099 CategoryNameLoc, Id,
1101 IvarLBraceLoc, IvarRBraceLoc);
1103 // Link this category into its class's category list.
1104 CatDecl->NextClassCategory = IDecl->getCategoryList();
1105 if (IDecl->hasDefinition()) {
1106 IDecl->setCategoryList(CatDecl);
1107 if (ASTMutationListener *L = C.getASTMutationListener())
1108 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1115 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1117 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1118 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1119 SourceLocation(), 0, 0);
1122 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1123 return getASTContext().getObjCImplementation(
1124 const_cast<ObjCCategoryDecl*>(this));
1127 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1128 getASTContext().setObjCImplementation(this, ImplD);
1132 //===----------------------------------------------------------------------===//
1133 // ObjCCategoryImplDecl
1134 //===----------------------------------------------------------------------===//
1136 void ObjCCategoryImplDecl::anchor() { }
1138 ObjCCategoryImplDecl *
1139 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1141 ObjCInterfaceDecl *ClassInterface,
1142 SourceLocation nameLoc,
1143 SourceLocation atStartLoc,
1144 SourceLocation CategoryNameLoc) {
1145 if (ClassInterface && ClassInterface->hasDefinition())
1146 ClassInterface = ClassInterface->getDefinition();
1147 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1148 nameLoc, atStartLoc, CategoryNameLoc);
1151 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1153 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1154 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1155 SourceLocation(), SourceLocation());
1158 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1159 // The class interface might be NULL if we are working with invalid code.
1160 if (const ObjCInterfaceDecl *ID = getClassInterface())
1161 return ID->FindCategoryDeclaration(getIdentifier());
1166 void ObjCImplDecl::anchor() { }
1168 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1169 // FIXME: The context should be correct before we get here.
1170 property->setLexicalDeclContext(this);
1174 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1175 ASTContext &Ctx = getASTContext();
1177 if (ObjCImplementationDecl *ImplD
1178 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1180 Ctx.setObjCImplementation(IFace, ImplD);
1182 } else if (ObjCCategoryImplDecl *ImplD =
1183 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1184 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1185 Ctx.setObjCImplementation(CD, ImplD);
1188 ClassInterface = IFace;
1191 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1192 /// properties implemented in this category \@implementation block and returns
1193 /// the implemented property that uses it.
1195 ObjCPropertyImplDecl *ObjCImplDecl::
1196 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1197 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1198 ObjCPropertyImplDecl *PID = *i;
1199 if (PID->getPropertyIvarDecl() &&
1200 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1206 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1207 /// added to the list of those properties \@synthesized/\@dynamic in this
1208 /// category \@implementation block.
1210 ObjCPropertyImplDecl *ObjCImplDecl::
1211 FindPropertyImplDecl(IdentifierInfo *Id) const {
1212 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1213 ObjCPropertyImplDecl *PID = *i;
1214 if (PID->getPropertyDecl()->getIdentifier() == Id)
1220 raw_ostream &clang::operator<<(raw_ostream &OS,
1221 const ObjCCategoryImplDecl &CID) {
1222 OS << CID.getName();
1226 //===----------------------------------------------------------------------===//
1227 // ObjCImplementationDecl
1228 //===----------------------------------------------------------------------===//
1230 void ObjCImplementationDecl::anchor() { }
1232 ObjCImplementationDecl *
1233 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1234 ObjCInterfaceDecl *ClassInterface,
1235 ObjCInterfaceDecl *SuperDecl,
1236 SourceLocation nameLoc,
1237 SourceLocation atStartLoc,
1238 SourceLocation IvarLBraceLoc,
1239 SourceLocation IvarRBraceLoc) {
1240 if (ClassInterface && ClassInterface->hasDefinition())
1241 ClassInterface = ClassInterface->getDefinition();
1242 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1243 nameLoc, atStartLoc,
1244 IvarLBraceLoc, IvarRBraceLoc);
1247 ObjCImplementationDecl *
1248 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1249 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1250 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1254 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1255 CXXCtorInitializer ** initializers,
1256 unsigned numInitializers) {
1257 if (numInitializers > 0) {
1258 NumIvarInitializers = numInitializers;
1259 CXXCtorInitializer **ivarInitializers =
1260 new (C) CXXCtorInitializer*[NumIvarInitializers];
1261 memcpy(ivarInitializers, initializers,
1262 numInitializers * sizeof(CXXCtorInitializer*));
1263 IvarInitializers = ivarInitializers;
1267 raw_ostream &clang::operator<<(raw_ostream &OS,
1268 const ObjCImplementationDecl &ID) {
1273 //===----------------------------------------------------------------------===//
1274 // ObjCCompatibleAliasDecl
1275 //===----------------------------------------------------------------------===//
1277 void ObjCCompatibleAliasDecl::anchor() { }
1279 ObjCCompatibleAliasDecl *
1280 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1283 ObjCInterfaceDecl* AliasedClass) {
1284 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1287 ObjCCompatibleAliasDecl *
1288 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1289 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1290 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1293 //===----------------------------------------------------------------------===//
1295 //===----------------------------------------------------------------------===//
1297 void ObjCPropertyDecl::anchor() { }
1299 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1302 SourceLocation AtLoc,
1303 SourceLocation LParenLoc,
1305 PropertyControl propControl) {
1306 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1309 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1311 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1312 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1317 //===----------------------------------------------------------------------===//
1318 // ObjCPropertyImplDecl
1319 //===----------------------------------------------------------------------===//
1321 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1323 SourceLocation atLoc,
1325 ObjCPropertyDecl *property,
1328 SourceLocation ivarLoc) {
1329 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1333 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1335 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1336 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1337 0, Dynamic, 0, SourceLocation());
1340 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1341 SourceLocation EndLoc = getLocation();
1342 if (IvarLoc.isValid())
1345 return SourceRange(AtLoc, EndLoc);