blob: ce765a7bf590e4a5ddf1847c9df615c220a7b4af [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner38af2de2009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000028 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000030
31
Chris Lattner4ee413b2009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor18df52b2010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner11e1e1a2009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie99ba9e32011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
David Blaikie3bc93e32012-12-19 00:45:41 +000057 lookup_const_result R = lookup(Id);
58 for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
65
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000069 // If this context is a hidden protocol definition, don't find any
70 // methods there.
71 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
72 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
73 if (Def->isHidden())
74 return 0;
75 }
76
Steve Naroff0de21fd2009-02-22 19:35:57 +000077 // Since instance & class methods can have the same name, the loop below
78 // ensures we get the correct method.
79 //
80 // @interface Whatever
81 // - (int) class_method;
82 // + (float) class_method;
83 // @end
84 //
David Blaikie3bc93e32012-12-19 00:45:41 +000085 lookup_const_result R = lookup(Sel);
86 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
87 Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000088 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000089 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000090 return MD;
91 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000092 return 0;
93}
94
Ted Kremenek9f550ff2010-03-15 20:11:46 +000095ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000096ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000097 IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000098 // If this context is a hidden protocol definition, don't find any
99 // property.
100 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
101 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
102 if (Def->isHidden())
103 return 0;
104 }
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000105
David Blaikie3bc93e32012-12-19 00:45:41 +0000106 DeclContext::lookup_const_result R = DC->lookup(propertyID);
107 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
108 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000109 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
110 return PD;
111
112 return 0;
113}
114
Anna Zaksad0ce532012-09-27 19:45:11 +0000115IdentifierInfo *
116ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
117 SmallString<128> ivarName;
118 {
119 llvm::raw_svector_ostream os(ivarName);
120 os << '_' << getIdentifier()->getName();
121 }
122 return &Ctx.Idents.get(ivarName.str());
123}
124
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000125/// FindPropertyDeclaration - Finds declaration of the property given its name
126/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000127ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000128ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000129 // Don't find properties within hidden protocol definitions.
130 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
131 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
132 if (Def->isHidden())
133 return 0;
134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000136 if (ObjCPropertyDecl *PD =
137 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
138 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000140 switch (getKind()) {
141 default:
142 break;
143 case Decl::ObjCProtocol: {
144 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
145 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
146 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000147 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
148 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000149 break;
150 }
151 case Decl::ObjCInterface: {
152 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000153 // Look through categories (but not extensions).
154 for (ObjCInterfaceDecl::visible_categories_iterator
155 Cat = OID->visible_categories_begin(),
156 CatEnd = OID->visible_categories_end();
157 Cat != CatEnd; ++Cat) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000158 if (!Cat->IsClassExtension())
159 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
160 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000161 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000162
163 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000164 for (ObjCInterfaceDecl::all_protocol_iterator
165 I = OID->all_referenced_protocol_begin(),
166 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
168 return P;
169
170 // Finally, check the super class.
171 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
172 return superClass->FindPropertyDeclaration(PropertyId);
173 break;
174 }
175 case Decl::ObjCCategory: {
176 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
177 // Look through protocols.
178 if (!OCD->IsClassExtension())
179 for (ObjCCategoryDecl::protocol_iterator
180 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
181 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
182 return P;
183
184 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000185 }
186 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000187 return 0;
188}
189
David Blaikie99ba9e32011-12-20 02:48:34 +0000190void ObjCInterfaceDecl::anchor() { }
191
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000192/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
193/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000194/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000195///
196ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000197ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000198 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000199 // FIXME: Should make sure no callers ever do this.
200 if (!hasDefinition())
201 return 0;
202
203 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000204 LoadExternalDefinition();
205
Ted Kremenek37cafb02010-03-15 20:30:07 +0000206 if (ObjCPropertyDecl *PD =
207 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
208 return PD;
209
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000210 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000211 for (ObjCInterfaceDecl::all_protocol_iterator
212 I = all_referenced_protocol_begin(),
213 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000216
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000217 return 0;
218}
219
Anna Zakse63aedd2012-10-31 01:18:22 +0000220void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000221 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
222 E = prop_end(); P != E; ++P) {
223 ObjCPropertyDecl *Prop = *P;
224 PM[Prop->getIdentifier()] = Prop;
225 }
226 for (ObjCInterfaceDecl::all_protocol_iterator
227 PI = all_referenced_protocol_begin(),
228 E = all_referenced_protocol_end(); PI != E; ++PI)
229 (*PI)->collectPropertiesToImplement(PM);
Anna Zakse63aedd2012-10-31 01:18:22 +0000230 // Note, the properties declared only in class extensions are still copied
231 // into the main @interface's property list, and therefore we don't
232 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000233}
234
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000235bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
236 const ObjCInterfaceDecl *Class = this;
237 while (Class) {
238 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
239 return true;
240 Class = Class->getSuperClass();
241 }
242 return false;
243}
244
245const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
246 const ObjCInterfaceDecl *Class = this;
247 while (Class) {
248 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
249 return Class;
250 Class = Class->getSuperClass();
251 }
252 return 0;
253}
254
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000255void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
256 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
257 ASTContext &C)
258{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000259 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000260 LoadExternalDefinition();
261
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000262 if (data().AllReferencedProtocols.empty() &&
263 data().ReferencedProtocols.empty()) {
264 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000265 return;
266 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000267
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000268 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000269 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000270 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000272 for (unsigned i = 0; i < ExtNum; i++) {
273 bool protocolExists = false;
274 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000275 for (all_protocol_iterator
276 p = all_referenced_protocol_begin(),
277 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000278 ObjCProtocolDecl *Proto = (*p);
279 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
280 protocolExists = true;
281 break;
282 }
283 }
284 // Do we want to warn on a protocol in extension class which
285 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000286 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000287 ProtocolRefs.push_back(ProtoInExtension);
288 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000289
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000290 if (ProtocolRefs.empty())
291 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000292
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000293 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000294 for (all_protocol_iterator p = all_referenced_protocol_begin(),
295 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000296 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000297 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000298
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000299 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000300}
301
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000302void ObjCInterfaceDecl::allocateDefinitionData() {
303 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000304 Data.setPointer(new (getASTContext()) DefinitionData());
305 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000306
307 // Make the type point at the definition, now that we have one.
308 if (TypeForDecl)
309 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000310}
311
312void ObjCInterfaceDecl::startDefinition() {
313 allocateDefinitionData();
314
Douglas Gregor53df7a12011-12-15 18:03:09 +0000315 // Update all of the declarations with a pointer to the definition.
316 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
317 RD != RDEnd; ++RD) {
318 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000319 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000320 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000321}
322
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
324 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000325 // FIXME: Should make sure no callers ever do this.
326 if (!hasDefinition())
327 return 0;
328
329 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000330 LoadExternalDefinition();
331
Chris Lattner1e03a562008-03-16 00:19:01 +0000332 ObjCInterfaceDecl* ClassDecl = this;
333 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000334 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000335 clsDeclared = ClassDecl;
336 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000337 }
Douglas Gregord3297242013-01-16 23:00:23 +0000338
339 for (ObjCInterfaceDecl::visible_extensions_iterator
340 Ext = ClassDecl->visible_extensions_begin(),
341 ExtEnd = ClassDecl->visible_extensions_end();
342 Ext != ExtEnd; ++Ext) {
343 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000344 clsDeclared = ClassDecl;
345 return I;
346 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000347 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000348
Chris Lattner1e03a562008-03-16 00:19:01 +0000349 ClassDecl = ClassDecl->getSuperClass();
350 }
351 return NULL;
352}
353
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000354/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
355/// class whose name is passed as argument. If it is not one of the super classes
356/// the it returns NULL.
357ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
358 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000359 // FIXME: Should make sure no callers ever do this.
360 if (!hasDefinition())
361 return 0;
362
363 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000364 LoadExternalDefinition();
365
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000366 ObjCInterfaceDecl* ClassDecl = this;
367 while (ClassDecl != NULL) {
368 if (ClassDecl->getIdentifier() == ICName)
369 return ClassDecl;
370 ClassDecl = ClassDecl->getSuperClass();
371 }
372 return NULL;
373}
374
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000375/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000376/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000377ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
378 bool isInstance,
379 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000380 // FIXME: Should make sure no callers ever do this.
381 if (!hasDefinition())
382 return 0;
383
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000384 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000385 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000387 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000388 LoadExternalDefinition();
389
Chris Lattner1e03a562008-03-16 00:19:01 +0000390 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000391 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000392 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner1e03a562008-03-16 00:19:01 +0000394 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000395 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
396 E = ClassDecl->protocol_end();
397 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000398 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000399 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000400
401 // Didn't find one yet - now look through categories.
Douglas Gregord3297242013-01-16 23:00:23 +0000402 for (ObjCInterfaceDecl::visible_categories_iterator
403 Cat = ClassDecl->visible_categories_begin(),
404 CatEnd = ClassDecl->visible_categories_end();
405 Cat != CatEnd; ++Cat) {
406 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000407 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000408
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000409 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000410 // Didn't find one yet - look through protocols.
411 const ObjCList<ObjCProtocolDecl> &Protocols =
Douglas Gregord3297242013-01-16 23:00:23 +0000412 Cat->getReferencedProtocols();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000413 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
414 E = Protocols.end(); I != E; ++I)
415 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
416 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000417 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000418 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000419
Chris Lattner1e03a562008-03-16 00:19:01 +0000420 ClassDecl = ClassDecl->getSuperClass();
421 }
422 return NULL;
423}
424
Anna Zakse61354b2012-07-27 19:07:44 +0000425// Will search "local" class/category implementations for a method decl.
426// If failed, then we search in class's root for an instance method.
427// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000428ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
429 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000430 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000431 // FIXME: Should make sure no callers ever do this.
432 if (!hasDefinition())
433 return 0;
434
435 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000436 LoadExternalDefinition();
437
Steve Naroffd789d3d2009-10-01 23:46:04 +0000438 ObjCMethodDecl *Method = 0;
439 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000440 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
441 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000442
443 // Look through local category implementations associated with the class.
444 if (!Method)
445 Method = Instance ? getCategoryInstanceMethod(Sel)
446 : getCategoryClassMethod(Sel);
447
448 // Before we give up, check if the selector is an instance method.
449 // But only in the root. This matches gcc's behavior and what the
450 // runtime expects.
451 if (!Instance && !Method && !getSuperClass()) {
452 Method = lookupInstanceMethod(Sel);
453 // Look through local category implementations associated
454 // with the root class.
455 if (!Method)
456 Method = lookupPrivateMethod(Sel, true);
457 }
458
Steve Naroffd789d3d2009-10-01 23:46:04 +0000459 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000460 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000461 return Method;
462}
Chris Lattnerab351632009-02-20 20:59:54 +0000463
464//===----------------------------------------------------------------------===//
465// ObjCMethodDecl
466//===----------------------------------------------------------------------===//
467
468ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000469 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000470 SourceLocation endLoc,
471 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000472 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000473 DeclContext *contextDecl,
474 bool isInstance,
475 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000476 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000477 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000478 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000479 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000480 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000481 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000482 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000483 isInstance, isVariadic, isPropertyAccessor,
484 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000485 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000486 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000487}
488
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000489ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
490 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
491 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
492 Selector(), QualType(), 0, 0);
493}
494
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000495Stmt *ObjCMethodDecl::getBody() const {
496 return Body.get(getASTContext().getExternalSource());
497}
498
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000499void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
500 assert(PrevMethod);
501 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
502 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000503 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000504}
505
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000506void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
507 ArrayRef<ParmVarDecl*> Params,
508 ArrayRef<SourceLocation> SelLocs) {
509 ParamsAndSelLocs = 0;
510 NumParams = Params.size();
511 if (Params.empty() && SelLocs.empty())
512 return;
513
514 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
515 sizeof(SourceLocation) * SelLocs.size();
516 ParamsAndSelLocs = C.Allocate(Size);
517 std::copy(Params.begin(), Params.end(), getParams());
518 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
519}
520
521void ObjCMethodDecl::getSelectorLocs(
522 SmallVectorImpl<SourceLocation> &SelLocs) const {
523 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
524 SelLocs.push_back(getSelectorLoc(i));
525}
526
527void ObjCMethodDecl::setMethodParams(ASTContext &C,
528 ArrayRef<ParmVarDecl*> Params,
529 ArrayRef<SourceLocation> SelLocs) {
530 assert((!SelLocs.empty() || isImplicit()) &&
531 "No selector locs for non-implicit method");
532 if (isImplicit())
533 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
534
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000535 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
536 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000537 if (SelLocsKind != SelLoc_NonStandard)
538 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
539
540 setParamsAndSelLocs(C, Params, SelLocs);
541}
542
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000543/// \brief A definition will return its interface declaration.
544/// An interface declaration will return its definition.
545/// Otherwise it will return itself.
546ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
547 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000548 ObjCMethodDecl *Redecl = 0;
549 if (HasRedeclaration)
550 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000551 if (Redecl)
552 return Redecl;
553
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000554 Decl *CtxD = cast<Decl>(getDeclContext());
555
556 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
557 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
558 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
559
560 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
561 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
562 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
563
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000564 } else if (ObjCImplementationDecl *ImplD =
565 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000566 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
567 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000568
569 } else if (ObjCCategoryImplDecl *CImplD =
570 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000571 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000572 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000573 }
574
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000575 if (!Redecl && isRedeclaration()) {
576 // This is the last redeclaration, go back to the first method.
577 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
578 isInstanceMethod());
579 }
580
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000581 return Redecl ? Redecl : this;
582}
583
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000584ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
585 Decl *CtxD = cast<Decl>(getDeclContext());
586
587 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
588 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
589 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
590 isInstanceMethod()))
591 return MD;
592
593 } else if (ObjCCategoryImplDecl *CImplD =
594 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000595 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000596 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
597 isInstanceMethod()))
598 return MD;
599 }
600
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000601 if (isRedeclaration())
602 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
603 isInstanceMethod());
604
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000605 return this;
606}
607
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000608SourceLocation ObjCMethodDecl::getLocEnd() const {
609 if (Stmt *Body = getBody())
610 return Body->getLocEnd();
611 return DeclEndLoc;
612}
613
John McCall85f3d762011-03-02 01:50:55 +0000614ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
615 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000616 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000617 return family;
618
John McCalld5313b02011-03-02 11:33:24 +0000619 // Check for an explicit attribute.
620 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
621 // The unfortunate necessity of mapping between enums here is due
622 // to the attributes framework.
623 switch (attr->getFamily()) {
624 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
625 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
626 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
627 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
628 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
629 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
630 }
631 Family = static_cast<unsigned>(family);
632 return family;
633 }
634
John McCall85f3d762011-03-02 01:50:55 +0000635 family = getSelector().getMethodFamily();
636 switch (family) {
637 case OMF_None: break;
638
639 // init only has a conventional meaning for an instance method, and
640 // it has to return an object.
641 case OMF_init:
642 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
643 family = OMF_None;
644 break;
645
646 // alloc/copy/new have a conventional meaning for both class and
647 // instance methods, but they require an object return.
648 case OMF_alloc:
649 case OMF_copy:
650 case OMF_mutableCopy:
651 case OMF_new:
652 if (!getResultType()->isObjCObjectPointerType())
653 family = OMF_None;
654 break;
655
656 // These selectors have a conventional meaning only for instance methods.
657 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000658 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000659 case OMF_retain:
660 case OMF_release:
661 case OMF_autorelease:
662 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000663 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000664 if (!isInstanceMethod())
665 family = OMF_None;
666 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000667
668 case OMF_performSelector:
669 if (!isInstanceMethod() ||
670 !getResultType()->isObjCIdType())
671 family = OMF_None;
672 else {
673 unsigned noParams = param_size();
674 if (noParams < 1 || noParams > 3)
675 family = OMF_None;
676 else {
677 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
678 QualType ArgT = (*it);
679 if (!ArgT->isObjCSelType()) {
680 family = OMF_None;
681 break;
682 }
683 while (--noParams) {
684 it++;
685 ArgT = (*it);
686 if (!ArgT->isObjCIdType()) {
687 family = OMF_None;
688 break;
689 }
690 }
691 }
692 }
693 break;
694
John McCall85f3d762011-03-02 01:50:55 +0000695 }
696
697 // Cache the result.
698 Family = static_cast<unsigned>(family);
699 return family;
700}
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000703 const ObjCInterfaceDecl *OID) {
704 QualType selfTy;
705 if (isInstanceMethod()) {
706 // There may be no interface context due to error in declaration
707 // of the interface (which has been reported). Recover gracefully.
708 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000709 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000710 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000711 } else {
712 selfTy = Context.getObjCIdType();
713 }
714 } else // we have a factory method.
715 selfTy = Context.getObjCClassType();
716
John McCall7acddac2011-06-17 06:42:21 +0000717 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000718 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000719
David Blaikie4e4d0842012-03-11 07:00:24 +0000720 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000721 if (isInstanceMethod()) {
722 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000723
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000724 // 'self' is always __strong. It's actually pseudo-strong except
725 // in init methods (or methods labeled ns_consumes_self), though.
726 Qualifiers qs;
727 qs.setObjCLifetime(Qualifiers::OCL_Strong);
728 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000729
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000730 // In addition, 'self' is const unless this is an init method.
731 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
732 selfTy = selfTy.withConst();
733 selfIsPseudoStrong = true;
734 }
735 }
736 else {
737 assert(isClassMethod());
738 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000739 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000740 selfIsPseudoStrong = true;
741 }
John McCallf85e1932011-06-15 23:02:42 +0000742 }
743
744 ImplicitParamDecl *self
745 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
746 &Context.Idents.get("self"), selfTy);
747 setSelfDecl(self);
748
749 if (selfIsConsumed)
750 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000751
John McCall7acddac2011-06-17 06:42:21 +0000752 if (selfIsPseudoStrong)
753 self->setARCPseudoStrong(true);
754
Mike Stump1eb44332009-09-09 15:08:12 +0000755 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
756 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000757 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000758}
759
Chris Lattnerab351632009-02-20 20:59:54 +0000760ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
761 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
762 return ID;
763 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
764 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000765 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000766 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000767
768 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000769 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000770}
771
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000772static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
773 const ObjCMethodDecl *Method,
774 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
775 bool MovedToSuper) {
776 if (!Container)
777 return;
778
779 // In categories look for overriden methods from protocols. A method from
780 // category is not "overriden" since it is considered as the "same" method
781 // (same USR) as the one from the interface.
782 if (const ObjCCategoryDecl *
783 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
784 // Check whether we have a matching method at this category but only if we
785 // are at the super class level.
786 if (MovedToSuper)
787 if (ObjCMethodDecl *
788 Overridden = Container->getMethod(Method->getSelector(),
789 Method->isInstanceMethod()))
790 if (Method != Overridden) {
791 // We found an override at this category; there is no need to look
792 // into its protocols.
793 Methods.push_back(Overridden);
794 return;
795 }
796
797 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
798 PEnd = Category->protocol_end();
799 P != PEnd; ++P)
800 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
801 return;
802 }
803
804 // Check whether we have a matching method at this level.
805 if (const ObjCMethodDecl *
806 Overridden = Container->getMethod(Method->getSelector(),
807 Method->isInstanceMethod()))
808 if (Method != Overridden) {
809 // We found an override at this level; there is no need to look
810 // into other protocols or categories.
811 Methods.push_back(Overridden);
812 return;
813 }
814
815 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
816 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
817 PEnd = Protocol->protocol_end();
818 P != PEnd; ++P)
819 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
820 }
821
822 if (const ObjCInterfaceDecl *
823 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
824 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
825 PEnd = Interface->protocol_end();
826 P != PEnd; ++P)
827 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
828
Douglas Gregord3297242013-01-16 23:00:23 +0000829 for (ObjCInterfaceDecl::visible_categories_iterator
830 Cat = Interface->visible_categories_begin(),
831 CatEnd = Interface->visible_categories_end();
832 Cat != CatEnd; ++Cat) {
833 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000834 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000835 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000836
837 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
838 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
839 /*MovedToSuper=*/true);
840 }
841}
842
843static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
844 const ObjCMethodDecl *Method,
845 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
846 CollectOverriddenMethodsRecurse(Container, Method, Methods,
847 /*MovedToSuper=*/false);
848}
849
850static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
851 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
852 assert(Method->isOverriding());
853
854 if (const ObjCProtocolDecl *
855 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
856 CollectOverriddenMethods(ProtD, Method, overridden);
857
858 } else if (const ObjCImplDecl *
859 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
860 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
861 if (!ID)
862 return;
863 // Start searching for overridden methods using the method from the
864 // interface as starting point.
865 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
866 Method->isInstanceMethod()))
867 Method = IFaceMeth;
868 CollectOverriddenMethods(ID, Method, overridden);
869
870 } else if (const ObjCCategoryDecl *
871 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
872 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
873 if (!ID)
874 return;
875 // Start searching for overridden methods using the method from the
876 // interface as starting point.
877 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
878 Method->isInstanceMethod()))
879 Method = IFaceMeth;
880 CollectOverriddenMethods(ID, Method, overridden);
881
882 } else {
883 CollectOverriddenMethods(
884 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
885 Method, overridden);
886 }
887}
888
889static void collectOnCategoriesAfterLocation(SourceLocation Loc,
890 const ObjCInterfaceDecl *Class,
891 SourceManager &SM,
892 const ObjCMethodDecl *Method,
893 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
894 if (!Class)
895 return;
896
Douglas Gregord3297242013-01-16 23:00:23 +0000897 for (ObjCInterfaceDecl::visible_categories_iterator
898 Cat = Class->visible_categories_begin(),
899 CatEnd = Class->visible_categories_end();
900 Cat != CatEnd; ++Cat) {
901 if (SM.isBeforeInTranslationUnit(Loc, Cat->getLocation()))
902 CollectOverriddenMethodsRecurse(*Cat, Method, Methods, true);
903 }
904
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000905 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
906 Method, Methods);
907}
908
909/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
910/// returns false.
911/// You'd think that in that case there are no overrides but categories can
912/// "introduce" new overridden methods that are missed by Sema because the
913/// overrides lookup that it does for methods, inside implementations, will
914/// stop at the interface level (if there is a method there) and not look
915/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000916/// Methods in an implementation can overide methods in super class's category
917/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000918static void collectOverriddenMethodsFast(SourceManager &SM,
919 const ObjCMethodDecl *Method,
920 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
921 assert(!Method->isOverriding());
922
923 const ObjCContainerDecl *
924 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
925 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
926 return;
927 const ObjCInterfaceDecl *Class = Method->getClassInterface();
928 if (!Class)
929 return;
930
931 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
932 SM, Method, Methods);
933}
934
935void ObjCMethodDecl::getOverriddenMethods(
936 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
937 const ObjCMethodDecl *Method = this;
938
939 if (Method->isRedeclaration()) {
940 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
941 getMethod(Method->getSelector(), Method->isInstanceMethod());
942 }
943
944 if (!Method->isOverriding()) {
945 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
946 Method, Overridden);
947 } else {
948 collectOverriddenMethodsSlow(Method, Overridden);
949 assert(!Overridden.empty() &&
950 "ObjCMethodDecl's overriding bit is not as expected");
951 }
952}
953
Jordan Rose04bec392012-10-10 16:42:54 +0000954const ObjCPropertyDecl *
955ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
956 Selector Sel = getSelector();
957 unsigned NumArgs = Sel.getNumArgs();
958 if (NumArgs > 1)
959 return 0;
960
Jordan Rose50d2b262012-10-11 16:02:02 +0000961 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000962 return 0;
963
964 if (isPropertyAccessor()) {
965 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +0000966 // If container is class extension, find its primary class.
967 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
968 if (CatDecl->IsClassExtension())
969 Container = CatDecl->getClassInterface();
970
Jordan Rose04bec392012-10-10 16:42:54 +0000971 bool IsGetter = (NumArgs == 0);
972
973 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
974 E = Container->prop_end();
975 I != E; ++I) {
976 Selector NextSel = IsGetter ? (*I)->getGetterName()
977 : (*I)->getSetterName();
978 if (NextSel == Sel)
979 return *I;
980 }
981
982 llvm_unreachable("Marked as a property accessor but no property found!");
983 }
984
985 if (!CheckOverrides)
986 return 0;
987
988 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
989 OverridesTy Overrides;
990 getOverriddenMethods(Overrides);
991 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
992 I != E; ++I) {
993 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
994 return Prop;
995 }
996
997 return 0;
998
999}
1000
Chris Lattnerab351632009-02-20 20:59:54 +00001001//===----------------------------------------------------------------------===//
1002// ObjCInterfaceDecl
1003//===----------------------------------------------------------------------===//
1004
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001005ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001006 DeclContext *DC,
1007 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001008 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001009 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001010 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001011 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +00001012 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001013 PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001014 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001015 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001016 return Result;
1017}
1018
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001019ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1020 unsigned ID) {
1021 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001022 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1023 0, SourceLocation(),
1024 0, false);
1025 Result->Data.setInt(!C.getLangOpts().Modules);
1026 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001027}
1028
1029ObjCInterfaceDecl::
1030ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001031 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1032 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001033 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001034 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001035{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001036 setPreviousDeclaration(PrevDecl);
1037
1038 // Copy the 'data' pointer over.
1039 if (PrevDecl)
1040 Data = PrevDecl->Data;
1041
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001042 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001043}
1044
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001045void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001046 assert(data().ExternallyCompleted && "Class is not externally completed");
1047 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001048 getASTContext().getExternalSource()->CompleteType(
1049 const_cast<ObjCInterfaceDecl *>(this));
1050}
1051
1052void ObjCInterfaceDecl::setExternallyCompleted() {
1053 assert(getASTContext().getExternalSource() &&
1054 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001055 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001056 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001057 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001058}
1059
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001060ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001061 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1062 if (data().ExternallyCompleted)
1063 LoadExternalDefinition();
1064
1065 return getASTContext().getObjCImplementation(
1066 const_cast<ObjCInterfaceDecl*>(Def));
1067 }
1068
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001069 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001070 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001071}
1072
1073void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001074 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001075}
1076
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001077/// all_declared_ivar_begin - return first ivar declared in this class,
1078/// its extensions and its implementation. Lazily build the list on first
1079/// access.
1080ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001081 // FIXME: Should make sure no callers ever do this.
1082 if (!hasDefinition())
1083 return 0;
1084
1085 if (data().IvarList)
1086 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001087
1088 ObjCIvarDecl *curIvar = 0;
1089 if (!ivar_empty()) {
1090 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001091 data().IvarList = *I; ++I;
1092 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1093 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001094 }
Douglas Gregord3297242013-01-16 23:00:23 +00001095
1096 for (ObjCInterfaceDecl::known_extensions_iterator
1097 Ext = known_extensions_begin(),
1098 ExtEnd = known_extensions_end();
1099 Ext != ExtEnd; ++Ext) {
1100 if (!Ext->ivar_empty()) {
1101 ObjCCategoryDecl::ivar_iterator I = Ext->ivar_begin(),E = Ext->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001102 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001103 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001104 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001105 }
David Blaikie581deb32012-06-06 20:45:41 +00001106 for ( ;I != E; curIvar = *I, ++I)
1107 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001108 }
1109 }
1110
1111 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1112 if (!ImplDecl->ivar_empty()) {
1113 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1114 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001115 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001116 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001117 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001118 }
David Blaikie581deb32012-06-06 20:45:41 +00001119 for ( ;I != E; curIvar = *I, ++I)
1120 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001121 }
1122 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001123 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001124}
Chris Lattnerab351632009-02-20 20:59:54 +00001125
1126/// FindCategoryDeclaration - Finds category declaration in the list of
1127/// categories for this class and returns it. Name of the category is passed
1128/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001129///
Chris Lattnerab351632009-02-20 20:59:54 +00001130ObjCCategoryDecl *
1131ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001132 // FIXME: Should make sure no callers ever do this.
1133 if (!hasDefinition())
1134 return 0;
1135
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001136 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001137 LoadExternalDefinition();
1138
Douglas Gregord3297242013-01-16 23:00:23 +00001139 for (visible_categories_iterator Cat = visible_categories_begin(),
1140 CatEnd = visible_categories_end();
1141 Cat != CatEnd;
1142 ++Cat) {
1143 if (Cat->getIdentifier() == CategoryId)
1144 return *Cat;
1145 }
1146
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001147 return 0;
1148}
1149
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001150ObjCMethodDecl *
1151ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001152 for (visible_categories_iterator Cat = visible_categories_begin(),
1153 CatEnd = visible_categories_end();
1154 Cat != CatEnd;
1155 ++Cat) {
1156 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001157 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1158 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001159 }
1160
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001161 return 0;
1162}
1163
1164ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001165 for (visible_categories_iterator Cat = visible_categories_begin(),
1166 CatEnd = visible_categories_end();
1167 Cat != CatEnd;
1168 ++Cat) {
1169 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001170 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1171 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001172 }
1173
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001174 return 0;
1175}
1176
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001177/// ClassImplementsProtocol - Checks that 'lProto' protocol
1178/// has been implemented in IDecl class, its super class or categories (if
1179/// lookupCategory is true).
1180bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1181 bool lookupCategory,
1182 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001183 if (!hasDefinition())
1184 return false;
1185
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001186 ObjCInterfaceDecl *IDecl = this;
1187 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001188 for (ObjCInterfaceDecl::protocol_iterator
1189 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001190 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1191 return true;
1192 // This is dubious and is added to be compatible with gcc. In gcc, it is
1193 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1194 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1195 // object. This IMO, should be a bug.
1196 // FIXME: Treat this as an extension, and flag this as an error when GCC
1197 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001198 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001199 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1200 return true;
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001203 // 2nd, look up the category.
1204 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001205 for (visible_categories_iterator Cat = visible_categories_begin(),
1206 CatEnd = visible_categories_end();
1207 Cat != CatEnd;
1208 ++Cat) {
1209 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1210 E = Cat->protocol_end();
1211 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001212 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1213 return true;
1214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001216 // 3rd, look up the super class(s)
1217 if (IDecl->getSuperClass())
1218 return
1219 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1220 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001222 return false;
1223}
1224
Chris Lattnerab351632009-02-20 20:59:54 +00001225//===----------------------------------------------------------------------===//
1226// ObjCIvarDecl
1227//===----------------------------------------------------------------------===//
1228
David Blaikie99ba9e32011-12-20 02:48:34 +00001229void ObjCIvarDecl::anchor() { }
1230
Daniel Dunbara0654922010-04-02 20:10:03 +00001231ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001232 SourceLocation StartLoc,
1233 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001234 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001235 AccessControl ac, Expr *BW,
1236 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001237 if (DC) {
1238 // Ivar's can only appear in interfaces, implementations (via synthesized
1239 // properties), and class extensions (via direct declaration, or synthesized
1240 // properties).
1241 //
1242 // FIXME: This should really be asserting this:
1243 // (isa<ObjCCategoryDecl>(DC) &&
1244 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1245 // but unfortunately we sometimes place ivars into non-class extension
1246 // categories on error. This breaks an AST invariant, and should not be
1247 // fixed.
1248 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1249 isa<ObjCCategoryDecl>(DC)) &&
1250 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001251 // Once a new ivar is created in any of class/class-extension/implementation
1252 // decl contexts, the previously built IvarList must be rebuilt.
1253 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1254 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001255 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001256 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001257 else
1258 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001259 }
1260 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001261 }
1262
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001263 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1264 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001265}
1266
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001267ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1268 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1269 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1270 QualType(), 0, ObjCIvarDecl::None, 0, false);
1271}
1272
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001273const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1274 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001275
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001276 switch (DC->getKind()) {
1277 default:
1278 case ObjCCategoryImpl:
1279 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001280 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001281
1282 // Ivars can only appear in class extension categories.
1283 case ObjCCategory: {
1284 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1285 assert(CD->IsClassExtension() && "invalid container for ivar!");
1286 return CD->getClassInterface();
1287 }
1288
1289 case ObjCImplementation:
1290 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1291
1292 case ObjCInterface:
1293 return cast<ObjCInterfaceDecl>(DC);
1294 }
1295}
Chris Lattnerab351632009-02-20 20:59:54 +00001296
1297//===----------------------------------------------------------------------===//
1298// ObjCAtDefsFieldDecl
1299//===----------------------------------------------------------------------===//
1300
David Blaikie99ba9e32011-12-20 02:48:34 +00001301void ObjCAtDefsFieldDecl::anchor() { }
1302
Chris Lattnerab351632009-02-20 20:59:54 +00001303ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001304*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1305 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001306 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001307 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001308}
1309
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001310ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1311 unsigned ID) {
1312 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1313 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1314 0, QualType(), 0);
1315}
1316
Chris Lattnerab351632009-02-20 20:59:54 +00001317//===----------------------------------------------------------------------===//
1318// ObjCProtocolDecl
1319//===----------------------------------------------------------------------===//
1320
David Blaikie99ba9e32011-12-20 02:48:34 +00001321void ObjCProtocolDecl::anchor() { }
1322
Douglas Gregor27c6da22012-01-01 20:30:41 +00001323ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1324 SourceLocation nameLoc,
1325 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001326 ObjCProtocolDecl *PrevDecl)
1327 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001328{
1329 setPreviousDeclaration(PrevDecl);
1330 if (PrevDecl)
1331 Data = PrevDecl->Data;
1332}
1333
Chris Lattnerab351632009-02-20 20:59:54 +00001334ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001335 IdentifierInfo *Id,
1336 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001337 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001338 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001339 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001340 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001341 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001342 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001343}
1344
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001345ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1346 unsigned ID) {
1347 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001348 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1349 SourceLocation(), 0);
1350 Result->Data.setInt(!C.getLangOpts().Modules);
1351 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001352}
1353
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001354ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1355 ObjCProtocolDecl *PDecl = this;
1356
1357 if (Name == getIdentifier())
1358 return PDecl;
1359
1360 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1361 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1362 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001364 return NULL;
1365}
1366
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001367// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001368// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001369ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1370 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001371 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001373 // If there is no definition or the definition is hidden, we don't find
1374 // anything.
1375 const ObjCProtocolDecl *Def = getDefinition();
1376 if (!Def || Def->isHidden())
1377 return NULL;
1378
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001379 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001380 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Chris Lattnerab351632009-02-20 20:59:54 +00001382 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001383 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001384 return MethodDecl;
1385 return NULL;
1386}
1387
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001388void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001389 assert(!Data.getPointer() && "Protocol already has a definition!");
1390 Data.setPointer(new (getASTContext()) DefinitionData);
1391 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001392}
1393
1394void ObjCProtocolDecl::startDefinition() {
1395 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001396
1397 // Update all of the declarations with a pointer to the definition.
1398 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1399 RD != RDEnd; ++RD)
1400 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001401}
1402
Anna Zakse63aedd2012-10-31 01:18:22 +00001403void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001404
1405 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1406 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1407 E = PDecl->prop_end(); P != E; ++P) {
1408 ObjCPropertyDecl *Prop = *P;
1409 // Insert into PM if not there already.
1410 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1411 }
1412 // Scan through protocol's protocols.
1413 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1414 E = PDecl->protocol_end(); PI != E; ++PI)
1415 (*PI)->collectPropertiesToImplement(PM);
Anna Zaksb36ea372012-10-18 19:17:53 +00001416 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001417}
1418
1419
Chris Lattnerab351632009-02-20 20:59:54 +00001420//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001421// ObjCCategoryDecl
1422//===----------------------------------------------------------------------===//
1423
David Blaikie99ba9e32011-12-20 02:48:34 +00001424void ObjCCategoryDecl::anchor() { }
1425
Chris Lattnerab351632009-02-20 20:59:54 +00001426ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001427 SourceLocation AtLoc,
1428 SourceLocation ClassNameLoc,
1429 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001430 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001431 ObjCInterfaceDecl *IDecl,
1432 SourceLocation IvarLBraceLoc,
1433 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001434 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1435 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001436 IDecl,
1437 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001438 if (IDecl) {
1439 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001440 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001441 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001442 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001443 if (ASTMutationListener *L = C.getASTMutationListener())
1444 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1445 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001446 }
1447
1448 return CatDecl;
1449}
1450
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001451ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1452 unsigned ID) {
1453 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1454 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1455 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001456}
1457
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001458ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1459 return getASTContext().getObjCImplementation(
1460 const_cast<ObjCCategoryDecl*>(this));
1461}
1462
1463void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1464 getASTContext().setObjCImplementation(this, ImplD);
1465}
1466
1467
Chris Lattnerab351632009-02-20 20:59:54 +00001468//===----------------------------------------------------------------------===//
1469// ObjCCategoryImplDecl
1470//===----------------------------------------------------------------------===//
1471
David Blaikie99ba9e32011-12-20 02:48:34 +00001472void ObjCCategoryImplDecl::anchor() { }
1473
Chris Lattnerab351632009-02-20 20:59:54 +00001474ObjCCategoryImplDecl *
1475ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001476 IdentifierInfo *Id,
1477 ObjCInterfaceDecl *ClassInterface,
1478 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001479 SourceLocation atStartLoc,
1480 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001481 if (ClassInterface && ClassInterface->hasDefinition())
1482 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001483 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001484 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001485}
1486
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001487ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1488 unsigned ID) {
1489 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1490 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1491 SourceLocation(), SourceLocation());
1492}
1493
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001494ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001495 // The class interface might be NULL if we are working with invalid code.
1496 if (const ObjCInterfaceDecl *ID = getClassInterface())
1497 return ID->FindCategoryDeclaration(getIdentifier());
1498 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001499}
1500
Chris Lattnerab351632009-02-20 20:59:54 +00001501
David Blaikie99ba9e32011-12-20 02:48:34 +00001502void ObjCImplDecl::anchor() { }
1503
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001504void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001505 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001506 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001507 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001508}
1509
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001510void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1511 ASTContext &Ctx = getASTContext();
1512
1513 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001514 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001515 if (IFace)
1516 Ctx.setObjCImplementation(IFace, ImplD);
1517
Duncan Sands98f2cca2009-07-21 07:56:29 +00001518 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001519 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1520 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1521 Ctx.setObjCImplementation(CD, ImplD);
1522 }
1523
1524 ClassInterface = IFace;
1525}
1526
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001527/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001528/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001529/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001530///
Chris Lattner3aa18612009-02-28 18:42:10 +00001531ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001532FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1533 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001534 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001535 if (PID->getPropertyIvarDecl() &&
1536 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1537 return PID;
1538 }
1539 return 0;
1540}
1541
1542/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001543/// added to the list of those properties \@synthesized/\@dynamic in this
1544/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001545///
Chris Lattner3aa18612009-02-28 18:42:10 +00001546ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001547FindPropertyImplDecl(IdentifierInfo *Id) const {
1548 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001549 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001550 if (PID->getPropertyDecl()->getIdentifier() == Id)
1551 return PID;
1552 }
1553 return 0;
1554}
1555
Chris Lattner5f9e2722011-07-23 10:55:15 +00001556raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001557 const ObjCCategoryImplDecl &CID) {
1558 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001559 return OS;
1560}
1561
Chris Lattnerab351632009-02-20 20:59:54 +00001562//===----------------------------------------------------------------------===//
1563// ObjCImplementationDecl
1564//===----------------------------------------------------------------------===//
1565
David Blaikie99ba9e32011-12-20 02:48:34 +00001566void ObjCImplementationDecl::anchor() { }
1567
Chris Lattnerab351632009-02-20 20:59:54 +00001568ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001569ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001570 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001571 ObjCInterfaceDecl *SuperDecl,
1572 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001573 SourceLocation atStartLoc,
1574 SourceLocation IvarLBraceLoc,
1575 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001576 if (ClassInterface && ClassInterface->hasDefinition())
1577 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001578 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001579 nameLoc, atStartLoc,
1580 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001581}
1582
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001583ObjCImplementationDecl *
1584ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1585 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1586 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1587 SourceLocation());
1588}
1589
John McCallda6d9762011-07-22 04:15:06 +00001590void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1591 CXXCtorInitializer ** initializers,
1592 unsigned numInitializers) {
1593 if (numInitializers > 0) {
1594 NumIvarInitializers = numInitializers;
1595 CXXCtorInitializer **ivarInitializers =
1596 new (C) CXXCtorInitializer*[NumIvarInitializers];
1597 memcpy(ivarInitializers, initializers,
1598 numInitializers * sizeof(CXXCtorInitializer*));
1599 IvarInitializers = ivarInitializers;
1600 }
1601}
1602
Chris Lattner5f9e2722011-07-23 10:55:15 +00001603raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001604 const ObjCImplementationDecl &ID) {
1605 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001606 return OS;
1607}
1608
Chris Lattnerab351632009-02-20 20:59:54 +00001609//===----------------------------------------------------------------------===//
1610// ObjCCompatibleAliasDecl
1611//===----------------------------------------------------------------------===//
1612
David Blaikie99ba9e32011-12-20 02:48:34 +00001613void ObjCCompatibleAliasDecl::anchor() { }
1614
Chris Lattnerab351632009-02-20 20:59:54 +00001615ObjCCompatibleAliasDecl *
1616ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1617 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001618 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001619 ObjCInterfaceDecl* AliasedClass) {
1620 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1621}
1622
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001623ObjCCompatibleAliasDecl *
1624ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1625 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1626 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1627}
1628
Chris Lattnerab351632009-02-20 20:59:54 +00001629//===----------------------------------------------------------------------===//
1630// ObjCPropertyDecl
1631//===----------------------------------------------------------------------===//
1632
David Blaikie99ba9e32011-12-20 02:48:34 +00001633void ObjCPropertyDecl::anchor() { }
1634
Chris Lattnerab351632009-02-20 20:59:54 +00001635ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1636 SourceLocation L,
1637 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001638 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001639 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001640 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001641 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001642 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001643}
1644
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001645ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1646 unsigned ID) {
1647 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1648 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001649 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001650 0);
1651}
1652
Chris Lattnerab351632009-02-20 20:59:54 +00001653//===----------------------------------------------------------------------===//
1654// ObjCPropertyImplDecl
1655//===----------------------------------------------------------------------===//
1656
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001657ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001658 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001659 SourceLocation atLoc,
1660 SourceLocation L,
1661 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001662 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001663 ObjCIvarDecl *ivar,
1664 SourceLocation ivarLoc) {
1665 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1666 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001667}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001668
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001669ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1670 unsigned ID) {
1671 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1672 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1673 0, Dynamic, 0, SourceLocation());
1674}
1675
Douglas Gregora4ffd852010-11-17 01:03:52 +00001676SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1677 SourceLocation EndLoc = getLocation();
1678 if (IvarLoc.isValid())
1679 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001680
Douglas Gregora4ffd852010-11-17 01:03:52 +00001681 return SourceRange(AtLoc, EndLoc);
1682}