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 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
369 // FIXME: Should make sure no callers ever do this.
370 if (!hasDefinition())
373 if (data().ExternallyCompleted)
374 LoadExternalDefinition();
376 ObjCMethodDecl *Method = 0;
377 if (ObjCImplementationDecl *ImpDecl = getImplementation())
378 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
379 : ImpDecl->getClassMethod(Sel);
381 if (!Method && getSuperClass())
382 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
386 //===----------------------------------------------------------------------===//
388 //===----------------------------------------------------------------------===//
390 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
391 SourceLocation beginLoc,
392 SourceLocation endLoc,
393 Selector SelInfo, QualType T,
394 TypeSourceInfo *ResultTInfo,
395 DeclContext *contextDecl,
399 bool isImplicitlyDeclared,
401 ImplementationControl impControl,
402 bool HasRelatedResultType) {
403 return new (C) ObjCMethodDecl(beginLoc, endLoc,
404 SelInfo, T, ResultTInfo, contextDecl,
406 isVariadic, isSynthesized, isImplicitlyDeclared,
409 HasRelatedResultType);
412 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
413 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
414 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
415 Selector(), QualType(), 0, 0);
418 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
420 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
421 IsRedeclaration = true;
422 PrevMethod->HasRedeclaration = true;
425 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
426 ArrayRef<ParmVarDecl*> Params,
427 ArrayRef<SourceLocation> SelLocs) {
428 ParamsAndSelLocs = 0;
429 NumParams = Params.size();
430 if (Params.empty() && SelLocs.empty())
433 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
434 sizeof(SourceLocation) * SelLocs.size();
435 ParamsAndSelLocs = C.Allocate(Size);
436 std::copy(Params.begin(), Params.end(), getParams());
437 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
440 void ObjCMethodDecl::getSelectorLocs(
441 SmallVectorImpl<SourceLocation> &SelLocs) const {
442 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
443 SelLocs.push_back(getSelectorLoc(i));
446 void ObjCMethodDecl::setMethodParams(ASTContext &C,
447 ArrayRef<ParmVarDecl*> Params,
448 ArrayRef<SourceLocation> SelLocs) {
449 assert((!SelLocs.empty() || isImplicit()) &&
450 "No selector locs for non-implicit method");
452 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
454 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
455 if (SelLocsKind != SelLoc_NonStandard)
456 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
458 setParamsAndSelLocs(C, Params, SelLocs);
461 /// \brief A definition will return its interface declaration.
462 /// An interface declaration will return its definition.
463 /// Otherwise it will return itself.
464 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
465 ASTContext &Ctx = getASTContext();
466 ObjCMethodDecl *Redecl = 0;
467 if (HasRedeclaration)
468 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
472 Decl *CtxD = cast<Decl>(getDeclContext());
474 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
475 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
476 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
478 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
479 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
480 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
482 } else if (ObjCImplementationDecl *ImplD =
483 dyn_cast<ObjCImplementationDecl>(CtxD)) {
484 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
485 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
487 } else if (ObjCCategoryImplDecl *CImplD =
488 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
489 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
490 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
493 if (!Redecl && isRedeclaration()) {
494 // This is the last redeclaration, go back to the first method.
495 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
499 return Redecl ? Redecl : this;
502 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
503 Decl *CtxD = cast<Decl>(getDeclContext());
505 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
506 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
507 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
511 } else if (ObjCCategoryImplDecl *CImplD =
512 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
513 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
514 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
519 if (isRedeclaration())
520 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
526 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
527 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
528 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
531 // Check for an explicit attribute.
532 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
533 // The unfortunate necessity of mapping between enums here is due
534 // to the attributes framework.
535 switch (attr->getFamily()) {
536 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
537 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
538 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
539 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
540 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
541 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
543 Family = static_cast<unsigned>(family);
547 family = getSelector().getMethodFamily();
549 case OMF_None: break;
551 // init only has a conventional meaning for an instance method, and
552 // it has to return an object.
554 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
558 // alloc/copy/new have a conventional meaning for both class and
559 // instance methods, but they require an object return.
562 case OMF_mutableCopy:
564 if (!getResultType()->isObjCObjectPointerType())
568 // These selectors have a conventional meaning only for instance methods.
573 case OMF_autorelease:
574 case OMF_retainCount:
576 if (!isInstanceMethod())
580 case OMF_performSelector:
581 if (!isInstanceMethod() ||
582 !getResultType()->isObjCIdType())
585 unsigned noParams = param_size();
586 if (noParams < 1 || noParams > 3)
589 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
590 QualType ArgT = (*it);
591 if (!ArgT->isObjCSelType()) {
598 if (!ArgT->isObjCIdType()) {
610 Family = static_cast<unsigned>(family);
614 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
615 const ObjCInterfaceDecl *OID) {
617 if (isInstanceMethod()) {
618 // There may be no interface context due to error in declaration
619 // of the interface (which has been reported). Recover gracefully.
621 selfTy = Context.getObjCInterfaceType(OID);
622 selfTy = Context.getObjCObjectPointerType(selfTy);
624 selfTy = Context.getObjCIdType();
626 } else // we have a factory method.
627 selfTy = Context.getObjCClassType();
629 bool selfIsPseudoStrong = false;
630 bool selfIsConsumed = false;
632 if (Context.getLangOpts().ObjCAutoRefCount) {
633 if (isInstanceMethod()) {
634 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
636 // 'self' is always __strong. It's actually pseudo-strong except
637 // in init methods (or methods labeled ns_consumes_self), though.
639 qs.setObjCLifetime(Qualifiers::OCL_Strong);
640 selfTy = Context.getQualifiedType(selfTy, qs);
642 // In addition, 'self' is const unless this is an init method.
643 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
644 selfTy = selfTy.withConst();
645 selfIsPseudoStrong = true;
649 assert(isClassMethod());
650 // 'self' is always const in class methods.
651 selfTy = selfTy.withConst();
652 selfIsPseudoStrong = true;
656 ImplicitParamDecl *self
657 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
658 &Context.Idents.get("self"), selfTy);
662 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
664 if (selfIsPseudoStrong)
665 self->setARCPseudoStrong(true);
667 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
668 &Context.Idents.get("_cmd"),
669 Context.getObjCSelType()));
672 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
673 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
675 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
676 return CD->getClassInterface();
677 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
678 return IMD->getClassInterface();
680 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
681 llvm_unreachable("unknown method context");
684 //===----------------------------------------------------------------------===//
686 //===----------------------------------------------------------------------===//
688 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
690 SourceLocation atLoc,
692 ObjCInterfaceDecl *PrevDecl,
693 SourceLocation ClassLoc,
695 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
696 PrevDecl, isInternal);
697 C.getObjCInterfaceType(Result, PrevDecl);
701 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
703 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
704 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
709 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
710 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
712 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
713 TypeForDecl(0), Data()
715 setPreviousDeclaration(PrevDecl);
717 // Copy the 'data' pointer over.
719 Data = PrevDecl->Data;
721 setImplicit(isInternal);
724 void ObjCInterfaceDecl::LoadExternalDefinition() const {
725 assert(data().ExternallyCompleted && "Class is not externally completed");
726 data().ExternallyCompleted = false;
727 getASTContext().getExternalSource()->CompleteType(
728 const_cast<ObjCInterfaceDecl *>(this));
731 void ObjCInterfaceDecl::setExternallyCompleted() {
732 assert(getASTContext().getExternalSource() &&
733 "Class can't be externally completed without an external source");
734 assert(hasDefinition() &&
735 "Forward declarations can't be externally completed");
736 data().ExternallyCompleted = true;
739 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
740 if (const ObjCInterfaceDecl *Def = getDefinition()) {
741 if (data().ExternallyCompleted)
742 LoadExternalDefinition();
744 return getASTContext().getObjCImplementation(
745 const_cast<ObjCInterfaceDecl*>(Def));
748 // FIXME: Should make sure no callers ever do this.
752 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
753 getASTContext().setObjCImplementation(getDefinition(), ImplD);
756 /// all_declared_ivar_begin - return first ivar declared in this class,
757 /// its extensions and its implementation. Lazily build the list on first
759 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
760 // FIXME: Should make sure no callers ever do this.
761 if (!hasDefinition())
765 return data().IvarList;
767 ObjCIvarDecl *curIvar = 0;
769 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
770 data().IvarList = (*I); ++I;
771 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
772 curIvar->setNextIvar(*I);
775 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
776 CDecl = CDecl->getNextClassExtension()) {
777 if (!CDecl->ivar_empty()) {
778 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
779 E = CDecl->ivar_end();
780 if (!data().IvarList) {
781 data().IvarList = (*I); ++I;
782 curIvar = data().IvarList;
784 for ( ;I != E; curIvar = *I, ++I)
785 curIvar->setNextIvar(*I);
789 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
790 if (!ImplDecl->ivar_empty()) {
791 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
792 E = ImplDecl->ivar_end();
793 if (!data().IvarList) {
794 data().IvarList = (*I); ++I;
795 curIvar = data().IvarList;
797 for ( ;I != E; curIvar = *I, ++I)
798 curIvar->setNextIvar(*I);
801 return data().IvarList;
804 /// FindCategoryDeclaration - Finds category declaration in the list of
805 /// categories for this class and returns it. Name of the category is passed
806 /// in 'CategoryId'. If category not found, return 0;
809 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
810 // FIXME: Should make sure no callers ever do this.
811 if (!hasDefinition())
814 if (data().ExternallyCompleted)
815 LoadExternalDefinition();
817 for (ObjCCategoryDecl *Category = getCategoryList();
818 Category; Category = Category->getNextClassCategory())
819 if (Category->getIdentifier() == CategoryId)
825 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
826 for (ObjCCategoryDecl *Category = getCategoryList();
827 Category; Category = Category->getNextClassCategory())
828 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
829 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
834 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
835 for (ObjCCategoryDecl *Category = getCategoryList();
836 Category; Category = Category->getNextClassCategory())
837 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
838 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
843 /// ClassImplementsProtocol - Checks that 'lProto' protocol
844 /// has been implemented in IDecl class, its super class or categories (if
845 /// lookupCategory is true).
846 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
848 bool RHSIsQualifiedID) {
849 if (!hasDefinition())
852 ObjCInterfaceDecl *IDecl = this;
853 // 1st, look up the class.
854 for (ObjCInterfaceDecl::protocol_iterator
855 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
856 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
858 // This is dubious and is added to be compatible with gcc. In gcc, it is
859 // also allowed assigning a protocol-qualified 'id' type to a LHS object
860 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
861 // object. This IMO, should be a bug.
862 // FIXME: Treat this as an extension, and flag this as an error when GCC
863 // extensions are not enabled.
864 if (RHSIsQualifiedID &&
865 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
869 // 2nd, look up the category.
871 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
872 CDecl = CDecl->getNextClassCategory()) {
873 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
874 E = CDecl->protocol_end(); PI != E; ++PI)
875 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
879 // 3rd, look up the super class(s)
880 if (IDecl->getSuperClass())
882 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
888 //===----------------------------------------------------------------------===//
890 //===----------------------------------------------------------------------===//
892 void ObjCIvarDecl::anchor() { }
894 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
895 SourceLocation StartLoc,
896 SourceLocation IdLoc, IdentifierInfo *Id,
897 QualType T, TypeSourceInfo *TInfo,
898 AccessControl ac, Expr *BW,
901 // Ivar's can only appear in interfaces, implementations (via synthesized
902 // properties), and class extensions (via direct declaration, or synthesized
905 // FIXME: This should really be asserting this:
906 // (isa<ObjCCategoryDecl>(DC) &&
907 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
908 // but unfortunately we sometimes place ivars into non-class extension
909 // categories on error. This breaks an AST invariant, and should not be
911 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
912 isa<ObjCCategoryDecl>(DC)) &&
913 "Invalid ivar decl context!");
914 // Once a new ivar is created in any of class/class-extension/implementation
915 // decl contexts, the previously built IvarList must be rebuilt.
916 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
918 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
919 ID = IM->getClassInterface();
921 IM->setHasSynthBitfield(true);
923 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
924 ID = CD->getClassInterface();
926 CD->setHasSynthBitfield(true);
932 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
933 ac, BW, synthesized);
936 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
937 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
938 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
939 QualType(), 0, ObjCIvarDecl::None, 0, false);
942 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
943 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
945 switch (DC->getKind()) {
947 case ObjCCategoryImpl:
949 llvm_unreachable("invalid ivar container!");
951 // Ivars can only appear in class extension categories.
953 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
954 assert(CD->IsClassExtension() && "invalid container for ivar!");
955 return CD->getClassInterface();
958 case ObjCImplementation:
959 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
962 return cast<ObjCInterfaceDecl>(DC);
966 //===----------------------------------------------------------------------===//
967 // ObjCAtDefsFieldDecl
968 //===----------------------------------------------------------------------===//
970 void ObjCAtDefsFieldDecl::anchor() { }
973 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
974 SourceLocation StartLoc, SourceLocation IdLoc,
975 IdentifierInfo *Id, QualType T, Expr *BW) {
976 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
979 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
981 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
982 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
986 //===----------------------------------------------------------------------===//
988 //===----------------------------------------------------------------------===//
990 void ObjCProtocolDecl::anchor() { }
992 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
993 SourceLocation nameLoc,
994 SourceLocation atStartLoc,
995 ObjCProtocolDecl *PrevDecl)
996 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
998 setPreviousDeclaration(PrevDecl);
1000 Data = PrevDecl->Data;
1003 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1005 SourceLocation nameLoc,
1006 SourceLocation atStartLoc,
1007 ObjCProtocolDecl *PrevDecl) {
1008 ObjCProtocolDecl *Result
1009 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1014 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1016 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1017 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1021 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1022 ObjCProtocolDecl *PDecl = this;
1024 if (Name == getIdentifier())
1027 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1028 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1034 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1036 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1037 bool isInstance) const {
1038 ObjCMethodDecl *MethodDecl = NULL;
1040 if ((MethodDecl = getMethod(Sel, isInstance)))
1043 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1044 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1049 void ObjCProtocolDecl::allocateDefinitionData() {
1050 assert(!Data && "Protocol already has a definition!");
1051 Data = new (getASTContext()) DefinitionData;
1052 Data->Definition = this;
1055 void ObjCProtocolDecl::startDefinition() {
1056 allocateDefinitionData();
1058 // Update all of the declarations with a pointer to the definition.
1059 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1061 RD->Data = this->Data;
1064 //===----------------------------------------------------------------------===//
1066 //===----------------------------------------------------------------------===//
1068 void ObjCCategoryDecl::anchor() { }
1070 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1071 SourceLocation AtLoc,
1072 SourceLocation ClassNameLoc,
1073 SourceLocation CategoryNameLoc,
1075 ObjCInterfaceDecl *IDecl,
1076 SourceLocation IvarLBraceLoc,
1077 SourceLocation IvarRBraceLoc) {
1078 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1079 CategoryNameLoc, Id,
1081 IvarLBraceLoc, IvarRBraceLoc);
1083 // Link this category into its class's category list.
1084 CatDecl->NextClassCategory = IDecl->getCategoryList();
1085 if (IDecl->hasDefinition()) {
1086 IDecl->setCategoryList(CatDecl);
1087 if (ASTMutationListener *L = C.getASTMutationListener())
1088 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1095 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1097 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1098 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1099 SourceLocation(), 0, 0);
1102 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1103 return getASTContext().getObjCImplementation(
1104 const_cast<ObjCCategoryDecl*>(this));
1107 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1108 getASTContext().setObjCImplementation(this, ImplD);
1112 //===----------------------------------------------------------------------===//
1113 // ObjCCategoryImplDecl
1114 //===----------------------------------------------------------------------===//
1116 void ObjCCategoryImplDecl::anchor() { }
1118 ObjCCategoryImplDecl *
1119 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1121 ObjCInterfaceDecl *ClassInterface,
1122 SourceLocation nameLoc,
1123 SourceLocation atStartLoc,
1124 SourceLocation CategoryNameLoc) {
1125 if (ClassInterface && ClassInterface->hasDefinition())
1126 ClassInterface = ClassInterface->getDefinition();
1127 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1128 nameLoc, atStartLoc, CategoryNameLoc);
1131 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1133 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1134 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1135 SourceLocation(), SourceLocation());
1138 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1139 // The class interface might be NULL if we are working with invalid code.
1140 if (const ObjCInterfaceDecl *ID = getClassInterface())
1141 return ID->FindCategoryDeclaration(getIdentifier());
1146 void ObjCImplDecl::anchor() { }
1148 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1149 // FIXME: The context should be correct before we get here.
1150 property->setLexicalDeclContext(this);
1154 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1155 ASTContext &Ctx = getASTContext();
1157 if (ObjCImplementationDecl *ImplD
1158 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1160 Ctx.setObjCImplementation(IFace, ImplD);
1162 } else if (ObjCCategoryImplDecl *ImplD =
1163 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1164 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1165 Ctx.setObjCImplementation(CD, ImplD);
1168 ClassInterface = IFace;
1171 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1172 /// properties implemented in this category @implementation block and returns
1173 /// the implemented property that uses it.
1175 ObjCPropertyImplDecl *ObjCImplDecl::
1176 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1177 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1178 ObjCPropertyImplDecl *PID = *i;
1179 if (PID->getPropertyIvarDecl() &&
1180 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1186 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1187 /// added to the list of those properties @synthesized/@dynamic in this
1188 /// category @implementation block.
1190 ObjCPropertyImplDecl *ObjCImplDecl::
1191 FindPropertyImplDecl(IdentifierInfo *Id) const {
1192 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1193 ObjCPropertyImplDecl *PID = *i;
1194 if (PID->getPropertyDecl()->getIdentifier() == Id)
1200 raw_ostream &clang::operator<<(raw_ostream &OS,
1201 const ObjCCategoryImplDecl &CID) {
1202 OS << CID.getName();
1206 //===----------------------------------------------------------------------===//
1207 // ObjCImplementationDecl
1208 //===----------------------------------------------------------------------===//
1210 void ObjCImplementationDecl::anchor() { }
1212 ObjCImplementationDecl *
1213 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1214 ObjCInterfaceDecl *ClassInterface,
1215 ObjCInterfaceDecl *SuperDecl,
1216 SourceLocation nameLoc,
1217 SourceLocation atStartLoc,
1218 SourceLocation IvarLBraceLoc,
1219 SourceLocation IvarRBraceLoc) {
1220 if (ClassInterface && ClassInterface->hasDefinition())
1221 ClassInterface = ClassInterface->getDefinition();
1222 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1223 nameLoc, atStartLoc,
1224 IvarLBraceLoc, IvarRBraceLoc);
1227 ObjCImplementationDecl *
1228 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1229 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1230 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1234 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1235 CXXCtorInitializer ** initializers,
1236 unsigned numInitializers) {
1237 if (numInitializers > 0) {
1238 NumIvarInitializers = numInitializers;
1239 CXXCtorInitializer **ivarInitializers =
1240 new (C) CXXCtorInitializer*[NumIvarInitializers];
1241 memcpy(ivarInitializers, initializers,
1242 numInitializers * sizeof(CXXCtorInitializer*));
1243 IvarInitializers = ivarInitializers;
1247 raw_ostream &clang::operator<<(raw_ostream &OS,
1248 const ObjCImplementationDecl &ID) {
1253 //===----------------------------------------------------------------------===//
1254 // ObjCCompatibleAliasDecl
1255 //===----------------------------------------------------------------------===//
1257 void ObjCCompatibleAliasDecl::anchor() { }
1259 ObjCCompatibleAliasDecl *
1260 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1263 ObjCInterfaceDecl* AliasedClass) {
1264 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1267 ObjCCompatibleAliasDecl *
1268 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1269 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1270 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1273 //===----------------------------------------------------------------------===//
1275 //===----------------------------------------------------------------------===//
1277 void ObjCPropertyDecl::anchor() { }
1279 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1282 SourceLocation AtLoc,
1283 SourceLocation LParenLoc,
1285 PropertyControl propControl) {
1286 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1289 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1291 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1292 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1297 //===----------------------------------------------------------------------===//
1298 // ObjCPropertyImplDecl
1299 //===----------------------------------------------------------------------===//
1301 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1303 SourceLocation atLoc,
1305 ObjCPropertyDecl *property,
1308 SourceLocation ivarLoc) {
1309 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1313 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1315 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1316 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1317 0, Dynamic, 0, SourceLocation());
1320 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1321 SourceLocation EndLoc = getLocation();
1322 if (IvarLoc.isValid())
1325 return SourceRange(AtLoc, EndLoc);