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 "llvm/ADT/STLExtras.h"
18 using namespace clang;
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
24 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
26 if (Elts == 0) return; // Setting to an empty list is a noop.
29 List = new (Ctx) void*[Elts];
31 memcpy(List, InList, sizeof(void*)*Elts);
34 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
35 const SourceLocation *Locs, ASTContext &Ctx) {
39 Locations = new (Ctx) SourceLocation[Elts];
40 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
41 set(InList, Elts, Ctx);
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
51 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
52 lookup_const_iterator Ivar, IvarEnd;
53 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
54 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
60 // Get the local instance/class method declared in this interface.
62 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
63 // Since instance & class methods can have the same name, the loop below
64 // ensures we get the correct method.
66 // @interface Whatever
67 // - (int) class_method;
68 // + (float) class_method;
71 lookup_const_iterator Meth, MethEnd;
72 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
73 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
74 if (MD && MD->isInstanceMethod() == isInstance)
81 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
82 IdentifierInfo *propertyID) {
84 DeclContext::lookup_const_iterator I, E;
85 llvm::tie(I, E) = DC->lookup(propertyID);
87 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
93 /// FindPropertyDeclaration - Finds declaration of the property given its name
94 /// in 'PropertyId' and returns it. It returns 0, if not found.
96 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
98 if (ObjCPropertyDecl *PD =
99 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
105 case Decl::ObjCProtocol: {
106 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
107 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
108 E = PID->protocol_end(); I != E; ++I)
109 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
113 case Decl::ObjCInterface: {
114 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
115 // Look through categories.
116 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
117 Cat; Cat = Cat->getNextClassCategory())
118 if (!Cat->IsClassExtension())
119 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
122 // Look through protocols.
123 for (ObjCInterfaceDecl::all_protocol_iterator
124 I = OID->all_referenced_protocol_begin(),
125 E = OID->all_referenced_protocol_end(); I != E; ++I)
126 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
129 // Finally, check the super class.
130 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
131 return superClass->FindPropertyDeclaration(PropertyId);
134 case Decl::ObjCCategory: {
135 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
136 // Look through protocols.
137 if (!OCD->IsClassExtension())
138 for (ObjCCategoryDecl::protocol_iterator
139 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
149 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
150 /// with name 'PropertyId' in the primary class; including those in protocols
151 /// (direct or indirect) used by the primary class.
154 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
155 IdentifierInfo *PropertyId) const {
156 if (ExternallyCompleted)
157 LoadExternalDefinition();
159 if (ObjCPropertyDecl *PD =
160 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
163 // Look through protocols.
164 for (ObjCInterfaceDecl::all_protocol_iterator
165 I = all_referenced_protocol_begin(),
166 E = all_referenced_protocol_end(); I != E; ++I)
167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
173 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
174 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
177 if (ExternallyCompleted)
178 LoadExternalDefinition();
180 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
181 AllReferencedProtocols.set(ExtList, ExtNum, C);
185 // Check for duplicate protocol in class's protocol list.
186 // This is O(n*m). But it is extremely rare and number of protocols in
187 // class or its extension are very few.
188 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
189 for (unsigned i = 0; i < ExtNum; i++) {
190 bool protocolExists = false;
191 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
192 for (all_protocol_iterator
193 p = all_referenced_protocol_begin(),
194 e = all_referenced_protocol_end(); p != e; ++p) {
195 ObjCProtocolDecl *Proto = (*p);
196 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
197 protocolExists = true;
201 // Do we want to warn on a protocol in extension class which
202 // already exist in the class? Probably not.
204 ProtocolRefs.push_back(ProtoInExtension);
207 if (ProtocolRefs.empty())
210 // Merge ProtocolRefs into class's protocol list;
211 for (all_protocol_iterator p = all_referenced_protocol_begin(),
212 e = all_referenced_protocol_end(); p != e; ++p) {
213 ProtocolRefs.push_back(*p);
216 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
219 /// getFirstClassExtension - Find first class extension of the given class.
220 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
221 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
222 CDecl = CDecl->getNextClassCategory())
223 if (CDecl->IsClassExtension())
228 /// getNextClassCategory - Find next class extension in list of categories.
229 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
230 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
231 CDecl = CDecl->getNextClassCategory())
232 if (CDecl->IsClassExtension())
237 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
238 ObjCInterfaceDecl *&clsDeclared) {
239 ObjCInterfaceDecl* ClassDecl = this;
240 while (ClassDecl != NULL) {
241 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
242 clsDeclared = ClassDecl;
245 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
246 CDecl; CDecl = CDecl->getNextClassExtension()) {
247 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
248 clsDeclared = ClassDecl;
253 ClassDecl = ClassDecl->getSuperClass();
258 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
259 /// class whose name is passed as argument. If it is not one of the super classes
260 /// the it returns NULL.
261 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
262 const IdentifierInfo*ICName) {
263 ObjCInterfaceDecl* ClassDecl = this;
264 while (ClassDecl != NULL) {
265 if (ClassDecl->getIdentifier() == ICName)
267 ClassDecl = ClassDecl->getSuperClass();
272 /// lookupMethod - This method returns an instance/class method by looking in
273 /// the class, its categories, and its super classes (using a linear search).
274 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
275 bool isInstance) const {
276 const ObjCInterfaceDecl* ClassDecl = this;
277 ObjCMethodDecl *MethodDecl = 0;
279 if (ExternallyCompleted)
280 LoadExternalDefinition();
282 while (ClassDecl != NULL) {
283 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
286 // Didn't find one yet - look through protocols.
287 const ObjCList<ObjCProtocolDecl> &Protocols =
288 ClassDecl->getReferencedProtocols();
289 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
290 E = Protocols.end(); I != E; ++I)
291 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
294 // Didn't find one yet - now look through categories.
295 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
297 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
300 // Didn't find one yet - look through protocols.
301 const ObjCList<ObjCProtocolDecl> &Protocols =
302 CatDecl->getReferencedProtocols();
303 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
304 E = Protocols.end(); I != E; ++I)
305 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
307 CatDecl = CatDecl->getNextClassCategory();
309 ClassDecl = ClassDecl->getSuperClass();
314 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
317 ObjCMethodDecl *Method = 0;
318 if (ObjCImplementationDecl *ImpDecl = getImplementation())
319 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
320 : ImpDecl->getClassMethod(Sel);
322 if (!Method && getSuperClass())
323 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
327 //===----------------------------------------------------------------------===//
329 //===----------------------------------------------------------------------===//
331 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
332 SourceLocation beginLoc,
333 SourceLocation endLoc,
334 Selector SelInfo, QualType T,
335 TypeSourceInfo *ResultTInfo,
336 DeclContext *contextDecl,
341 ImplementationControl impControl,
342 bool HasRelatedResultType,
343 unsigned numSelectorArgs) {
344 return new (C) ObjCMethodDecl(beginLoc, endLoc,
345 SelInfo, T, ResultTInfo, contextDecl,
347 isVariadic, isSynthesized, isDefined,
349 HasRelatedResultType,
353 /// \brief A definition will return its interface declaration.
354 /// An interface declaration will return its definition.
355 /// Otherwise it will return itself.
356 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
357 ASTContext &Ctx = getASTContext();
358 ObjCMethodDecl *Redecl = 0;
359 Decl *CtxD = cast<Decl>(getDeclContext());
361 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
362 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
363 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
365 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
366 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
367 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
369 } else if (ObjCImplementationDecl *ImplD =
370 dyn_cast<ObjCImplementationDecl>(CtxD)) {
371 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
372 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
374 } else if (ObjCCategoryImplDecl *CImplD =
375 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
376 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
377 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
380 return Redecl ? Redecl : this;
383 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
384 Decl *CtxD = cast<Decl>(getDeclContext());
386 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
387 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
388 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
392 } else if (ObjCCategoryImplDecl *CImplD =
393 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
394 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
395 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
403 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
404 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
405 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
408 // Check for an explicit attribute.
409 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
410 // The unfortunate necessity of mapping between enums here is due
411 // to the attributes framework.
412 switch (attr->getFamily()) {
413 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
414 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
415 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
416 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
417 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
418 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
420 Family = static_cast<unsigned>(family);
424 family = getSelector().getMethodFamily();
426 case OMF_None: break;
428 // init only has a conventional meaning for an instance method, and
429 // it has to return an object.
431 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
435 // alloc/copy/new have a conventional meaning for both class and
436 // instance methods, but they require an object return.
439 case OMF_mutableCopy:
441 if (!getResultType()->isObjCObjectPointerType())
445 // These selectors have a conventional meaning only for instance methods.
449 case OMF_autorelease:
450 case OMF_retainCount:
452 if (!isInstanceMethod())
458 Family = static_cast<unsigned>(family);
462 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
463 const ObjCInterfaceDecl *OID) {
465 if (isInstanceMethod()) {
466 // There may be no interface context due to error in declaration
467 // of the interface (which has been reported). Recover gracefully.
469 selfTy = Context.getObjCInterfaceType(OID);
470 selfTy = Context.getObjCObjectPointerType(selfTy);
472 selfTy = Context.getObjCIdType();
474 } else // we have a factory method.
475 selfTy = Context.getObjCClassType();
477 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
478 &Context.Idents.get("self"), selfTy));
480 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
481 &Context.Idents.get("_cmd"),
482 Context.getObjCSelType()));
485 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
486 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
488 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
489 return CD->getClassInterface();
490 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
491 return IMD->getClassInterface();
493 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
494 assert(false && "unknown method context");
498 //===----------------------------------------------------------------------===//
500 //===----------------------------------------------------------------------===//
502 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
504 SourceLocation atLoc,
506 SourceLocation ClassLoc,
507 bool ForwardDecl, bool isInternal){
508 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
513 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
514 SourceLocation CLoc, bool FD, bool isInternal)
515 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
516 TypeForDecl(0), SuperClass(0),
517 CategoryList(0), IvarList(0),
518 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false),
522 void ObjCInterfaceDecl::LoadExternalDefinition() const {
523 assert(ExternallyCompleted && "Class is not externally completed");
524 ExternallyCompleted = false;
525 getASTContext().getExternalSource()->CompleteType(
526 const_cast<ObjCInterfaceDecl *>(this));
529 void ObjCInterfaceDecl::setExternallyCompleted() {
530 assert(getASTContext().getExternalSource() &&
531 "Class can't be externally completed without an external source");
532 assert(!ForwardDecl &&
533 "Forward declarations can't be externally completed");
534 ExternallyCompleted = true;
537 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
538 if (ExternallyCompleted)
539 LoadExternalDefinition();
541 return getASTContext().getObjCImplementation(
542 const_cast<ObjCInterfaceDecl*>(this));
545 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
546 getASTContext().setObjCImplementation(this, ImplD);
549 /// all_declared_ivar_begin - return first ivar declared in this class,
550 /// its extensions and its implementation. Lazily build the list on first
552 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
556 ObjCIvarDecl *curIvar = 0;
558 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
559 IvarList = (*I); ++I;
560 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
561 curIvar->setNextIvar(*I);
564 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
565 CDecl = CDecl->getNextClassExtension()) {
566 if (!CDecl->ivar_empty()) {
567 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
568 E = CDecl->ivar_end();
570 IvarList = (*I); ++I;
573 for ( ;I != E; curIvar = *I, ++I)
574 curIvar->setNextIvar(*I);
578 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
579 if (!ImplDecl->ivar_empty()) {
580 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
581 E = ImplDecl->ivar_end();
583 IvarList = (*I); ++I;
586 for ( ;I != E; curIvar = *I, ++I)
587 curIvar->setNextIvar(*I);
593 /// FindCategoryDeclaration - Finds category declaration in the list of
594 /// categories for this class and returns it. Name of the category is passed
595 /// in 'CategoryId'. If category not found, return 0;
598 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
599 if (ExternallyCompleted)
600 LoadExternalDefinition();
602 for (ObjCCategoryDecl *Category = getCategoryList();
603 Category; Category = Category->getNextClassCategory())
604 if (Category->getIdentifier() == CategoryId)
610 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
611 for (ObjCCategoryDecl *Category = getCategoryList();
612 Category; Category = Category->getNextClassCategory())
613 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
614 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
619 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
620 for (ObjCCategoryDecl *Category = getCategoryList();
621 Category; Category = Category->getNextClassCategory())
622 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
623 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
628 /// ClassImplementsProtocol - Checks that 'lProto' protocol
629 /// has been implemented in IDecl class, its super class or categories (if
630 /// lookupCategory is true).
631 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
633 bool RHSIsQualifiedID) {
634 ObjCInterfaceDecl *IDecl = this;
635 // 1st, look up the class.
636 const ObjCList<ObjCProtocolDecl> &Protocols =
637 IDecl->getReferencedProtocols();
639 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
640 E = Protocols.end(); PI != E; ++PI) {
641 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
643 // This is dubious and is added to be compatible with gcc. In gcc, it is
644 // also allowed assigning a protocol-qualified 'id' type to a LHS object
645 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
646 // object. This IMO, should be a bug.
647 // FIXME: Treat this as an extension, and flag this as an error when GCC
648 // extensions are not enabled.
649 if (RHSIsQualifiedID &&
650 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
654 // 2nd, look up the category.
656 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
657 CDecl = CDecl->getNextClassCategory()) {
658 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
659 E = CDecl->protocol_end(); PI != E; ++PI)
660 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
664 // 3rd, look up the super class(s)
665 if (IDecl->getSuperClass())
667 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
673 //===----------------------------------------------------------------------===//
675 //===----------------------------------------------------------------------===//
677 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
678 SourceLocation StartLoc,
679 SourceLocation IdLoc, IdentifierInfo *Id,
680 QualType T, TypeSourceInfo *TInfo,
681 AccessControl ac, Expr *BW,
684 // Ivar's can only appear in interfaces, implementations (via synthesized
685 // properties), and class extensions (via direct declaration, or synthesized
688 // FIXME: This should really be asserting this:
689 // (isa<ObjCCategoryDecl>(DC) &&
690 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
691 // but unfortunately we sometimes place ivars into non-class extension
692 // categories on error. This breaks an AST invariant, and should not be
694 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
695 isa<ObjCCategoryDecl>(DC)) &&
696 "Invalid ivar decl context!");
697 // Once a new ivar is created in any of class/class-extension/implementation
698 // decl contexts, the previously built IvarList must be rebuilt.
699 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
701 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
702 ID = IM->getClassInterface();
704 IM->setHasSynthBitfield(true);
707 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
708 ID = CD->getClassInterface();
710 CD->setHasSynthBitfield(true);
716 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
717 ac, BW, synthesized);
720 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
721 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
723 switch (DC->getKind()) {
725 case ObjCCategoryImpl:
727 assert(0 && "invalid ivar container!");
730 // Ivars can only appear in class extension categories.
732 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
733 assert(CD->IsClassExtension() && "invalid container for ivar!");
734 return CD->getClassInterface();
737 case ObjCImplementation:
738 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
741 return cast<ObjCInterfaceDecl>(DC);
745 //===----------------------------------------------------------------------===//
746 // ObjCAtDefsFieldDecl
747 //===----------------------------------------------------------------------===//
750 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
751 SourceLocation StartLoc, SourceLocation IdLoc,
752 IdentifierInfo *Id, QualType T, Expr *BW) {
753 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
756 //===----------------------------------------------------------------------===//
758 //===----------------------------------------------------------------------===//
760 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
762 IdentifierInfo *Id) {
763 return new (C) ObjCProtocolDecl(DC, L, Id);
766 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
767 ObjCProtocolDecl *PDecl = this;
769 if (Name == getIdentifier())
772 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
773 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
779 // lookupMethod - Lookup a instance/class method in the protocol and protocols
781 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
782 bool isInstance) const {
783 ObjCMethodDecl *MethodDecl = NULL;
785 if ((MethodDecl = getMethod(Sel, isInstance)))
788 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
789 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
794 //===----------------------------------------------------------------------===//
796 //===----------------------------------------------------------------------===//
798 ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
799 ObjCInterfaceDecl *const *Elts,
800 const SourceLocation *Locs,
803 : Decl(ObjCClass, DC, L) {
804 setClassList(C, Elts, Locs, nElts);
807 void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
808 const SourceLocation *Locs, unsigned Num) {
809 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
810 llvm::alignOf<ObjCClassRef>());
811 for (unsigned i = 0; i < Num; ++i)
812 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
817 ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
819 ObjCInterfaceDecl *const *Elts,
820 const SourceLocation *Locs,
822 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
825 SourceRange ObjCClassDecl::getSourceRange() const {
826 // FIXME: We should include the semicolon
828 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
831 //===----------------------------------------------------------------------===//
832 // ObjCForwardProtocolDecl
833 //===----------------------------------------------------------------------===//
835 ObjCForwardProtocolDecl::
836 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
837 ObjCProtocolDecl *const *Elts, unsigned nElts,
838 const SourceLocation *Locs, ASTContext &C)
839 : Decl(ObjCForwardProtocol, DC, L) {
840 ReferencedProtocols.set(Elts, nElts, Locs, C);
844 ObjCForwardProtocolDecl *
845 ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
847 ObjCProtocolDecl *const *Elts,
849 const SourceLocation *Locs) {
850 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
853 //===----------------------------------------------------------------------===//
855 //===----------------------------------------------------------------------===//
857 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
858 SourceLocation AtLoc,
859 SourceLocation ClassNameLoc,
860 SourceLocation CategoryNameLoc,
861 IdentifierInfo *Id) {
862 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
865 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
866 return getASTContext().getObjCImplementation(
867 const_cast<ObjCCategoryDecl*>(this));
870 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
871 getASTContext().setObjCImplementation(this, ImplD);
875 //===----------------------------------------------------------------------===//
876 // ObjCCategoryImplDecl
877 //===----------------------------------------------------------------------===//
879 ObjCCategoryImplDecl *
880 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
881 SourceLocation L,IdentifierInfo *Id,
882 ObjCInterfaceDecl *ClassInterface) {
883 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
886 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
887 // The class interface might be NULL if we are working with invalid code.
888 if (const ObjCInterfaceDecl *ID = getClassInterface())
889 return ID->FindCategoryDeclaration(getIdentifier());
894 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
895 // FIXME: The context should be correct before we get here.
896 property->setLexicalDeclContext(this);
900 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
901 ASTContext &Ctx = getASTContext();
903 if (ObjCImplementationDecl *ImplD
904 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
906 Ctx.setObjCImplementation(IFace, ImplD);
908 } else if (ObjCCategoryImplDecl *ImplD =
909 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
910 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
911 Ctx.setObjCImplementation(CD, ImplD);
914 ClassInterface = IFace;
917 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
918 /// properties implemented in this category @implementation block and returns
919 /// the implemented property that uses it.
921 ObjCPropertyImplDecl *ObjCImplDecl::
922 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
923 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
924 ObjCPropertyImplDecl *PID = *i;
925 if (PID->getPropertyIvarDecl() &&
926 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
932 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
933 /// added to the list of those properties @synthesized/@dynamic in this
934 /// category @implementation block.
936 ObjCPropertyImplDecl *ObjCImplDecl::
937 FindPropertyImplDecl(IdentifierInfo *Id) const {
938 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
939 ObjCPropertyImplDecl *PID = *i;
940 if (PID->getPropertyDecl()->getIdentifier() == Id)
946 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
947 const ObjCCategoryImplDecl *CID) {
948 OS << CID->getName();
952 //===----------------------------------------------------------------------===//
953 // ObjCImplementationDecl
954 //===----------------------------------------------------------------------===//
956 ObjCImplementationDecl *
957 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
959 ObjCInterfaceDecl *ClassInterface,
960 ObjCInterfaceDecl *SuperDecl) {
961 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
964 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
965 const ObjCImplementationDecl *ID) {
970 //===----------------------------------------------------------------------===//
971 // ObjCCompatibleAliasDecl
972 //===----------------------------------------------------------------------===//
974 ObjCCompatibleAliasDecl *
975 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
978 ObjCInterfaceDecl* AliasedClass) {
979 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
982 //===----------------------------------------------------------------------===//
984 //===----------------------------------------------------------------------===//
986 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
989 SourceLocation AtLoc,
991 PropertyControl propControl) {
992 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
995 //===----------------------------------------------------------------------===//
996 // ObjCPropertyImplDecl
997 //===----------------------------------------------------------------------===//
999 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1001 SourceLocation atLoc,
1003 ObjCPropertyDecl *property,
1006 SourceLocation ivarLoc) {
1007 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1011 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1012 SourceLocation EndLoc = getLocation();
1013 if (IvarLoc.isValid())
1016 return SourceRange(AtLoc, EndLoc);