blob: 3895a520857c5e6b85c02763f4a2676414b987bd [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 Kyrtzidis04593d02013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000075 return 0;
76 }
77
Steve Naroff0de21fd2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
David Blaikie3bc93e32012-12-19 00:45:41 +000086 lookup_const_result R = lookup(Sel);
87 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88 Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000091 return MD;
92 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000093 return 0;
94}
95
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +000096/// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
97/// method was found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite' property.
99/// This is because, user must provide a setter method for the category's 'readwrite'
100/// property.
101bool
102ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
103 Selector Sel = Property->getSetterName();
104 lookup_const_result R = lookup(Sel);
105 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
115 for (ObjCInterfaceDecl::visible_categories_iterator
116 Cat = ID->visible_categories_begin(),
117 CatEnd = ID->visible_categories_end();
118 Cat != CatEnd;
119 ++Cat) {
120 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
121 if (!MD->isImplicit())
122 return true;
123 if (Cat->IsClassExtension())
124 continue;
125 // Also search through the categories looking for a 'readwrite' declaration
126 // of this property. If one found, presumably a setter will be provided
127 // (properties declared in categories will not get auto-synthesized).
128 for (ObjCContainerDecl::prop_iterator P = Cat->prop_begin(),
129 E = Cat->prop_end(); P != E; ++P)
130 if (P->getIdentifier() == Property->getIdentifier()) {
131 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
132 return true;
133 break;
134 }
135 }
136
137 // Also look into protocols, for a user declared instance method.
138 for (ObjCInterfaceDecl::all_protocol_iterator P =
139 ID->all_referenced_protocol_begin(),
140 PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
141 ObjCProtocolDecl *Proto = (*P);
142 if (Proto->HasUserDeclaredSetterMethod(Property))
143 return true;
144 }
145 // And in its super class.
146 ObjCInterfaceDecl *OSC = ID->getSuperClass();
147 while (OSC) {
148 if (OSC->HasUserDeclaredSetterMethod(Property))
149 return true;
150 OSC = OSC->getSuperClass();
151 }
152 }
153 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
154 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
155 E = PD->protocol_end(); PI != E; ++PI) {
156 if ((*PI)->HasUserDeclaredSetterMethod(Property))
157 return true;
158 }
159 return false;
160}
161
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000162ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000163ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000164 IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000165 // If this context is a hidden protocol definition, don't find any
166 // property.
167 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
168 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
169 if (Def->isHidden())
170 return 0;
171 }
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000172
David Blaikie3bc93e32012-12-19 00:45:41 +0000173 DeclContext::lookup_const_result R = DC->lookup(propertyID);
174 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
175 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000176 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
177 return PD;
178
179 return 0;
180}
181
Anna Zaksad0ce532012-09-27 19:45:11 +0000182IdentifierInfo *
183ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
184 SmallString<128> ivarName;
185 {
186 llvm::raw_svector_ostream os(ivarName);
187 os << '_' << getIdentifier()->getName();
188 }
189 return &Ctx.Idents.get(ivarName.str());
190}
191
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000192/// FindPropertyDeclaration - Finds declaration of the property given its name
193/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000194ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000195ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000196 // Don't find properties within hidden protocol definitions.
197 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
198 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
199 if (Def->isHidden())
200 return 0;
201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000203 if (ObjCPropertyDecl *PD =
204 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
205 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000207 switch (getKind()) {
208 default:
209 break;
210 case Decl::ObjCProtocol: {
211 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
212 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
213 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000216 break;
217 }
218 case Decl::ObjCInterface: {
219 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000220 // Look through categories (but not extensions).
221 for (ObjCInterfaceDecl::visible_categories_iterator
222 Cat = OID->visible_categories_begin(),
223 CatEnd = OID->visible_categories_end();
224 Cat != CatEnd; ++Cat) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000225 if (!Cat->IsClassExtension())
226 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
227 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000228 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000229
230 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000231 for (ObjCInterfaceDecl::all_protocol_iterator
232 I = OID->all_referenced_protocol_begin(),
233 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000234 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
235 return P;
236
237 // Finally, check the super class.
238 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
239 return superClass->FindPropertyDeclaration(PropertyId);
240 break;
241 }
242 case Decl::ObjCCategory: {
243 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
244 // Look through protocols.
245 if (!OCD->IsClassExtension())
246 for (ObjCCategoryDecl::protocol_iterator
247 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
248 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
249 return P;
250
251 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000252 }
253 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000254 return 0;
255}
256
David Blaikie99ba9e32011-12-20 02:48:34 +0000257void ObjCInterfaceDecl::anchor() { }
258
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000259/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
260/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000261/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000262///
263ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000264ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000265 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000266 // FIXME: Should make sure no callers ever do this.
267 if (!hasDefinition())
268 return 0;
269
270 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000271 LoadExternalDefinition();
272
Ted Kremenek37cafb02010-03-15 20:30:07 +0000273 if (ObjCPropertyDecl *PD =
274 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
275 return PD;
276
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000277 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000278 for (ObjCInterfaceDecl::all_protocol_iterator
279 I = all_referenced_protocol_begin(),
280 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000281 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
282 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000283
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000284 return 0;
285}
286
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000287void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
288 PropertyDeclOrder &PO) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000289 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
290 E = prop_end(); P != E; ++P) {
291 ObjCPropertyDecl *Prop = *P;
292 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000293 PO.push_back(Prop);
Anna Zaksb36ea372012-10-18 19:17:53 +0000294 }
295 for (ObjCInterfaceDecl::all_protocol_iterator
296 PI = all_referenced_protocol_begin(),
297 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000298 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zakse63aedd2012-10-31 01:18:22 +0000299 // Note, the properties declared only in class extensions are still copied
300 // into the main @interface's property list, and therefore we don't
301 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000302}
303
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000304bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
305 const ObjCInterfaceDecl *Class = this;
306 while (Class) {
307 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
308 return true;
309 Class = Class->getSuperClass();
310 }
311 return false;
312}
313
314const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
315 const ObjCInterfaceDecl *Class = this;
316 while (Class) {
317 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
318 return Class;
319 Class = Class->getSuperClass();
320 }
321 return 0;
322}
323
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000324void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
325 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
326 ASTContext &C)
327{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000328 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000329 LoadExternalDefinition();
330
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000331 if (data().AllReferencedProtocols.empty() &&
332 data().ReferencedProtocols.empty()) {
333 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000334 return;
335 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000336
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000337 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000338 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000339 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000340 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000341 for (unsigned i = 0; i < ExtNum; i++) {
342 bool protocolExists = false;
343 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000344 for (all_protocol_iterator
345 p = all_referenced_protocol_begin(),
346 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000347 ObjCProtocolDecl *Proto = (*p);
348 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
349 protocolExists = true;
350 break;
351 }
352 }
353 // Do we want to warn on a protocol in extension class which
354 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000355 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000356 ProtocolRefs.push_back(ProtoInExtension);
357 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000358
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000359 if (ProtocolRefs.empty())
360 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000361
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000362 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000363 for (all_protocol_iterator p = all_referenced_protocol_begin(),
364 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000365 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000366 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000367
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000368 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000369}
370
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000371void ObjCInterfaceDecl::allocateDefinitionData() {
372 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000373 Data.setPointer(new (getASTContext()) DefinitionData());
374 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000375
376 // Make the type point at the definition, now that we have one.
377 if (TypeForDecl)
378 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000379}
380
381void ObjCInterfaceDecl::startDefinition() {
382 allocateDefinitionData();
383
Douglas Gregor53df7a12011-12-15 18:03:09 +0000384 // Update all of the declarations with a pointer to the definition.
385 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
386 RD != RDEnd; ++RD) {
387 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000388 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000389 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000390}
391
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000392ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
393 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000394 // FIXME: Should make sure no callers ever do this.
395 if (!hasDefinition())
396 return 0;
397
398 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000399 LoadExternalDefinition();
400
Chris Lattner1e03a562008-03-16 00:19:01 +0000401 ObjCInterfaceDecl* ClassDecl = this;
402 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000403 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000404 clsDeclared = ClassDecl;
405 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000406 }
Douglas Gregord3297242013-01-16 23:00:23 +0000407
408 for (ObjCInterfaceDecl::visible_extensions_iterator
409 Ext = ClassDecl->visible_extensions_begin(),
410 ExtEnd = ClassDecl->visible_extensions_end();
411 Ext != ExtEnd; ++Ext) {
412 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000413 clsDeclared = ClassDecl;
414 return I;
415 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000416 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000417
Chris Lattner1e03a562008-03-16 00:19:01 +0000418 ClassDecl = ClassDecl->getSuperClass();
419 }
420 return NULL;
421}
422
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000423/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
424/// class whose name is passed as argument. If it is not one of the super classes
425/// the it returns NULL.
426ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
427 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000428 // FIXME: Should make sure no callers ever do this.
429 if (!hasDefinition())
430 return 0;
431
432 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000433 LoadExternalDefinition();
434
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000435 ObjCInterfaceDecl* ClassDecl = this;
436 while (ClassDecl != NULL) {
437 if (ClassDecl->getIdentifier() == ICName)
438 return ClassDecl;
439 ClassDecl = ClassDecl->getSuperClass();
440 }
441 return NULL;
442}
443
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000444/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000445/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000446/// When argument category "C" is specified, any implicit method found
447/// in this category is ignored.
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000448ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
449 bool isInstance,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000450 bool shallowCategoryLookup,
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000451 const ObjCCategoryDecl *C) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000452 // FIXME: Should make sure no callers ever do this.
453 if (!hasDefinition())
454 return 0;
455
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000456 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000457 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000459 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000460 LoadExternalDefinition();
461
Chris Lattner1e03a562008-03-16 00:19:01 +0000462 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000463 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000464 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattner1e03a562008-03-16 00:19:01 +0000466 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000467 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
468 E = ClassDecl->protocol_end();
469 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000470 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000471 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000472
473 // Didn't find one yet - now look through categories.
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000474 for (ObjCInterfaceDecl::visible_categories_iterator
475 Cat = ClassDecl->visible_categories_begin(),
476 CatEnd = ClassDecl->visible_categories_end();
477 Cat != CatEnd; ++Cat) {
478 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
479 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000480 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000481
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000482 if (!shallowCategoryLookup) {
483 // Didn't find one yet - look through protocols.
484 const ObjCList<ObjCProtocolDecl> &Protocols =
485 Cat->getReferencedProtocols();
486 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
487 E = Protocols.end(); I != E; ++I)
488 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
489 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000490 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000491 }
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000492 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000493
Chris Lattner1e03a562008-03-16 00:19:01 +0000494 ClassDecl = ClassDecl->getSuperClass();
495 }
496 return NULL;
497}
498
Anna Zakse61354b2012-07-27 19:07:44 +0000499// Will search "local" class/category implementations for a method decl.
500// If failed, then we search in class's root for an instance method.
501// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000502ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
503 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000504 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000505 // FIXME: Should make sure no callers ever do this.
506 if (!hasDefinition())
507 return 0;
508
509 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000510 LoadExternalDefinition();
511
Steve Naroffd789d3d2009-10-01 23:46:04 +0000512 ObjCMethodDecl *Method = 0;
513 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000514 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
515 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000516
517 // Look through local category implementations associated with the class.
518 if (!Method)
519 Method = Instance ? getCategoryInstanceMethod(Sel)
520 : getCategoryClassMethod(Sel);
521
522 // Before we give up, check if the selector is an instance method.
523 // But only in the root. This matches gcc's behavior and what the
524 // runtime expects.
525 if (!Instance && !Method && !getSuperClass()) {
526 Method = lookupInstanceMethod(Sel);
527 // Look through local category implementations associated
528 // with the root class.
529 if (!Method)
530 Method = lookupPrivateMethod(Sel, true);
531 }
532
Steve Naroffd789d3d2009-10-01 23:46:04 +0000533 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000534 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000535 return Method;
536}
Chris Lattnerab351632009-02-20 20:59:54 +0000537
538//===----------------------------------------------------------------------===//
539// ObjCMethodDecl
540//===----------------------------------------------------------------------===//
541
542ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000543 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000544 SourceLocation endLoc,
545 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000546 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000547 DeclContext *contextDecl,
548 bool isInstance,
549 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000550 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000551 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000552 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000553 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000554 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000555 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000556 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000557 isInstance, isVariadic, isPropertyAccessor,
558 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000559 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000560 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000561}
562
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000563ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
564 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
565 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
566 Selector(), QualType(), 0, 0);
567}
568
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000569Stmt *ObjCMethodDecl::getBody() const {
570 return Body.get(getASTContext().getExternalSource());
571}
572
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000573void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
574 assert(PrevMethod);
575 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
576 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000577 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000578}
579
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000580void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
581 ArrayRef<ParmVarDecl*> Params,
582 ArrayRef<SourceLocation> SelLocs) {
583 ParamsAndSelLocs = 0;
584 NumParams = Params.size();
585 if (Params.empty() && SelLocs.empty())
586 return;
587
588 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
589 sizeof(SourceLocation) * SelLocs.size();
590 ParamsAndSelLocs = C.Allocate(Size);
591 std::copy(Params.begin(), Params.end(), getParams());
592 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
593}
594
595void ObjCMethodDecl::getSelectorLocs(
596 SmallVectorImpl<SourceLocation> &SelLocs) const {
597 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
598 SelLocs.push_back(getSelectorLoc(i));
599}
600
601void ObjCMethodDecl::setMethodParams(ASTContext &C,
602 ArrayRef<ParmVarDecl*> Params,
603 ArrayRef<SourceLocation> SelLocs) {
604 assert((!SelLocs.empty() || isImplicit()) &&
605 "No selector locs for non-implicit method");
606 if (isImplicit())
Dmitri Gribenko55431692013-05-05 00:41:58 +0000607 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000608
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000609 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
610 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000611 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko55431692013-05-05 00:41:58 +0000612 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000613
614 setParamsAndSelLocs(C, Params, SelLocs);
615}
616
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000617/// \brief A definition will return its interface declaration.
618/// An interface declaration will return its definition.
619/// Otherwise it will return itself.
620ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
621 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000622 ObjCMethodDecl *Redecl = 0;
623 if (HasRedeclaration)
624 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000625 if (Redecl)
626 return Redecl;
627
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000628 Decl *CtxD = cast<Decl>(getDeclContext());
629
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000630 if (!CtxD->isInvalidDecl()) {
631 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
632 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
633 if (!ImplD->isInvalidDecl())
634 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000635
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000636 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
637 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
638 if (!ImplD->isInvalidDecl())
639 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000640
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000641 } else if (ObjCImplementationDecl *ImplD =
642 dyn_cast<ObjCImplementationDecl>(CtxD)) {
643 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
644 if (!IFD->isInvalidDecl())
645 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000646
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000647 } else if (ObjCCategoryImplDecl *CImplD =
648 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
649 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
650 if (!CatD->isInvalidDecl())
651 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
652 }
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000653 }
654
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000655 if (!Redecl && isRedeclaration()) {
656 // This is the last redeclaration, go back to the first method.
657 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
658 isInstanceMethod());
659 }
660
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000661 return Redecl ? Redecl : this;
662}
663
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000664ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
665 Decl *CtxD = cast<Decl>(getDeclContext());
666
667 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
668 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
669 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
670 isInstanceMethod()))
671 return MD;
672
673 } else if (ObjCCategoryImplDecl *CImplD =
674 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000675 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000676 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
677 isInstanceMethod()))
678 return MD;
679 }
680
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000681 if (isRedeclaration())
682 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
683 isInstanceMethod());
684
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000685 return this;
686}
687
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000688SourceLocation ObjCMethodDecl::getLocEnd() const {
689 if (Stmt *Body = getBody())
690 return Body->getLocEnd();
691 return DeclEndLoc;
692}
693
John McCall85f3d762011-03-02 01:50:55 +0000694ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
695 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000696 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000697 return family;
698
John McCalld5313b02011-03-02 11:33:24 +0000699 // Check for an explicit attribute.
700 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
701 // The unfortunate necessity of mapping between enums here is due
702 // to the attributes framework.
703 switch (attr->getFamily()) {
704 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
705 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
706 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
707 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
708 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
709 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
710 }
711 Family = static_cast<unsigned>(family);
712 return family;
713 }
714
John McCall85f3d762011-03-02 01:50:55 +0000715 family = getSelector().getMethodFamily();
716 switch (family) {
717 case OMF_None: break;
718
719 // init only has a conventional meaning for an instance method, and
720 // it has to return an object.
721 case OMF_init:
722 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
723 family = OMF_None;
724 break;
725
726 // alloc/copy/new have a conventional meaning for both class and
727 // instance methods, but they require an object return.
728 case OMF_alloc:
729 case OMF_copy:
730 case OMF_mutableCopy:
731 case OMF_new:
732 if (!getResultType()->isObjCObjectPointerType())
733 family = OMF_None;
734 break;
735
736 // These selectors have a conventional meaning only for instance methods.
737 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000738 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000739 case OMF_retain:
740 case OMF_release:
741 case OMF_autorelease:
742 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000743 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000744 if (!isInstanceMethod())
745 family = OMF_None;
746 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000747
748 case OMF_performSelector:
749 if (!isInstanceMethod() ||
750 !getResultType()->isObjCIdType())
751 family = OMF_None;
752 else {
753 unsigned noParams = param_size();
754 if (noParams < 1 || noParams > 3)
755 family = OMF_None;
756 else {
757 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
758 QualType ArgT = (*it);
759 if (!ArgT->isObjCSelType()) {
760 family = OMF_None;
761 break;
762 }
763 while (--noParams) {
764 it++;
765 ArgT = (*it);
766 if (!ArgT->isObjCIdType()) {
767 family = OMF_None;
768 break;
769 }
770 }
771 }
772 }
773 break;
774
John McCall85f3d762011-03-02 01:50:55 +0000775 }
776
777 // Cache the result.
778 Family = static_cast<unsigned>(family);
779 return family;
780}
781
Mike Stump1eb44332009-09-09 15:08:12 +0000782void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000783 const ObjCInterfaceDecl *OID) {
784 QualType selfTy;
785 if (isInstanceMethod()) {
786 // There may be no interface context due to error in declaration
787 // of the interface (which has been reported). Recover gracefully.
788 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000789 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000790 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000791 } else {
792 selfTy = Context.getObjCIdType();
793 }
794 } else // we have a factory method.
795 selfTy = Context.getObjCClassType();
796
John McCall7acddac2011-06-17 06:42:21 +0000797 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000798 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000799
David Blaikie4e4d0842012-03-11 07:00:24 +0000800 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000801 if (isInstanceMethod()) {
802 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000803
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000804 // 'self' is always __strong. It's actually pseudo-strong except
805 // in init methods (or methods labeled ns_consumes_self), though.
806 Qualifiers qs;
807 qs.setObjCLifetime(Qualifiers::OCL_Strong);
808 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000809
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000810 // In addition, 'self' is const unless this is an init method.
811 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
812 selfTy = selfTy.withConst();
813 selfIsPseudoStrong = true;
814 }
815 }
816 else {
817 assert(isClassMethod());
818 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000819 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000820 selfIsPseudoStrong = true;
821 }
John McCallf85e1932011-06-15 23:02:42 +0000822 }
823
824 ImplicitParamDecl *self
825 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
826 &Context.Idents.get("self"), selfTy);
827 setSelfDecl(self);
828
829 if (selfIsConsumed)
830 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000831
John McCall7acddac2011-06-17 06:42:21 +0000832 if (selfIsPseudoStrong)
833 self->setARCPseudoStrong(true);
834
Mike Stump1eb44332009-09-09 15:08:12 +0000835 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
836 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000837 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000838}
839
Chris Lattnerab351632009-02-20 20:59:54 +0000840ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
841 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
842 return ID;
843 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
844 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000845 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000846 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000847
848 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000849 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000850}
851
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000852static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
853 const ObjCMethodDecl *Method,
854 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
855 bool MovedToSuper) {
856 if (!Container)
857 return;
858
859 // In categories look for overriden methods from protocols. A method from
860 // category is not "overriden" since it is considered as the "same" method
861 // (same USR) as the one from the interface.
862 if (const ObjCCategoryDecl *
863 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
864 // Check whether we have a matching method at this category but only if we
865 // are at the super class level.
866 if (MovedToSuper)
867 if (ObjCMethodDecl *
868 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000869 Method->isInstanceMethod(),
870 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000871 if (Method != Overridden) {
872 // We found an override at this category; there is no need to look
873 // into its protocols.
874 Methods.push_back(Overridden);
875 return;
876 }
877
878 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
879 PEnd = Category->protocol_end();
880 P != PEnd; ++P)
881 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
882 return;
883 }
884
885 // Check whether we have a matching method at this level.
886 if (const ObjCMethodDecl *
887 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000888 Method->isInstanceMethod(),
889 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000890 if (Method != Overridden) {
891 // We found an override at this level; there is no need to look
892 // into other protocols or categories.
893 Methods.push_back(Overridden);
894 return;
895 }
896
897 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
898 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
899 PEnd = Protocol->protocol_end();
900 P != PEnd; ++P)
901 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
902 }
903
904 if (const ObjCInterfaceDecl *
905 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
906 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
907 PEnd = Interface->protocol_end();
908 P != PEnd; ++P)
909 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
910
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000911 for (ObjCInterfaceDecl::known_categories_iterator
912 Cat = Interface->known_categories_begin(),
913 CatEnd = Interface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +0000914 Cat != CatEnd; ++Cat) {
915 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000916 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000917 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000918
919 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
920 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
921 /*MovedToSuper=*/true);
922 }
923}
924
925static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
926 const ObjCMethodDecl *Method,
927 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
928 CollectOverriddenMethodsRecurse(Container, Method, Methods,
929 /*MovedToSuper=*/false);
930}
931
932static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
933 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
934 assert(Method->isOverriding());
935
936 if (const ObjCProtocolDecl *
937 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
938 CollectOverriddenMethods(ProtD, Method, overridden);
939
940 } else if (const ObjCImplDecl *
941 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
942 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
943 if (!ID)
944 return;
945 // Start searching for overridden methods using the method from the
946 // interface as starting point.
947 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000948 Method->isInstanceMethod(),
949 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000950 Method = IFaceMeth;
951 CollectOverriddenMethods(ID, Method, overridden);
952
953 } else if (const ObjCCategoryDecl *
954 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
955 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
956 if (!ID)
957 return;
958 // Start searching for overridden methods using the method from the
959 // interface as starting point.
960 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000961 Method->isInstanceMethod(),
962 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000963 Method = IFaceMeth;
964 CollectOverriddenMethods(ID, Method, overridden);
965
966 } else {
967 CollectOverriddenMethods(
968 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
969 Method, overridden);
970 }
971}
972
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000973void ObjCMethodDecl::getOverriddenMethods(
974 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
975 const ObjCMethodDecl *Method = this;
976
977 if (Method->isRedeclaration()) {
978 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
979 getMethod(Method->getSelector(), Method->isInstanceMethod());
980 }
981
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +0000982 if (Method->isOverriding()) {
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000983 collectOverriddenMethodsSlow(Method, Overridden);
984 assert(!Overridden.empty() &&
985 "ObjCMethodDecl's overriding bit is not as expected");
986 }
987}
988
Jordan Rose04bec392012-10-10 16:42:54 +0000989const ObjCPropertyDecl *
990ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
991 Selector Sel = getSelector();
992 unsigned NumArgs = Sel.getNumArgs();
993 if (NumArgs > 1)
994 return 0;
995
Jordan Rose50d2b262012-10-11 16:02:02 +0000996 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000997 return 0;
998
999 if (isPropertyAccessor()) {
1000 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +00001001 // If container is class extension, find its primary class.
1002 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1003 if (CatDecl->IsClassExtension())
1004 Container = CatDecl->getClassInterface();
1005
Jordan Rose04bec392012-10-10 16:42:54 +00001006 bool IsGetter = (NumArgs == 0);
1007
1008 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1009 E = Container->prop_end();
1010 I != E; ++I) {
1011 Selector NextSel = IsGetter ? (*I)->getGetterName()
1012 : (*I)->getSetterName();
1013 if (NextSel == Sel)
1014 return *I;
1015 }
1016
1017 llvm_unreachable("Marked as a property accessor but no property found!");
1018 }
1019
1020 if (!CheckOverrides)
1021 return 0;
1022
1023 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1024 OverridesTy Overrides;
1025 getOverriddenMethods(Overrides);
1026 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1027 I != E; ++I) {
1028 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1029 return Prop;
1030 }
1031
1032 return 0;
1033
1034}
1035
Chris Lattnerab351632009-02-20 20:59:54 +00001036//===----------------------------------------------------------------------===//
1037// ObjCInterfaceDecl
1038//===----------------------------------------------------------------------===//
1039
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001040ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001041 DeclContext *DC,
1042 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001043 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001044 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001045 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001046 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +00001047 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001048 PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001049 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001050 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001051 return Result;
1052}
1053
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001054ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1055 unsigned ID) {
1056 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001057 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1058 0, SourceLocation(),
1059 0, false);
1060 Result->Data.setInt(!C.getLangOpts().Modules);
1061 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001062}
1063
1064ObjCInterfaceDecl::
1065ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001066 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1067 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001068 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001069 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001070{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001071 setPreviousDeclaration(PrevDecl);
1072
1073 // Copy the 'data' pointer over.
1074 if (PrevDecl)
1075 Data = PrevDecl->Data;
1076
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001077 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001078}
1079
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001080void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001081 assert(data().ExternallyCompleted && "Class is not externally completed");
1082 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001083 getASTContext().getExternalSource()->CompleteType(
1084 const_cast<ObjCInterfaceDecl *>(this));
1085}
1086
1087void ObjCInterfaceDecl::setExternallyCompleted() {
1088 assert(getASTContext().getExternalSource() &&
1089 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001090 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001091 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001092 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001093}
1094
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001095ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001096 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1097 if (data().ExternallyCompleted)
1098 LoadExternalDefinition();
1099
1100 return getASTContext().getObjCImplementation(
1101 const_cast<ObjCInterfaceDecl*>(Def));
1102 }
1103
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001104 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001105 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001106}
1107
1108void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001109 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001110}
1111
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001112namespace {
1113 struct SynthesizeIvarChunk {
1114 uint64_t Size;
1115 ObjCIvarDecl *Ivar;
1116 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1117 : Size(size), Ivar(ivar) {}
1118 };
1119
1120 bool operator<(const SynthesizeIvarChunk & LHS,
1121 const SynthesizeIvarChunk &RHS) {
1122 return LHS.Size < RHS.Size;
1123 }
1124}
1125
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001126/// all_declared_ivar_begin - return first ivar declared in this class,
1127/// its extensions and its implementation. Lazily build the list on first
1128/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001129///
1130/// Caveat: The list returned by this method reflects the current
1131/// state of the parser. The cache will be updated for every ivar
1132/// added by an extension or the implementation when they are
1133/// encountered.
1134/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001135ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001136 // FIXME: Should make sure no callers ever do this.
1137 if (!hasDefinition())
1138 return 0;
1139
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001140 ObjCIvarDecl *curIvar = 0;
Adrian Prantl4919de62013-03-06 22:03:30 +00001141 if (!data().IvarList) {
1142 if (!ivar_empty()) {
1143 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1144 data().IvarList = *I; ++I;
1145 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001146 curIvar->setNextIvar(*I);
1147 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001148
1149 for (ObjCInterfaceDecl::known_extensions_iterator
1150 Ext = known_extensions_begin(),
1151 ExtEnd = known_extensions_end();
1152 Ext != ExtEnd; ++Ext) {
1153 if (!Ext->ivar_empty()) {
1154 ObjCCategoryDecl::ivar_iterator
1155 I = Ext->ivar_begin(),
1156 E = Ext->ivar_end();
1157 if (!data().IvarList) {
1158 data().IvarList = *I; ++I;
1159 curIvar = data().IvarList;
1160 }
1161 for ( ;I != E; curIvar = *I, ++I)
1162 curIvar->setNextIvar(*I);
1163 }
1164 }
1165 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001166 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001167
1168 // cached and complete!
1169 if (!data().IvarListMissingImplementation)
1170 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001171
1172 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001173 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001174 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001175 SmallVector<SynthesizeIvarChunk, 16> layout;
1176 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1177 E = ImplDecl->ivar_end(); I != E; ++I) {
1178 ObjCIvarDecl *IV = *I;
1179 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1180 layout.push_back(SynthesizeIvarChunk(
1181 IV->getASTContext().getTypeSize(IV->getType()), IV));
1182 continue;
1183 }
1184 if (!data().IvarList)
1185 data().IvarList = *I;
1186 else
1187 curIvar->setNextIvar(*I);
1188 curIvar = *I;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001189 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001190
1191 if (!layout.empty()) {
1192 // Order synthesized ivars by their size.
1193 std::stable_sort(layout.begin(), layout.end());
1194 unsigned Ix = 0, EIx = layout.size();
1195 if (!data().IvarList) {
1196 data().IvarList = layout[0].Ivar; Ix++;
1197 curIvar = data().IvarList;
1198 }
1199 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1200 curIvar->setNextIvar(layout[Ix].Ivar);
1201 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001202 }
1203 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001204 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001205}
Chris Lattnerab351632009-02-20 20:59:54 +00001206
1207/// FindCategoryDeclaration - Finds category declaration in the list of
1208/// categories for this class and returns it. Name of the category is passed
1209/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001210///
Chris Lattnerab351632009-02-20 20:59:54 +00001211ObjCCategoryDecl *
1212ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001213 // FIXME: Should make sure no callers ever do this.
1214 if (!hasDefinition())
1215 return 0;
1216
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001217 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001218 LoadExternalDefinition();
1219
Douglas Gregord3297242013-01-16 23:00:23 +00001220 for (visible_categories_iterator Cat = visible_categories_begin(),
1221 CatEnd = visible_categories_end();
1222 Cat != CatEnd;
1223 ++Cat) {
1224 if (Cat->getIdentifier() == CategoryId)
1225 return *Cat;
1226 }
1227
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001228 return 0;
1229}
1230
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001231ObjCMethodDecl *
1232ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001233 for (visible_categories_iterator Cat = visible_categories_begin(),
1234 CatEnd = visible_categories_end();
1235 Cat != CatEnd;
1236 ++Cat) {
1237 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001238 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1239 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001240 }
1241
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001242 return 0;
1243}
1244
1245ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001246 for (visible_categories_iterator Cat = visible_categories_begin(),
1247 CatEnd = visible_categories_end();
1248 Cat != CatEnd;
1249 ++Cat) {
1250 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001251 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1252 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001253 }
1254
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001255 return 0;
1256}
1257
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001258/// ClassImplementsProtocol - Checks that 'lProto' protocol
1259/// has been implemented in IDecl class, its super class or categories (if
1260/// lookupCategory is true).
1261bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1262 bool lookupCategory,
1263 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001264 if (!hasDefinition())
1265 return false;
1266
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001267 ObjCInterfaceDecl *IDecl = this;
1268 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001269 for (ObjCInterfaceDecl::protocol_iterator
1270 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001271 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1272 return true;
1273 // This is dubious and is added to be compatible with gcc. In gcc, it is
1274 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1275 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1276 // object. This IMO, should be a bug.
1277 // FIXME: Treat this as an extension, and flag this as an error when GCC
1278 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001279 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001280 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1281 return true;
1282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001284 // 2nd, look up the category.
1285 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001286 for (visible_categories_iterator Cat = visible_categories_begin(),
1287 CatEnd = visible_categories_end();
1288 Cat != CatEnd;
1289 ++Cat) {
1290 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1291 E = Cat->protocol_end();
1292 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001293 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1294 return true;
1295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001297 // 3rd, look up the super class(s)
1298 if (IDecl->getSuperClass())
1299 return
1300 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1301 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001303 return false;
1304}
1305
Chris Lattnerab351632009-02-20 20:59:54 +00001306//===----------------------------------------------------------------------===//
1307// ObjCIvarDecl
1308//===----------------------------------------------------------------------===//
1309
David Blaikie99ba9e32011-12-20 02:48:34 +00001310void ObjCIvarDecl::anchor() { }
1311
Daniel Dunbara0654922010-04-02 20:10:03 +00001312ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001313 SourceLocation StartLoc,
1314 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001315 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001316 AccessControl ac, Expr *BW,
1317 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001318 if (DC) {
1319 // Ivar's can only appear in interfaces, implementations (via synthesized
1320 // properties), and class extensions (via direct declaration, or synthesized
1321 // properties).
1322 //
1323 // FIXME: This should really be asserting this:
1324 // (isa<ObjCCategoryDecl>(DC) &&
1325 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1326 // but unfortunately we sometimes place ivars into non-class extension
1327 // categories on error. This breaks an AST invariant, and should not be
1328 // fixed.
1329 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1330 isa<ObjCCategoryDecl>(DC)) &&
1331 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001332 // Once a new ivar is created in any of class/class-extension/implementation
1333 // decl contexts, the previously built IvarList must be rebuilt.
1334 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1335 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001336 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001337 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001338 else
1339 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001340 }
1341 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001342 }
1343
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001344 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1345 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001346}
1347
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001348ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1349 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1350 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1351 QualType(), 0, ObjCIvarDecl::None, 0, false);
1352}
1353
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001354const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1355 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001356
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001357 switch (DC->getKind()) {
1358 default:
1359 case ObjCCategoryImpl:
1360 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001361 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001362
1363 // Ivars can only appear in class extension categories.
1364 case ObjCCategory: {
1365 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1366 assert(CD->IsClassExtension() && "invalid container for ivar!");
1367 return CD->getClassInterface();
1368 }
1369
1370 case ObjCImplementation:
1371 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1372
1373 case ObjCInterface:
1374 return cast<ObjCInterfaceDecl>(DC);
1375 }
1376}
Chris Lattnerab351632009-02-20 20:59:54 +00001377
1378//===----------------------------------------------------------------------===//
1379// ObjCAtDefsFieldDecl
1380//===----------------------------------------------------------------------===//
1381
David Blaikie99ba9e32011-12-20 02:48:34 +00001382void ObjCAtDefsFieldDecl::anchor() { }
1383
Chris Lattnerab351632009-02-20 20:59:54 +00001384ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001385*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1386 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001387 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001388 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001389}
1390
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001391ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1392 unsigned ID) {
1393 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1394 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1395 0, QualType(), 0);
1396}
1397
Chris Lattnerab351632009-02-20 20:59:54 +00001398//===----------------------------------------------------------------------===//
1399// ObjCProtocolDecl
1400//===----------------------------------------------------------------------===//
1401
David Blaikie99ba9e32011-12-20 02:48:34 +00001402void ObjCProtocolDecl::anchor() { }
1403
Douglas Gregor27c6da22012-01-01 20:30:41 +00001404ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1405 SourceLocation nameLoc,
1406 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001407 ObjCProtocolDecl *PrevDecl)
1408 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001409{
1410 setPreviousDeclaration(PrevDecl);
1411 if (PrevDecl)
1412 Data = PrevDecl->Data;
1413}
1414
Chris Lattnerab351632009-02-20 20:59:54 +00001415ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001416 IdentifierInfo *Id,
1417 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001418 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001419 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001420 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001421 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001422 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001423 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001424}
1425
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001426ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1427 unsigned ID) {
1428 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001429 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1430 SourceLocation(), 0);
1431 Result->Data.setInt(!C.getLangOpts().Modules);
1432 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001433}
1434
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001435ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1436 ObjCProtocolDecl *PDecl = this;
1437
1438 if (Name == getIdentifier())
1439 return PDecl;
1440
1441 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1442 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1443 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001445 return NULL;
1446}
1447
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001448// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001449// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001450ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1451 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001452 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001454 // If there is no definition or the definition is hidden, we don't find
1455 // anything.
1456 const ObjCProtocolDecl *Def = getDefinition();
1457 if (!Def || Def->isHidden())
1458 return NULL;
1459
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001460 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001461 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Chris Lattnerab351632009-02-20 20:59:54 +00001463 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001464 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001465 return MethodDecl;
1466 return NULL;
1467}
1468
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001469void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001470 assert(!Data.getPointer() && "Protocol already has a definition!");
1471 Data.setPointer(new (getASTContext()) DefinitionData);
1472 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001473}
1474
1475void ObjCProtocolDecl::startDefinition() {
1476 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001477
1478 // Update all of the declarations with a pointer to the definition.
1479 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1480 RD != RDEnd; ++RD)
1481 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001482}
1483
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001484void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1485 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001486
1487 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1488 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1489 E = PDecl->prop_end(); P != E; ++P) {
1490 ObjCPropertyDecl *Prop = *P;
1491 // Insert into PM if not there already.
1492 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001493 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001494 }
1495 // Scan through protocol's protocols.
1496 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1497 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001498 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001499 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001500}
1501
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001502
1503void ObjCProtocolDecl::collectInheritedProtocolProperties(
1504 const ObjCPropertyDecl *Property,
1505 ProtocolPropertyMap &PM) const {
1506 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1507 bool MatchFound = false;
1508 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1509 E = PDecl->prop_end(); P != E; ++P) {
1510 ObjCPropertyDecl *Prop = *P;
1511 if (Prop == Property)
1512 continue;
1513 if (Prop->getIdentifier() == Property->getIdentifier()) {
1514 PM[PDecl] = Prop;
1515 MatchFound = true;
1516 break;
1517 }
1518 }
1519 // Scan through protocol's protocols which did not have a matching property.
1520 if (!MatchFound)
1521 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1522 E = PDecl->protocol_end(); PI != E; ++PI)
1523 (*PI)->collectInheritedProtocolProperties(Property, PM);
1524 }
1525}
Anna Zaksb36ea372012-10-18 19:17:53 +00001526
Chris Lattnerab351632009-02-20 20:59:54 +00001527//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001528// ObjCCategoryDecl
1529//===----------------------------------------------------------------------===//
1530
David Blaikie99ba9e32011-12-20 02:48:34 +00001531void ObjCCategoryDecl::anchor() { }
1532
Chris Lattnerab351632009-02-20 20:59:54 +00001533ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001534 SourceLocation AtLoc,
1535 SourceLocation ClassNameLoc,
1536 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001537 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001538 ObjCInterfaceDecl *IDecl,
1539 SourceLocation IvarLBraceLoc,
1540 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001541 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1542 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001543 IDecl,
1544 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001545 if (IDecl) {
1546 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001547 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001548 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001549 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001550 if (ASTMutationListener *L = C.getASTMutationListener())
1551 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1552 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001553 }
1554
1555 return CatDecl;
1556}
1557
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001558ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1559 unsigned ID) {
1560 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1561 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1562 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001563}
1564
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001565ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1566 return getASTContext().getObjCImplementation(
1567 const_cast<ObjCCategoryDecl*>(this));
1568}
1569
1570void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1571 getASTContext().setObjCImplementation(this, ImplD);
1572}
1573
1574
Chris Lattnerab351632009-02-20 20:59:54 +00001575//===----------------------------------------------------------------------===//
1576// ObjCCategoryImplDecl
1577//===----------------------------------------------------------------------===//
1578
David Blaikie99ba9e32011-12-20 02:48:34 +00001579void ObjCCategoryImplDecl::anchor() { }
1580
Chris Lattnerab351632009-02-20 20:59:54 +00001581ObjCCategoryImplDecl *
1582ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001583 IdentifierInfo *Id,
1584 ObjCInterfaceDecl *ClassInterface,
1585 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001586 SourceLocation atStartLoc,
1587 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001588 if (ClassInterface && ClassInterface->hasDefinition())
1589 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001590 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001591 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001592}
1593
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001594ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1595 unsigned ID) {
1596 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1597 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1598 SourceLocation(), SourceLocation());
1599}
1600
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001601ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001602 // The class interface might be NULL if we are working with invalid code.
1603 if (const ObjCInterfaceDecl *ID = getClassInterface())
1604 return ID->FindCategoryDeclaration(getIdentifier());
1605 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001606}
1607
Chris Lattnerab351632009-02-20 20:59:54 +00001608
David Blaikie99ba9e32011-12-20 02:48:34 +00001609void ObjCImplDecl::anchor() { }
1610
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001611void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001612 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001613 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001614 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001615}
1616
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001617void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1618 ASTContext &Ctx = getASTContext();
1619
1620 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001621 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001622 if (IFace)
1623 Ctx.setObjCImplementation(IFace, ImplD);
1624
Duncan Sands98f2cca2009-07-21 07:56:29 +00001625 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001626 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1627 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1628 Ctx.setObjCImplementation(CD, ImplD);
1629 }
1630
1631 ClassInterface = IFace;
1632}
1633
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001634/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001635/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001636/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001637///
Chris Lattner3aa18612009-02-28 18:42:10 +00001638ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1640 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001641 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001642 if (PID->getPropertyIvarDecl() &&
1643 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1644 return PID;
1645 }
1646 return 0;
1647}
1648
1649/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001650/// added to the list of those properties \@synthesized/\@dynamic in this
1651/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001652///
Chris Lattner3aa18612009-02-28 18:42:10 +00001653ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001654FindPropertyImplDecl(IdentifierInfo *Id) const {
1655 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001656 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001657 if (PID->getPropertyDecl()->getIdentifier() == Id)
1658 return PID;
1659 }
1660 return 0;
1661}
1662
Chris Lattner5f9e2722011-07-23 10:55:15 +00001663raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001664 const ObjCCategoryImplDecl &CID) {
1665 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001666 return OS;
1667}
1668
Chris Lattnerab351632009-02-20 20:59:54 +00001669//===----------------------------------------------------------------------===//
1670// ObjCImplementationDecl
1671//===----------------------------------------------------------------------===//
1672
David Blaikie99ba9e32011-12-20 02:48:34 +00001673void ObjCImplementationDecl::anchor() { }
1674
Chris Lattnerab351632009-02-20 20:59:54 +00001675ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001676ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001677 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001678 ObjCInterfaceDecl *SuperDecl,
1679 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001680 SourceLocation atStartLoc,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001681 SourceLocation superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001682 SourceLocation IvarLBraceLoc,
1683 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001684 if (ClassInterface && ClassInterface->hasDefinition())
1685 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001686 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001687 nameLoc, atStartLoc, superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001688 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001689}
1690
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001691ObjCImplementationDecl *
1692ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1693 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1694 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1695 SourceLocation());
1696}
1697
John McCallda6d9762011-07-22 04:15:06 +00001698void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1699 CXXCtorInitializer ** initializers,
1700 unsigned numInitializers) {
1701 if (numInitializers > 0) {
1702 NumIvarInitializers = numInitializers;
1703 CXXCtorInitializer **ivarInitializers =
1704 new (C) CXXCtorInitializer*[NumIvarInitializers];
1705 memcpy(ivarInitializers, initializers,
1706 numInitializers * sizeof(CXXCtorInitializer*));
1707 IvarInitializers = ivarInitializers;
1708 }
1709}
1710
Chris Lattner5f9e2722011-07-23 10:55:15 +00001711raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001712 const ObjCImplementationDecl &ID) {
1713 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001714 return OS;
1715}
1716
Chris Lattnerab351632009-02-20 20:59:54 +00001717//===----------------------------------------------------------------------===//
1718// ObjCCompatibleAliasDecl
1719//===----------------------------------------------------------------------===//
1720
David Blaikie99ba9e32011-12-20 02:48:34 +00001721void ObjCCompatibleAliasDecl::anchor() { }
1722
Chris Lattnerab351632009-02-20 20:59:54 +00001723ObjCCompatibleAliasDecl *
1724ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1725 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001726 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001727 ObjCInterfaceDecl* AliasedClass) {
1728 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1729}
1730
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001731ObjCCompatibleAliasDecl *
1732ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1733 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1734 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1735}
1736
Chris Lattnerab351632009-02-20 20:59:54 +00001737//===----------------------------------------------------------------------===//
1738// ObjCPropertyDecl
1739//===----------------------------------------------------------------------===//
1740
David Blaikie99ba9e32011-12-20 02:48:34 +00001741void ObjCPropertyDecl::anchor() { }
1742
Chris Lattnerab351632009-02-20 20:59:54 +00001743ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1744 SourceLocation L,
1745 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001746 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001747 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001748 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001749 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001750 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001751}
1752
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001753ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1754 unsigned ID) {
1755 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1756 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001757 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001758 0);
1759}
1760
Chris Lattnerab351632009-02-20 20:59:54 +00001761//===----------------------------------------------------------------------===//
1762// ObjCPropertyImplDecl
1763//===----------------------------------------------------------------------===//
1764
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001765ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001766 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001767 SourceLocation atLoc,
1768 SourceLocation L,
1769 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001770 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001771 ObjCIvarDecl *ivar,
1772 SourceLocation ivarLoc) {
1773 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1774 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001775}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001776
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001777ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1778 unsigned ID) {
1779 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1780 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1781 0, Dynamic, 0, SourceLocation());
1782}
1783
Douglas Gregora4ffd852010-11-17 01:03:52 +00001784SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1785 SourceLocation EndLoc = getLocation();
1786 if (IvarLoc.isValid())
1787 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001788
Douglas Gregora4ffd852010-11-17 01:03:52 +00001789 return SourceRange(AtLoc, EndLoc);
1790}