blob: 0720b203d62bfdd9f5664b615d0b21c2f165414c [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
Sebastian Redlab6a0882010-08-09 21:55:28 +000048ObjCInterfaceDecl *ObjCInterfaceDecl::getDefinition() {
49 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
50 if (I->isDefinition())
51 return *I;
52 }
53 return 0;
54}
55
Fariborz Jahanian68453832009-06-05 18:16:35 +000056/// getIvarDecl - This method looks up an ivar in this ContextDecl.
57///
58ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000059ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000061 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000062 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
63 return ivar;
64 }
65 return 0;
66}
67
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000068// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000069ObjCMethodDecl *
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000070ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroffc4173fa2009-02-22 19:35:57 +000071 // Since instance & class methods can have the same name, the loop below
72 // ensures we get the correct method.
73 //
74 // @interface Whatever
75 // - (int) class_method;
76 // + (float) class_method;
77 // @end
78 //
79 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000080 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000081 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000082 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000083 return MD;
84 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000085 return 0;
86}
87
Ted Kremenek4fb821e2010-03-15 20:11:46 +000088ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +000089ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +000090 IdentifierInfo *propertyID) {
91
Ted Kremenekddcd1092010-03-15 20:11:53 +000092 DeclContext::lookup_const_iterator I, E;
Ted Kremenek4fb821e2010-03-15 20:11:46 +000093 llvm::tie(I, E) = DC->lookup(propertyID);
94 for ( ; I != E; ++I)
95 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
96 return PD;
97
98 return 0;
99}
100
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000101/// FindPropertyDeclaration - Finds declaration of the property given its name
102/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000103ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000104ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenekddcd1092010-03-15 20:11:53 +0000106 if (ObjCPropertyDecl *PD =
107 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
108 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenekddcd1092010-03-15 20:11:53 +0000110 switch (getKind()) {
111 default:
112 break;
113 case Decl::ObjCProtocol: {
114 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
115 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
116 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000117 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
118 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000119 break;
120 }
121 case Decl::ObjCInterface: {
122 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
123 // Look through categories.
124 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
125 Cat; Cat = Cat->getNextClassCategory())
126 if (!Cat->IsClassExtension())
127 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
128 return P;
129
130 // Look through protocols.
131 for (ObjCInterfaceDecl::protocol_iterator
132 I = OID->protocol_begin(), E = OID->protocol_end(); I != E; ++I)
133 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
134 return P;
135
136 // Finally, check the super class.
137 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
138 return superClass->FindPropertyDeclaration(PropertyId);
139 break;
140 }
141 case Decl::ObjCCategory: {
142 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
143 // Look through protocols.
144 if (!OCD->IsClassExtension())
145 for (ObjCCategoryDecl::protocol_iterator
146 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
147 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
148 return P;
149
150 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000151 }
152 }
Steve Narofff9c65242008-06-05 13:55:23 +0000153 return 0;
154}
155
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000156/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
157/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000158/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000159///
160ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000161ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000162 IdentifierInfo *PropertyId) const {
Ted Kremenekd133a862010-03-15 20:30:07 +0000163 if (ObjCPropertyDecl *PD =
164 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
165 return PD;
166
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000167 // Look through protocols.
Ted Kremenekd133a862010-03-15 20:30:07 +0000168 for (ObjCInterfaceDecl::protocol_iterator
169 I = protocol_begin(), E = protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000170 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
171 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000172
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000173 return 0;
174}
175
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000176void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
177 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Douglas Gregor002b6712010-01-16 15:02:53 +0000178 const SourceLocation *Locs,
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000179 ASTContext &C)
180{
181 if (ReferencedProtocols.empty()) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000182 ReferencedProtocols.set(ExtList, ExtNum, Locs, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000183 return;
184 }
185 // Check for duplicate protocol in class's protocol list.
186 // This is (O)2. But it is extremely rare and number of protocols in
187 // class or its extension are very few.
188 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Douglas Gregor002b6712010-01-16 15:02:53 +0000189 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000190 for (unsigned i = 0; i < ExtNum; i++) {
191 bool protocolExists = false;
192 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
193 for (protocol_iterator p = protocol_begin(), e = protocol_end();
194 p != e; p++) {
195 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.
Douglas Gregor002b6712010-01-16 15:02:53 +0000203 if (!protocolExists) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000204 ProtocolRefs.push_back(ProtoInExtension);
Douglas Gregor002b6712010-01-16 15:02:53 +0000205 ProtocolLocs.push_back(Locs[i]);
206 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000207 }
208 if (ProtocolRefs.empty())
209 return;
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000210 // Merge ProtocolRefs into class's protocol list;
Douglas Gregor002b6712010-01-16 15:02:53 +0000211 protocol_loc_iterator pl = protocol_loc_begin();
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000212 for (protocol_iterator p = protocol_begin(), e = protocol_end();
Douglas Gregor002b6712010-01-16 15:02:53 +0000213 p != e; ++p, ++pl) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000214 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000215 ProtocolLocs.push_back(*pl);
216 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000217 unsigned NumProtoRefs = ProtocolRefs.size();
Douglas Gregor002b6712010-01-16 15:02:53 +0000218 setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000219}
220
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000221/// getFirstClassExtension - Find first class extension of the given class.
222ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
223 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000224 CDecl = CDecl->getNextClassCategory())
225 if (CDecl->IsClassExtension())
226 return CDecl;
227 return 0;
228}
229
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000230/// getNextClassCategory - Find next class extension in list of categories.
231const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
232 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
233 CDecl = CDecl->getNextClassCategory())
234 if (CDecl->IsClassExtension())
235 return CDecl;
236 return 0;
237}
238
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000239ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
240 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner89375192008-03-16 00:19:01 +0000241 ObjCInterfaceDecl* ClassDecl = this;
242 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000243 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000244 clsDeclared = ClassDecl;
245 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000246 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000247 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
248 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000249 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
250 clsDeclared = ClassDecl;
251 return I;
252 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000253 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000254
Chris Lattner89375192008-03-16 00:19:01 +0000255 ClassDecl = ClassDecl->getSuperClass();
256 }
257 return NULL;
258}
259
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000260/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
261/// class whose name is passed as argument. If it is not one of the super classes
262/// the it returns NULL.
263ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
264 const IdentifierInfo*ICName) {
265 ObjCInterfaceDecl* ClassDecl = this;
266 while (ClassDecl != NULL) {
267 if (ClassDecl->getIdentifier() == ICName)
268 return ClassDecl;
269 ClassDecl = ClassDecl->getSuperClass();
270 }
271 return NULL;
272}
273
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000274/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000275/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000276ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
277 bool isInstance) const {
278 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000279 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner89375192008-03-16 00:19:01 +0000281 while (ClassDecl != NULL) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000282 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000283 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattner89375192008-03-16 00:19:01 +0000285 // Didn't find one yet - look through protocols.
Chris Lattnerd0045052008-07-21 18:19:38 +0000286 const ObjCList<ObjCProtocolDecl> &Protocols =
287 ClassDecl->getReferencedProtocols();
288 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
289 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000290 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000291 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner89375192008-03-16 00:19:01 +0000293 // Didn't find one yet - now look through categories.
294 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
295 while (CatDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000296 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000297 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Steve Naroff5fd31b12008-12-08 20:57:28 +0000299 // Didn't find one yet - look through protocols.
300 const ObjCList<ObjCProtocolDecl> &Protocols =
301 CatDecl->getReferencedProtocols();
302 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
303 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000304 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff8ec27842009-02-26 11:32:02 +0000305 return MethodDecl;
Chris Lattner89375192008-03-16 00:19:01 +0000306 CatDecl = CatDecl->getNextClassCategory();
307 }
308 ClassDecl = ClassDecl->getSuperClass();
309 }
310 return NULL;
311}
312
Steve Naroffbb69c942009-10-01 23:46:04 +0000313ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
314 const Selector &Sel) {
315 ObjCMethodDecl *Method = 0;
316 if (ObjCImplementationDecl *ImpDecl = getImplementation())
317 Method = ImpDecl->getInstanceMethod(Sel);
318
319 if (!Method && getSuperClass())
320 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
321 return Method;
322}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000323
324//===----------------------------------------------------------------------===//
325// ObjCMethodDecl
326//===----------------------------------------------------------------------===//
327
328ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000329 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000330 SourceLocation endLoc,
331 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000332 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000333 DeclContext *contextDecl,
334 bool isInstance,
335 bool isVariadic,
336 bool isSynthesized,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000337 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000338 ImplementationControl impControl,
339 unsigned numSelectorArgs) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000340 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000341 SelInfo, T, ResultTInfo, contextDecl,
342 isInstance,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000343 isVariadic, isSynthesized, isDefined,
344 impControl,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000345 numSelectorArgs);
Chris Lattner89375192008-03-16 00:19:01 +0000346}
347
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000348/// \brief A definition will return its interface declaration.
349/// An interface declaration will return its definition.
350/// Otherwise it will return itself.
351ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
352 ASTContext &Ctx = getASTContext();
353 ObjCMethodDecl *Redecl = 0;
354 Decl *CtxD = cast<Decl>(getDeclContext());
355
356 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
357 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
358 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
359
360 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
361 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
362 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
363
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000364 } else if (ObjCImplementationDecl *ImplD =
365 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000366 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
367 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000368
369 } else if (ObjCCategoryImplDecl *CImplD =
370 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000371 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000372 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000373 }
374
375 return Redecl ? Redecl : this;
376}
377
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000378ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
379 Decl *CtxD = cast<Decl>(getDeclContext());
380
381 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
382 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
383 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
384 isInstanceMethod()))
385 return MD;
386
387 } else if (ObjCCategoryImplDecl *CImplD =
388 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000389 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000390 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
391 isInstanceMethod()))
392 return MD;
393 }
394
395 return this;
396}
397
Mike Stump11289f42009-09-09 15:08:12 +0000398void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000399 const ObjCInterfaceDecl *OID) {
400 QualType selfTy;
401 if (isInstanceMethod()) {
402 // There may be no interface context due to error in declaration
403 // of the interface (which has been reported). Recover gracefully.
404 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000405 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000406 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000407 } else {
408 selfTy = Context.getObjCIdType();
409 }
410 } else // we have a factory method.
411 selfTy = Context.getObjCClassType();
412
Mike Stump11289f42009-09-09 15:08:12 +0000413 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff04f2d142009-04-20 15:06:07 +0000414 &Context.Idents.get("self"), selfTy));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000415
Mike Stump11289f42009-09-09 15:08:12 +0000416 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
417 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000418 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000419}
420
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000421ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
422 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
423 return ID;
424 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
425 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000426 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000427 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000428
429 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000430 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000431 return 0;
432}
433
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000434//===----------------------------------------------------------------------===//
435// ObjCInterfaceDecl
436//===----------------------------------------------------------------------===//
437
438ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
439 DeclContext *DC,
440 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000441 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000442 SourceLocation ClassLoc,
Sebastian Redlab6a0882010-08-09 21:55:28 +0000443 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000444 bool ForwardDecl, bool isInternal){
Sebastian Redlab6a0882010-08-09 21:55:28 +0000445 ObjCInterfaceDecl *D = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
446 PrevDecl, ForwardDecl,
447 isInternal);
448 C.getObjCInterfaceType(D, PrevDecl);
449 return D;
450}
451
452ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, EmptyShell) {
453 return new (C) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
454 0, false, false);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000455}
456
457ObjCInterfaceDecl::
458ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Sebastian Redlab6a0882010-08-09 21:55:28 +0000459 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
460 bool FD, bool isInternal)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000461 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
462 TypeForDecl(0), SuperClass(0),
463 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
464 ClassLoc(CLoc) {
Sebastian Redlab6a0882010-08-09 21:55:28 +0000465 if (PrevDecl)
466 setPreviousDeclaration(PrevDecl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000467}
468
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000469ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
470 return getASTContext().getObjCImplementation(
471 const_cast<ObjCInterfaceDecl*>(this));
472}
473
474void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
475 getASTContext().setObjCImplementation(this, ImplD);
476}
477
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000478
479/// FindCategoryDeclaration - Finds category declaration in the list of
480/// categories for this class and returns it. Name of the category is passed
481/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000482///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000483ObjCCategoryDecl *
484ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
485 for (ObjCCategoryDecl *Category = getCategoryList();
486 Category; Category = Category->getNextClassCategory())
487 if (Category->getIdentifier() == CategoryId)
488 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000489 return 0;
490}
491
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000492ObjCMethodDecl *
493ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
494 for (ObjCCategoryDecl *Category = getCategoryList();
495 Category; Category = Category->getNextClassCategory())
496 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
497 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
498 return MD;
499 return 0;
500}
501
502ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
503 for (ObjCCategoryDecl *Category = getCategoryList();
504 Category; Category = Category->getNextClassCategory())
505 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
506 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
507 return MD;
508 return 0;
509}
510
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000511/// ClassImplementsProtocol - Checks that 'lProto' protocol
512/// has been implemented in IDecl class, its super class or categories (if
513/// lookupCategory is true).
514bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
515 bool lookupCategory,
516 bool RHSIsQualifiedID) {
517 ObjCInterfaceDecl *IDecl = this;
518 // 1st, look up the class.
519 const ObjCList<ObjCProtocolDecl> &Protocols =
520 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000521
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000522 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
523 E = Protocols.end(); PI != E; ++PI) {
524 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
525 return true;
526 // This is dubious and is added to be compatible with gcc. In gcc, it is
527 // also allowed assigning a protocol-qualified 'id' type to a LHS object
528 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
529 // object. This IMO, should be a bug.
530 // FIXME: Treat this as an extension, and flag this as an error when GCC
531 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000532 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000533 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
534 return true;
535 }
Mike Stump11289f42009-09-09 15:08:12 +0000536
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000537 // 2nd, look up the category.
538 if (lookupCategory)
539 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
540 CDecl = CDecl->getNextClassCategory()) {
541 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
542 E = CDecl->protocol_end(); PI != E; ++PI)
543 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
544 return true;
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000547 // 3rd, look up the super class(s)
548 if (IDecl->getSuperClass())
549 return
550 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
551 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000552
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000553 return false;
554}
555
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000556//===----------------------------------------------------------------------===//
557// ObjCIvarDecl
558//===----------------------------------------------------------------------===//
559
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000560ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000561 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000562 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +0000563 AccessControl ac, Expr *BW,
564 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000565 if (DC) {
566 // Ivar's can only appear in interfaces, implementations (via synthesized
567 // properties), and class extensions (via direct declaration, or synthesized
568 // properties).
569 //
570 // FIXME: This should really be asserting this:
571 // (isa<ObjCCategoryDecl>(DC) &&
572 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
573 // but unfortunately we sometimes place ivars into non-class extension
574 // categories on error. This breaks an AST invariant, and should not be
575 // fixed.
576 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
577 isa<ObjCCategoryDecl>(DC)) &&
578 "Invalid ivar decl context!");
579 }
580
Fariborz Jahanian18722982010-07-17 00:59:30 +0000581 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000582}
583
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000584const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
585 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000586
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000587 switch (DC->getKind()) {
588 default:
589 case ObjCCategoryImpl:
590 case ObjCProtocol:
591 assert(0 && "invalid ivar container!");
592 return 0;
593
594 // Ivars can only appear in class extension categories.
595 case ObjCCategory: {
596 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
597 assert(CD->IsClassExtension() && "invalid container for ivar!");
598 return CD->getClassInterface();
599 }
600
601 case ObjCImplementation:
602 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
603
604 case ObjCInterface:
605 return cast<ObjCInterfaceDecl>(DC);
606 }
607}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000608
609//===----------------------------------------------------------------------===//
610// ObjCAtDefsFieldDecl
611//===----------------------------------------------------------------------===//
612
613ObjCAtDefsFieldDecl
614*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
615 IdentifierInfo *Id, QualType T, Expr *BW) {
616 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
617}
618
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000619//===----------------------------------------------------------------------===//
620// ObjCProtocolDecl
621//===----------------------------------------------------------------------===//
622
623ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000624 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000625 IdentifierInfo *Id) {
626 return new (C) ObjCProtocolDecl(DC, L, Id);
627}
628
Steve Naroff114aecb2009-03-01 16:12:44 +0000629ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
630 ObjCProtocolDecl *PDecl = this;
631
632 if (Name == getIdentifier())
633 return PDecl;
634
635 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
636 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
637 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Steve Naroff114aecb2009-03-01 16:12:44 +0000639 return NULL;
640}
641
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000642// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000643// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000644ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
645 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000646 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000648 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000649 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000650
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000651 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000652 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000653 return MethodDecl;
654 return NULL;
655}
656
657//===----------------------------------------------------------------------===//
658// ObjCClassDecl
659//===----------------------------------------------------------------------===//
660
Mike Stump11289f42009-09-09 15:08:12 +0000661ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000662 ObjCInterfaceDecl *const *Elts,
663 const SourceLocation *Locs,
664 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000665 ASTContext &C)
666 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000667 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000668}
669
Ted Kremenek9b124e12009-11-18 00:28:11 +0000670void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
671 const SourceLocation *Locs, unsigned Num) {
672 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
673 llvm::alignof<ObjCClassRef>());
674 for (unsigned i = 0; i < Num; ++i)
675 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
676
677 NumDecls = Num;
678}
Chris Lattner22298722009-02-20 21:35:13 +0000679
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000680ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
681 SourceLocation L,
682 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000683 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000684 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000685 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000686}
687
Ted Kremenek49939c22009-11-18 01:26:56 +0000688SourceRange ObjCClassDecl::getSourceRange() const {
689 // FIXME: We should include the semicolon
690 assert(NumDecls);
691 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
692}
693
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000694//===----------------------------------------------------------------------===//
695// ObjCForwardProtocolDecl
696//===----------------------------------------------------------------------===//
697
Chris Lattner22298722009-02-20 21:35:13 +0000698ObjCForwardProtocolDecl::
699ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
700 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000701 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000702: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000703 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000704}
705
706
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000707ObjCForwardProtocolDecl *
708ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000709 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000710 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000711 unsigned NumElts,
712 const SourceLocation *Locs) {
713 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000714}
715
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000716//===----------------------------------------------------------------------===//
717// ObjCCategoryDecl
718//===----------------------------------------------------------------------===//
719
720ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000721 SourceLocation AtLoc,
722 SourceLocation ClassNameLoc,
723 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000724 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000725 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000726}
727
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000728ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
729 return getASTContext().getObjCImplementation(
730 const_cast<ObjCCategoryDecl*>(this));
731}
732
733void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
734 getASTContext().setObjCImplementation(this, ImplD);
735}
736
737
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000738//===----------------------------------------------------------------------===//
739// ObjCCategoryImplDecl
740//===----------------------------------------------------------------------===//
741
742ObjCCategoryImplDecl *
743ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
744 SourceLocation L,IdentifierInfo *Id,
745 ObjCInterfaceDecl *ClassInterface) {
746 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
747}
748
Steve Narofff406f4d2009-10-29 21:11:04 +0000749ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000750 // The class interface might be NULL if we are working with invalid code.
751 if (const ObjCInterfaceDecl *ID = getClassInterface())
752 return ID->FindCategoryDeclaration(getIdentifier());
753 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000754}
755
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000756
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000757void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000758 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000759 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000760 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000761}
762
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000763void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
764 ASTContext &Ctx = getASTContext();
765
766 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000767 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000768 if (IFace)
769 Ctx.setObjCImplementation(IFace, ImplD);
770
Duncan Sands49c29ee2009-07-21 07:56:29 +0000771 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000772 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
773 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
774 Ctx.setObjCImplementation(CD, ImplD);
775 }
776
777 ClassInterface = IFace;
778}
779
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000780/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000781/// properties implemented in this category @implementation block and returns
782/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000783///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000784ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000785FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
786 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000787 ObjCPropertyImplDecl *PID = *i;
788 if (PID->getPropertyIvarDecl() &&
789 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
790 return PID;
791 }
792 return 0;
793}
794
795/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
796/// added to the list of those properties @synthesized/@dynamic in this
797/// category @implementation block.
798///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000799ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000800FindPropertyImplDecl(IdentifierInfo *Id) const {
801 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000802 ObjCPropertyImplDecl *PID = *i;
803 if (PID->getPropertyDecl()->getIdentifier() == Id)
804 return PID;
805 }
806 return 0;
807}
808
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000809llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
810 const ObjCCategoryImplDecl *CID) {
811 OS << CID->getName();
812 return OS;
813}
814
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000815//===----------------------------------------------------------------------===//
816// ObjCImplementationDecl
817//===----------------------------------------------------------------------===//
818
819ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000820ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000821 SourceLocation L,
822 ObjCInterfaceDecl *ClassInterface,
823 ObjCInterfaceDecl *SuperDecl) {
824 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
825}
826
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000827llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
828 const ObjCImplementationDecl *ID) {
829 OS << ID->getName();
830 return OS;
831}
832
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000833//===----------------------------------------------------------------------===//
834// ObjCCompatibleAliasDecl
835//===----------------------------------------------------------------------===//
836
837ObjCCompatibleAliasDecl *
838ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
839 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000840 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000841 ObjCInterfaceDecl* AliasedClass) {
842 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
843}
844
845//===----------------------------------------------------------------------===//
846// ObjCPropertyDecl
847//===----------------------------------------------------------------------===//
848
849ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
850 SourceLocation L,
851 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000852 SourceLocation AtLoc,
John McCall339bb662010-06-04 20:50:08 +0000853 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000854 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000855 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000856}
857
858
859//===----------------------------------------------------------------------===//
860// ObjCPropertyImplDecl
861//===----------------------------------------------------------------------===//
862
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000863ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000864 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000865 SourceLocation atLoc,
866 SourceLocation L,
867 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +0000868 Kind PK,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000869 ObjCIvarDecl *ivar) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000870 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000871}
Chris Lattnered0e1642008-03-17 01:19:02 +0000872
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000873