blob: 50bf121af38e3376134216d29a889477d047ec34 [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
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +000095/// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
96/// method was found in the class, its protocols, its super classes or categories.
97/// It also returns 'true' if one of its categories has declared a 'readwrite' property.
98/// This is because, user must provide a setter method for the category's 'readwrite'
99/// property.
100bool
101ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
102 Selector Sel = Property->getSetterName();
103 lookup_const_result R = lookup(Sel);
104 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
105 Meth != MethEnd; ++Meth) {
106 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
107 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
108 return true;
109 }
110
111 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
112 // Also look into categories, including class extensions, looking
113 // for a user declared instance method.
114 for (ObjCInterfaceDecl::visible_categories_iterator
115 Cat = ID->visible_categories_begin(),
116 CatEnd = ID->visible_categories_end();
117 Cat != CatEnd;
118 ++Cat) {
119 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
120 if (!MD->isImplicit())
121 return true;
122 if (Cat->IsClassExtension())
123 continue;
124 // Also search through the categories looking for a 'readwrite' declaration
125 // of this property. If one found, presumably a setter will be provided
126 // (properties declared in categories will not get auto-synthesized).
127 for (ObjCContainerDecl::prop_iterator P = Cat->prop_begin(),
128 E = Cat->prop_end(); P != E; ++P)
129 if (P->getIdentifier() == Property->getIdentifier()) {
130 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
131 return true;
132 break;
133 }
134 }
135
136 // Also look into protocols, for a user declared instance method.
137 for (ObjCInterfaceDecl::all_protocol_iterator P =
138 ID->all_referenced_protocol_begin(),
139 PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
140 ObjCProtocolDecl *Proto = (*P);
141 if (Proto->HasUserDeclaredSetterMethod(Property))
142 return true;
143 }
144 // And in its super class.
145 ObjCInterfaceDecl *OSC = ID->getSuperClass();
146 while (OSC) {
147 if (OSC->HasUserDeclaredSetterMethod(Property))
148 return true;
149 OSC = OSC->getSuperClass();
150 }
151 }
152 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
153 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
154 E = PD->protocol_end(); PI != E; ++PI) {
155 if ((*PI)->HasUserDeclaredSetterMethod(Property))
156 return true;
157 }
158 return false;
159}
160
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000161ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000162ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000163 IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000164 // If this context is a hidden protocol definition, don't find any
165 // property.
166 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
167 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
168 if (Def->isHidden())
169 return 0;
170 }
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000171
David Blaikie3bc93e32012-12-19 00:45:41 +0000172 DeclContext::lookup_const_result R = DC->lookup(propertyID);
173 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
174 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000175 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
176 return PD;
177
178 return 0;
179}
180
Anna Zaksad0ce532012-09-27 19:45:11 +0000181IdentifierInfo *
182ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
183 SmallString<128> ivarName;
184 {
185 llvm::raw_svector_ostream os(ivarName);
186 os << '_' << getIdentifier()->getName();
187 }
188 return &Ctx.Idents.get(ivarName.str());
189}
190
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000191/// FindPropertyDeclaration - Finds declaration of the property given its name
192/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000193ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000194ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000195 // Don't find properties within hidden protocol definitions.
196 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
197 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
198 if (Def->isHidden())
199 return 0;
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000202 if (ObjCPropertyDecl *PD =
203 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
204 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000206 switch (getKind()) {
207 default:
208 break;
209 case Decl::ObjCProtocol: {
210 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
211 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
212 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000213 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
214 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000215 break;
216 }
217 case Decl::ObjCInterface: {
218 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000219 // Look through categories (but not extensions).
220 for (ObjCInterfaceDecl::visible_categories_iterator
221 Cat = OID->visible_categories_begin(),
222 CatEnd = OID->visible_categories_end();
223 Cat != CatEnd; ++Cat) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000224 if (!Cat->IsClassExtension())
225 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
226 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000227 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000228
229 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000230 for (ObjCInterfaceDecl::all_protocol_iterator
231 I = OID->all_referenced_protocol_begin(),
232 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000233 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
234 return P;
235
236 // Finally, check the super class.
237 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
238 return superClass->FindPropertyDeclaration(PropertyId);
239 break;
240 }
241 case Decl::ObjCCategory: {
242 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
243 // Look through protocols.
244 if (!OCD->IsClassExtension())
245 for (ObjCCategoryDecl::protocol_iterator
246 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
247 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
248 return P;
249
250 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000251 }
252 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000253 return 0;
254}
255
David Blaikie99ba9e32011-12-20 02:48:34 +0000256void ObjCInterfaceDecl::anchor() { }
257
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000258/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
259/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000260/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000261///
262ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000263ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000264 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000265 // FIXME: Should make sure no callers ever do this.
266 if (!hasDefinition())
267 return 0;
268
269 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000270 LoadExternalDefinition();
271
Ted Kremenek37cafb02010-03-15 20:30:07 +0000272 if (ObjCPropertyDecl *PD =
273 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
274 return PD;
275
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000276 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000277 for (ObjCInterfaceDecl::all_protocol_iterator
278 I = all_referenced_protocol_begin(),
279 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000280 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
281 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000282
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000283 return 0;
284}
285
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000286void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
287 PropertyDeclOrder &PO) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000288 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
289 E = prop_end(); P != E; ++P) {
290 ObjCPropertyDecl *Prop = *P;
291 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000292 PO.push_back(Prop);
Anna Zaksb36ea372012-10-18 19:17:53 +0000293 }
294 for (ObjCInterfaceDecl::all_protocol_iterator
295 PI = all_referenced_protocol_begin(),
296 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000297 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zakse63aedd2012-10-31 01:18:22 +0000298 // Note, the properties declared only in class extensions are still copied
299 // into the main @interface's property list, and therefore we don't
300 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000301}
302
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000303bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
304 const ObjCInterfaceDecl *Class = this;
305 while (Class) {
306 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
307 return true;
308 Class = Class->getSuperClass();
309 }
310 return false;
311}
312
313const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
314 const ObjCInterfaceDecl *Class = this;
315 while (Class) {
316 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
317 return Class;
318 Class = Class->getSuperClass();
319 }
320 return 0;
321}
322
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000323void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
324 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
325 ASTContext &C)
326{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000327 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000328 LoadExternalDefinition();
329
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000330 if (data().AllReferencedProtocols.empty() &&
331 data().ReferencedProtocols.empty()) {
332 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000333 return;
334 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000335
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000336 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000337 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000338 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000339 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000340 for (unsigned i = 0; i < ExtNum; i++) {
341 bool protocolExists = false;
342 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000343 for (all_protocol_iterator
344 p = all_referenced_protocol_begin(),
345 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000346 ObjCProtocolDecl *Proto = (*p);
347 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
348 protocolExists = true;
349 break;
350 }
351 }
352 // Do we want to warn on a protocol in extension class which
353 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000354 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000355 ProtocolRefs.push_back(ProtoInExtension);
356 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000357
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000358 if (ProtocolRefs.empty())
359 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000360
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000361 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000362 for (all_protocol_iterator p = all_referenced_protocol_begin(),
363 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000364 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000365 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000366
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000367 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000368}
369
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000370void ObjCInterfaceDecl::allocateDefinitionData() {
371 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000372 Data.setPointer(new (getASTContext()) DefinitionData());
373 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000374
375 // Make the type point at the definition, now that we have one.
376 if (TypeForDecl)
377 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000378}
379
380void ObjCInterfaceDecl::startDefinition() {
381 allocateDefinitionData();
382
Douglas Gregor53df7a12011-12-15 18:03:09 +0000383 // Update all of the declarations with a pointer to the definition.
384 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
385 RD != RDEnd; ++RD) {
386 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000387 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000388 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000389}
390
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000391ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
392 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000393 // FIXME: Should make sure no callers ever do this.
394 if (!hasDefinition())
395 return 0;
396
397 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000398 LoadExternalDefinition();
399
Chris Lattner1e03a562008-03-16 00:19:01 +0000400 ObjCInterfaceDecl* ClassDecl = this;
401 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000402 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000403 clsDeclared = ClassDecl;
404 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000405 }
Douglas Gregord3297242013-01-16 23:00:23 +0000406
407 for (ObjCInterfaceDecl::visible_extensions_iterator
408 Ext = ClassDecl->visible_extensions_begin(),
409 ExtEnd = ClassDecl->visible_extensions_end();
410 Ext != ExtEnd; ++Ext) {
411 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000412 clsDeclared = ClassDecl;
413 return I;
414 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000415 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000416
Chris Lattner1e03a562008-03-16 00:19:01 +0000417 ClassDecl = ClassDecl->getSuperClass();
418 }
419 return NULL;
420}
421
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000422/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
423/// class whose name is passed as argument. If it is not one of the super classes
424/// the it returns NULL.
425ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
426 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000427 // FIXME: Should make sure no callers ever do this.
428 if (!hasDefinition())
429 return 0;
430
431 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000432 LoadExternalDefinition();
433
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000434 ObjCInterfaceDecl* ClassDecl = this;
435 while (ClassDecl != NULL) {
436 if (ClassDecl->getIdentifier() == ICName)
437 return ClassDecl;
438 ClassDecl = ClassDecl->getSuperClass();
439 }
440 return NULL;
441}
442
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000443/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000444/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000445ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
446 bool isInstance,
447 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000448 // FIXME: Should make sure no callers ever do this.
449 if (!hasDefinition())
450 return 0;
451
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000452 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000453 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000455 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000456 LoadExternalDefinition();
457
Chris Lattner1e03a562008-03-16 00:19:01 +0000458 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000459 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000460 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner1e03a562008-03-16 00:19:01 +0000462 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000463 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
464 E = ClassDecl->protocol_end();
465 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000466 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000467 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000468
469 // Didn't find one yet - now look through categories.
Douglas Gregord3297242013-01-16 23:00:23 +0000470 for (ObjCInterfaceDecl::visible_categories_iterator
471 Cat = ClassDecl->visible_categories_begin(),
472 CatEnd = ClassDecl->visible_categories_end();
473 Cat != CatEnd; ++Cat) {
474 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000475 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000476
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000477 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000478 // Didn't find one yet - look through protocols.
479 const ObjCList<ObjCProtocolDecl> &Protocols =
Douglas Gregord3297242013-01-16 23:00:23 +0000480 Cat->getReferencedProtocols();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000481 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
482 E = Protocols.end(); I != E; ++I)
483 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
484 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000485 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000486 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000487
Chris Lattner1e03a562008-03-16 00:19:01 +0000488 ClassDecl = ClassDecl->getSuperClass();
489 }
490 return NULL;
491}
492
Anna Zakse61354b2012-07-27 19:07:44 +0000493// Will search "local" class/category implementations for a method decl.
494// If failed, then we search in class's root for an instance method.
495// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000496ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
497 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000498 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000499 // FIXME: Should make sure no callers ever do this.
500 if (!hasDefinition())
501 return 0;
502
503 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000504 LoadExternalDefinition();
505
Steve Naroffd789d3d2009-10-01 23:46:04 +0000506 ObjCMethodDecl *Method = 0;
507 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000508 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
509 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000510
511 // Look through local category implementations associated with the class.
512 if (!Method)
513 Method = Instance ? getCategoryInstanceMethod(Sel)
514 : getCategoryClassMethod(Sel);
515
516 // Before we give up, check if the selector is an instance method.
517 // But only in the root. This matches gcc's behavior and what the
518 // runtime expects.
519 if (!Instance && !Method && !getSuperClass()) {
520 Method = lookupInstanceMethod(Sel);
521 // Look through local category implementations associated
522 // with the root class.
523 if (!Method)
524 Method = lookupPrivateMethod(Sel, true);
525 }
526
Steve Naroffd789d3d2009-10-01 23:46:04 +0000527 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000528 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000529 return Method;
530}
Chris Lattnerab351632009-02-20 20:59:54 +0000531
532//===----------------------------------------------------------------------===//
533// ObjCMethodDecl
534//===----------------------------------------------------------------------===//
535
536ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000537 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000538 SourceLocation endLoc,
539 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000540 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000541 DeclContext *contextDecl,
542 bool isInstance,
543 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000544 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000545 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000546 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000547 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000548 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000549 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000550 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000551 isInstance, isVariadic, isPropertyAccessor,
552 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000553 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000554 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000555}
556
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000557ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
558 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
559 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
560 Selector(), QualType(), 0, 0);
561}
562
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000563Stmt *ObjCMethodDecl::getBody() const {
564 return Body.get(getASTContext().getExternalSource());
565}
566
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000567void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
568 assert(PrevMethod);
569 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
570 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000571 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000572}
573
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000574void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
575 ArrayRef<ParmVarDecl*> Params,
576 ArrayRef<SourceLocation> SelLocs) {
577 ParamsAndSelLocs = 0;
578 NumParams = Params.size();
579 if (Params.empty() && SelLocs.empty())
580 return;
581
582 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
583 sizeof(SourceLocation) * SelLocs.size();
584 ParamsAndSelLocs = C.Allocate(Size);
585 std::copy(Params.begin(), Params.end(), getParams());
586 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
587}
588
589void ObjCMethodDecl::getSelectorLocs(
590 SmallVectorImpl<SourceLocation> &SelLocs) const {
591 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
592 SelLocs.push_back(getSelectorLoc(i));
593}
594
595void ObjCMethodDecl::setMethodParams(ASTContext &C,
596 ArrayRef<ParmVarDecl*> Params,
597 ArrayRef<SourceLocation> SelLocs) {
598 assert((!SelLocs.empty() || isImplicit()) &&
599 "No selector locs for non-implicit method");
600 if (isImplicit())
601 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
602
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000603 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
604 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000605 if (SelLocsKind != SelLoc_NonStandard)
606 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
607
608 setParamsAndSelLocs(C, Params, SelLocs);
609}
610
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000611/// \brief A definition will return its interface declaration.
612/// An interface declaration will return its definition.
613/// Otherwise it will return itself.
614ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
615 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000616 ObjCMethodDecl *Redecl = 0;
617 if (HasRedeclaration)
618 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000619 if (Redecl)
620 return Redecl;
621
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000622 Decl *CtxD = cast<Decl>(getDeclContext());
623
624 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
625 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
626 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
627
628 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
629 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
630 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
631
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000632 } else if (ObjCImplementationDecl *ImplD =
633 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000634 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
635 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000636
637 } else if (ObjCCategoryImplDecl *CImplD =
638 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000639 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000640 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000641 }
642
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000643 if (!Redecl && isRedeclaration()) {
644 // This is the last redeclaration, go back to the first method.
645 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
646 isInstanceMethod());
647 }
648
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000649 return Redecl ? Redecl : this;
650}
651
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000652ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
653 Decl *CtxD = cast<Decl>(getDeclContext());
654
655 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
656 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
657 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
658 isInstanceMethod()))
659 return MD;
660
661 } else if (ObjCCategoryImplDecl *CImplD =
662 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000663 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000664 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
665 isInstanceMethod()))
666 return MD;
667 }
668
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000669 if (isRedeclaration())
670 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
671 isInstanceMethod());
672
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000673 return this;
674}
675
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000676SourceLocation ObjCMethodDecl::getLocEnd() const {
677 if (Stmt *Body = getBody())
678 return Body->getLocEnd();
679 return DeclEndLoc;
680}
681
John McCall85f3d762011-03-02 01:50:55 +0000682ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
683 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000684 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000685 return family;
686
John McCalld5313b02011-03-02 11:33:24 +0000687 // Check for an explicit attribute.
688 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
689 // The unfortunate necessity of mapping between enums here is due
690 // to the attributes framework.
691 switch (attr->getFamily()) {
692 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
693 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
694 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
695 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
696 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
697 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
698 }
699 Family = static_cast<unsigned>(family);
700 return family;
701 }
702
John McCall85f3d762011-03-02 01:50:55 +0000703 family = getSelector().getMethodFamily();
704 switch (family) {
705 case OMF_None: break;
706
707 // init only has a conventional meaning for an instance method, and
708 // it has to return an object.
709 case OMF_init:
710 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
711 family = OMF_None;
712 break;
713
714 // alloc/copy/new have a conventional meaning for both class and
715 // instance methods, but they require an object return.
716 case OMF_alloc:
717 case OMF_copy:
718 case OMF_mutableCopy:
719 case OMF_new:
720 if (!getResultType()->isObjCObjectPointerType())
721 family = OMF_None;
722 break;
723
724 // These selectors have a conventional meaning only for instance methods.
725 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000726 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000727 case OMF_retain:
728 case OMF_release:
729 case OMF_autorelease:
730 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000731 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000732 if (!isInstanceMethod())
733 family = OMF_None;
734 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000735
736 case OMF_performSelector:
737 if (!isInstanceMethod() ||
738 !getResultType()->isObjCIdType())
739 family = OMF_None;
740 else {
741 unsigned noParams = param_size();
742 if (noParams < 1 || noParams > 3)
743 family = OMF_None;
744 else {
745 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
746 QualType ArgT = (*it);
747 if (!ArgT->isObjCSelType()) {
748 family = OMF_None;
749 break;
750 }
751 while (--noParams) {
752 it++;
753 ArgT = (*it);
754 if (!ArgT->isObjCIdType()) {
755 family = OMF_None;
756 break;
757 }
758 }
759 }
760 }
761 break;
762
John McCall85f3d762011-03-02 01:50:55 +0000763 }
764
765 // Cache the result.
766 Family = static_cast<unsigned>(family);
767 return family;
768}
769
Mike Stump1eb44332009-09-09 15:08:12 +0000770void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000771 const ObjCInterfaceDecl *OID) {
772 QualType selfTy;
773 if (isInstanceMethod()) {
774 // There may be no interface context due to error in declaration
775 // of the interface (which has been reported). Recover gracefully.
776 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000777 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000778 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000779 } else {
780 selfTy = Context.getObjCIdType();
781 }
782 } else // we have a factory method.
783 selfTy = Context.getObjCClassType();
784
John McCall7acddac2011-06-17 06:42:21 +0000785 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000786 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000787
David Blaikie4e4d0842012-03-11 07:00:24 +0000788 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000789 if (isInstanceMethod()) {
790 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000791
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000792 // 'self' is always __strong. It's actually pseudo-strong except
793 // in init methods (or methods labeled ns_consumes_self), though.
794 Qualifiers qs;
795 qs.setObjCLifetime(Qualifiers::OCL_Strong);
796 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000797
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000798 // In addition, 'self' is const unless this is an init method.
799 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
800 selfTy = selfTy.withConst();
801 selfIsPseudoStrong = true;
802 }
803 }
804 else {
805 assert(isClassMethod());
806 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000807 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000808 selfIsPseudoStrong = true;
809 }
John McCallf85e1932011-06-15 23:02:42 +0000810 }
811
812 ImplicitParamDecl *self
813 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
814 &Context.Idents.get("self"), selfTy);
815 setSelfDecl(self);
816
817 if (selfIsConsumed)
818 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000819
John McCall7acddac2011-06-17 06:42:21 +0000820 if (selfIsPseudoStrong)
821 self->setARCPseudoStrong(true);
822
Mike Stump1eb44332009-09-09 15:08:12 +0000823 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
824 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000825 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000826}
827
Chris Lattnerab351632009-02-20 20:59:54 +0000828ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
829 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
830 return ID;
831 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
832 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000833 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000834 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000835
836 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000837 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000838}
839
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000840static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
841 const ObjCMethodDecl *Method,
842 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
843 bool MovedToSuper) {
844 if (!Container)
845 return;
846
847 // In categories look for overriden methods from protocols. A method from
848 // category is not "overriden" since it is considered as the "same" method
849 // (same USR) as the one from the interface.
850 if (const ObjCCategoryDecl *
851 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
852 // Check whether we have a matching method at this category but only if we
853 // are at the super class level.
854 if (MovedToSuper)
855 if (ObjCMethodDecl *
856 Overridden = Container->getMethod(Method->getSelector(),
857 Method->isInstanceMethod()))
858 if (Method != Overridden) {
859 // We found an override at this category; there is no need to look
860 // into its protocols.
861 Methods.push_back(Overridden);
862 return;
863 }
864
865 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
866 PEnd = Category->protocol_end();
867 P != PEnd; ++P)
868 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
869 return;
870 }
871
872 // Check whether we have a matching method at this level.
873 if (const ObjCMethodDecl *
874 Overridden = Container->getMethod(Method->getSelector(),
875 Method->isInstanceMethod()))
876 if (Method != Overridden) {
877 // We found an override at this level; there is no need to look
878 // into other protocols or categories.
879 Methods.push_back(Overridden);
880 return;
881 }
882
883 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
884 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
885 PEnd = Protocol->protocol_end();
886 P != PEnd; ++P)
887 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
888 }
889
890 if (const ObjCInterfaceDecl *
891 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
892 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
893 PEnd = Interface->protocol_end();
894 P != PEnd; ++P)
895 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
896
Douglas Gregord3297242013-01-16 23:00:23 +0000897 for (ObjCInterfaceDecl::visible_categories_iterator
898 Cat = Interface->visible_categories_begin(),
899 CatEnd = Interface->visible_categories_end();
900 Cat != CatEnd; ++Cat) {
901 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000902 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000903 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000904
905 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
906 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
907 /*MovedToSuper=*/true);
908 }
909}
910
911static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
912 const ObjCMethodDecl *Method,
913 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
914 CollectOverriddenMethodsRecurse(Container, Method, Methods,
915 /*MovedToSuper=*/false);
916}
917
918static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
919 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
920 assert(Method->isOverriding());
921
922 if (const ObjCProtocolDecl *
923 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
924 CollectOverriddenMethods(ProtD, Method, overridden);
925
926 } else if (const ObjCImplDecl *
927 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
928 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
929 if (!ID)
930 return;
931 // Start searching for overridden methods using the method from the
932 // interface as starting point.
933 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
934 Method->isInstanceMethod()))
935 Method = IFaceMeth;
936 CollectOverriddenMethods(ID, Method, overridden);
937
938 } else if (const ObjCCategoryDecl *
939 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
940 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
941 if (!ID)
942 return;
943 // Start searching for overridden methods using the method from the
944 // interface as starting point.
945 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
946 Method->isInstanceMethod()))
947 Method = IFaceMeth;
948 CollectOverriddenMethods(ID, Method, overridden);
949
950 } else {
951 CollectOverriddenMethods(
952 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
953 Method, overridden);
954 }
955}
956
957static void collectOnCategoriesAfterLocation(SourceLocation Loc,
958 const ObjCInterfaceDecl *Class,
959 SourceManager &SM,
960 const ObjCMethodDecl *Method,
961 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
962 if (!Class)
963 return;
964
Douglas Gregord3297242013-01-16 23:00:23 +0000965 for (ObjCInterfaceDecl::visible_categories_iterator
966 Cat = Class->visible_categories_begin(),
967 CatEnd = Class->visible_categories_end();
968 Cat != CatEnd; ++Cat) {
969 if (SM.isBeforeInTranslationUnit(Loc, Cat->getLocation()))
970 CollectOverriddenMethodsRecurse(*Cat, Method, Methods, true);
971 }
972
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000973 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
974 Method, Methods);
975}
976
977/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
978/// returns false.
979/// You'd think that in that case there are no overrides but categories can
980/// "introduce" new overridden methods that are missed by Sema because the
981/// overrides lookup that it does for methods, inside implementations, will
982/// stop at the interface level (if there is a method there) and not look
983/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000984/// Methods in an implementation can overide methods in super class's category
985/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000986static void collectOverriddenMethodsFast(SourceManager &SM,
987 const ObjCMethodDecl *Method,
988 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
989 assert(!Method->isOverriding());
990
991 const ObjCContainerDecl *
992 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
993 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
994 return;
995 const ObjCInterfaceDecl *Class = Method->getClassInterface();
996 if (!Class)
997 return;
998
999 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
1000 SM, Method, Methods);
1001}
1002
1003void ObjCMethodDecl::getOverriddenMethods(
1004 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1005 const ObjCMethodDecl *Method = this;
1006
1007 if (Method->isRedeclaration()) {
1008 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1009 getMethod(Method->getSelector(), Method->isInstanceMethod());
1010 }
1011
1012 if (!Method->isOverriding()) {
1013 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
1014 Method, Overridden);
1015 } else {
1016 collectOverriddenMethodsSlow(Method, Overridden);
1017 assert(!Overridden.empty() &&
1018 "ObjCMethodDecl's overriding bit is not as expected");
1019 }
1020}
1021
Jordan Rose04bec392012-10-10 16:42:54 +00001022const ObjCPropertyDecl *
1023ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1024 Selector Sel = getSelector();
1025 unsigned NumArgs = Sel.getNumArgs();
1026 if (NumArgs > 1)
1027 return 0;
1028
Jordan Rose50d2b262012-10-11 16:02:02 +00001029 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +00001030 return 0;
1031
1032 if (isPropertyAccessor()) {
1033 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +00001034 // If container is class extension, find its primary class.
1035 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1036 if (CatDecl->IsClassExtension())
1037 Container = CatDecl->getClassInterface();
1038
Jordan Rose04bec392012-10-10 16:42:54 +00001039 bool IsGetter = (NumArgs == 0);
1040
1041 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1042 E = Container->prop_end();
1043 I != E; ++I) {
1044 Selector NextSel = IsGetter ? (*I)->getGetterName()
1045 : (*I)->getSetterName();
1046 if (NextSel == Sel)
1047 return *I;
1048 }
1049
1050 llvm_unreachable("Marked as a property accessor but no property found!");
1051 }
1052
1053 if (!CheckOverrides)
1054 return 0;
1055
1056 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1057 OverridesTy Overrides;
1058 getOverriddenMethods(Overrides);
1059 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1060 I != E; ++I) {
1061 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1062 return Prop;
1063 }
1064
1065 return 0;
1066
1067}
1068
Chris Lattnerab351632009-02-20 20:59:54 +00001069//===----------------------------------------------------------------------===//
1070// ObjCInterfaceDecl
1071//===----------------------------------------------------------------------===//
1072
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001073ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001074 DeclContext *DC,
1075 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001076 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001077 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001078 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001079 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +00001080 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001081 PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001082 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001083 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001084 return Result;
1085}
1086
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001087ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1088 unsigned ID) {
1089 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001090 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1091 0, SourceLocation(),
1092 0, false);
1093 Result->Data.setInt(!C.getLangOpts().Modules);
1094 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001095}
1096
1097ObjCInterfaceDecl::
1098ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001099 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1100 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001101 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001102 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001103{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001104 setPreviousDeclaration(PrevDecl);
1105
1106 // Copy the 'data' pointer over.
1107 if (PrevDecl)
1108 Data = PrevDecl->Data;
1109
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001110 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001111}
1112
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001113void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001114 assert(data().ExternallyCompleted && "Class is not externally completed");
1115 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001116 getASTContext().getExternalSource()->CompleteType(
1117 const_cast<ObjCInterfaceDecl *>(this));
1118}
1119
1120void ObjCInterfaceDecl::setExternallyCompleted() {
1121 assert(getASTContext().getExternalSource() &&
1122 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001123 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001124 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001125 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001126}
1127
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001128ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001129 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1130 if (data().ExternallyCompleted)
1131 LoadExternalDefinition();
1132
1133 return getASTContext().getObjCImplementation(
1134 const_cast<ObjCInterfaceDecl*>(Def));
1135 }
1136
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001137 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001138 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001139}
1140
1141void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001142 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001143}
1144
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001145namespace {
1146 struct SynthesizeIvarChunk {
1147 uint64_t Size;
1148 ObjCIvarDecl *Ivar;
1149 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1150 : Size(size), Ivar(ivar) {}
1151 };
1152
1153 bool operator<(const SynthesizeIvarChunk & LHS,
1154 const SynthesizeIvarChunk &RHS) {
1155 return LHS.Size < RHS.Size;
1156 }
1157}
1158
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001159/// all_declared_ivar_begin - return first ivar declared in this class,
1160/// its extensions and its implementation. Lazily build the list on first
1161/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001162///
1163/// Caveat: The list returned by this method reflects the current
1164/// state of the parser. The cache will be updated for every ivar
1165/// added by an extension or the implementation when they are
1166/// encountered.
1167/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001168ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001169 // FIXME: Should make sure no callers ever do this.
1170 if (!hasDefinition())
1171 return 0;
1172
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001173 ObjCIvarDecl *curIvar = 0;
Adrian Prantl4919de62013-03-06 22:03:30 +00001174 if (!data().IvarList) {
1175 if (!ivar_empty()) {
1176 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1177 data().IvarList = *I; ++I;
1178 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001179 curIvar->setNextIvar(*I);
1180 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001181
1182 for (ObjCInterfaceDecl::known_extensions_iterator
1183 Ext = known_extensions_begin(),
1184 ExtEnd = known_extensions_end();
1185 Ext != ExtEnd; ++Ext) {
1186 if (!Ext->ivar_empty()) {
1187 ObjCCategoryDecl::ivar_iterator
1188 I = Ext->ivar_begin(),
1189 E = Ext->ivar_end();
1190 if (!data().IvarList) {
1191 data().IvarList = *I; ++I;
1192 curIvar = data().IvarList;
1193 }
1194 for ( ;I != E; curIvar = *I, ++I)
1195 curIvar->setNextIvar(*I);
1196 }
1197 }
1198 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001199 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001200
1201 // cached and complete!
1202 if (!data().IvarListMissingImplementation)
1203 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001204
1205 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001206 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001207 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001208 SmallVector<SynthesizeIvarChunk, 16> layout;
1209 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1210 E = ImplDecl->ivar_end(); I != E; ++I) {
1211 ObjCIvarDecl *IV = *I;
1212 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1213 layout.push_back(SynthesizeIvarChunk(
1214 IV->getASTContext().getTypeSize(IV->getType()), IV));
1215 continue;
1216 }
1217 if (!data().IvarList)
1218 data().IvarList = *I;
1219 else
1220 curIvar->setNextIvar(*I);
1221 curIvar = *I;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001222 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001223
1224 if (!layout.empty()) {
1225 // Order synthesized ivars by their size.
1226 std::stable_sort(layout.begin(), layout.end());
1227 unsigned Ix = 0, EIx = layout.size();
1228 if (!data().IvarList) {
1229 data().IvarList = layout[0].Ivar; Ix++;
1230 curIvar = data().IvarList;
1231 }
1232 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1233 curIvar->setNextIvar(layout[Ix].Ivar);
1234 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001235 }
1236 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001237 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001238}
Chris Lattnerab351632009-02-20 20:59:54 +00001239
1240/// FindCategoryDeclaration - Finds category declaration in the list of
1241/// categories for this class and returns it. Name of the category is passed
1242/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001243///
Chris Lattnerab351632009-02-20 20:59:54 +00001244ObjCCategoryDecl *
1245ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001246 // FIXME: Should make sure no callers ever do this.
1247 if (!hasDefinition())
1248 return 0;
1249
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001250 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001251 LoadExternalDefinition();
1252
Douglas Gregord3297242013-01-16 23:00:23 +00001253 for (visible_categories_iterator Cat = visible_categories_begin(),
1254 CatEnd = visible_categories_end();
1255 Cat != CatEnd;
1256 ++Cat) {
1257 if (Cat->getIdentifier() == CategoryId)
1258 return *Cat;
1259 }
1260
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001261 return 0;
1262}
1263
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001264ObjCMethodDecl *
1265ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001266 for (visible_categories_iterator Cat = visible_categories_begin(),
1267 CatEnd = visible_categories_end();
1268 Cat != CatEnd;
1269 ++Cat) {
1270 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001271 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1272 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001273 }
1274
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001275 return 0;
1276}
1277
1278ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001279 for (visible_categories_iterator Cat = visible_categories_begin(),
1280 CatEnd = visible_categories_end();
1281 Cat != CatEnd;
1282 ++Cat) {
1283 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001284 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1285 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001286 }
1287
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001288 return 0;
1289}
1290
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001291/// ClassImplementsProtocol - Checks that 'lProto' protocol
1292/// has been implemented in IDecl class, its super class or categories (if
1293/// lookupCategory is true).
1294bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1295 bool lookupCategory,
1296 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001297 if (!hasDefinition())
1298 return false;
1299
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001300 ObjCInterfaceDecl *IDecl = this;
1301 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001302 for (ObjCInterfaceDecl::protocol_iterator
1303 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001304 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1305 return true;
1306 // This is dubious and is added to be compatible with gcc. In gcc, it is
1307 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1308 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1309 // object. This IMO, should be a bug.
1310 // FIXME: Treat this as an extension, and flag this as an error when GCC
1311 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001312 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001313 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1314 return true;
1315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001317 // 2nd, look up the category.
1318 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001319 for (visible_categories_iterator Cat = visible_categories_begin(),
1320 CatEnd = visible_categories_end();
1321 Cat != CatEnd;
1322 ++Cat) {
1323 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1324 E = Cat->protocol_end();
1325 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001326 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1327 return true;
1328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001330 // 3rd, look up the super class(s)
1331 if (IDecl->getSuperClass())
1332 return
1333 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1334 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001336 return false;
1337}
1338
Chris Lattnerab351632009-02-20 20:59:54 +00001339//===----------------------------------------------------------------------===//
1340// ObjCIvarDecl
1341//===----------------------------------------------------------------------===//
1342
David Blaikie99ba9e32011-12-20 02:48:34 +00001343void ObjCIvarDecl::anchor() { }
1344
Daniel Dunbara0654922010-04-02 20:10:03 +00001345ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001346 SourceLocation StartLoc,
1347 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001348 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001349 AccessControl ac, Expr *BW,
1350 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001351 if (DC) {
1352 // Ivar's can only appear in interfaces, implementations (via synthesized
1353 // properties), and class extensions (via direct declaration, or synthesized
1354 // properties).
1355 //
1356 // FIXME: This should really be asserting this:
1357 // (isa<ObjCCategoryDecl>(DC) &&
1358 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1359 // but unfortunately we sometimes place ivars into non-class extension
1360 // categories on error. This breaks an AST invariant, and should not be
1361 // fixed.
1362 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1363 isa<ObjCCategoryDecl>(DC)) &&
1364 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001365 // Once a new ivar is created in any of class/class-extension/implementation
1366 // decl contexts, the previously built IvarList must be rebuilt.
1367 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1368 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001369 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001370 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001371 else
1372 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001373 }
1374 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001375 }
1376
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001377 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1378 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001379}
1380
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001381ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1382 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1383 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1384 QualType(), 0, ObjCIvarDecl::None, 0, false);
1385}
1386
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001387const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1388 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001389
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001390 switch (DC->getKind()) {
1391 default:
1392 case ObjCCategoryImpl:
1393 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001394 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001395
1396 // Ivars can only appear in class extension categories.
1397 case ObjCCategory: {
1398 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1399 assert(CD->IsClassExtension() && "invalid container for ivar!");
1400 return CD->getClassInterface();
1401 }
1402
1403 case ObjCImplementation:
1404 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1405
1406 case ObjCInterface:
1407 return cast<ObjCInterfaceDecl>(DC);
1408 }
1409}
Chris Lattnerab351632009-02-20 20:59:54 +00001410
1411//===----------------------------------------------------------------------===//
1412// ObjCAtDefsFieldDecl
1413//===----------------------------------------------------------------------===//
1414
David Blaikie99ba9e32011-12-20 02:48:34 +00001415void ObjCAtDefsFieldDecl::anchor() { }
1416
Chris Lattnerab351632009-02-20 20:59:54 +00001417ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001418*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1419 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001420 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001421 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001422}
1423
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001424ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1425 unsigned ID) {
1426 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1427 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1428 0, QualType(), 0);
1429}
1430
Chris Lattnerab351632009-02-20 20:59:54 +00001431//===----------------------------------------------------------------------===//
1432// ObjCProtocolDecl
1433//===----------------------------------------------------------------------===//
1434
David Blaikie99ba9e32011-12-20 02:48:34 +00001435void ObjCProtocolDecl::anchor() { }
1436
Douglas Gregor27c6da22012-01-01 20:30:41 +00001437ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1438 SourceLocation nameLoc,
1439 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001440 ObjCProtocolDecl *PrevDecl)
1441 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001442{
1443 setPreviousDeclaration(PrevDecl);
1444 if (PrevDecl)
1445 Data = PrevDecl->Data;
1446}
1447
Chris Lattnerab351632009-02-20 20:59:54 +00001448ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001449 IdentifierInfo *Id,
1450 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001451 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001452 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001453 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001454 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001455 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001456 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001457}
1458
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001459ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1460 unsigned ID) {
1461 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001462 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1463 SourceLocation(), 0);
1464 Result->Data.setInt(!C.getLangOpts().Modules);
1465 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001466}
1467
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001468ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1469 ObjCProtocolDecl *PDecl = this;
1470
1471 if (Name == getIdentifier())
1472 return PDecl;
1473
1474 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1475 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1476 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001478 return NULL;
1479}
1480
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001481// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001482// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001483ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1484 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001485 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001487 // If there is no definition or the definition is hidden, we don't find
1488 // anything.
1489 const ObjCProtocolDecl *Def = getDefinition();
1490 if (!Def || Def->isHidden())
1491 return NULL;
1492
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001493 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001494 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Chris Lattnerab351632009-02-20 20:59:54 +00001496 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001497 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001498 return MethodDecl;
1499 return NULL;
1500}
1501
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001502void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001503 assert(!Data.getPointer() && "Protocol already has a definition!");
1504 Data.setPointer(new (getASTContext()) DefinitionData);
1505 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001506}
1507
1508void ObjCProtocolDecl::startDefinition() {
1509 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001510
1511 // Update all of the declarations with a pointer to the definition.
1512 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1513 RD != RDEnd; ++RD)
1514 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001515}
1516
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001517void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1518 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001519
1520 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1521 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1522 E = PDecl->prop_end(); P != E; ++P) {
1523 ObjCPropertyDecl *Prop = *P;
1524 // Insert into PM if not there already.
1525 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001526 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001527 }
1528 // Scan through protocol's protocols.
1529 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1530 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001531 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001532 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001533}
1534
1535
Chris Lattnerab351632009-02-20 20:59:54 +00001536//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001537// ObjCCategoryDecl
1538//===----------------------------------------------------------------------===//
1539
David Blaikie99ba9e32011-12-20 02:48:34 +00001540void ObjCCategoryDecl::anchor() { }
1541
Chris Lattnerab351632009-02-20 20:59:54 +00001542ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001543 SourceLocation AtLoc,
1544 SourceLocation ClassNameLoc,
1545 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001546 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001547 ObjCInterfaceDecl *IDecl,
1548 SourceLocation IvarLBraceLoc,
1549 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001550 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1551 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001552 IDecl,
1553 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001554 if (IDecl) {
1555 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001556 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001557 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001558 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001559 if (ASTMutationListener *L = C.getASTMutationListener())
1560 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1561 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001562 }
1563
1564 return CatDecl;
1565}
1566
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001567ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1568 unsigned ID) {
1569 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1570 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1571 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001572}
1573
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001574ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1575 return getASTContext().getObjCImplementation(
1576 const_cast<ObjCCategoryDecl*>(this));
1577}
1578
1579void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1580 getASTContext().setObjCImplementation(this, ImplD);
1581}
1582
1583
Chris Lattnerab351632009-02-20 20:59:54 +00001584//===----------------------------------------------------------------------===//
1585// ObjCCategoryImplDecl
1586//===----------------------------------------------------------------------===//
1587
David Blaikie99ba9e32011-12-20 02:48:34 +00001588void ObjCCategoryImplDecl::anchor() { }
1589
Chris Lattnerab351632009-02-20 20:59:54 +00001590ObjCCategoryImplDecl *
1591ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001592 IdentifierInfo *Id,
1593 ObjCInterfaceDecl *ClassInterface,
1594 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001595 SourceLocation atStartLoc,
1596 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001597 if (ClassInterface && ClassInterface->hasDefinition())
1598 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001599 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001600 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001601}
1602
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001603ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1604 unsigned ID) {
1605 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1606 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1607 SourceLocation(), SourceLocation());
1608}
1609
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001610ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001611 // The class interface might be NULL if we are working with invalid code.
1612 if (const ObjCInterfaceDecl *ID = getClassInterface())
1613 return ID->FindCategoryDeclaration(getIdentifier());
1614 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001615}
1616
Chris Lattnerab351632009-02-20 20:59:54 +00001617
David Blaikie99ba9e32011-12-20 02:48:34 +00001618void ObjCImplDecl::anchor() { }
1619
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001620void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001621 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001622 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001623 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001624}
1625
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001626void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1627 ASTContext &Ctx = getASTContext();
1628
1629 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001630 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001631 if (IFace)
1632 Ctx.setObjCImplementation(IFace, ImplD);
1633
Duncan Sands98f2cca2009-07-21 07:56:29 +00001634 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001635 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1636 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1637 Ctx.setObjCImplementation(CD, ImplD);
1638 }
1639
1640 ClassInterface = IFace;
1641}
1642
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001643/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001644/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001645/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001646///
Chris Lattner3aa18612009-02-28 18:42:10 +00001647ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001648FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1649 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001650 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001651 if (PID->getPropertyIvarDecl() &&
1652 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1653 return PID;
1654 }
1655 return 0;
1656}
1657
1658/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001659/// added to the list of those properties \@synthesized/\@dynamic in this
1660/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001661///
Chris Lattner3aa18612009-02-28 18:42:10 +00001662ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001663FindPropertyImplDecl(IdentifierInfo *Id) const {
1664 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001665 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001666 if (PID->getPropertyDecl()->getIdentifier() == Id)
1667 return PID;
1668 }
1669 return 0;
1670}
1671
Chris Lattner5f9e2722011-07-23 10:55:15 +00001672raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001673 const ObjCCategoryImplDecl &CID) {
1674 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001675 return OS;
1676}
1677
Chris Lattnerab351632009-02-20 20:59:54 +00001678//===----------------------------------------------------------------------===//
1679// ObjCImplementationDecl
1680//===----------------------------------------------------------------------===//
1681
David Blaikie99ba9e32011-12-20 02:48:34 +00001682void ObjCImplementationDecl::anchor() { }
1683
Chris Lattnerab351632009-02-20 20:59:54 +00001684ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001685ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001686 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001687 ObjCInterfaceDecl *SuperDecl,
1688 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001689 SourceLocation atStartLoc,
1690 SourceLocation IvarLBraceLoc,
1691 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001692 if (ClassInterface && ClassInterface->hasDefinition())
1693 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001694 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001695 nameLoc, atStartLoc,
1696 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001697}
1698
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001699ObjCImplementationDecl *
1700ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1701 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1702 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1703 SourceLocation());
1704}
1705
John McCallda6d9762011-07-22 04:15:06 +00001706void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1707 CXXCtorInitializer ** initializers,
1708 unsigned numInitializers) {
1709 if (numInitializers > 0) {
1710 NumIvarInitializers = numInitializers;
1711 CXXCtorInitializer **ivarInitializers =
1712 new (C) CXXCtorInitializer*[NumIvarInitializers];
1713 memcpy(ivarInitializers, initializers,
1714 numInitializers * sizeof(CXXCtorInitializer*));
1715 IvarInitializers = ivarInitializers;
1716 }
1717}
1718
Chris Lattner5f9e2722011-07-23 10:55:15 +00001719raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001720 const ObjCImplementationDecl &ID) {
1721 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001722 return OS;
1723}
1724
Chris Lattnerab351632009-02-20 20:59:54 +00001725//===----------------------------------------------------------------------===//
1726// ObjCCompatibleAliasDecl
1727//===----------------------------------------------------------------------===//
1728
David Blaikie99ba9e32011-12-20 02:48:34 +00001729void ObjCCompatibleAliasDecl::anchor() { }
1730
Chris Lattnerab351632009-02-20 20:59:54 +00001731ObjCCompatibleAliasDecl *
1732ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1733 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001734 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001735 ObjCInterfaceDecl* AliasedClass) {
1736 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1737}
1738
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001739ObjCCompatibleAliasDecl *
1740ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1741 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1742 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1743}
1744
Chris Lattnerab351632009-02-20 20:59:54 +00001745//===----------------------------------------------------------------------===//
1746// ObjCPropertyDecl
1747//===----------------------------------------------------------------------===//
1748
David Blaikie99ba9e32011-12-20 02:48:34 +00001749void ObjCPropertyDecl::anchor() { }
1750
Chris Lattnerab351632009-02-20 20:59:54 +00001751ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1752 SourceLocation L,
1753 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001754 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001755 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001756 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001757 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001758 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001759}
1760
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001761ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1762 unsigned ID) {
1763 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1764 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001765 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001766 0);
1767}
1768
Chris Lattnerab351632009-02-20 20:59:54 +00001769//===----------------------------------------------------------------------===//
1770// ObjCPropertyImplDecl
1771//===----------------------------------------------------------------------===//
1772
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001773ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001774 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001775 SourceLocation atLoc,
1776 SourceLocation L,
1777 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001778 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001779 ObjCIvarDecl *ivar,
1780 SourceLocation ivarLoc) {
1781 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1782 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001783}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001784
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001785ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1786 unsigned ID) {
1787 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1788 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1789 0, Dynamic, 0, SourceLocation());
1790}
1791
Douglas Gregora4ffd852010-11-17 01:03:52 +00001792SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1793 SourceLocation EndLoc = getLocation();
1794 if (IvarLoc.isValid())
1795 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001796
Douglas Gregora4ffd852010-11-17 01:03:52 +00001797 return SourceRange(AtLoc, EndLoc);
1798}