blob: 67f6531d79beadbfc8e72c2031a46db818d2feb8 [file] [log] [blame]
Chris Lattner1e03a562008-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 Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner38af2de2009-02-20 21:35:13 +000024void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000025 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000026 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000027
28
Chris Lattner4ee413b2009-02-20 21:44:01 +000029 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000030 NumElts = Elts;
31 memcpy(List, InList, sizeof(void*)*Elts);
32}
33
Douglas Gregor18df52b2010-01-16 15:02:53 +000034void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
35 const SourceLocation *Locs, ASTContext &Ctx) {
36 if (Elts == 0)
37 return;
38
39 Locations = new (Ctx) SourceLocation[Elts];
40 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
41 set(InList, Elts, Ctx);
42}
43
Chris Lattner11e1e1a2009-02-20 21:16:26 +000044//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000045// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000046//===----------------------------------------------------------------------===//
47
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000048/// getIvarDecl - This method looks up an ivar in this ContextDecl.
49///
50ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000051ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000052 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000053 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000054 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
55 return ivar;
56 }
57 return 0;
58}
59
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000060// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000061ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000062ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000063 // Since instance & class methods can have the same name, the loop below
64 // ensures we get the correct method.
65 //
66 // @interface Whatever
67 // - (int) class_method;
68 // + (float) class_method;
69 // @end
70 //
71 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000072 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000073 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000074 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000075 return MD;
76 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000077 return 0;
78}
79
Ted Kremenek9f550ff2010-03-15 20:11:46 +000080ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000081ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000082 IdentifierInfo *propertyID) {
83
Ted Kremenekde09d0c2010-03-15 20:11:53 +000084 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000085 llvm::tie(I, E) = DC->lookup(propertyID);
86 for ( ; I != E; ++I)
87 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
88 return PD;
89
90 return 0;
91}
92
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000093/// FindPropertyDeclaration - Finds declaration of the property given its name
94/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000095ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000096ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekde09d0c2010-03-15 20:11:53 +000098 if (ObjCPropertyDecl *PD =
99 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
100 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000102 switch (getKind()) {
103 default:
104 break;
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)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000109 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
110 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000111 break;
112 }
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))
120 return P;
121
122 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000123 for (ObjCInterfaceDecl::all_protocol_iterator
124 I = OID->all_referenced_protocol_begin(),
125 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000126 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
127 return P;
128
129 // Finally, check the super class.
130 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
131 return superClass->FindPropertyDeclaration(PropertyId);
132 break;
133 }
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))
141 return P;
142
143 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000144 }
145 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000146 return 0;
147}
148
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000149/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
150/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000151/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000152///
153ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000154ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000155 IdentifierInfo *PropertyId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000156 if (ExternallyCompleted)
157 LoadExternalDefinition();
158
Ted Kremenek37cafb02010-03-15 20:30:07 +0000159 if (ObjCPropertyDecl *PD =
160 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
161 return PD;
162
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000163 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000164 for (ObjCInterfaceDecl::all_protocol_iterator
165 I = all_referenced_protocol_begin(),
166 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
168 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000169
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000170 return 0;
171}
172
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000173void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
174 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
175 ASTContext &C)
176{
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000177 if (ExternallyCompleted)
178 LoadExternalDefinition();
179
Ted Kremenek53b94412010-09-01 01:21:15 +0000180 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
181 AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000182 return;
183 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000184
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000185 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000186 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000187 // 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];
Ted Kremenek53b94412010-09-01 01:21:15 +0000192 for (all_protocol_iterator
193 p = all_referenced_protocol_begin(),
194 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000195 ObjCProtocolDecl *Proto = (*p);
196 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
197 protocolExists = true;
198 break;
199 }
200 }
201 // Do we want to warn on a protocol in extension class which
202 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000203 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000204 ProtocolRefs.push_back(ProtoInExtension);
205 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000206
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000207 if (ProtocolRefs.empty())
208 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000209
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000210 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000211 for (all_protocol_iterator p = all_referenced_protocol_begin(),
212 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000213 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000214 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000215
216 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000217}
218
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000219/// getFirstClassExtension - Find first class extension of the given class.
220ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
221 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000222 CDecl = CDecl->getNextClassCategory())
223 if (CDecl->IsClassExtension())
224 return CDecl;
225 return 0;
226}
227
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000228/// getNextClassCategory - Find next class extension in list of categories.
229const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
230 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
231 CDecl = CDecl->getNextClassCategory())
232 if (CDecl->IsClassExtension())
233 return CDecl;
234 return 0;
235}
236
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000237ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
238 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000239 ObjCInterfaceDecl* ClassDecl = this;
240 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000241 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000242 clsDeclared = ClassDecl;
243 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000244 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000245 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
246 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000247 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
248 clsDeclared = ClassDecl;
249 return I;
250 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000251 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000252
Chris Lattner1e03a562008-03-16 00:19:01 +0000253 ClassDecl = ClassDecl->getSuperClass();
254 }
255 return NULL;
256}
257
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000258/// 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.
261ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
262 const IdentifierInfo*ICName) {
263 ObjCInterfaceDecl* ClassDecl = this;
264 while (ClassDecl != NULL) {
265 if (ClassDecl->getIdentifier() == ICName)
266 return ClassDecl;
267 ClassDecl = ClassDecl->getSuperClass();
268 }
269 return NULL;
270}
271
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000272/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000273/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000274ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
275 bool isInstance) const {
276 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000277 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000279 if (ExternallyCompleted)
280 LoadExternalDefinition();
281
Chris Lattner1e03a562008-03-16 00:19:01 +0000282 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000283 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000284 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner1e03a562008-03-16 00:19:01 +0000286 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000287 const ObjCList<ObjCProtocolDecl> &Protocols =
288 ClassDecl->getReferencedProtocols();
289 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
290 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000291 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000292 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner1e03a562008-03-16 00:19:01 +0000294 // Didn't find one yet - now look through categories.
295 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
296 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000297 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000298 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Naroffb79c01e2008-12-08 20:57:28 +0000300 // 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)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000305 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000306 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000307 CatDecl = CatDecl->getNextClassCategory();
308 }
309 ClassDecl = ClassDecl->getSuperClass();
310 }
311 return NULL;
312}
313
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000314ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
315 const Selector &Sel,
316 bool Instance) {
Steve Naroffd789d3d2009-10-01 23:46:04 +0000317 ObjCMethodDecl *Method = 0;
318 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000319 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
320 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000321
322 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000323 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000324 return Method;
325}
Chris Lattnerab351632009-02-20 20:59:54 +0000326
327//===----------------------------------------------------------------------===//
328// ObjCMethodDecl
329//===----------------------------------------------------------------------===//
330
331ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000332 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000333 SourceLocation endLoc,
334 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000335 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000336 DeclContext *contextDecl,
337 bool isInstance,
338 bool isVariadic,
339 bool isSynthesized,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000340 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000341 ImplementationControl impControl,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000342 bool HasRelatedResultType,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000343 unsigned numSelectorArgs) {
Chris Lattnerab351632009-02-20 20:59:54 +0000344 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000345 SelInfo, T, ResultTInfo, contextDecl,
346 isInstance,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000347 isVariadic, isSynthesized, isDefined,
348 impControl,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000349 HasRelatedResultType,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000350 numSelectorArgs);
Chris Lattner1e03a562008-03-16 00:19:01 +0000351}
352
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000353/// \brief A definition will return its interface declaration.
354/// An interface declaration will return its definition.
355/// Otherwise it will return itself.
356ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
357 ASTContext &Ctx = getASTContext();
358 ObjCMethodDecl *Redecl = 0;
359 Decl *CtxD = cast<Decl>(getDeclContext());
360
361 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
362 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
363 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
364
365 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
366 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
367 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
368
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000369 } else if (ObjCImplementationDecl *ImplD =
370 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000371 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
372 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000373
374 } else if (ObjCCategoryImplDecl *CImplD =
375 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000376 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000377 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000378 }
379
380 return Redecl ? Redecl : this;
381}
382
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000383ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
384 Decl *CtxD = cast<Decl>(getDeclContext());
385
386 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
387 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
388 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
389 isInstanceMethod()))
390 return MD;
391
392 } else if (ObjCCategoryImplDecl *CImplD =
393 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000394 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000395 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
396 isInstanceMethod()))
397 return MD;
398 }
399
400 return this;
401}
402
John McCall85f3d762011-03-02 01:50:55 +0000403ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
404 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000405 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000406 return family;
407
John McCalld5313b02011-03-02 11:33:24 +0000408 // 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;
419 }
420 Family = static_cast<unsigned>(family);
421 return family;
422 }
423
John McCall85f3d762011-03-02 01:50:55 +0000424 family = getSelector().getMethodFamily();
425 switch (family) {
426 case OMF_None: break;
427
428 // init only has a conventional meaning for an instance method, and
429 // it has to return an object.
430 case OMF_init:
431 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
432 family = OMF_None;
433 break;
434
435 // alloc/copy/new have a conventional meaning for both class and
436 // instance methods, but they require an object return.
437 case OMF_alloc:
438 case OMF_copy:
439 case OMF_mutableCopy:
440 case OMF_new:
441 if (!getResultType()->isObjCObjectPointerType())
442 family = OMF_None;
443 break;
444
445 // These selectors have a conventional meaning only for instance methods.
446 case OMF_dealloc:
447 case OMF_retain:
448 case OMF_release:
449 case OMF_autorelease:
450 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000451 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000452 if (!isInstanceMethod())
453 family = OMF_None;
454 break;
455 }
456
457 // Cache the result.
458 Family = static_cast<unsigned>(family);
459 return family;
460}
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000463 const ObjCInterfaceDecl *OID) {
464 QualType selfTy;
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.
468 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000469 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000470 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000471 } else {
472 selfTy = Context.getObjCIdType();
473 }
474 } else // we have a factory method.
475 selfTy = Context.getObjCClassType();
476
John McCall7acddac2011-06-17 06:42:21 +0000477 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000478 bool selfIsConsumed = false;
479 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
480 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
481
John McCall7acddac2011-06-17 06:42:21 +0000482 // 'self' is always __strong. It's actually pseudo-strong except
483 // in init methods, though.
John McCallf85e1932011-06-15 23:02:42 +0000484 Qualifiers qs;
485 qs.setObjCLifetime(Qualifiers::OCL_Strong);
486 selfTy = Context.getQualifiedType(selfTy, qs);
487
488 // In addition, 'self' is const unless this is an init method.
John McCall7acddac2011-06-17 06:42:21 +0000489 if (getMethodFamily() != OMF_init) {
John McCallf85e1932011-06-15 23:02:42 +0000490 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000491 selfIsPseudoStrong = true;
492 }
John McCallf85e1932011-06-15 23:02:42 +0000493 }
494
495 ImplicitParamDecl *self
496 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
497 &Context.Idents.get("self"), selfTy);
498 setSelfDecl(self);
499
500 if (selfIsConsumed)
501 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000502
John McCall7acddac2011-06-17 06:42:21 +0000503 if (selfIsPseudoStrong)
504 self->setARCPseudoStrong(true);
505
Mike Stump1eb44332009-09-09 15:08:12 +0000506 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
507 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000508 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000509}
510
Chris Lattnerab351632009-02-20 20:59:54 +0000511ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
512 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
513 return ID;
514 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
515 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000516 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000517 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000518
519 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerab351632009-02-20 20:59:54 +0000520 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000521 return 0;
522}
523
Chris Lattnerab351632009-02-20 20:59:54 +0000524//===----------------------------------------------------------------------===//
525// ObjCInterfaceDecl
526//===----------------------------------------------------------------------===//
527
528ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
529 DeclContext *DC,
530 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000531 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000532 SourceLocation ClassLoc,
533 bool ForwardDecl, bool isInternal){
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000534 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
535 isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000536}
537
538ObjCInterfaceDecl::
539ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000540 SourceLocation CLoc, bool FD, bool isInternal)
Chris Lattnerab351632009-02-20 20:59:54 +0000541 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
542 TypeForDecl(0), SuperClass(0),
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000543 CategoryList(0), IvarList(0),
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000544 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false),
Chris Lattnerab351632009-02-20 20:59:54 +0000545 ClassLoc(CLoc) {
546}
547
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000548void ObjCInterfaceDecl::LoadExternalDefinition() const {
549 assert(ExternallyCompleted && "Class is not externally completed");
550 ExternallyCompleted = false;
551 getASTContext().getExternalSource()->CompleteType(
552 const_cast<ObjCInterfaceDecl *>(this));
553}
554
555void ObjCInterfaceDecl::setExternallyCompleted() {
556 assert(getASTContext().getExternalSource() &&
557 "Class can't be externally completed without an external source");
558 assert(!ForwardDecl &&
559 "Forward declarations can't be externally completed");
560 ExternallyCompleted = true;
561}
562
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000563ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000564 if (ExternallyCompleted)
565 LoadExternalDefinition();
566
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000567 return getASTContext().getObjCImplementation(
568 const_cast<ObjCInterfaceDecl*>(this));
569}
570
571void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
572 getASTContext().setObjCImplementation(this, ImplD);
573}
574
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000575/// all_declared_ivar_begin - return first ivar declared in this class,
576/// its extensions and its implementation. Lazily build the list on first
577/// access.
578ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
579 if (IvarList)
580 return IvarList;
581
582 ObjCIvarDecl *curIvar = 0;
583 if (!ivar_empty()) {
584 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
585 IvarList = (*I); ++I;
586 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
587 curIvar->setNextIvar(*I);
588 }
589
590 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
591 CDecl = CDecl->getNextClassExtension()) {
592 if (!CDecl->ivar_empty()) {
593 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
594 E = CDecl->ivar_end();
595 if (!IvarList) {
596 IvarList = (*I); ++I;
597 curIvar = IvarList;
598 }
599 for ( ;I != E; curIvar = *I, ++I)
600 curIvar->setNextIvar(*I);
601 }
602 }
603
604 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
605 if (!ImplDecl->ivar_empty()) {
606 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
607 E = ImplDecl->ivar_end();
608 if (!IvarList) {
609 IvarList = (*I); ++I;
610 curIvar = IvarList;
611 }
612 for ( ;I != E; curIvar = *I, ++I)
613 curIvar->setNextIvar(*I);
614 }
615 }
616 return IvarList;
617}
Chris Lattnerab351632009-02-20 20:59:54 +0000618
619/// FindCategoryDeclaration - Finds category declaration in the list of
620/// categories for this class and returns it. Name of the category is passed
621/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000622///
Chris Lattnerab351632009-02-20 20:59:54 +0000623ObjCCategoryDecl *
624ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000625 if (ExternallyCompleted)
626 LoadExternalDefinition();
627
Chris Lattnerab351632009-02-20 20:59:54 +0000628 for (ObjCCategoryDecl *Category = getCategoryList();
629 Category; Category = Category->getNextClassCategory())
630 if (Category->getIdentifier() == CategoryId)
631 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000632 return 0;
633}
634
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000635ObjCMethodDecl *
636ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
637 for (ObjCCategoryDecl *Category = getCategoryList();
638 Category; Category = Category->getNextClassCategory())
639 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
640 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
641 return MD;
642 return 0;
643}
644
645ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
646 for (ObjCCategoryDecl *Category = getCategoryList();
647 Category; Category = Category->getNextClassCategory())
648 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
649 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
650 return MD;
651 return 0;
652}
653
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000654/// ClassImplementsProtocol - Checks that 'lProto' protocol
655/// has been implemented in IDecl class, its super class or categories (if
656/// lookupCategory is true).
657bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
658 bool lookupCategory,
659 bool RHSIsQualifiedID) {
660 ObjCInterfaceDecl *IDecl = this;
661 // 1st, look up the class.
662 const ObjCList<ObjCProtocolDecl> &Protocols =
663 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000665 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
666 E = Protocols.end(); PI != E; ++PI) {
667 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
668 return true;
669 // This is dubious and is added to be compatible with gcc. In gcc, it is
670 // also allowed assigning a protocol-qualified 'id' type to a LHS object
671 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
672 // object. This IMO, should be a bug.
673 // FIXME: Treat this as an extension, and flag this as an error when GCC
674 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000675 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000676 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
677 return true;
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000680 // 2nd, look up the category.
681 if (lookupCategory)
682 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
683 CDecl = CDecl->getNextClassCategory()) {
684 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
685 E = CDecl->protocol_end(); PI != E; ++PI)
686 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
687 return true;
688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000690 // 3rd, look up the super class(s)
691 if (IDecl->getSuperClass())
692 return
693 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
694 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000696 return false;
697}
698
Chris Lattnerab351632009-02-20 20:59:54 +0000699//===----------------------------------------------------------------------===//
700// ObjCIvarDecl
701//===----------------------------------------------------------------------===//
702
Daniel Dunbara0654922010-04-02 20:10:03 +0000703ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000704 SourceLocation StartLoc,
705 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000706 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000707 AccessControl ac, Expr *BW,
708 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000709 if (DC) {
710 // Ivar's can only appear in interfaces, implementations (via synthesized
711 // properties), and class extensions (via direct declaration, or synthesized
712 // properties).
713 //
714 // FIXME: This should really be asserting this:
715 // (isa<ObjCCategoryDecl>(DC) &&
716 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
717 // but unfortunately we sometimes place ivars into non-class extension
718 // categories on error. This breaks an AST invariant, and should not be
719 // fixed.
720 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
721 isa<ObjCCategoryDecl>(DC)) &&
722 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000723 // Once a new ivar is created in any of class/class-extension/implementation
724 // decl contexts, the previously built IvarList must be rebuilt.
725 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
726 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000727 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000728 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000729 if (BW)
730 IM->setHasSynthBitfield(true);
731 }
732 else {
733 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
734 ID = CD->getClassInterface();
735 if (BW)
736 CD->setHasSynthBitfield(true);
737 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000738 }
739 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000740 }
741
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000742 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
743 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000744}
745
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000746const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
747 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000748
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000749 switch (DC->getKind()) {
750 default:
751 case ObjCCategoryImpl:
752 case ObjCProtocol:
753 assert(0 && "invalid ivar container!");
754 return 0;
755
756 // Ivars can only appear in class extension categories.
757 case ObjCCategory: {
758 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
759 assert(CD->IsClassExtension() && "invalid container for ivar!");
760 return CD->getClassInterface();
761 }
762
763 case ObjCImplementation:
764 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
765
766 case ObjCInterface:
767 return cast<ObjCInterfaceDecl>(DC);
768 }
769}
Chris Lattnerab351632009-02-20 20:59:54 +0000770
771//===----------------------------------------------------------------------===//
772// ObjCAtDefsFieldDecl
773//===----------------------------------------------------------------------===//
774
775ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000776*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
777 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000778 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000779 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000780}
781
Chris Lattnerab351632009-02-20 20:59:54 +0000782//===----------------------------------------------------------------------===//
783// ObjCProtocolDecl
784//===----------------------------------------------------------------------===//
785
786ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000787 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000788 IdentifierInfo *Id) {
789 return new (C) ObjCProtocolDecl(DC, L, Id);
790}
791
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000792ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
793 ObjCProtocolDecl *PDecl = this;
794
795 if (Name == getIdentifier())
796 return PDecl;
797
798 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
799 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
800 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000802 return NULL;
803}
804
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000805// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000806// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000807ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
808 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000809 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000811 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000812 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Chris Lattnerab351632009-02-20 20:59:54 +0000814 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000815 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000816 return MethodDecl;
817 return NULL;
818}
819
820//===----------------------------------------------------------------------===//
821// ObjCClassDecl
822//===----------------------------------------------------------------------===//
823
Mike Stump1eb44332009-09-09 15:08:12 +0000824ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000825 ObjCInterfaceDecl *const *Elts,
826 const SourceLocation *Locs,
827 unsigned nElts,
Chris Lattner38af2de2009-02-20 21:35:13 +0000828 ASTContext &C)
829 : Decl(ObjCClass, DC, L) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000830 setClassList(C, Elts, Locs, nElts);
Chris Lattner38af2de2009-02-20 21:35:13 +0000831}
832
Ted Kremenek321c22f2009-11-18 00:28:11 +0000833void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
834 const SourceLocation *Locs, unsigned Num) {
835 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
Chris Lattner32488542010-10-30 05:14:06 +0000836 llvm::alignOf<ObjCClassRef>());
Ted Kremenek321c22f2009-11-18 00:28:11 +0000837 for (unsigned i = 0; i < Num; ++i)
838 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
839
840 NumDecls = Num;
841}
Chris Lattner38af2de2009-02-20 21:35:13 +0000842
Chris Lattnerab351632009-02-20 20:59:54 +0000843ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
844 SourceLocation L,
845 ObjCInterfaceDecl *const *Elts,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000846 const SourceLocation *Locs,
Chris Lattnerab351632009-02-20 20:59:54 +0000847 unsigned nElts) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000848 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000849}
850
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000851SourceRange ObjCClassDecl::getSourceRange() const {
852 // FIXME: We should include the semicolon
853 assert(NumDecls);
854 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
855}
856
Chris Lattnerab351632009-02-20 20:59:54 +0000857//===----------------------------------------------------------------------===//
858// ObjCForwardProtocolDecl
859//===----------------------------------------------------------------------===//
860
Chris Lattner38af2de2009-02-20 21:35:13 +0000861ObjCForwardProtocolDecl::
862ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
863 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000864 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000865: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000866 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000867}
868
869
Chris Lattnerab351632009-02-20 20:59:54 +0000870ObjCForwardProtocolDecl *
871ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000872 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000873 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000874 unsigned NumElts,
875 const SourceLocation *Locs) {
876 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000877}
878
Chris Lattnerab351632009-02-20 20:59:54 +0000879//===----------------------------------------------------------------------===//
880// ObjCCategoryDecl
881//===----------------------------------------------------------------------===//
882
883ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000884 SourceLocation AtLoc,
885 SourceLocation ClassNameLoc,
886 SourceLocation CategoryNameLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000887 IdentifierInfo *Id) {
Douglas Gregor3db211b2010-01-16 16:38:58 +0000888 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerab351632009-02-20 20:59:54 +0000889}
890
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000891ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
892 return getASTContext().getObjCImplementation(
893 const_cast<ObjCCategoryDecl*>(this));
894}
895
896void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
897 getASTContext().setObjCImplementation(this, ImplD);
898}
899
900
Chris Lattnerab351632009-02-20 20:59:54 +0000901//===----------------------------------------------------------------------===//
902// ObjCCategoryImplDecl
903//===----------------------------------------------------------------------===//
904
905ObjCCategoryImplDecl *
906ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
907 SourceLocation L,IdentifierInfo *Id,
908 ObjCInterfaceDecl *ClassInterface) {
909 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
910}
911
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000912ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000913 // The class interface might be NULL if we are working with invalid code.
914 if (const ObjCInterfaceDecl *ID = getClassInterface())
915 return ID->FindCategoryDeclaration(getIdentifier());
916 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000917}
918
Chris Lattnerab351632009-02-20 20:59:54 +0000919
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000920void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000921 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000922 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000924}
925
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000926void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
927 ASTContext &Ctx = getASTContext();
928
929 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000930 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000931 if (IFace)
932 Ctx.setObjCImplementation(IFace, ImplD);
933
Duncan Sands98f2cca2009-07-21 07:56:29 +0000934 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000935 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
936 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
937 Ctx.setObjCImplementation(CD, ImplD);
938 }
939
940 ClassInterface = IFace;
941}
942
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000943/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000944/// properties implemented in this category @implementation block and returns
945/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000946///
Chris Lattner3aa18612009-02-28 18:42:10 +0000947ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000948FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
949 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000950 ObjCPropertyImplDecl *PID = *i;
951 if (PID->getPropertyIvarDecl() &&
952 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
953 return PID;
954 }
955 return 0;
956}
957
958/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
959/// added to the list of those properties @synthesized/@dynamic in this
960/// category @implementation block.
961///
Chris Lattner3aa18612009-02-28 18:42:10 +0000962ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963FindPropertyImplDecl(IdentifierInfo *Id) const {
964 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000965 ObjCPropertyImplDecl *PID = *i;
966 if (PID->getPropertyDecl()->getIdentifier() == Id)
967 return PID;
968 }
969 return 0;
970}
971
Benjamin Kramer900fc632010-04-17 09:33:03 +0000972llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
973 const ObjCCategoryImplDecl *CID) {
974 OS << CID->getName();
975 return OS;
976}
977
Chris Lattnerab351632009-02-20 20:59:54 +0000978//===----------------------------------------------------------------------===//
979// ObjCImplementationDecl
980//===----------------------------------------------------------------------===//
981
982ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000983ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +0000984 SourceLocation L,
985 ObjCInterfaceDecl *ClassInterface,
986 ObjCInterfaceDecl *SuperDecl) {
987 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
988}
989
Benjamin Kramer900fc632010-04-17 09:33:03 +0000990llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
991 const ObjCImplementationDecl *ID) {
992 OS << ID->getName();
993 return OS;
994}
995
Chris Lattnerab351632009-02-20 20:59:54 +0000996//===----------------------------------------------------------------------===//
997// ObjCCompatibleAliasDecl
998//===----------------------------------------------------------------------===//
999
1000ObjCCompatibleAliasDecl *
1001ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1002 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001003 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001004 ObjCInterfaceDecl* AliasedClass) {
1005 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1006}
1007
1008//===----------------------------------------------------------------------===//
1009// ObjCPropertyDecl
1010//===----------------------------------------------------------------------===//
1011
1012ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1013 SourceLocation L,
1014 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001015 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001016 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001017 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001018 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001019}
1020
Chris Lattnerab351632009-02-20 20:59:54 +00001021//===----------------------------------------------------------------------===//
1022// ObjCPropertyImplDecl
1023//===----------------------------------------------------------------------===//
1024
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001025ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001026 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001027 SourceLocation atLoc,
1028 SourceLocation L,
1029 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001030 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001031 ObjCIvarDecl *ivar,
1032 SourceLocation ivarLoc) {
1033 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1034 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001035}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001036
Douglas Gregora4ffd852010-11-17 01:03:52 +00001037SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1038 SourceLocation EndLoc = getLocation();
1039 if (IvarLoc.isValid())
1040 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001041
Douglas Gregora4ffd852010-11-17 01:03:52 +00001042 return SourceRange(AtLoc, EndLoc);
1043}