blob: f0a16db64450dbb21ea20e4a9bc7d1bbc4414595 [file] [log] [blame]
Chris Lattner89375192008-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 Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner89375192008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner8d8829e2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner22298722009-02-20 21:35:13 +000024void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorb412e172010-07-25 18:17:45 +000025 List = 0;
Chris Lattner4d1eb762009-02-20 21:16:26 +000026 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000027
28
Chris Lattner7c981a72009-02-20 21:44:01 +000029 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000030 NumElts = Elts;
31 memcpy(List, InList, sizeof(void*)*Elts);
32}
33
Douglas Gregor002b6712010-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 Lattner4d1eb762009-02-20 21:16:26 +000044//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000045// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000046//===----------------------------------------------------------------------===//
47
Fariborz Jahanian68453832009-06-05 18:16:35 +000048/// getIvarDecl - This method looks up an ivar in this ContextDecl.
49///
50ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000051ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian68453832009-06-05 18:16:35 +000052 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000053 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000054 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
55 return ivar;
56 }
57 return 0;
58}
59
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000060// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000061ObjCMethodDecl *
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000062ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroffc4173fa2009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +000072 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000073 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000074 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000075 return MD;
76 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000077 return 0;
78}
79
Ted Kremenek4fb821e2010-03-15 20:11:46 +000080ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +000081ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +000082 IdentifierInfo *propertyID) {
83
Ted Kremenekddcd1092010-03-15 20:11:53 +000084 DeclContext::lookup_const_iterator I, E;
Ted Kremenek4fb821e2010-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 Jahaniana054e992008-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 Jahaniana054e992008-04-21 19:04:53 +000095ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000096ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremenekddcd1092010-03-15 20:11:53 +000098 if (ObjCPropertyDecl *PD =
99 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
100 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Ted Kremenekddcd1092010-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 Jahanian30a42922010-02-15 21:55:26 +0000109 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
110 return P;
Ted Kremenekddcd1092010-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 Kremenek0ef508d2010-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 Kremenekddcd1092010-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 Jahaniandab04842009-01-19 18:16:19 +0000144 }
145 }
Steve Narofff9c65242008-06-05 13:55:23 +0000146 return 0;
147}
148
Fariborz Jahaniande8db162009-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 Kremenekd133a862010-03-15 20:30:07 +0000151/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000152///
153ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000154ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000155 IdentifierInfo *PropertyId) const {
Douglas Gregor73693022010-12-01 23:49:52 +0000156 if (ExternallyCompleted)
157 LoadExternalDefinition();
158
Ted Kremenekd133a862010-03-15 20:30:07 +0000159 if (ObjCPropertyDecl *PD =
160 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
161 return PD;
162
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000163 // Look through protocols.
Ted Kremenek0ef508d2010-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 Jahaniande8db162009-11-02 22:45:15 +0000167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
168 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000169
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000170 return 0;
171}
172
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000173void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
174 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
175 ASTContext &C)
176{
Douglas Gregor73693022010-12-01 23:49:52 +0000177 if (ExternallyCompleted)
178 LoadExternalDefinition();
179
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000180 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
181 AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000182 return;
183 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000184
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000185 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000186 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-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 Kremenek0ef508d2010-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 Jahanian092cd6e2009-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 Kremenek0ef508d2010-09-01 01:21:15 +0000203 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000204 ProtocolRefs.push_back(ProtoInExtension);
205 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000206
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000207 if (ProtocolRefs.empty())
208 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000209
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000210 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-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 Jahanian092cd6e2009-10-05 20:41:32 +0000213 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000214 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000215
216 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000217}
218
Fariborz Jahanian3bf0ded2010-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 Jahanianafe13862010-02-23 01:26:30 +0000222 CDecl = CDecl->getNextClassCategory())
223 if (CDecl->IsClassExtension())
224 return CDecl;
225 return 0;
226}
227
Fariborz Jahanian3bf0ded2010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000237ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
238 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner89375192008-03-16 00:19:01 +0000239 ObjCInterfaceDecl* ClassDecl = this;
240 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000241 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000242 clsDeclared = ClassDecl;
243 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000244 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000245 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
246 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000247 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
248 clsDeclared = ClassDecl;
249 return I;
250 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000251 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000252
Chris Lattner89375192008-03-16 00:19:01 +0000253 ClassDecl = ClassDecl->getSuperClass();
254 }
255 return NULL;
256}
257
Fariborz Jahaniandb3a4c12009-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 Kyrtzidis553376b2009-07-25 22:15:51 +0000272/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000273/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000274ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
275 bool isInstance) const {
276 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000277 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Douglas Gregor73693022010-12-01 23:49:52 +0000279 if (ExternallyCompleted)
280 LoadExternalDefinition();
281
Chris Lattner89375192008-03-16 00:19:01 +0000282 while (ClassDecl != NULL) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000283 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000284 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner89375192008-03-16 00:19:01 +0000286 // Didn't find one yet - look through protocols.
Chris Lattnerd0045052008-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 Kyrtzidis553376b2009-07-25 22:15:51 +0000291 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000292 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner89375192008-03-16 00:19:01 +0000294 // Didn't find one yet - now look through categories.
295 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
296 while (CatDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000297 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000298 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Steve Naroff5fd31b12008-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 Kyrtzidis553376b2009-07-25 22:15:51 +0000305 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff8ec27842009-02-26 11:32:02 +0000306 return MethodDecl;
Chris Lattner89375192008-03-16 00:19:01 +0000307 CatDecl = CatDecl->getNextClassCategory();
308 }
309 ClassDecl = ClassDecl->getSuperClass();
310 }
311 return NULL;
312}
313
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000314ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
315 const Selector &Sel,
316 bool Instance) {
Steve Naroffbb69c942009-10-01 23:46:04 +0000317 ObjCMethodDecl *Method = 0;
318 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000319 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
320 : ImpDecl->getClassMethod(Sel);
Steve Naroffbb69c942009-10-01 23:46:04 +0000321
322 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000323 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000324 return Method;
325}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000326
327//===----------------------------------------------------------------------===//
328// ObjCMethodDecl
329//===----------------------------------------------------------------------===//
330
331ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000332 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000333 SourceLocation endLoc,
334 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000335 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000336 DeclContext *contextDecl,
337 bool isInstance,
338 bool isVariadic,
339 bool isSynthesized,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000340 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000341 ImplementationControl impControl,
Douglas Gregor33823722011-06-11 01:09:30 +0000342 bool HasRelatedResultType,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000343 unsigned numSelectorArgs) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000344 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000345 SelInfo, T, ResultTInfo, contextDecl,
346 isInstance,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000347 isVariadic, isSynthesized, isDefined,
348 impControl,
Douglas Gregor33823722011-06-11 01:09:30 +0000349 HasRelatedResultType,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000350 numSelectorArgs);
Chris Lattner89375192008-03-16 00:19:01 +0000351}
352
Argyrios Kyrtzidisa8cf0be2009-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 Kyrtzidisa56fa192009-07-28 05:11:05 +0000369 } else if (ObjCImplementationDecl *ImplD =
370 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000371 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
372 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000373
374 } else if (ObjCCategoryImplDecl *CImplD =
375 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000376 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000377 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000378 }
379
380 return Redecl ? Redecl : this;
381}
382
Argyrios Kyrtzidisf390c432009-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 Narofff406f4d2009-10-29 21:11:04 +0000394 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-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 McCallb4526252011-03-02 01:50:55 +0000403ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
404 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000405 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000406 return family;
407
John McCall86bc21f2011-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 McCallb4526252011-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 Gregor33823722011-06-11 01:09:30 +0000451 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000452 if (!isInstanceMethod())
453 family = OMF_None;
454 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000455
456 case OMF_performSelector:
457 if (!isInstanceMethod() ||
458 !getResultType()->isObjCIdType())
459 family = OMF_None;
460 else {
461 unsigned noParams = param_size();
462 if (noParams < 1 || noParams > 3)
463 family = OMF_None;
464 else {
465 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
466 QualType ArgT = (*it);
467 if (!ArgT->isObjCSelType()) {
468 family = OMF_None;
469 break;
470 }
471 while (--noParams) {
472 it++;
473 ArgT = (*it);
474 if (!ArgT->isObjCIdType()) {
475 family = OMF_None;
476 break;
477 }
478 }
479 }
480 }
481 break;
482
John McCallb4526252011-03-02 01:50:55 +0000483 }
484
485 // Cache the result.
486 Family = static_cast<unsigned>(family);
487 return family;
488}
489
Mike Stump11289f42009-09-09 15:08:12 +0000490void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000491 const ObjCInterfaceDecl *OID) {
492 QualType selfTy;
493 if (isInstanceMethod()) {
494 // There may be no interface context due to error in declaration
495 // of the interface (which has been reported). Recover gracefully.
496 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000497 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000498 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000499 } else {
500 selfTy = Context.getObjCIdType();
501 }
502 } else // we have a factory method.
503 selfTy = Context.getObjCClassType();
504
John McCalld4631322011-06-17 06:42:21 +0000505 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000506 bool selfIsConsumed = false;
507 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
508 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
509
John McCalld4631322011-06-17 06:42:21 +0000510 // 'self' is always __strong. It's actually pseudo-strong except
511 // in init methods, though.
John McCall31168b02011-06-15 23:02:42 +0000512 Qualifiers qs;
513 qs.setObjCLifetime(Qualifiers::OCL_Strong);
514 selfTy = Context.getQualifiedType(selfTy, qs);
515
516 // In addition, 'self' is const unless this is an init method.
John McCalld4631322011-06-17 06:42:21 +0000517 if (getMethodFamily() != OMF_init) {
John McCall31168b02011-06-15 23:02:42 +0000518 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000519 selfIsPseudoStrong = true;
520 }
John McCall31168b02011-06-15 23:02:42 +0000521 }
522
523 ImplicitParamDecl *self
524 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
525 &Context.Idents.get("self"), selfTy);
526 setSelfDecl(self);
527
528 if (selfIsConsumed)
529 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000530
John McCalld4631322011-06-17 06:42:21 +0000531 if (selfIsPseudoStrong)
532 self->setARCPseudoStrong(true);
533
Mike Stump11289f42009-09-09 15:08:12 +0000534 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
535 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000536 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000537}
538
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000539ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
540 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
541 return ID;
542 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
543 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000544 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000545 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000546
547 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000548 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000549 return 0;
550}
551
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000552//===----------------------------------------------------------------------===//
553// ObjCInterfaceDecl
554//===----------------------------------------------------------------------===//
555
556ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
557 DeclContext *DC,
558 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000559 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000560 SourceLocation ClassLoc,
561 bool ForwardDecl, bool isInternal){
Douglas Gregor1c283312010-08-11 12:19:30 +0000562 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
563 isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000564}
565
566ObjCInterfaceDecl::
567ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor1c283312010-08-11 12:19:30 +0000568 SourceLocation CLoc, bool FD, bool isInternal)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000569 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
570 TypeForDecl(0), SuperClass(0),
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000571 CategoryList(0), IvarList(0),
Douglas Gregor73693022010-12-01 23:49:52 +0000572 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false),
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000573 ClassLoc(CLoc) {
574}
575
Douglas Gregor73693022010-12-01 23:49:52 +0000576void ObjCInterfaceDecl::LoadExternalDefinition() const {
577 assert(ExternallyCompleted && "Class is not externally completed");
578 ExternallyCompleted = false;
579 getASTContext().getExternalSource()->CompleteType(
580 const_cast<ObjCInterfaceDecl *>(this));
581}
582
583void ObjCInterfaceDecl::setExternallyCompleted() {
584 assert(getASTContext().getExternalSource() &&
585 "Class can't be externally completed without an external source");
586 assert(!ForwardDecl &&
587 "Forward declarations can't be externally completed");
588 ExternallyCompleted = true;
589}
590
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000591ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor73693022010-12-01 23:49:52 +0000592 if (ExternallyCompleted)
593 LoadExternalDefinition();
594
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000595 return getASTContext().getObjCImplementation(
596 const_cast<ObjCInterfaceDecl*>(this));
597}
598
599void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
600 getASTContext().setObjCImplementation(this, ImplD);
601}
602
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000603/// all_declared_ivar_begin - return first ivar declared in this class,
604/// its extensions and its implementation. Lazily build the list on first
605/// access.
606ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
607 if (IvarList)
608 return IvarList;
609
610 ObjCIvarDecl *curIvar = 0;
611 if (!ivar_empty()) {
612 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
613 IvarList = (*I); ++I;
614 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
615 curIvar->setNextIvar(*I);
616 }
617
618 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
619 CDecl = CDecl->getNextClassExtension()) {
620 if (!CDecl->ivar_empty()) {
621 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
622 E = CDecl->ivar_end();
623 if (!IvarList) {
624 IvarList = (*I); ++I;
625 curIvar = IvarList;
626 }
627 for ( ;I != E; curIvar = *I, ++I)
628 curIvar->setNextIvar(*I);
629 }
630 }
631
632 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
633 if (!ImplDecl->ivar_empty()) {
634 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
635 E = ImplDecl->ivar_end();
636 if (!IvarList) {
637 IvarList = (*I); ++I;
638 curIvar = IvarList;
639 }
640 for ( ;I != E; curIvar = *I, ++I)
641 curIvar->setNextIvar(*I);
642 }
643 }
644 return IvarList;
645}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000646
647/// FindCategoryDeclaration - Finds category declaration in the list of
648/// categories for this class and returns it. Name of the category is passed
649/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000650///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000651ObjCCategoryDecl *
652ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor73693022010-12-01 23:49:52 +0000653 if (ExternallyCompleted)
654 LoadExternalDefinition();
655
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000656 for (ObjCCategoryDecl *Category = getCategoryList();
657 Category; Category = Category->getNextClassCategory())
658 if (Category->getIdentifier() == CategoryId)
659 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000660 return 0;
661}
662
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000663ObjCMethodDecl *
664ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
665 for (ObjCCategoryDecl *Category = getCategoryList();
666 Category; Category = Category->getNextClassCategory())
667 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
668 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
669 return MD;
670 return 0;
671}
672
673ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
674 for (ObjCCategoryDecl *Category = getCategoryList();
675 Category; Category = Category->getNextClassCategory())
676 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
677 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
678 return MD;
679 return 0;
680}
681
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000682/// ClassImplementsProtocol - Checks that 'lProto' protocol
683/// has been implemented in IDecl class, its super class or categories (if
684/// lookupCategory is true).
685bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
686 bool lookupCategory,
687 bool RHSIsQualifiedID) {
688 ObjCInterfaceDecl *IDecl = this;
689 // 1st, look up the class.
690 const ObjCList<ObjCProtocolDecl> &Protocols =
691 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000692
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000693 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
694 E = Protocols.end(); PI != E; ++PI) {
695 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
696 return true;
697 // This is dubious and is added to be compatible with gcc. In gcc, it is
698 // also allowed assigning a protocol-qualified 'id' type to a LHS object
699 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
700 // object. This IMO, should be a bug.
701 // FIXME: Treat this as an extension, and flag this as an error when GCC
702 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000703 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000704 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
705 return true;
706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000708 // 2nd, look up the category.
709 if (lookupCategory)
710 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
711 CDecl = CDecl->getNextClassCategory()) {
712 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
713 E = CDecl->protocol_end(); PI != E; ++PI)
714 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
715 return true;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000718 // 3rd, look up the super class(s)
719 if (IDecl->getSuperClass())
720 return
721 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
722 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000724 return false;
725}
726
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000727//===----------------------------------------------------------------------===//
728// ObjCIvarDecl
729//===----------------------------------------------------------------------===//
730
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000731ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000732 SourceLocation StartLoc,
733 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000734 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +0000735 AccessControl ac, Expr *BW,
736 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000737 if (DC) {
738 // Ivar's can only appear in interfaces, implementations (via synthesized
739 // properties), and class extensions (via direct declaration, or synthesized
740 // properties).
741 //
742 // FIXME: This should really be asserting this:
743 // (isa<ObjCCategoryDecl>(DC) &&
744 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
745 // but unfortunately we sometimes place ivars into non-class extension
746 // categories on error. This breaks an AST invariant, and should not be
747 // fixed.
748 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
749 isa<ObjCCategoryDecl>(DC)) &&
750 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000751 // Once a new ivar is created in any of class/class-extension/implementation
752 // decl contexts, the previously built IvarList must be rebuilt.
753 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
754 if (!ID) {
Fariborz Jahanianbf9294f2010-08-23 18:51:39 +0000755 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000756 ID = IM->getClassInterface();
Fariborz Jahanianbf9294f2010-08-23 18:51:39 +0000757 if (BW)
758 IM->setHasSynthBitfield(true);
759 }
760 else {
761 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
762 ID = CD->getClassInterface();
763 if (BW)
764 CD->setHasSynthBitfield(true);
765 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000766 }
767 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000768 }
769
Abramo Bagnaradff19302011-03-08 08:55:46 +0000770 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
771 ac, BW, synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000772}
773
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000774const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
775 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000776
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000777 switch (DC->getKind()) {
778 default:
779 case ObjCCategoryImpl:
780 case ObjCProtocol:
781 assert(0 && "invalid ivar container!");
782 return 0;
783
784 // Ivars can only appear in class extension categories.
785 case ObjCCategory: {
786 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
787 assert(CD->IsClassExtension() && "invalid container for ivar!");
788 return CD->getClassInterface();
789 }
790
791 case ObjCImplementation:
792 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
793
794 case ObjCInterface:
795 return cast<ObjCInterfaceDecl>(DC);
796 }
797}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000798
799//===----------------------------------------------------------------------===//
800// ObjCAtDefsFieldDecl
801//===----------------------------------------------------------------------===//
802
803ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +0000804*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
805 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000806 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaradff19302011-03-08 08:55:46 +0000807 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000808}
809
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000810//===----------------------------------------------------------------------===//
811// ObjCProtocolDecl
812//===----------------------------------------------------------------------===//
813
814ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000815 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000816 IdentifierInfo *Id) {
817 return new (C) ObjCProtocolDecl(DC, L, Id);
818}
819
Steve Naroff114aecb2009-03-01 16:12:44 +0000820ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
821 ObjCProtocolDecl *PDecl = this;
822
823 if (Name == getIdentifier())
824 return PDecl;
825
826 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
827 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
828 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Steve Naroff114aecb2009-03-01 16:12:44 +0000830 return NULL;
831}
832
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000833// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000834// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000835ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
836 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000837 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000838
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000839 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000840 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000842 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000843 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000844 return MethodDecl;
845 return NULL;
846}
847
848//===----------------------------------------------------------------------===//
849// ObjCClassDecl
850//===----------------------------------------------------------------------===//
851
Mike Stump11289f42009-09-09 15:08:12 +0000852ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000853 ObjCInterfaceDecl *const *Elts,
854 const SourceLocation *Locs,
855 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000856 ASTContext &C)
857 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000858 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000859}
860
Ted Kremenek9b124e12009-11-18 00:28:11 +0000861void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
862 const SourceLocation *Locs, unsigned Num) {
863 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
Chris Lattner5c0b4052010-10-30 05:14:06 +0000864 llvm::alignOf<ObjCClassRef>());
Ted Kremenek9b124e12009-11-18 00:28:11 +0000865 for (unsigned i = 0; i < Num; ++i)
866 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
867
868 NumDecls = Num;
869}
Chris Lattner22298722009-02-20 21:35:13 +0000870
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000871ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
872 SourceLocation L,
873 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000874 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000875 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000876 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000877}
878
Ted Kremenek49939c22009-11-18 01:26:56 +0000879SourceRange ObjCClassDecl::getSourceRange() const {
880 // FIXME: We should include the semicolon
881 assert(NumDecls);
882 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
883}
884
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000885//===----------------------------------------------------------------------===//
886// ObjCForwardProtocolDecl
887//===----------------------------------------------------------------------===//
888
Chris Lattner22298722009-02-20 21:35:13 +0000889ObjCForwardProtocolDecl::
890ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
891 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000892 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000893: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000894 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000895}
896
897
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000898ObjCForwardProtocolDecl *
899ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000900 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000901 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000902 unsigned NumElts,
903 const SourceLocation *Locs) {
904 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000905}
906
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000907//===----------------------------------------------------------------------===//
908// ObjCCategoryDecl
909//===----------------------------------------------------------------------===//
910
911ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000912 SourceLocation AtLoc,
913 SourceLocation ClassNameLoc,
914 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000915 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000916 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000917}
918
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000919ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
920 return getASTContext().getObjCImplementation(
921 const_cast<ObjCCategoryDecl*>(this));
922}
923
924void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
925 getASTContext().setObjCImplementation(this, ImplD);
926}
927
928
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000929//===----------------------------------------------------------------------===//
930// ObjCCategoryImplDecl
931//===----------------------------------------------------------------------===//
932
933ObjCCategoryImplDecl *
934ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
935 SourceLocation L,IdentifierInfo *Id,
936 ObjCInterfaceDecl *ClassInterface) {
937 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
938}
939
Steve Narofff406f4d2009-10-29 21:11:04 +0000940ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000941 // The class interface might be NULL if we are working with invalid code.
942 if (const ObjCInterfaceDecl *ID = getClassInterface())
943 return ID->FindCategoryDeclaration(getIdentifier());
944 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000945}
946
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000947
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000948void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000949 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000950 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000951 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000952}
953
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000954void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
955 ASTContext &Ctx = getASTContext();
956
957 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000958 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000959 if (IFace)
960 Ctx.setObjCImplementation(IFace, ImplD);
961
Duncan Sands49c29ee2009-07-21 07:56:29 +0000962 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000963 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
964 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
965 Ctx.setObjCImplementation(CD, ImplD);
966 }
967
968 ClassInterface = IFace;
969}
970
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000971/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000972/// properties implemented in this category @implementation block and returns
973/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000974///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000975ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000976FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
977 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000978 ObjCPropertyImplDecl *PID = *i;
979 if (PID->getPropertyIvarDecl() &&
980 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
981 return PID;
982 }
983 return 0;
984}
985
986/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
987/// added to the list of those properties @synthesized/@dynamic in this
988/// category @implementation block.
989///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000990ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000991FindPropertyImplDecl(IdentifierInfo *Id) const {
992 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000993 ObjCPropertyImplDecl *PID = *i;
994 if (PID->getPropertyDecl()->getIdentifier() == Id)
995 return PID;
996 }
997 return 0;
998}
999
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001000llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
1001 const ObjCCategoryImplDecl *CID) {
1002 OS << CID->getName();
1003 return OS;
1004}
1005
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001006//===----------------------------------------------------------------------===//
1007// ObjCImplementationDecl
1008//===----------------------------------------------------------------------===//
1009
1010ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001011ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001012 SourceLocation L,
1013 ObjCInterfaceDecl *ClassInterface,
1014 ObjCInterfaceDecl *SuperDecl) {
1015 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
1016}
1017
John McCall0410e572011-07-22 04:15:06 +00001018void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1019 CXXCtorInitializer ** initializers,
1020 unsigned numInitializers) {
1021 if (numInitializers > 0) {
1022 NumIvarInitializers = numInitializers;
1023 CXXCtorInitializer **ivarInitializers =
1024 new (C) CXXCtorInitializer*[NumIvarInitializers];
1025 memcpy(ivarInitializers, initializers,
1026 numInitializers * sizeof(CXXCtorInitializer*));
1027 IvarInitializers = ivarInitializers;
1028 }
1029}
1030
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001031llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
1032 const ObjCImplementationDecl *ID) {
1033 OS << ID->getName();
1034 return OS;
1035}
1036
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001037//===----------------------------------------------------------------------===//
1038// ObjCCompatibleAliasDecl
1039//===----------------------------------------------------------------------===//
1040
1041ObjCCompatibleAliasDecl *
1042ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1043 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001044 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001045 ObjCInterfaceDecl* AliasedClass) {
1046 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1047}
1048
1049//===----------------------------------------------------------------------===//
1050// ObjCPropertyDecl
1051//===----------------------------------------------------------------------===//
1052
1053ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1054 SourceLocation L,
1055 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001056 SourceLocation AtLoc,
John McCall339bb662010-06-04 20:50:08 +00001057 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001058 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001059 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001060}
1061
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001062//===----------------------------------------------------------------------===//
1063// ObjCPropertyImplDecl
1064//===----------------------------------------------------------------------===//
1065
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001066ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001067 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001068 SourceLocation atLoc,
1069 SourceLocation L,
1070 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001071 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001072 ObjCIvarDecl *ivar,
1073 SourceLocation ivarLoc) {
1074 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1075 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001076}
Chris Lattnered0e1642008-03-17 01:19:02 +00001077
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001078SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1079 SourceLocation EndLoc = getLocation();
1080 if (IvarLoc.isValid())
1081 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001082
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001083 return SourceRange(AtLoc, EndLoc);
1084}