blob: 41678418380e864540022625c5e7170398830c79 [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.
123 for (ObjCInterfaceDecl::protocol_iterator
124 I = OID->protocol_begin(), E = OID->protocol_end(); I != E; ++I)
125 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
126 return P;
127
128 // Finally, check the super class.
129 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
130 return superClass->FindPropertyDeclaration(PropertyId);
131 break;
132 }
133 case Decl::ObjCCategory: {
134 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
135 // Look through protocols.
136 if (!OCD->IsClassExtension())
137 for (ObjCCategoryDecl::protocol_iterator
138 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
139 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
140 return P;
141
142 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000143 }
144 }
Steve Narofff9c65242008-06-05 13:55:23 +0000145 return 0;
146}
147
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000148/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
149/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000150/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000151///
152ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000153ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000154 IdentifierInfo *PropertyId) const {
Ted Kremenekd133a862010-03-15 20:30:07 +0000155 if (ObjCPropertyDecl *PD =
156 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
157 return PD;
158
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000159 // Look through protocols.
Ted Kremenekd133a862010-03-15 20:30:07 +0000160 for (ObjCInterfaceDecl::protocol_iterator
161 I = protocol_begin(), E = protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000162 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
163 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000164
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000165 return 0;
166}
167
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000168void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
169 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Douglas Gregor002b6712010-01-16 15:02:53 +0000170 const SourceLocation *Locs,
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000171 ASTContext &C)
172{
173 if (ReferencedProtocols.empty()) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000174 ReferencedProtocols.set(ExtList, ExtNum, Locs, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000175 return;
176 }
177 // Check for duplicate protocol in class's protocol list.
178 // This is (O)2. But it is extremely rare and number of protocols in
179 // class or its extension are very few.
180 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Douglas Gregor002b6712010-01-16 15:02:53 +0000181 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000182 for (unsigned i = 0; i < ExtNum; i++) {
183 bool protocolExists = false;
184 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
185 for (protocol_iterator p = protocol_begin(), e = protocol_end();
186 p != e; p++) {
187 ObjCProtocolDecl *Proto = (*p);
188 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
189 protocolExists = true;
190 break;
191 }
192 }
193 // Do we want to warn on a protocol in extension class which
194 // already exist in the class? Probably not.
Douglas Gregor002b6712010-01-16 15:02:53 +0000195 if (!protocolExists) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000196 ProtocolRefs.push_back(ProtoInExtension);
Douglas Gregor002b6712010-01-16 15:02:53 +0000197 ProtocolLocs.push_back(Locs[i]);
198 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000199 }
200 if (ProtocolRefs.empty())
201 return;
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000202 // Merge ProtocolRefs into class's protocol list;
Douglas Gregor002b6712010-01-16 15:02:53 +0000203 protocol_loc_iterator pl = protocol_loc_begin();
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000204 for (protocol_iterator p = protocol_begin(), e = protocol_end();
Douglas Gregor002b6712010-01-16 15:02:53 +0000205 p != e; ++p, ++pl) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000206 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000207 ProtocolLocs.push_back(*pl);
208 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000209 unsigned NumProtoRefs = ProtocolRefs.size();
Douglas Gregor002b6712010-01-16 15:02:53 +0000210 setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000211}
212
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000213/// getFirstClassExtension - Find first class extension of the given class.
214ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
215 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000216 CDecl = CDecl->getNextClassCategory())
217 if (CDecl->IsClassExtension())
218 return CDecl;
219 return 0;
220}
221
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000222/// getNextClassCategory - Find next class extension in list of categories.
223const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
224 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
225 CDecl = CDecl->getNextClassCategory())
226 if (CDecl->IsClassExtension())
227 return CDecl;
228 return 0;
229}
230
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000231ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
232 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner89375192008-03-16 00:19:01 +0000233 ObjCInterfaceDecl* ClassDecl = this;
234 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000235 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000236 clsDeclared = ClassDecl;
237 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000238 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000239 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
240 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000241 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
242 clsDeclared = ClassDecl;
243 return I;
244 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000245 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000246
Chris Lattner89375192008-03-16 00:19:01 +0000247 ClassDecl = ClassDecl->getSuperClass();
248 }
249 return NULL;
250}
251
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000252/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
253/// class whose name is passed as argument. If it is not one of the super classes
254/// the it returns NULL.
255ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
256 const IdentifierInfo*ICName) {
257 ObjCInterfaceDecl* ClassDecl = this;
258 while (ClassDecl != NULL) {
259 if (ClassDecl->getIdentifier() == ICName)
260 return ClassDecl;
261 ClassDecl = ClassDecl->getSuperClass();
262 }
263 return NULL;
264}
265
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000266/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000267/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000268ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
269 bool isInstance) const {
270 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000271 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner89375192008-03-16 00:19:01 +0000273 while (ClassDecl != NULL) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000274 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000275 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner89375192008-03-16 00:19:01 +0000277 // Didn't find one yet - look through protocols.
Chris Lattnerd0045052008-07-21 18:19:38 +0000278 const ObjCList<ObjCProtocolDecl> &Protocols =
279 ClassDecl->getReferencedProtocols();
280 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
281 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000282 if ((MethodDecl = (*I)->lookupMethod(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 - now look through categories.
286 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
287 while (CatDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000288 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000289 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Steve Naroff5fd31b12008-12-08 20:57:28 +0000291 // Didn't find one yet - look through protocols.
292 const ObjCList<ObjCProtocolDecl> &Protocols =
293 CatDecl->getReferencedProtocols();
294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
295 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000296 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff8ec27842009-02-26 11:32:02 +0000297 return MethodDecl;
Chris Lattner89375192008-03-16 00:19:01 +0000298 CatDecl = CatDecl->getNextClassCategory();
299 }
300 ClassDecl = ClassDecl->getSuperClass();
301 }
302 return NULL;
303}
304
Steve Naroffbb69c942009-10-01 23:46:04 +0000305ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
306 const Selector &Sel) {
307 ObjCMethodDecl *Method = 0;
308 if (ObjCImplementationDecl *ImpDecl = getImplementation())
309 Method = ImpDecl->getInstanceMethod(Sel);
310
311 if (!Method && getSuperClass())
312 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
313 return Method;
314}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000315
316//===----------------------------------------------------------------------===//
317// ObjCMethodDecl
318//===----------------------------------------------------------------------===//
319
320ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000321 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000322 SourceLocation endLoc,
323 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000324 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000325 DeclContext *contextDecl,
326 bool isInstance,
327 bool isVariadic,
328 bool isSynthesized,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000329 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000330 ImplementationControl impControl,
331 unsigned numSelectorArgs) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000332 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000333 SelInfo, T, ResultTInfo, contextDecl,
334 isInstance,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000335 isVariadic, isSynthesized, isDefined,
336 impControl,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000337 numSelectorArgs);
Chris Lattner89375192008-03-16 00:19:01 +0000338}
339
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000340/// \brief A definition will return its interface declaration.
341/// An interface declaration will return its definition.
342/// Otherwise it will return itself.
343ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
344 ASTContext &Ctx = getASTContext();
345 ObjCMethodDecl *Redecl = 0;
346 Decl *CtxD = cast<Decl>(getDeclContext());
347
348 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
349 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
350 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
351
352 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
353 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
354 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
355
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000356 } else if (ObjCImplementationDecl *ImplD =
357 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000358 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
359 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000360
361 } else if (ObjCCategoryImplDecl *CImplD =
362 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000363 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000364 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000365 }
366
367 return Redecl ? Redecl : this;
368}
369
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000370ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
371 Decl *CtxD = cast<Decl>(getDeclContext());
372
373 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
374 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
375 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
376 isInstanceMethod()))
377 return MD;
378
379 } else if (ObjCCategoryImplDecl *CImplD =
380 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000381 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000382 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
383 isInstanceMethod()))
384 return MD;
385 }
386
387 return this;
388}
389
Mike Stump11289f42009-09-09 15:08:12 +0000390void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000391 const ObjCInterfaceDecl *OID) {
392 QualType selfTy;
393 if (isInstanceMethod()) {
394 // There may be no interface context due to error in declaration
395 // of the interface (which has been reported). Recover gracefully.
396 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000397 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000398 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000399 } else {
400 selfTy = Context.getObjCIdType();
401 }
402 } else // we have a factory method.
403 selfTy = Context.getObjCClassType();
404
Mike Stump11289f42009-09-09 15:08:12 +0000405 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff04f2d142009-04-20 15:06:07 +0000406 &Context.Idents.get("self"), selfTy));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000407
Mike Stump11289f42009-09-09 15:08:12 +0000408 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
409 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000410 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000411}
412
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000413ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
414 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
415 return ID;
416 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
417 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000418 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000419 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000420
421 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000422 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000423 return 0;
424}
425
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000426//===----------------------------------------------------------------------===//
427// ObjCInterfaceDecl
428//===----------------------------------------------------------------------===//
429
430ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
431 DeclContext *DC,
432 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000433 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000434 SourceLocation ClassLoc,
435 bool ForwardDecl, bool isInternal){
Douglas Gregor1c283312010-08-11 12:19:30 +0000436 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
437 isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000438}
439
440ObjCInterfaceDecl::
441ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor1c283312010-08-11 12:19:30 +0000442 SourceLocation CLoc, bool FD, bool isInternal)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000443 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
444 TypeForDecl(0), SuperClass(0),
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000445 CategoryList(0), IvarList(0),
446 ForwardDecl(FD), InternalInterface(isInternal),
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000447 ClassLoc(CLoc) {
448}
449
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000450ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
451 return getASTContext().getObjCImplementation(
452 const_cast<ObjCInterfaceDecl*>(this));
453}
454
455void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
456 getASTContext().setObjCImplementation(this, ImplD);
457}
458
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000459/// all_declared_ivar_begin - return first ivar declared in this class,
460/// its extensions and its implementation. Lazily build the list on first
461/// access.
462ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
463 if (IvarList)
464 return IvarList;
465
466 ObjCIvarDecl *curIvar = 0;
467 if (!ivar_empty()) {
468 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
469 IvarList = (*I); ++I;
470 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
471 curIvar->setNextIvar(*I);
472 }
473
474 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
475 CDecl = CDecl->getNextClassExtension()) {
476 if (!CDecl->ivar_empty()) {
477 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
478 E = CDecl->ivar_end();
479 if (!IvarList) {
480 IvarList = (*I); ++I;
481 curIvar = IvarList;
482 }
483 for ( ;I != E; curIvar = *I, ++I)
484 curIvar->setNextIvar(*I);
485 }
486 }
487
488 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
489 if (!ImplDecl->ivar_empty()) {
490 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
491 E = ImplDecl->ivar_end();
492 if (!IvarList) {
493 IvarList = (*I); ++I;
494 curIvar = IvarList;
495 }
496 for ( ;I != E; curIvar = *I, ++I)
497 curIvar->setNextIvar(*I);
498 }
499 }
500 return IvarList;
501}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000502
503/// FindCategoryDeclaration - Finds category declaration in the list of
504/// categories for this class and returns it. Name of the category is passed
505/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000506///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000507ObjCCategoryDecl *
508ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
509 for (ObjCCategoryDecl *Category = getCategoryList();
510 Category; Category = Category->getNextClassCategory())
511 if (Category->getIdentifier() == CategoryId)
512 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000513 return 0;
514}
515
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000516ObjCMethodDecl *
517ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
518 for (ObjCCategoryDecl *Category = getCategoryList();
519 Category; Category = Category->getNextClassCategory())
520 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
521 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
522 return MD;
523 return 0;
524}
525
526ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
527 for (ObjCCategoryDecl *Category = getCategoryList();
528 Category; Category = Category->getNextClassCategory())
529 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
530 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
531 return MD;
532 return 0;
533}
534
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000535/// ClassImplementsProtocol - Checks that 'lProto' protocol
536/// has been implemented in IDecl class, its super class or categories (if
537/// lookupCategory is true).
538bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
539 bool lookupCategory,
540 bool RHSIsQualifiedID) {
541 ObjCInterfaceDecl *IDecl = this;
542 // 1st, look up the class.
543 const ObjCList<ObjCProtocolDecl> &Protocols =
544 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000545
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000546 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
547 E = Protocols.end(); PI != E; ++PI) {
548 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
549 return true;
550 // This is dubious and is added to be compatible with gcc. In gcc, it is
551 // also allowed assigning a protocol-qualified 'id' type to a LHS object
552 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
553 // object. This IMO, should be a bug.
554 // FIXME: Treat this as an extension, and flag this as an error when GCC
555 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000556 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000557 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
558 return true;
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000561 // 2nd, look up the category.
562 if (lookupCategory)
563 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
564 CDecl = CDecl->getNextClassCategory()) {
565 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
566 E = CDecl->protocol_end(); PI != E; ++PI)
567 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
568 return true;
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000571 // 3rd, look up the super class(s)
572 if (IDecl->getSuperClass())
573 return
574 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
575 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000577 return false;
578}
579
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000580//===----------------------------------------------------------------------===//
581// ObjCIvarDecl
582//===----------------------------------------------------------------------===//
583
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000584ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000585 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000586 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +0000587 AccessControl ac, Expr *BW,
588 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000589 if (DC) {
590 // Ivar's can only appear in interfaces, implementations (via synthesized
591 // properties), and class extensions (via direct declaration, or synthesized
592 // properties).
593 //
594 // FIXME: This should really be asserting this:
595 // (isa<ObjCCategoryDecl>(DC) &&
596 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
597 // but unfortunately we sometimes place ivars into non-class extension
598 // categories on error. This breaks an AST invariant, and should not be
599 // fixed.
600 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
601 isa<ObjCCategoryDecl>(DC)) &&
602 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000603 // Once a new ivar is created in any of class/class-extension/implementation
604 // decl contexts, the previously built IvarList must be rebuilt.
605 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
606 if (!ID) {
607 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
608 ID = IM->getClassInterface();
609 else
610 ID = (cast<ObjCCategoryDecl>(DC))->getClassInterface();
611 }
612 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000613 }
614
Fariborz Jahanian18722982010-07-17 00:59:30 +0000615 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000616}
617
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000618const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
619 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000620
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000621 switch (DC->getKind()) {
622 default:
623 case ObjCCategoryImpl:
624 case ObjCProtocol:
625 assert(0 && "invalid ivar container!");
626 return 0;
627
628 // Ivars can only appear in class extension categories.
629 case ObjCCategory: {
630 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
631 assert(CD->IsClassExtension() && "invalid container for ivar!");
632 return CD->getClassInterface();
633 }
634
635 case ObjCImplementation:
636 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
637
638 case ObjCInterface:
639 return cast<ObjCInterfaceDecl>(DC);
640 }
641}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000642
643//===----------------------------------------------------------------------===//
644// ObjCAtDefsFieldDecl
645//===----------------------------------------------------------------------===//
646
647ObjCAtDefsFieldDecl
648*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
649 IdentifierInfo *Id, QualType T, Expr *BW) {
650 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
651}
652
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000653//===----------------------------------------------------------------------===//
654// ObjCProtocolDecl
655//===----------------------------------------------------------------------===//
656
657ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000658 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000659 IdentifierInfo *Id) {
660 return new (C) ObjCProtocolDecl(DC, L, Id);
661}
662
Steve Naroff114aecb2009-03-01 16:12:44 +0000663ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
664 ObjCProtocolDecl *PDecl = this;
665
666 if (Name == getIdentifier())
667 return PDecl;
668
669 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
670 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
671 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Steve Naroff114aecb2009-03-01 16:12:44 +0000673 return NULL;
674}
675
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000676// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000677// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000678ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
679 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000680 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000682 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000683 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000685 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000686 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000687 return MethodDecl;
688 return NULL;
689}
690
691//===----------------------------------------------------------------------===//
692// ObjCClassDecl
693//===----------------------------------------------------------------------===//
694
Mike Stump11289f42009-09-09 15:08:12 +0000695ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000696 ObjCInterfaceDecl *const *Elts,
697 const SourceLocation *Locs,
698 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000699 ASTContext &C)
700 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000701 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000702}
703
Ted Kremenek9b124e12009-11-18 00:28:11 +0000704void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
705 const SourceLocation *Locs, unsigned Num) {
706 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
707 llvm::alignof<ObjCClassRef>());
708 for (unsigned i = 0; i < Num; ++i)
709 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
710
711 NumDecls = Num;
712}
Chris Lattner22298722009-02-20 21:35:13 +0000713
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000714ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
715 SourceLocation L,
716 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000717 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000718 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000719 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000720}
721
Ted Kremenek49939c22009-11-18 01:26:56 +0000722SourceRange ObjCClassDecl::getSourceRange() const {
723 // FIXME: We should include the semicolon
724 assert(NumDecls);
725 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
726}
727
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000728//===----------------------------------------------------------------------===//
729// ObjCForwardProtocolDecl
730//===----------------------------------------------------------------------===//
731
Chris Lattner22298722009-02-20 21:35:13 +0000732ObjCForwardProtocolDecl::
733ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
734 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000735 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000736: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000737 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000738}
739
740
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000741ObjCForwardProtocolDecl *
742ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000743 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000744 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000745 unsigned NumElts,
746 const SourceLocation *Locs) {
747 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000748}
749
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000750//===----------------------------------------------------------------------===//
751// ObjCCategoryDecl
752//===----------------------------------------------------------------------===//
753
754ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000755 SourceLocation AtLoc,
756 SourceLocation ClassNameLoc,
757 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000758 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000759 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000760}
761
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000762ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
763 return getASTContext().getObjCImplementation(
764 const_cast<ObjCCategoryDecl*>(this));
765}
766
767void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
768 getASTContext().setObjCImplementation(this, ImplD);
769}
770
771
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000772//===----------------------------------------------------------------------===//
773// ObjCCategoryImplDecl
774//===----------------------------------------------------------------------===//
775
776ObjCCategoryImplDecl *
777ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
778 SourceLocation L,IdentifierInfo *Id,
779 ObjCInterfaceDecl *ClassInterface) {
780 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
781}
782
Steve Narofff406f4d2009-10-29 21:11:04 +0000783ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000784 // The class interface might be NULL if we are working with invalid code.
785 if (const ObjCInterfaceDecl *ID = getClassInterface())
786 return ID->FindCategoryDeclaration(getIdentifier());
787 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000788}
789
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000790
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000791void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000792 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000793 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000794 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000795}
796
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000797void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
798 ASTContext &Ctx = getASTContext();
799
800 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000801 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000802 if (IFace)
803 Ctx.setObjCImplementation(IFace, ImplD);
804
Duncan Sands49c29ee2009-07-21 07:56:29 +0000805 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000806 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
807 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
808 Ctx.setObjCImplementation(CD, ImplD);
809 }
810
811 ClassInterface = IFace;
812}
813
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000814/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000815/// properties implemented in this category @implementation block and returns
816/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000817///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000818ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000819FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
820 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000821 ObjCPropertyImplDecl *PID = *i;
822 if (PID->getPropertyIvarDecl() &&
823 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
824 return PID;
825 }
826 return 0;
827}
828
829/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
830/// added to the list of those properties @synthesized/@dynamic in this
831/// category @implementation block.
832///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000833ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000834FindPropertyImplDecl(IdentifierInfo *Id) const {
835 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000836 ObjCPropertyImplDecl *PID = *i;
837 if (PID->getPropertyDecl()->getIdentifier() == Id)
838 return PID;
839 }
840 return 0;
841}
842
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000843llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
844 const ObjCCategoryImplDecl *CID) {
845 OS << CID->getName();
846 return OS;
847}
848
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000849//===----------------------------------------------------------------------===//
850// ObjCImplementationDecl
851//===----------------------------------------------------------------------===//
852
853ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000854ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000855 SourceLocation L,
856 ObjCInterfaceDecl *ClassInterface,
857 ObjCInterfaceDecl *SuperDecl) {
858 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
859}
860
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000861llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
862 const ObjCImplementationDecl *ID) {
863 OS << ID->getName();
864 return OS;
865}
866
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000867//===----------------------------------------------------------------------===//
868// ObjCCompatibleAliasDecl
869//===----------------------------------------------------------------------===//
870
871ObjCCompatibleAliasDecl *
872ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
873 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000874 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000875 ObjCInterfaceDecl* AliasedClass) {
876 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
877}
878
879//===----------------------------------------------------------------------===//
880// ObjCPropertyDecl
881//===----------------------------------------------------------------------===//
882
883ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
884 SourceLocation L,
885 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000886 SourceLocation AtLoc,
John McCall339bb662010-06-04 20:50:08 +0000887 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000888 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000889 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000890}
891
892
893//===----------------------------------------------------------------------===//
894// ObjCPropertyImplDecl
895//===----------------------------------------------------------------------===//
896
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000897ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000898 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000899 SourceLocation atLoc,
900 SourceLocation L,
901 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +0000902 Kind PK,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000903 ObjCIvarDecl *ivar) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000904 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000905}
Chris Lattnered0e1642008-03-17 01:19:02 +0000906
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000907