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 (ObjCPropertyDecl *PD =
157 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
160 // Look through protocols.
161 for (ObjCInterfaceDecl::all_protocol_iterator
162 I = all_referenced_protocol_begin(),
163 E = all_referenced_protocol_end(); I != E; ++I)
164 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
170 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
171 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
174 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
175 AllReferencedProtocols.set(ExtList, ExtNum, C);
179 // Check for duplicate protocol in class's protocol list.
180 // This is O(n*m). But it is extremely rare and number of protocols in
181 // class or its extension are very few.
182 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
183 for (unsigned i = 0; i < ExtNum; i++) {
184 bool protocolExists = false;
185 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
186 for (all_protocol_iterator
187 p = all_referenced_protocol_begin(),
188 e = all_referenced_protocol_end(); p != e; ++p) {
189 ObjCProtocolDecl *Proto = (*p);
190 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
191 protocolExists = true;
195 // Do we want to warn on a protocol in extension class which
196 // already exist in the class? Probably not.
198 ProtocolRefs.push_back(ProtoInExtension);
201 if (ProtocolRefs.empty())
204 // Merge ProtocolRefs into class's protocol list;
205 for (all_protocol_iterator p = all_referenced_protocol_begin(),
206 e = all_referenced_protocol_end(); p != e; ++p) {
207 ProtocolRefs.push_back(*p);
210 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
213 /// getFirstClassExtension - Find first class extension of the given class.
214 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
215 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
216 CDecl = CDecl->getNextClassCategory())
217 if (CDecl->IsClassExtension())
222 /// getNextClassCategory - Find next class extension in list of categories.
223 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
224 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
225 CDecl = CDecl->getNextClassCategory())
226 if (CDecl->IsClassExtension())
231 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
232 ObjCInterfaceDecl *&clsDeclared) {
233 ObjCInterfaceDecl* ClassDecl = this;
234 while (ClassDecl != NULL) {
235 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
236 clsDeclared = ClassDecl;
239 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
240 CDecl; CDecl = CDecl->getNextClassExtension()) {
241 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
242 clsDeclared = ClassDecl;
247 ClassDecl = ClassDecl->getSuperClass();
252 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
253 /// class whose name is passed as argument. If it is not one of the super classes
254 /// the it returns NULL.
255 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
256 const IdentifierInfo*ICName) {
257 ObjCInterfaceDecl* ClassDecl = this;
258 while (ClassDecl != NULL) {
259 if (ClassDecl->getIdentifier() == ICName)
261 ClassDecl = ClassDecl->getSuperClass();
266 /// lookupMethod - This method returns an instance/class method by looking in
267 /// the class, its categories, and its super classes (using a linear search).
268 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
269 bool isInstance) const {
270 const ObjCInterfaceDecl* ClassDecl = this;
271 ObjCMethodDecl *MethodDecl = 0;
273 while (ClassDecl != NULL) {
274 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
277 // Didn't find one yet - look through protocols.
278 const ObjCList<ObjCProtocolDecl> &Protocols =
279 ClassDecl->getReferencedProtocols();
280 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
281 E = Protocols.end(); I != E; ++I)
282 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
285 // Didn't find one yet - now look through categories.
286 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
288 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
291 // Didn't find one yet - look through protocols.
292 const ObjCList<ObjCProtocolDecl> &Protocols =
293 CatDecl->getReferencedProtocols();
294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
295 E = Protocols.end(); I != E; ++I)
296 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
298 CatDecl = CatDecl->getNextClassCategory();
300 ClassDecl = ClassDecl->getSuperClass();
305 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
306 const Selector &Sel) {
307 ObjCMethodDecl *Method = 0;
308 if (ObjCImplementationDecl *ImpDecl = getImplementation())
309 Method = ImpDecl->getInstanceMethod(Sel);
311 if (!Method && getSuperClass())
312 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
316 //===----------------------------------------------------------------------===//
318 //===----------------------------------------------------------------------===//
320 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
321 SourceLocation beginLoc,
322 SourceLocation endLoc,
323 Selector SelInfo, QualType T,
324 TypeSourceInfo *ResultTInfo,
325 DeclContext *contextDecl,
330 ImplementationControl impControl,
331 unsigned numSelectorArgs) {
332 return new (C) ObjCMethodDecl(beginLoc, endLoc,
333 SelInfo, T, ResultTInfo, contextDecl,
335 isVariadic, isSynthesized, isDefined,
340 /// \brief A definition will return its interface declaration.
341 /// An interface declaration will return its definition.
342 /// Otherwise it will return itself.
343 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
344 ASTContext &Ctx = getASTContext();
345 ObjCMethodDecl *Redecl = 0;
346 Decl *CtxD = cast<Decl>(getDeclContext());
348 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
349 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
350 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
352 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
353 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
354 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
356 } else if (ObjCImplementationDecl *ImplD =
357 dyn_cast<ObjCImplementationDecl>(CtxD)) {
358 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
359 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
361 } else if (ObjCCategoryImplDecl *CImplD =
362 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
363 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
364 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
367 return Redecl ? Redecl : this;
370 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
371 Decl *CtxD = cast<Decl>(getDeclContext());
373 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
374 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
375 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
379 } else if (ObjCCategoryImplDecl *CImplD =
380 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
381 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
382 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
390 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
391 const ObjCInterfaceDecl *OID) {
393 if (isInstanceMethod()) {
394 // There may be no interface context due to error in declaration
395 // of the interface (which has been reported). Recover gracefully.
397 selfTy = Context.getObjCInterfaceType(OID);
398 selfTy = Context.getObjCObjectPointerType(selfTy);
400 selfTy = Context.getObjCIdType();
402 } else // we have a factory method.
403 selfTy = Context.getObjCClassType();
405 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
406 &Context.Idents.get("self"), selfTy));
408 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
409 &Context.Idents.get("_cmd"),
410 Context.getObjCSelType()));
413 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
414 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
416 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
417 return CD->getClassInterface();
418 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
419 return IMD->getClassInterface();
421 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
422 assert(false && "unknown method context");
426 //===----------------------------------------------------------------------===//
428 //===----------------------------------------------------------------------===//
430 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
432 SourceLocation atLoc,
434 SourceLocation ClassLoc,
435 bool ForwardDecl, bool isInternal){
436 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
441 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
442 SourceLocation CLoc, bool FD, bool isInternal)
443 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
444 TypeForDecl(0), SuperClass(0),
445 CategoryList(0), IvarList(0),
446 ForwardDecl(FD), InternalInterface(isInternal),
450 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
451 return getASTContext().getObjCImplementation(
452 const_cast<ObjCInterfaceDecl*>(this));
455 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
456 getASTContext().setObjCImplementation(this, ImplD);
459 /// all_declared_ivar_begin - return first ivar declared in this class,
460 /// its extensions and its implementation. Lazily build the list on first
462 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
466 ObjCIvarDecl *curIvar = 0;
468 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
469 IvarList = (*I); ++I;
470 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
471 curIvar->setNextIvar(*I);
474 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
475 CDecl = CDecl->getNextClassExtension()) {
476 if (!CDecl->ivar_empty()) {
477 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
478 E = CDecl->ivar_end();
480 IvarList = (*I); ++I;
483 for ( ;I != E; curIvar = *I, ++I)
484 curIvar->setNextIvar(*I);
488 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
489 if (!ImplDecl->ivar_empty()) {
490 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
491 E = ImplDecl->ivar_end();
493 IvarList = (*I); ++I;
496 for ( ;I != E; curIvar = *I, ++I)
497 curIvar->setNextIvar(*I);
503 /// FindCategoryDeclaration - Finds category declaration in the list of
504 /// categories for this class and returns it. Name of the category is passed
505 /// in 'CategoryId'. If category not found, return 0;
508 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
509 for (ObjCCategoryDecl *Category = getCategoryList();
510 Category; Category = Category->getNextClassCategory())
511 if (Category->getIdentifier() == CategoryId)
517 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
518 for (ObjCCategoryDecl *Category = getCategoryList();
519 Category; Category = Category->getNextClassCategory())
520 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
521 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
526 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
527 for (ObjCCategoryDecl *Category = getCategoryList();
528 Category; Category = Category->getNextClassCategory())
529 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
530 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
535 /// ClassImplementsProtocol - Checks that 'lProto' protocol
536 /// has been implemented in IDecl class, its super class or categories (if
537 /// lookupCategory is true).
538 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
540 bool RHSIsQualifiedID) {
541 ObjCInterfaceDecl *IDecl = this;
542 // 1st, look up the class.
543 const ObjCList<ObjCProtocolDecl> &Protocols =
544 IDecl->getReferencedProtocols();
546 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
547 E = Protocols.end(); PI != E; ++PI) {
548 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
550 // This is dubious and is added to be compatible with gcc. In gcc, it is
551 // also allowed assigning a protocol-qualified 'id' type to a LHS object
552 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
553 // object. This IMO, should be a bug.
554 // FIXME: Treat this as an extension, and flag this as an error when GCC
555 // extensions are not enabled.
556 if (RHSIsQualifiedID &&
557 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
561 // 2nd, look up the category.
563 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
564 CDecl = CDecl->getNextClassCategory()) {
565 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
566 E = CDecl->protocol_end(); PI != E; ++PI)
567 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
571 // 3rd, look up the super class(s)
572 if (IDecl->getSuperClass())
574 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
580 //===----------------------------------------------------------------------===//
582 //===----------------------------------------------------------------------===//
584 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
585 SourceLocation L, IdentifierInfo *Id,
586 QualType T, TypeSourceInfo *TInfo,
587 AccessControl ac, Expr *BW,
590 // Ivar's can only appear in interfaces, implementations (via synthesized
591 // properties), and class extensions (via direct declaration, or synthesized
594 // FIXME: This should really be asserting this:
595 // (isa<ObjCCategoryDecl>(DC) &&
596 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
597 // but unfortunately we sometimes place ivars into non-class extension
598 // categories on error. This breaks an AST invariant, and should not be
600 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
601 isa<ObjCCategoryDecl>(DC)) &&
602 "Invalid ivar decl context!");
603 // Once a new ivar is created in any of class/class-extension/implementation
604 // decl contexts, the previously built IvarList must be rebuilt.
605 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
607 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
608 ID = IM->getClassInterface();
610 IM->setHasSynthBitfield(true);
613 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
614 ID = CD->getClassInterface();
616 CD->setHasSynthBitfield(true);
622 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
625 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
626 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
628 switch (DC->getKind()) {
630 case ObjCCategoryImpl:
632 assert(0 && "invalid ivar container!");
635 // Ivars can only appear in class extension categories.
637 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
638 assert(CD->IsClassExtension() && "invalid container for ivar!");
639 return CD->getClassInterface();
642 case ObjCImplementation:
643 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
646 return cast<ObjCInterfaceDecl>(DC);
650 //===----------------------------------------------------------------------===//
651 // ObjCAtDefsFieldDecl
652 //===----------------------------------------------------------------------===//
655 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
656 IdentifierInfo *Id, QualType T, Expr *BW) {
657 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
660 //===----------------------------------------------------------------------===//
662 //===----------------------------------------------------------------------===//
664 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
666 IdentifierInfo *Id) {
667 return new (C) ObjCProtocolDecl(DC, L, Id);
670 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
671 ObjCProtocolDecl *PDecl = this;
673 if (Name == getIdentifier())
676 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
677 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
683 // lookupMethod - Lookup a instance/class method in the protocol and protocols
685 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
686 bool isInstance) const {
687 ObjCMethodDecl *MethodDecl = NULL;
689 if ((MethodDecl = getMethod(Sel, isInstance)))
692 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
693 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
698 //===----------------------------------------------------------------------===//
700 //===----------------------------------------------------------------------===//
702 ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
703 ObjCInterfaceDecl *const *Elts,
704 const SourceLocation *Locs,
707 : Decl(ObjCClass, DC, L) {
708 setClassList(C, Elts, Locs, nElts);
711 void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
712 const SourceLocation *Locs, unsigned Num) {
713 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
714 llvm::alignof<ObjCClassRef>());
715 for (unsigned i = 0; i < Num; ++i)
716 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
721 ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
723 ObjCInterfaceDecl *const *Elts,
724 const SourceLocation *Locs,
726 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
729 SourceRange ObjCClassDecl::getSourceRange() const {
730 // FIXME: We should include the semicolon
732 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
735 //===----------------------------------------------------------------------===//
736 // ObjCForwardProtocolDecl
737 //===----------------------------------------------------------------------===//
739 ObjCForwardProtocolDecl::
740 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
741 ObjCProtocolDecl *const *Elts, unsigned nElts,
742 const SourceLocation *Locs, ASTContext &C)
743 : Decl(ObjCForwardProtocol, DC, L) {
744 ReferencedProtocols.set(Elts, nElts, Locs, C);
748 ObjCForwardProtocolDecl *
749 ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
751 ObjCProtocolDecl *const *Elts,
753 const SourceLocation *Locs) {
754 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
757 //===----------------------------------------------------------------------===//
759 //===----------------------------------------------------------------------===//
761 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
762 SourceLocation AtLoc,
763 SourceLocation ClassNameLoc,
764 SourceLocation CategoryNameLoc,
765 IdentifierInfo *Id) {
766 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
769 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
770 return getASTContext().getObjCImplementation(
771 const_cast<ObjCCategoryDecl*>(this));
774 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
775 getASTContext().setObjCImplementation(this, ImplD);
779 //===----------------------------------------------------------------------===//
780 // ObjCCategoryImplDecl
781 //===----------------------------------------------------------------------===//
783 ObjCCategoryImplDecl *
784 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
785 SourceLocation L,IdentifierInfo *Id,
786 ObjCInterfaceDecl *ClassInterface) {
787 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
790 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
791 // The class interface might be NULL if we are working with invalid code.
792 if (const ObjCInterfaceDecl *ID = getClassInterface())
793 return ID->FindCategoryDeclaration(getIdentifier());
798 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
799 // FIXME: The context should be correct before we get here.
800 property->setLexicalDeclContext(this);
804 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
805 ASTContext &Ctx = getASTContext();
807 if (ObjCImplementationDecl *ImplD
808 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
810 Ctx.setObjCImplementation(IFace, ImplD);
812 } else if (ObjCCategoryImplDecl *ImplD =
813 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
814 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
815 Ctx.setObjCImplementation(CD, ImplD);
818 ClassInterface = IFace;
821 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
822 /// properties implemented in this category @implementation block and returns
823 /// the implemented property that uses it.
825 ObjCPropertyImplDecl *ObjCImplDecl::
826 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
827 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
828 ObjCPropertyImplDecl *PID = *i;
829 if (PID->getPropertyIvarDecl() &&
830 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
836 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
837 /// added to the list of those properties @synthesized/@dynamic in this
838 /// category @implementation block.
840 ObjCPropertyImplDecl *ObjCImplDecl::
841 FindPropertyImplDecl(IdentifierInfo *Id) const {
842 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
843 ObjCPropertyImplDecl *PID = *i;
844 if (PID->getPropertyDecl()->getIdentifier() == Id)
850 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
851 const ObjCCategoryImplDecl *CID) {
852 OS << CID->getName();
856 //===----------------------------------------------------------------------===//
857 // ObjCImplementationDecl
858 //===----------------------------------------------------------------------===//
860 ObjCImplementationDecl *
861 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
863 ObjCInterfaceDecl *ClassInterface,
864 ObjCInterfaceDecl *SuperDecl) {
865 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
868 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
869 const ObjCImplementationDecl *ID) {
874 //===----------------------------------------------------------------------===//
875 // ObjCCompatibleAliasDecl
876 //===----------------------------------------------------------------------===//
878 ObjCCompatibleAliasDecl *
879 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
882 ObjCInterfaceDecl* AliasedClass) {
883 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
886 //===----------------------------------------------------------------------===//
888 //===----------------------------------------------------------------------===//
890 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
893 SourceLocation AtLoc,
895 PropertyControl propControl) {
896 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
900 //===----------------------------------------------------------------------===//
901 // ObjCPropertyImplDecl
902 //===----------------------------------------------------------------------===//
904 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
906 SourceLocation atLoc,
908 ObjCPropertyDecl *property,
910 ObjCIvarDecl *ivar) {
911 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);