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 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
52 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
53 lookup_const_iterator Ivar, IvarEnd;
54 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
55 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 // Get the local instance/class method declared in this interface.
63 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
64 // Since instance & class methods can have the same name, the loop below
65 // ensures we get the correct method.
67 // @interface Whatever
68 // - (int) class_method;
69 // + (float) class_method;
72 lookup_const_iterator Meth, MethEnd;
73 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
74 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
75 if (MD && MD->isInstanceMethod() == isInstance)
82 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
83 IdentifierInfo *propertyID) {
85 DeclContext::lookup_const_iterator I, E;
86 llvm::tie(I, E) = DC->lookup(propertyID);
88 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
94 /// FindPropertyDeclaration - Finds declaration of the property given its name
95 /// in 'PropertyId' and returns it. It returns 0, if not found.
97 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
99 if (ObjCPropertyDecl *PD =
100 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
106 case Decl::ObjCProtocol: {
107 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
108 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
109 E = PID->protocol_end(); I != E; ++I)
110 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
114 case Decl::ObjCInterface: {
115 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
116 // Look through categories.
117 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
118 Cat; Cat = Cat->getNextClassCategory())
119 if (!Cat->IsClassExtension())
120 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
123 // Look through protocols.
124 for (ObjCInterfaceDecl::all_protocol_iterator
125 I = OID->all_referenced_protocol_begin(),
126 E = OID->all_referenced_protocol_end(); I != E; ++I)
127 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
130 // Finally, check the super class.
131 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
132 return superClass->FindPropertyDeclaration(PropertyId);
135 case Decl::ObjCCategory: {
136 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
137 // Look through protocols.
138 if (!OCD->IsClassExtension())
139 for (ObjCCategoryDecl::protocol_iterator
140 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
141 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
150 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
151 /// with name 'PropertyId' in the primary class; including those in protocols
152 /// (direct or indirect) used by the primary class.
155 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
156 IdentifierInfo *PropertyId) const {
157 if (ExternallyCompleted)
158 LoadExternalDefinition();
160 if (ObjCPropertyDecl *PD =
161 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
164 // Look through protocols.
165 for (ObjCInterfaceDecl::all_protocol_iterator
166 I = all_referenced_protocol_begin(),
167 E = all_referenced_protocol_end(); I != E; ++I)
168 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
174 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
175 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
178 if (ExternallyCompleted)
179 LoadExternalDefinition();
181 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
182 AllReferencedProtocols.set(ExtList, ExtNum, C);
186 // Check for duplicate protocol in class's protocol list.
187 // This is O(n*m). But it is extremely rare and number of protocols in
188 // class or its extension are very few.
189 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
190 for (unsigned i = 0; i < ExtNum; i++) {
191 bool protocolExists = false;
192 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
193 for (all_protocol_iterator
194 p = all_referenced_protocol_begin(),
195 e = all_referenced_protocol_end(); p != e; ++p) {
196 ObjCProtocolDecl *Proto = (*p);
197 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
198 protocolExists = true;
202 // Do we want to warn on a protocol in extension class which
203 // already exist in the class? Probably not.
205 ProtocolRefs.push_back(ProtoInExtension);
208 if (ProtocolRefs.empty())
211 // Merge ProtocolRefs into class's protocol list;
212 for (all_protocol_iterator p = all_referenced_protocol_begin(),
213 e = all_referenced_protocol_end(); p != e; ++p) {
214 ProtocolRefs.push_back(*p);
217 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
220 /// getFirstClassExtension - Find first class extension of the given class.
221 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
222 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
223 CDecl = CDecl->getNextClassCategory())
224 if (CDecl->IsClassExtension())
229 /// getNextClassCategory - Find next class extension in list of categories.
230 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
231 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
232 CDecl = CDecl->getNextClassCategory())
233 if (CDecl->IsClassExtension())
238 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
239 ObjCInterfaceDecl *&clsDeclared) {
240 ObjCInterfaceDecl* ClassDecl = this;
241 while (ClassDecl != NULL) {
242 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
243 clsDeclared = ClassDecl;
246 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
247 CDecl; CDecl = CDecl->getNextClassExtension()) {
248 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
249 clsDeclared = ClassDecl;
254 ClassDecl = ClassDecl->getSuperClass();
259 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
260 /// class whose name is passed as argument. If it is not one of the super classes
261 /// the it returns NULL.
262 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
263 const IdentifierInfo*ICName) {
264 ObjCInterfaceDecl* ClassDecl = this;
265 while (ClassDecl != NULL) {
266 if (ClassDecl->getIdentifier() == ICName)
268 ClassDecl = ClassDecl->getSuperClass();
273 /// lookupMethod - This method returns an instance/class method by looking in
274 /// the class, its categories, and its super classes (using a linear search).
275 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
276 bool isInstance) const {
277 const ObjCInterfaceDecl* ClassDecl = this;
278 ObjCMethodDecl *MethodDecl = 0;
280 if (ExternallyCompleted)
281 LoadExternalDefinition();
283 while (ClassDecl != NULL) {
284 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
287 // Didn't find one yet - look through protocols.
288 const ObjCList<ObjCProtocolDecl> &Protocols =
289 ClassDecl->getReferencedProtocols();
290 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
291 E = Protocols.end(); I != E; ++I)
292 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
295 // Didn't find one yet - now look through categories.
296 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
298 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
301 // Didn't find one yet - look through protocols.
302 const ObjCList<ObjCProtocolDecl> &Protocols =
303 CatDecl->getReferencedProtocols();
304 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
305 E = Protocols.end(); I != E; ++I)
306 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
308 CatDecl = CatDecl->getNextClassCategory();
310 ClassDecl = ClassDecl->getSuperClass();
315 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
318 ObjCMethodDecl *Method = 0;
319 if (ObjCImplementationDecl *ImpDecl = getImplementation())
320 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
321 : ImpDecl->getClassMethod(Sel);
323 if (!Method && getSuperClass())
324 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
328 //===----------------------------------------------------------------------===//
330 //===----------------------------------------------------------------------===//
332 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
333 SourceLocation beginLoc,
334 SourceLocation endLoc,
335 Selector SelInfo, QualType T,
336 TypeSourceInfo *ResultTInfo,
337 DeclContext *contextDecl,
341 bool isImplicitlyDeclared,
343 ImplementationControl impControl,
344 bool HasRelatedResultType) {
345 return new (C) ObjCMethodDecl(beginLoc, endLoc,
346 SelInfo, T, ResultTInfo, contextDecl,
348 isVariadic, isSynthesized, isImplicitlyDeclared,
351 HasRelatedResultType);
354 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
356 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
357 IsRedeclaration = true;
358 PrevMethod->HasRedeclaration = true;
361 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
362 ArrayRef<ParmVarDecl*> Params,
363 ArrayRef<SourceLocation> SelLocs) {
364 ParamsAndSelLocs = 0;
365 NumParams = Params.size();
366 if (Params.empty() && SelLocs.empty())
369 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
370 sizeof(SourceLocation) * SelLocs.size();
371 ParamsAndSelLocs = C.Allocate(Size);
372 std::copy(Params.begin(), Params.end(), getParams());
373 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
376 void ObjCMethodDecl::getSelectorLocs(
377 SmallVectorImpl<SourceLocation> &SelLocs) const {
378 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
379 SelLocs.push_back(getSelectorLoc(i));
382 void ObjCMethodDecl::setMethodParams(ASTContext &C,
383 ArrayRef<ParmVarDecl*> Params,
384 ArrayRef<SourceLocation> SelLocs) {
385 assert((!SelLocs.empty() || isImplicit()) &&
386 "No selector locs for non-implicit method");
388 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
390 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
391 if (SelLocsKind != SelLoc_NonStandard)
392 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
394 setParamsAndSelLocs(C, Params, SelLocs);
397 /// \brief A definition will return its interface declaration.
398 /// An interface declaration will return its definition.
399 /// Otherwise it will return itself.
400 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
401 ASTContext &Ctx = getASTContext();
402 ObjCMethodDecl *Redecl = 0;
403 if (HasRedeclaration)
404 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
408 Decl *CtxD = cast<Decl>(getDeclContext());
410 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
411 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
412 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
414 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
415 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
416 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
418 } else if (ObjCImplementationDecl *ImplD =
419 dyn_cast<ObjCImplementationDecl>(CtxD)) {
420 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
421 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
423 } else if (ObjCCategoryImplDecl *CImplD =
424 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
425 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
426 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
429 if (!Redecl && isRedeclaration()) {
430 // This is the last redeclaration, go back to the first method.
431 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
435 return Redecl ? Redecl : this;
438 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
439 Decl *CtxD = cast<Decl>(getDeclContext());
441 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
442 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
443 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
447 } else if (ObjCCategoryImplDecl *CImplD =
448 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
449 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
450 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
458 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
459 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
460 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
463 // Check for an explicit attribute.
464 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
465 // The unfortunate necessity of mapping between enums here is due
466 // to the attributes framework.
467 switch (attr->getFamily()) {
468 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
469 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
470 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
471 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
472 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
473 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
475 Family = static_cast<unsigned>(family);
479 family = getSelector().getMethodFamily();
481 case OMF_None: break;
483 // init only has a conventional meaning for an instance method, and
484 // it has to return an object.
486 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
490 // alloc/copy/new have a conventional meaning for both class and
491 // instance methods, but they require an object return.
494 case OMF_mutableCopy:
496 if (!getResultType()->isObjCObjectPointerType())
500 // These selectors have a conventional meaning only for instance methods.
505 case OMF_autorelease:
506 case OMF_retainCount:
508 if (!isInstanceMethod())
512 case OMF_performSelector:
513 if (!isInstanceMethod() ||
514 !getResultType()->isObjCIdType())
517 unsigned noParams = param_size();
518 if (noParams < 1 || noParams > 3)
521 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
522 QualType ArgT = (*it);
523 if (!ArgT->isObjCSelType()) {
530 if (!ArgT->isObjCIdType()) {
542 Family = static_cast<unsigned>(family);
546 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
547 const ObjCInterfaceDecl *OID) {
549 if (isInstanceMethod()) {
550 // There may be no interface context due to error in declaration
551 // of the interface (which has been reported). Recover gracefully.
553 selfTy = Context.getObjCInterfaceType(OID);
554 selfTy = Context.getObjCObjectPointerType(selfTy);
556 selfTy = Context.getObjCIdType();
558 } else // we have a factory method.
559 selfTy = Context.getObjCClassType();
561 bool selfIsPseudoStrong = false;
562 bool selfIsConsumed = false;
563 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
564 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
566 // 'self' is always __strong. It's actually pseudo-strong except
567 // in init methods, though.
569 qs.setObjCLifetime(Qualifiers::OCL_Strong);
570 selfTy = Context.getQualifiedType(selfTy, qs);
572 // In addition, 'self' is const unless this is an init method.
573 if (getMethodFamily() != OMF_init) {
574 selfTy = selfTy.withConst();
575 selfIsPseudoStrong = true;
579 ImplicitParamDecl *self
580 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
581 &Context.Idents.get("self"), selfTy);
585 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
587 if (selfIsPseudoStrong)
588 self->setARCPseudoStrong(true);
590 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
591 &Context.Idents.get("_cmd"),
592 Context.getObjCSelType()));
595 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
596 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
598 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
599 return CD->getClassInterface();
600 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
601 return IMD->getClassInterface();
603 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
604 llvm_unreachable("unknown method context");
607 //===----------------------------------------------------------------------===//
609 //===----------------------------------------------------------------------===//
611 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
613 SourceLocation atLoc,
615 SourceLocation ClassLoc,
616 bool ForwardDecl, bool isInternal){
617 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
622 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
623 SourceLocation CLoc, bool FD, bool isInternal)
624 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
625 TypeForDecl(0), SuperClass(0),
626 CategoryList(0), IvarList(0),
627 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false) {
630 void ObjCInterfaceDecl::LoadExternalDefinition() const {
631 assert(ExternallyCompleted && "Class is not externally completed");
632 ExternallyCompleted = false;
633 getASTContext().getExternalSource()->CompleteType(
634 const_cast<ObjCInterfaceDecl *>(this));
637 void ObjCInterfaceDecl::setExternallyCompleted() {
638 assert(getASTContext().getExternalSource() &&
639 "Class can't be externally completed without an external source");
640 assert(!ForwardDecl &&
641 "Forward declarations can't be externally completed");
642 ExternallyCompleted = true;
645 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
646 if (ExternallyCompleted)
647 LoadExternalDefinition();
649 return getASTContext().getObjCImplementation(
650 const_cast<ObjCInterfaceDecl*>(this));
653 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
654 getASTContext().setObjCImplementation(this, ImplD);
657 /// all_declared_ivar_begin - return first ivar declared in this class,
658 /// its extensions and its implementation. Lazily build the list on first
660 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
664 ObjCIvarDecl *curIvar = 0;
666 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
667 IvarList = (*I); ++I;
668 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
669 curIvar->setNextIvar(*I);
672 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
673 CDecl = CDecl->getNextClassExtension()) {
674 if (!CDecl->ivar_empty()) {
675 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
676 E = CDecl->ivar_end();
678 IvarList = (*I); ++I;
681 for ( ;I != E; curIvar = *I, ++I)
682 curIvar->setNextIvar(*I);
686 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
687 if (!ImplDecl->ivar_empty()) {
688 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
689 E = ImplDecl->ivar_end();
691 IvarList = (*I); ++I;
694 for ( ;I != E; curIvar = *I, ++I)
695 curIvar->setNextIvar(*I);
701 /// FindCategoryDeclaration - Finds category declaration in the list of
702 /// categories for this class and returns it. Name of the category is passed
703 /// in 'CategoryId'. If category not found, return 0;
706 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
707 if (ExternallyCompleted)
708 LoadExternalDefinition();
710 for (ObjCCategoryDecl *Category = getCategoryList();
711 Category; Category = Category->getNextClassCategory())
712 if (Category->getIdentifier() == CategoryId)
718 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
719 for (ObjCCategoryDecl *Category = getCategoryList();
720 Category; Category = Category->getNextClassCategory())
721 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
722 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
727 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
728 for (ObjCCategoryDecl *Category = getCategoryList();
729 Category; Category = Category->getNextClassCategory())
730 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
731 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
736 /// ClassImplementsProtocol - Checks that 'lProto' protocol
737 /// has been implemented in IDecl class, its super class or categories (if
738 /// lookupCategory is true).
739 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
741 bool RHSIsQualifiedID) {
742 ObjCInterfaceDecl *IDecl = this;
743 // 1st, look up the class.
744 const ObjCList<ObjCProtocolDecl> &Protocols =
745 IDecl->getReferencedProtocols();
747 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
748 E = Protocols.end(); PI != E; ++PI) {
749 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
751 // This is dubious and is added to be compatible with gcc. In gcc, it is
752 // also allowed assigning a protocol-qualified 'id' type to a LHS object
753 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
754 // object. This IMO, should be a bug.
755 // FIXME: Treat this as an extension, and flag this as an error when GCC
756 // extensions are not enabled.
757 if (RHSIsQualifiedID &&
758 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
762 // 2nd, look up the category.
764 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
765 CDecl = CDecl->getNextClassCategory()) {
766 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
767 E = CDecl->protocol_end(); PI != E; ++PI)
768 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
772 // 3rd, look up the super class(s)
773 if (IDecl->getSuperClass())
775 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
781 //===----------------------------------------------------------------------===//
783 //===----------------------------------------------------------------------===//
785 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
786 SourceLocation StartLoc,
787 SourceLocation IdLoc, IdentifierInfo *Id,
788 QualType T, TypeSourceInfo *TInfo,
789 AccessControl ac, Expr *BW,
792 // Ivar's can only appear in interfaces, implementations (via synthesized
793 // properties), and class extensions (via direct declaration, or synthesized
796 // FIXME: This should really be asserting this:
797 // (isa<ObjCCategoryDecl>(DC) &&
798 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
799 // but unfortunately we sometimes place ivars into non-class extension
800 // categories on error. This breaks an AST invariant, and should not be
802 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
803 isa<ObjCCategoryDecl>(DC)) &&
804 "Invalid ivar decl context!");
805 // Once a new ivar is created in any of class/class-extension/implementation
806 // decl contexts, the previously built IvarList must be rebuilt.
807 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
809 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
810 ID = IM->getClassInterface();
812 IM->setHasSynthBitfield(true);
814 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
815 ID = CD->getClassInterface();
817 CD->setHasSynthBitfield(true);
823 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
824 ac, BW, synthesized);
827 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
828 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
830 switch (DC->getKind()) {
832 case ObjCCategoryImpl:
834 llvm_unreachable("invalid ivar container!");
836 // Ivars can only appear in class extension categories.
838 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
839 assert(CD->IsClassExtension() && "invalid container for ivar!");
840 return CD->getClassInterface();
843 case ObjCImplementation:
844 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
847 return cast<ObjCInterfaceDecl>(DC);
851 //===----------------------------------------------------------------------===//
852 // ObjCAtDefsFieldDecl
853 //===----------------------------------------------------------------------===//
856 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
857 SourceLocation StartLoc, SourceLocation IdLoc,
858 IdentifierInfo *Id, QualType T, Expr *BW) {
859 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
862 //===----------------------------------------------------------------------===//
864 //===----------------------------------------------------------------------===//
866 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
868 SourceLocation nameLoc,
869 SourceLocation atStartLoc) {
870 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc);
873 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
874 ObjCProtocolDecl *PDecl = this;
876 if (Name == getIdentifier())
879 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
880 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
886 // lookupMethod - Lookup a instance/class method in the protocol and protocols
888 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
889 bool isInstance) const {
890 ObjCMethodDecl *MethodDecl = NULL;
892 if ((MethodDecl = getMethod(Sel, isInstance)))
895 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
896 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
901 //===----------------------------------------------------------------------===//
903 //===----------------------------------------------------------------------===//
905 ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
906 ObjCInterfaceDecl *const Elt,
907 const SourceLocation Loc,
909 : Decl(ObjCClass, DC, L) {
910 setClass(C, Elt, Loc);
913 ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
915 ObjCInterfaceDecl *const Elt,
916 const SourceLocation Loc) {
917 return new (C) ObjCClassDecl(DC, L, Elt, Loc, C);
920 void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls,
921 const SourceLocation Loc) {
923 ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef),
924 llvm::alignOf<ObjCClassRef>());
925 new (ForwardDecl) ObjCClassRef(Cls, Loc);
928 SourceRange ObjCClassDecl::getSourceRange() const {
929 // FIXME: We should include the semicolon
930 return SourceRange(getLocation(), ForwardDecl->getLocation());
933 //===----------------------------------------------------------------------===//
934 // ObjCForwardProtocolDecl
935 //===----------------------------------------------------------------------===//
937 ObjCForwardProtocolDecl::
938 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
939 ObjCProtocolDecl *const *Elts, unsigned nElts,
940 const SourceLocation *Locs, ASTContext &C)
941 : Decl(ObjCForwardProtocol, DC, L) {
942 ReferencedProtocols.set(Elts, nElts, Locs, C);
946 ObjCForwardProtocolDecl *
947 ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
949 ObjCProtocolDecl *const *Elts,
951 const SourceLocation *Locs) {
952 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
955 //===----------------------------------------------------------------------===//
957 //===----------------------------------------------------------------------===//
959 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
960 SourceLocation AtLoc,
961 SourceLocation ClassNameLoc,
962 SourceLocation CategoryNameLoc,
964 ObjCInterfaceDecl *IDecl) {
965 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
969 // Link this category into its class's category list.
970 CatDecl->NextClassCategory = IDecl->getCategoryList();
971 IDecl->setCategoryList(CatDecl);
972 if (ASTMutationListener *L = C.getASTMutationListener())
973 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
979 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
980 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
981 SourceLocation(), 0, 0);
984 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
985 return getASTContext().getObjCImplementation(
986 const_cast<ObjCCategoryDecl*>(this));
989 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
990 getASTContext().setObjCImplementation(this, ImplD);
994 //===----------------------------------------------------------------------===//
995 // ObjCCategoryImplDecl
996 //===----------------------------------------------------------------------===//
998 ObjCCategoryImplDecl *
999 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1001 ObjCInterfaceDecl *ClassInterface,
1002 SourceLocation nameLoc,
1003 SourceLocation atStartLoc) {
1004 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1005 nameLoc, atStartLoc);
1008 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1009 // The class interface might be NULL if we are working with invalid code.
1010 if (const ObjCInterfaceDecl *ID = getClassInterface())
1011 return ID->FindCategoryDeclaration(getIdentifier());
1016 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1017 // FIXME: The context should be correct before we get here.
1018 property->setLexicalDeclContext(this);
1022 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1023 ASTContext &Ctx = getASTContext();
1025 if (ObjCImplementationDecl *ImplD
1026 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1028 Ctx.setObjCImplementation(IFace, ImplD);
1030 } else if (ObjCCategoryImplDecl *ImplD =
1031 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1032 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1033 Ctx.setObjCImplementation(CD, ImplD);
1036 ClassInterface = IFace;
1039 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1040 /// properties implemented in this category @implementation block and returns
1041 /// the implemented property that uses it.
1043 ObjCPropertyImplDecl *ObjCImplDecl::
1044 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1045 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1046 ObjCPropertyImplDecl *PID = *i;
1047 if (PID->getPropertyIvarDecl() &&
1048 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1054 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1055 /// added to the list of those properties @synthesized/@dynamic in this
1056 /// category @implementation block.
1058 ObjCPropertyImplDecl *ObjCImplDecl::
1059 FindPropertyImplDecl(IdentifierInfo *Id) const {
1060 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1061 ObjCPropertyImplDecl *PID = *i;
1062 if (PID->getPropertyDecl()->getIdentifier() == Id)
1068 raw_ostream &clang::operator<<(raw_ostream &OS,
1069 const ObjCCategoryImplDecl *CID) {
1070 OS << CID->getName();
1074 //===----------------------------------------------------------------------===//
1075 // ObjCImplementationDecl
1076 //===----------------------------------------------------------------------===//
1078 ObjCImplementationDecl *
1079 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1080 ObjCInterfaceDecl *ClassInterface,
1081 ObjCInterfaceDecl *SuperDecl,
1082 SourceLocation nameLoc,
1083 SourceLocation atStartLoc) {
1084 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1085 nameLoc, atStartLoc);
1088 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1089 CXXCtorInitializer ** initializers,
1090 unsigned numInitializers) {
1091 if (numInitializers > 0) {
1092 NumIvarInitializers = numInitializers;
1093 CXXCtorInitializer **ivarInitializers =
1094 new (C) CXXCtorInitializer*[NumIvarInitializers];
1095 memcpy(ivarInitializers, initializers,
1096 numInitializers * sizeof(CXXCtorInitializer*));
1097 IvarInitializers = ivarInitializers;
1101 raw_ostream &clang::operator<<(raw_ostream &OS,
1102 const ObjCImplementationDecl *ID) {
1103 OS << ID->getName();
1107 //===----------------------------------------------------------------------===//
1108 // ObjCCompatibleAliasDecl
1109 //===----------------------------------------------------------------------===//
1111 ObjCCompatibleAliasDecl *
1112 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1115 ObjCInterfaceDecl* AliasedClass) {
1116 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1119 //===----------------------------------------------------------------------===//
1121 //===----------------------------------------------------------------------===//
1123 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1126 SourceLocation AtLoc,
1128 PropertyControl propControl) {
1129 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
1132 //===----------------------------------------------------------------------===//
1133 // ObjCPropertyImplDecl
1134 //===----------------------------------------------------------------------===//
1136 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1138 SourceLocation atLoc,
1140 ObjCPropertyDecl *property,
1143 SourceLocation ivarLoc) {
1144 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1148 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1149 SourceLocation EndLoc = getLocation();
1150 if (IvarLoc.isValid())
1153 return SourceRange(AtLoc, EndLoc);