blob: d6e4b2c44bba7529ae45eaf779aa05206bae49ee [file] [log] [blame]
Chris Lattner10318b82008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroffd85ba922009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner10318b82008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner114add62008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner217eb8c2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattnerdf6133b2009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattnercf6c8282009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner217eb8c2009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattnerdf6133b2009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner217eb8c2009-02-20 21:16:26 +000031 assert(List == 0 && "Elements already set!");
32 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump25cf7602009-09-09 15:08:12 +000033
34
Chris Lattnercf6c8282009-02-20 21:44:01 +000035 List = new (Ctx) void*[Elts];
Chris Lattner217eb8c2009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
40
41//===----------------------------------------------------------------------===//
Chris Lattner13f63602009-02-20 20:59:54 +000042// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000043//===----------------------------------------------------------------------===//
44
Fariborz Jahanian074cf972009-06-05 18:16:35 +000045/// getIvarDecl - This method looks up an ivar in this ContextDecl.
46///
47ObjCIvarDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000048ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian074cf972009-06-05 18:16:35 +000049 lookup_const_iterator Ivar, IvarEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000050 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian074cf972009-06-05 18:16:35 +000051 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
52 return ivar;
53 }
54 return 0;
55}
56
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000057// Get the local instance/class method declared in this interface.
Douglas Gregorc55b0b02009-04-09 21:40:53 +000058ObjCMethodDecl *
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000059ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000060 // Since instance & class methods can have the same name, the loop below
61 // ensures we get the correct method.
62 //
63 // @interface Whatever
64 // - (int) class_method;
65 // + (float) class_method;
66 // @end
67 //
68 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000069 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffd85ba922009-02-22 19:35:57 +000070 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000071 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffd85ba922009-02-22 19:35:57 +000072 return MD;
73 }
Steve Naroffab63fd62009-01-08 17:28:14 +000074 return 0;
75}
76
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000077/// FindPropertyDeclaration - Finds declaration of the property given its name
78/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000079/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000080///
81ObjCPropertyDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000082ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
83 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +000084 if ((*I)->getIdentifier() == PropertyId)
85 return *I;
Mike Stump25cf7602009-09-09 15:08:12 +000086
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000087 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
88 if (PID) {
Mike Stump25cf7602009-09-09 15:08:12 +000089 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Chris Lattner13f63602009-02-20 20:59:54 +000090 E = PID->protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000091 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +000092 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000093 }
Mike Stump25cf7602009-09-09 15:08:12 +000094
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000095 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +000096 // Look through categories.
97 for (ObjCCategoryDecl *Category = OID->getCategoryList();
98 Category; Category = Category->getNextClassCategory()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000099 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000100 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000101 }
102 // Look through protocols.
103 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
104 E = OID->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000105 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000106 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000107 }
108 if (OID->getSuperClass())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000109 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +0000110 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000111 // Look through protocols.
112 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
113 E = OCD->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000114 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000115 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000116 }
117 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000118 return 0;
119}
120
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000121ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
122 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner10318b82008-03-16 00:19:01 +0000123 ObjCInterfaceDecl* ClassDecl = this;
124 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000125 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian074cf972009-06-05 18:16:35 +0000126 clsDeclared = ClassDecl;
127 return I;
Chris Lattner10318b82008-03-16 00:19:01 +0000128 }
129 ClassDecl = ClassDecl->getSuperClass();
130 }
131 return NULL;
132}
133
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000134/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
135/// class whose name is passed as argument. If it is not one of the super classes
136/// the it returns NULL.
137ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
138 const IdentifierInfo*ICName) {
139 ObjCInterfaceDecl* ClassDecl = this;
140 while (ClassDecl != NULL) {
141 if (ClassDecl->getIdentifier() == ICName)
142 return ClassDecl;
143 ClassDecl = ClassDecl->getSuperClass();
144 }
145 return NULL;
146}
147
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000148/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000149/// the class, its categories, and its super classes (using a linear search).
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000150ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
151 bool isInstance) const {
152 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner10318b82008-03-16 00:19:01 +0000153 ObjCMethodDecl *MethodDecl = 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000154
Chris Lattner10318b82008-03-16 00:19:01 +0000155 while (ClassDecl != NULL) {
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000156 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner10318b82008-03-16 00:19:01 +0000157 return MethodDecl;
Mike Stump25cf7602009-09-09 15:08:12 +0000158
Chris Lattner10318b82008-03-16 00:19:01 +0000159 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000160 const ObjCList<ObjCProtocolDecl> &Protocols =
161 ClassDecl->getReferencedProtocols();
162 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
163 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000164 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner10318b82008-03-16 00:19:01 +0000165 return MethodDecl;
Mike Stump25cf7602009-09-09 15:08:12 +0000166
Chris Lattner10318b82008-03-16 00:19:01 +0000167 // Didn't find one yet - now look through categories.
168 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
169 while (CatDecl) {
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000170 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner10318b82008-03-16 00:19:01 +0000171 return MethodDecl;
Mike Stump25cf7602009-09-09 15:08:12 +0000172
Steve Naroffaf151802008-12-08 20:57:28 +0000173 // Didn't find one yet - look through protocols.
174 const ObjCList<ObjCProtocolDecl> &Protocols =
175 CatDecl->getReferencedProtocols();
176 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
177 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidis9ac39f32009-07-25 22:15:51 +0000178 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff0ba24842009-02-26 11:32:02 +0000179 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000180 CatDecl = CatDecl->getNextClassCategory();
181 }
182 ClassDecl = ClassDecl->getSuperClass();
183 }
184 return NULL;
185}
186
Chris Lattner13f63602009-02-20 20:59:54 +0000187
188
189//===----------------------------------------------------------------------===//
190// ObjCMethodDecl
191//===----------------------------------------------------------------------===//
192
193ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump25cf7602009-09-09 15:08:12 +0000194 SourceLocation beginLoc,
Chris Lattner13f63602009-02-20 20:59:54 +0000195 SourceLocation endLoc,
196 Selector SelInfo, QualType T,
197 DeclContext *contextDecl,
198 bool isInstance,
199 bool isVariadic,
200 bool isSynthesized,
201 ImplementationControl impControl) {
202 return new (C) ObjCMethodDecl(beginLoc, endLoc,
203 SelInfo, T, contextDecl,
Mike Stump25cf7602009-09-09 15:08:12 +0000204 isInstance,
Chris Lattner13f63602009-02-20 20:59:54 +0000205 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000206}
207
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000208void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000209 if (Body) Body->Destroy(C);
210 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump25cf7602009-09-09 15:08:12 +0000211
Chris Lattner13f63602009-02-20 20:59:54 +0000212 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
213 if (*I) (*I)->Destroy(C);
214
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000215 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000216
217 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000218}
219
Argiris Kirtzidis29c33872009-07-21 00:06:36 +0000220/// \brief A definition will return its interface declaration.
221/// An interface declaration will return its definition.
222/// Otherwise it will return itself.
223ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
224 ASTContext &Ctx = getASTContext();
225 ObjCMethodDecl *Redecl = 0;
226 Decl *CtxD = cast<Decl>(getDeclContext());
227
228 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
229 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
230 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
231
232 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
233 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
234 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
235
Argiris Kirtzidis0f3b8422009-07-28 05:11:05 +0000236 } else if (ObjCImplementationDecl *ImplD =
237 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argiris Kirtzidis29c33872009-07-21 00:06:36 +0000238 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
239 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argiris Kirtzidis0f3b8422009-07-28 05:11:05 +0000240
241 } else if (ObjCCategoryImplDecl *CImplD =
242 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
243 if (ObjCCategoryDecl *CatD = CImplD->getCategoryClass())
244 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argiris Kirtzidis29c33872009-07-21 00:06:36 +0000245 }
246
247 return Redecl ? Redecl : this;
248}
249
Argiris Kirtzidis029f7872009-07-28 05:11:17 +0000250ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
251 Decl *CtxD = cast<Decl>(getDeclContext());
252
253 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
254 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
255 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
256 isInstanceMethod()))
257 return MD;
258
259 } else if (ObjCCategoryImplDecl *CImplD =
260 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
261 if (ObjCCategoryDecl *CatD = CImplD->getCategoryClass())
262 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
263 isInstanceMethod()))
264 return MD;
265 }
266
267 return this;
268}
269
Mike Stump25cf7602009-09-09 15:08:12 +0000270void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattner13f63602009-02-20 20:59:54 +0000271 const ObjCInterfaceDecl *OID) {
272 QualType selfTy;
273 if (isInstanceMethod()) {
274 // There may be no interface context due to error in declaration
275 // of the interface (which has been reported). Recover gracefully.
276 if (OID) {
Daniel Dunbarbe1ff272009-04-22 04:34:53 +0000277 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff329ec222009-07-10 23:34:53 +0000278 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattner13f63602009-02-20 20:59:54 +0000279 } else {
280 selfTy = Context.getObjCIdType();
281 }
282 } else // we have a factory method.
283 selfTy = Context.getObjCClassType();
284
Mike Stump25cf7602009-09-09 15:08:12 +0000285 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff79ea0e02009-04-20 15:06:07 +0000286 &Context.Idents.get("self"), selfTy));
Chris Lattner13f63602009-02-20 20:59:54 +0000287
Mike Stump25cf7602009-09-09 15:08:12 +0000288 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
289 &Context.Idents.get("_cmd"),
Steve Naroff79ea0e02009-04-20 15:06:07 +0000290 Context.getObjCSelType()));
Chris Lattner13f63602009-02-20 20:59:54 +0000291}
292
Chris Lattner13f63602009-02-20 20:59:54 +0000293ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
294 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
295 return ID;
296 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
297 return CD->getClassInterface();
Argiris Kirtzidisa06e3ba2009-07-28 05:10:52 +0000298 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattner13f63602009-02-20 20:59:54 +0000299 return IMD->getClassInterface();
Argiris Kirtzidisa06e3ba2009-07-28 05:10:52 +0000300
301 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattner13f63602009-02-20 20:59:54 +0000302 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000303 return 0;
304}
305
Chris Lattner13f63602009-02-20 20:59:54 +0000306//===----------------------------------------------------------------------===//
307// ObjCInterfaceDecl
308//===----------------------------------------------------------------------===//
309
310ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
311 DeclContext *DC,
312 SourceLocation atLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000313 IdentifierInfo *Id,
Chris Lattner13f63602009-02-20 20:59:54 +0000314 SourceLocation ClassLoc,
315 bool ForwardDecl, bool isInternal){
316 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
317 isInternal);
318}
319
320ObjCInterfaceDecl::
321ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
322 SourceLocation CLoc, bool FD, bool isInternal)
323 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
324 TypeForDecl(0), SuperClass(0),
325 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
326 ClassLoc(CLoc) {
327}
328
Mike Stump25cf7602009-09-09 15:08:12 +0000329void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnereff36ed2009-03-31 08:36:08 +0000330 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +0000331 if (*I) (*I)->Destroy(C);
Mike Stump25cf7602009-09-09 15:08:12 +0000332
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000333 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000334 // FIXME: CategoryList?
Mike Stump25cf7602009-09-09 15:08:12 +0000335
Chris Lattner13f63602009-02-20 20:59:54 +0000336 // FIXME: Because there is no clear ownership
337 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
338 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
339 Decl::Destroy(C);
340}
341
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000342ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
343 return getASTContext().getObjCImplementation(
344 const_cast<ObjCInterfaceDecl*>(this));
345}
346
347void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
348 getASTContext().setObjCImplementation(this, ImplD);
349}
350
Chris Lattner13f63602009-02-20 20:59:54 +0000351
352/// FindCategoryDeclaration - Finds category declaration in the list of
353/// categories for this class and returns it. Name of the category is passed
354/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000355///
Chris Lattner13f63602009-02-20 20:59:54 +0000356ObjCCategoryDecl *
357ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
358 for (ObjCCategoryDecl *Category = getCategoryList();
359 Category; Category = Category->getNextClassCategory())
360 if (Category->getIdentifier() == CategoryId)
361 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000362 return 0;
363}
364
Argiris Kirtzidis20096862009-07-21 00:06:20 +0000365ObjCMethodDecl *
366ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
367 for (ObjCCategoryDecl *Category = getCategoryList();
368 Category; Category = Category->getNextClassCategory())
369 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
370 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
371 return MD;
372 return 0;
373}
374
375ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
376 for (ObjCCategoryDecl *Category = getCategoryList();
377 Category; Category = Category->getNextClassCategory())
378 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
379 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
380 return MD;
381 return 0;
382}
383
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000384/// ClassImplementsProtocol - Checks that 'lProto' protocol
385/// has been implemented in IDecl class, its super class or categories (if
386/// lookupCategory is true).
387bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
388 bool lookupCategory,
389 bool RHSIsQualifiedID) {
390 ObjCInterfaceDecl *IDecl = this;
391 // 1st, look up the class.
392 const ObjCList<ObjCProtocolDecl> &Protocols =
393 IDecl->getReferencedProtocols();
Mike Stump25cf7602009-09-09 15:08:12 +0000394
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000395 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
396 E = Protocols.end(); PI != E; ++PI) {
397 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
398 return true;
399 // This is dubious and is added to be compatible with gcc. In gcc, it is
400 // also allowed assigning a protocol-qualified 'id' type to a LHS object
401 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
402 // object. This IMO, should be a bug.
403 // FIXME: Treat this as an extension, and flag this as an error when GCC
404 // extensions are not enabled.
Mike Stump25cf7602009-09-09 15:08:12 +0000405 if (RHSIsQualifiedID &&
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000406 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
407 return true;
408 }
Mike Stump25cf7602009-09-09 15:08:12 +0000409
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000410 // 2nd, look up the category.
411 if (lookupCategory)
412 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
413 CDecl = CDecl->getNextClassCategory()) {
414 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
415 E = CDecl->protocol_end(); PI != E; ++PI)
416 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
417 return true;
418 }
Mike Stump25cf7602009-09-09 15:08:12 +0000419
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000420 // 3rd, look up the super class(s)
421 if (IDecl->getSuperClass())
422 return
423 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
424 RHSIsQualifiedID);
Mike Stump25cf7602009-09-09 15:08:12 +0000425
Fariborz Jahanian92c7b162009-08-11 22:02:25 +0000426 return false;
427}
428
Chris Lattner13f63602009-02-20 20:59:54 +0000429//===----------------------------------------------------------------------===//
430// ObjCIvarDecl
431//===----------------------------------------------------------------------===//
432
433ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
434 SourceLocation L, IdentifierInfo *Id,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000435 QualType T, DeclaratorInfo *DInfo,
436 AccessControl ac, Expr *BW) {
437 return new (C) ObjCIvarDecl(DC, L, Id, T, DInfo, ac, BW);
Chris Lattner13f63602009-02-20 20:59:54 +0000438}
439
440
441
442//===----------------------------------------------------------------------===//
443// ObjCAtDefsFieldDecl
444//===----------------------------------------------------------------------===//
445
446ObjCAtDefsFieldDecl
447*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
448 IdentifierInfo *Id, QualType T, Expr *BW) {
449 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
450}
451
452void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
453 this->~ObjCAtDefsFieldDecl();
Mike Stump25cf7602009-09-09 15:08:12 +0000454 C.Deallocate((void *)this);
Chris Lattner13f63602009-02-20 20:59:54 +0000455}
456
457//===----------------------------------------------------------------------===//
458// ObjCProtocolDecl
459//===----------------------------------------------------------------------===//
460
461ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump25cf7602009-09-09 15:08:12 +0000462 SourceLocation L,
Chris Lattner13f63602009-02-20 20:59:54 +0000463 IdentifierInfo *Id) {
464 return new (C) ObjCProtocolDecl(DC, L, Id);
465}
466
467void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000468 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000469 ObjCContainerDecl::Destroy(C);
470}
471
Steve Naroff98e71b82009-03-01 16:12:44 +0000472ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
473 ObjCProtocolDecl *PDecl = this;
474
475 if (Name == getIdentifier())
476 return PDecl;
477
478 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
479 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
480 return PDecl;
Mike Stump25cf7602009-09-09 15:08:12 +0000481
Steve Naroff98e71b82009-03-01 16:12:44 +0000482 return NULL;
483}
484
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000485// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattner13f63602009-02-20 20:59:54 +0000486// it inherited.
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000487ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
488 bool isInstance) const {
Chris Lattner13f63602009-02-20 20:59:54 +0000489 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump25cf7602009-09-09 15:08:12 +0000490
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000491 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattner13f63602009-02-20 20:59:54 +0000492 return MethodDecl;
Mike Stump25cf7602009-09-09 15:08:12 +0000493
Chris Lattner13f63602009-02-20 20:59:54 +0000494 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000495 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner13f63602009-02-20 20:59:54 +0000496 return MethodDecl;
497 return NULL;
498}
499
500//===----------------------------------------------------------------------===//
501// ObjCClassDecl
502//===----------------------------------------------------------------------===//
503
Mike Stump25cf7602009-09-09 15:08:12 +0000504ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000505 ObjCInterfaceDecl *const *Elts, unsigned nElts,
506 ASTContext &C)
507 : Decl(ObjCClass, DC, L) {
508 ForwardDecls.set(Elts, nElts, C);
509}
510
511
Chris Lattner13f63602009-02-20 20:59:54 +0000512ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
513 SourceLocation L,
514 ObjCInterfaceDecl *const *Elts,
515 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000516 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000517}
518
519void ObjCClassDecl::Destroy(ASTContext &C) {
Mike Stump25cf7602009-09-09 15:08:12 +0000520
Chris Lattner13f63602009-02-20 20:59:54 +0000521 // FIXME: There is no clear ownership policy now for referenced
522 // ObjCInterfaceDecls. Some of them can be forward declarations that
523 // are never later defined (in which case the ObjCClassDecl owns them)
524 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
525 // we should have separate objects for forward declarations and definitions,
526 // obviating this problem. Because of this situation, referenced
527 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
Mike Stump25cf7602009-09-09 15:08:12 +0000528
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000529 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000530 Decl::Destroy(C);
531}
532
533//===----------------------------------------------------------------------===//
534// ObjCForwardProtocolDecl
535//===----------------------------------------------------------------------===//
536
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000537ObjCForwardProtocolDecl::
538ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
539 ObjCProtocolDecl *const *Elts, unsigned nElts,
540 ASTContext &C)
Mike Stump25cf7602009-09-09 15:08:12 +0000541: Decl(ObjCForwardProtocol, DC, L) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000542 ReferencedProtocols.set(Elts, nElts, C);
543}
544
545
Chris Lattner13f63602009-02-20 20:59:54 +0000546ObjCForwardProtocolDecl *
547ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump25cf7602009-09-09 15:08:12 +0000548 SourceLocation L,
Chris Lattner13f63602009-02-20 20:59:54 +0000549 ObjCProtocolDecl *const *Elts,
550 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000551 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000552}
553
554void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000555 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000556 Decl::Destroy(C);
557}
558
559//===----------------------------------------------------------------------===//
560// ObjCCategoryDecl
561//===----------------------------------------------------------------------===//
562
563ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
564 SourceLocation L,
565 IdentifierInfo *Id) {
566 return new (C) ObjCCategoryDecl(DC, L, Id);
567}
568
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000569ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
570 return getASTContext().getObjCImplementation(
571 const_cast<ObjCCategoryDecl*>(this));
572}
573
574void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
575 getASTContext().setObjCImplementation(this, ImplD);
576}
577
578
Chris Lattner13f63602009-02-20 20:59:54 +0000579//===----------------------------------------------------------------------===//
580// ObjCCategoryImplDecl
581//===----------------------------------------------------------------------===//
582
583ObjCCategoryImplDecl *
584ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
585 SourceLocation L,IdentifierInfo *Id,
586 ObjCInterfaceDecl *ClassInterface) {
587 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
588}
589
Argiris Kirtzidis0f3b8422009-07-28 05:11:05 +0000590ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryClass() const {
591 return getClassInterface()->FindCategoryDeclaration(getIdentifier());
592}
593
Chris Lattner13f63602009-02-20 20:59:54 +0000594
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000595void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregorbd336c52009-04-23 02:42:49 +0000596 // FIXME: The context should be correct before we get here.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000597 property->setLexicalDeclContext(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000598 addDecl(property);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000599}
600
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000601void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
602 ASTContext &Ctx = getASTContext();
603
604 if (ObjCImplementationDecl *ImplD
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000605 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000606 if (IFace)
607 Ctx.setObjCImplementation(IFace, ImplD);
608
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000609 } else if (ObjCCategoryImplDecl *ImplD =
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000610 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
611 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
612 Ctx.setObjCImplementation(CD, ImplD);
613 }
614
615 ClassInterface = IFace;
616}
617
Fariborz Jahanian68342282008-12-05 22:32:48 +0000618/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000619/// properties implemented in this category @implementation block and returns
620/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000621///
Chris Lattner649b3da2009-02-28 18:42:10 +0000622ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000623FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
624 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000625 ObjCPropertyImplDecl *PID = *i;
626 if (PID->getPropertyIvarDecl() &&
627 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
628 return PID;
629 }
630 return 0;
631}
632
633/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
634/// added to the list of those properties @synthesized/@dynamic in this
635/// category @implementation block.
636///
Chris Lattner649b3da2009-02-28 18:42:10 +0000637ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000638FindPropertyImplDecl(IdentifierInfo *Id) const {
639 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000640 ObjCPropertyImplDecl *PID = *i;
641 if (PID->getPropertyDecl()->getIdentifier() == Id)
642 return PID;
643 }
644 return 0;
645}
646
Chris Lattner13f63602009-02-20 20:59:54 +0000647//===----------------------------------------------------------------------===//
648// ObjCImplementationDecl
649//===----------------------------------------------------------------------===//
650
651ObjCImplementationDecl *
Mike Stump25cf7602009-09-09 15:08:12 +0000652ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner13f63602009-02-20 20:59:54 +0000653 SourceLocation L,
654 ObjCInterfaceDecl *ClassInterface,
655 ObjCInterfaceDecl *SuperDecl) {
656 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
657}
658
Chris Lattner13f63602009-02-20 20:59:54 +0000659//===----------------------------------------------------------------------===//
660// ObjCCompatibleAliasDecl
661//===----------------------------------------------------------------------===//
662
663ObjCCompatibleAliasDecl *
664ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
665 SourceLocation L,
Mike Stump25cf7602009-09-09 15:08:12 +0000666 IdentifierInfo *Id,
Chris Lattner13f63602009-02-20 20:59:54 +0000667 ObjCInterfaceDecl* AliasedClass) {
668 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
669}
670
671//===----------------------------------------------------------------------===//
672// ObjCPropertyDecl
673//===----------------------------------------------------------------------===//
674
675ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
676 SourceLocation L,
677 IdentifierInfo *Id,
678 QualType T,
679 PropertyControl propControl) {
680 return new (C) ObjCPropertyDecl(DC, L, Id, T);
681}
682
683
684//===----------------------------------------------------------------------===//
685// ObjCPropertyImplDecl
686//===----------------------------------------------------------------------===//
687
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000688ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000689 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000690 SourceLocation atLoc,
691 SourceLocation L,
692 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000693 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000694 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000695 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000696}
Chris Lattner44859612008-03-17 01:19:02 +0000697
Chris Lattnereee57c02008-04-04 06:12:32 +0000698