blob: c15f01debaf38bca9487c8192f76596c4634daf7 [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
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000444ObjCProtocolDecl *
445ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
446 for (ObjCInterfaceDecl::all_protocol_iterator P =
447 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
448 P != PE; ++P)
449 if ((*P)->lookupProtocolNamed(Name))
450 return (*P);
451 ObjCInterfaceDecl *SuperClass = getSuperClass();
452 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
453}
454
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000455/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000456/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000457/// When argument category "C" is specified, any implicit method found
458/// in this category is ignored.
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000459ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
460 bool isInstance,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000461 bool shallowCategoryLookup,
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000462 const ObjCCategoryDecl *C) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000463 // FIXME: Should make sure no callers ever do this.
464 if (!hasDefinition())
465 return 0;
466
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000467 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000468 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000470 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000471 LoadExternalDefinition();
472
Chris Lattner1e03a562008-03-16 00:19:01 +0000473 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000474 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000475 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner1e03a562008-03-16 00:19:01 +0000477 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000478 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
479 E = ClassDecl->protocol_end();
480 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000481 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000482 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000483
484 // Didn't find one yet - now look through categories.
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000485 for (ObjCInterfaceDecl::visible_categories_iterator
486 Cat = ClassDecl->visible_categories_begin(),
487 CatEnd = ClassDecl->visible_categories_end();
488 Cat != CatEnd; ++Cat) {
489 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
490 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000491 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000492
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000493 if (!shallowCategoryLookup) {
494 // Didn't find one yet - look through protocols.
495 const ObjCList<ObjCProtocolDecl> &Protocols =
496 Cat->getReferencedProtocols();
497 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
498 E = Protocols.end(); I != E; ++I)
499 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
500 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000501 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000502 }
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000503 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000504
Chris Lattner1e03a562008-03-16 00:19:01 +0000505 ClassDecl = ClassDecl->getSuperClass();
506 }
507 return NULL;
508}
509
Anna Zakse61354b2012-07-27 19:07:44 +0000510// Will search "local" class/category implementations for a method decl.
511// If failed, then we search in class's root for an instance method.
512// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000513ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
514 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000515 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000516 // FIXME: Should make sure no callers ever do this.
517 if (!hasDefinition())
518 return 0;
519
520 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000521 LoadExternalDefinition();
522
Steve Naroffd789d3d2009-10-01 23:46:04 +0000523 ObjCMethodDecl *Method = 0;
524 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000525 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
526 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000527
528 // Look through local category implementations associated with the class.
529 if (!Method)
530 Method = Instance ? getCategoryInstanceMethod(Sel)
531 : getCategoryClassMethod(Sel);
532
533 // Before we give up, check if the selector is an instance method.
534 // But only in the root. This matches gcc's behavior and what the
535 // runtime expects.
536 if (!Instance && !Method && !getSuperClass()) {
537 Method = lookupInstanceMethod(Sel);
538 // Look through local category implementations associated
539 // with the root class.
540 if (!Method)
541 Method = lookupPrivateMethod(Sel, true);
542 }
543
Steve Naroffd789d3d2009-10-01 23:46:04 +0000544 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000545 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000546 return Method;
547}
Chris Lattnerab351632009-02-20 20:59:54 +0000548
549//===----------------------------------------------------------------------===//
550// ObjCMethodDecl
551//===----------------------------------------------------------------------===//
552
553ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000554 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000555 SourceLocation endLoc,
556 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000557 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000558 DeclContext *contextDecl,
559 bool isInstance,
560 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000561 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000562 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000563 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000564 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000565 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000566 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000567 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000568 isInstance, isVariadic, isPropertyAccessor,
569 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000570 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000571 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000572}
573
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000574ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
575 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
576 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
577 Selector(), QualType(), 0, 0);
578}
579
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000580Stmt *ObjCMethodDecl::getBody() const {
581 return Body.get(getASTContext().getExternalSource());
582}
583
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000584void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
585 assert(PrevMethod);
586 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
587 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000588 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000589}
590
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000591void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
592 ArrayRef<ParmVarDecl*> Params,
593 ArrayRef<SourceLocation> SelLocs) {
594 ParamsAndSelLocs = 0;
595 NumParams = Params.size();
596 if (Params.empty() && SelLocs.empty())
597 return;
598
599 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
600 sizeof(SourceLocation) * SelLocs.size();
601 ParamsAndSelLocs = C.Allocate(Size);
602 std::copy(Params.begin(), Params.end(), getParams());
603 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
604}
605
606void ObjCMethodDecl::getSelectorLocs(
607 SmallVectorImpl<SourceLocation> &SelLocs) const {
608 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
609 SelLocs.push_back(getSelectorLoc(i));
610}
611
612void ObjCMethodDecl::setMethodParams(ASTContext &C,
613 ArrayRef<ParmVarDecl*> Params,
614 ArrayRef<SourceLocation> SelLocs) {
615 assert((!SelLocs.empty() || isImplicit()) &&
616 "No selector locs for non-implicit method");
617 if (isImplicit())
Dmitri Gribenko55431692013-05-05 00:41:58 +0000618 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000619
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000620 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
621 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000622 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko55431692013-05-05 00:41:58 +0000623 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000624
625 setParamsAndSelLocs(C, Params, SelLocs);
626}
627
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000628/// \brief A definition will return its interface declaration.
629/// An interface declaration will return its definition.
630/// Otherwise it will return itself.
631ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
632 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000633 ObjCMethodDecl *Redecl = 0;
634 if (HasRedeclaration)
635 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000636 if (Redecl)
637 return Redecl;
638
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000639 Decl *CtxD = cast<Decl>(getDeclContext());
640
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000641 if (!CtxD->isInvalidDecl()) {
642 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
643 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
644 if (!ImplD->isInvalidDecl())
645 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000646
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000647 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
648 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
649 if (!ImplD->isInvalidDecl())
650 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000651
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000652 } else if (ObjCImplementationDecl *ImplD =
653 dyn_cast<ObjCImplementationDecl>(CtxD)) {
654 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
655 if (!IFD->isInvalidDecl())
656 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000657
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000658 } else if (ObjCCategoryImplDecl *CImplD =
659 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
660 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
661 if (!CatD->isInvalidDecl())
662 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
663 }
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000664 }
665
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000666 if (!Redecl && isRedeclaration()) {
667 // This is the last redeclaration, go back to the first method.
668 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
669 isInstanceMethod());
670 }
671
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000672 return Redecl ? Redecl : this;
673}
674
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000675ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
676 Decl *CtxD = cast<Decl>(getDeclContext());
677
678 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
679 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
680 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
681 isInstanceMethod()))
682 return MD;
683
684 } else if (ObjCCategoryImplDecl *CImplD =
685 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000686 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000687 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
688 isInstanceMethod()))
689 return MD;
690 }
691
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000692 if (isRedeclaration())
693 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
694 isInstanceMethod());
695
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000696 return this;
697}
698
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000699SourceLocation ObjCMethodDecl::getLocEnd() const {
700 if (Stmt *Body = getBody())
701 return Body->getLocEnd();
702 return DeclEndLoc;
703}
704
John McCall85f3d762011-03-02 01:50:55 +0000705ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
706 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000707 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000708 return family;
709
John McCalld5313b02011-03-02 11:33:24 +0000710 // Check for an explicit attribute.
711 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
712 // The unfortunate necessity of mapping between enums here is due
713 // to the attributes framework.
714 switch (attr->getFamily()) {
715 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
716 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
717 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
718 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
719 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
720 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
721 }
722 Family = static_cast<unsigned>(family);
723 return family;
724 }
725
John McCall85f3d762011-03-02 01:50:55 +0000726 family = getSelector().getMethodFamily();
727 switch (family) {
728 case OMF_None: break;
729
730 // init only has a conventional meaning for an instance method, and
731 // it has to return an object.
732 case OMF_init:
733 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
734 family = OMF_None;
735 break;
736
737 // alloc/copy/new have a conventional meaning for both class and
738 // instance methods, but they require an object return.
739 case OMF_alloc:
740 case OMF_copy:
741 case OMF_mutableCopy:
742 case OMF_new:
743 if (!getResultType()->isObjCObjectPointerType())
744 family = OMF_None;
745 break;
746
747 // These selectors have a conventional meaning only for instance methods.
748 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000749 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000750 case OMF_retain:
751 case OMF_release:
752 case OMF_autorelease:
753 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000754 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000755 if (!isInstanceMethod())
756 family = OMF_None;
757 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000758
759 case OMF_performSelector:
760 if (!isInstanceMethod() ||
761 !getResultType()->isObjCIdType())
762 family = OMF_None;
763 else {
764 unsigned noParams = param_size();
765 if (noParams < 1 || noParams > 3)
766 family = OMF_None;
767 else {
768 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
769 QualType ArgT = (*it);
770 if (!ArgT->isObjCSelType()) {
771 family = OMF_None;
772 break;
773 }
774 while (--noParams) {
775 it++;
776 ArgT = (*it);
777 if (!ArgT->isObjCIdType()) {
778 family = OMF_None;
779 break;
780 }
781 }
782 }
783 }
784 break;
785
John McCall85f3d762011-03-02 01:50:55 +0000786 }
787
788 // Cache the result.
789 Family = static_cast<unsigned>(family);
790 return family;
791}
792
Mike Stump1eb44332009-09-09 15:08:12 +0000793void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000794 const ObjCInterfaceDecl *OID) {
795 QualType selfTy;
796 if (isInstanceMethod()) {
797 // There may be no interface context due to error in declaration
798 // of the interface (which has been reported). Recover gracefully.
799 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000800 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000801 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000802 } else {
803 selfTy = Context.getObjCIdType();
804 }
805 } else // we have a factory method.
806 selfTy = Context.getObjCClassType();
807
John McCall7acddac2011-06-17 06:42:21 +0000808 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000809 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000810
David Blaikie4e4d0842012-03-11 07:00:24 +0000811 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000812 if (isInstanceMethod()) {
813 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000814
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000815 // 'self' is always __strong. It's actually pseudo-strong except
816 // in init methods (or methods labeled ns_consumes_self), though.
817 Qualifiers qs;
818 qs.setObjCLifetime(Qualifiers::OCL_Strong);
819 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000820
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000821 // In addition, 'self' is const unless this is an init method.
822 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
823 selfTy = selfTy.withConst();
824 selfIsPseudoStrong = true;
825 }
826 }
827 else {
828 assert(isClassMethod());
829 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000830 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000831 selfIsPseudoStrong = true;
832 }
John McCallf85e1932011-06-15 23:02:42 +0000833 }
834
835 ImplicitParamDecl *self
836 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
837 &Context.Idents.get("self"), selfTy);
838 setSelfDecl(self);
839
840 if (selfIsConsumed)
841 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000842
John McCall7acddac2011-06-17 06:42:21 +0000843 if (selfIsPseudoStrong)
844 self->setARCPseudoStrong(true);
845
Mike Stump1eb44332009-09-09 15:08:12 +0000846 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
847 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000848 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000849}
850
Chris Lattnerab351632009-02-20 20:59:54 +0000851ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
852 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
853 return ID;
854 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
855 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000856 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000857 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000858
859 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000860 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000861}
862
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000863static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
864 const ObjCMethodDecl *Method,
865 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
866 bool MovedToSuper) {
867 if (!Container)
868 return;
869
870 // In categories look for overriden methods from protocols. A method from
871 // category is not "overriden" since it is considered as the "same" method
872 // (same USR) as the one from the interface.
873 if (const ObjCCategoryDecl *
874 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
875 // Check whether we have a matching method at this category but only if we
876 // are at the super class level.
877 if (MovedToSuper)
878 if (ObjCMethodDecl *
879 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000880 Method->isInstanceMethod(),
881 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000882 if (Method != Overridden) {
883 // We found an override at this category; there is no need to look
884 // into its protocols.
885 Methods.push_back(Overridden);
886 return;
887 }
888
889 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
890 PEnd = Category->protocol_end();
891 P != PEnd; ++P)
892 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
893 return;
894 }
895
896 // Check whether we have a matching method at this level.
897 if (const ObjCMethodDecl *
898 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000899 Method->isInstanceMethod(),
900 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000901 if (Method != Overridden) {
902 // We found an override at this level; there is no need to look
903 // into other protocols or categories.
904 Methods.push_back(Overridden);
905 return;
906 }
907
908 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
909 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
910 PEnd = Protocol->protocol_end();
911 P != PEnd; ++P)
912 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
913 }
914
915 if (const ObjCInterfaceDecl *
916 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
917 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
918 PEnd = Interface->protocol_end();
919 P != PEnd; ++P)
920 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
921
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000922 for (ObjCInterfaceDecl::known_categories_iterator
923 Cat = Interface->known_categories_begin(),
924 CatEnd = Interface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +0000925 Cat != CatEnd; ++Cat) {
926 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000927 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000928 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000929
930 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
931 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
932 /*MovedToSuper=*/true);
933 }
934}
935
936static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
937 const ObjCMethodDecl *Method,
938 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
939 CollectOverriddenMethodsRecurse(Container, Method, Methods,
940 /*MovedToSuper=*/false);
941}
942
943static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
944 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
945 assert(Method->isOverriding());
946
947 if (const ObjCProtocolDecl *
948 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
949 CollectOverriddenMethods(ProtD, Method, overridden);
950
951 } else if (const ObjCImplDecl *
952 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
953 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
954 if (!ID)
955 return;
956 // Start searching for overridden methods using the method from the
957 // interface as starting point.
958 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000959 Method->isInstanceMethod(),
960 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000961 Method = IFaceMeth;
962 CollectOverriddenMethods(ID, Method, overridden);
963
964 } else if (const ObjCCategoryDecl *
965 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
966 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
967 if (!ID)
968 return;
969 // Start searching for overridden methods using the method from the
970 // interface as starting point.
971 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000972 Method->isInstanceMethod(),
973 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000974 Method = IFaceMeth;
975 CollectOverriddenMethods(ID, Method, overridden);
976
977 } else {
978 CollectOverriddenMethods(
979 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
980 Method, overridden);
981 }
982}
983
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000984void ObjCMethodDecl::getOverriddenMethods(
985 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
986 const ObjCMethodDecl *Method = this;
987
988 if (Method->isRedeclaration()) {
989 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
990 getMethod(Method->getSelector(), Method->isInstanceMethod());
991 }
992
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +0000993 if (Method->isOverriding()) {
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000994 collectOverriddenMethodsSlow(Method, Overridden);
995 assert(!Overridden.empty() &&
996 "ObjCMethodDecl's overriding bit is not as expected");
997 }
998}
999
Jordan Rose04bec392012-10-10 16:42:54 +00001000const ObjCPropertyDecl *
1001ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1002 Selector Sel = getSelector();
1003 unsigned NumArgs = Sel.getNumArgs();
1004 if (NumArgs > 1)
1005 return 0;
1006
Jordan Rose50d2b262012-10-11 16:02:02 +00001007 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +00001008 return 0;
1009
1010 if (isPropertyAccessor()) {
1011 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +00001012 // If container is class extension, find its primary class.
1013 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1014 if (CatDecl->IsClassExtension())
1015 Container = CatDecl->getClassInterface();
1016
Jordan Rose04bec392012-10-10 16:42:54 +00001017 bool IsGetter = (NumArgs == 0);
1018
1019 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1020 E = Container->prop_end();
1021 I != E; ++I) {
1022 Selector NextSel = IsGetter ? (*I)->getGetterName()
1023 : (*I)->getSetterName();
1024 if (NextSel == Sel)
1025 return *I;
1026 }
1027
1028 llvm_unreachable("Marked as a property accessor but no property found!");
1029 }
1030
1031 if (!CheckOverrides)
1032 return 0;
1033
1034 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1035 OverridesTy Overrides;
1036 getOverriddenMethods(Overrides);
1037 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1038 I != E; ++I) {
1039 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1040 return Prop;
1041 }
1042
1043 return 0;
1044
1045}
1046
Chris Lattnerab351632009-02-20 20:59:54 +00001047//===----------------------------------------------------------------------===//
1048// ObjCInterfaceDecl
1049//===----------------------------------------------------------------------===//
1050
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001051ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001052 DeclContext *DC,
1053 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001054 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001055 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001056 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001057 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +00001058 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001059 PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001060 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001061 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001062 return Result;
1063}
1064
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001065ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1066 unsigned ID) {
1067 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001068 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1069 0, SourceLocation(),
1070 0, false);
1071 Result->Data.setInt(!C.getLangOpts().Modules);
1072 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001073}
1074
1075ObjCInterfaceDecl::
1076ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001077 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1078 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001079 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001080 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001081{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001082 setPreviousDeclaration(PrevDecl);
1083
1084 // Copy the 'data' pointer over.
1085 if (PrevDecl)
1086 Data = PrevDecl->Data;
1087
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001088 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001089}
1090
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001091void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001092 assert(data().ExternallyCompleted && "Class is not externally completed");
1093 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001094 getASTContext().getExternalSource()->CompleteType(
1095 const_cast<ObjCInterfaceDecl *>(this));
1096}
1097
1098void ObjCInterfaceDecl::setExternallyCompleted() {
1099 assert(getASTContext().getExternalSource() &&
1100 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001101 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001102 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001103 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001104}
1105
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001106ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001107 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1108 if (data().ExternallyCompleted)
1109 LoadExternalDefinition();
1110
1111 return getASTContext().getObjCImplementation(
1112 const_cast<ObjCInterfaceDecl*>(Def));
1113 }
1114
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001115 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001116 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001117}
1118
1119void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001120 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001121}
1122
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001123namespace {
1124 struct SynthesizeIvarChunk {
1125 uint64_t Size;
1126 ObjCIvarDecl *Ivar;
1127 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1128 : Size(size), Ivar(ivar) {}
1129 };
1130
1131 bool operator<(const SynthesizeIvarChunk & LHS,
1132 const SynthesizeIvarChunk &RHS) {
1133 return LHS.Size < RHS.Size;
1134 }
1135}
1136
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001137/// all_declared_ivar_begin - return first ivar declared in this class,
1138/// its extensions and its implementation. Lazily build the list on first
1139/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001140///
1141/// Caveat: The list returned by this method reflects the current
1142/// state of the parser. The cache will be updated for every ivar
1143/// added by an extension or the implementation when they are
1144/// encountered.
1145/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001146ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001147 // FIXME: Should make sure no callers ever do this.
1148 if (!hasDefinition())
1149 return 0;
1150
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001151 ObjCIvarDecl *curIvar = 0;
Adrian Prantl4919de62013-03-06 22:03:30 +00001152 if (!data().IvarList) {
1153 if (!ivar_empty()) {
1154 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1155 data().IvarList = *I; ++I;
1156 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001157 curIvar->setNextIvar(*I);
1158 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001159
1160 for (ObjCInterfaceDecl::known_extensions_iterator
1161 Ext = known_extensions_begin(),
1162 ExtEnd = known_extensions_end();
1163 Ext != ExtEnd; ++Ext) {
1164 if (!Ext->ivar_empty()) {
1165 ObjCCategoryDecl::ivar_iterator
1166 I = Ext->ivar_begin(),
1167 E = Ext->ivar_end();
1168 if (!data().IvarList) {
1169 data().IvarList = *I; ++I;
1170 curIvar = data().IvarList;
1171 }
1172 for ( ;I != E; curIvar = *I, ++I)
1173 curIvar->setNextIvar(*I);
1174 }
1175 }
1176 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001177 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001178
1179 // cached and complete!
1180 if (!data().IvarListMissingImplementation)
1181 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001182
1183 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001184 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001185 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001186 SmallVector<SynthesizeIvarChunk, 16> layout;
1187 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1188 E = ImplDecl->ivar_end(); I != E; ++I) {
1189 ObjCIvarDecl *IV = *I;
1190 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1191 layout.push_back(SynthesizeIvarChunk(
1192 IV->getASTContext().getTypeSize(IV->getType()), IV));
1193 continue;
1194 }
1195 if (!data().IvarList)
1196 data().IvarList = *I;
1197 else
1198 curIvar->setNextIvar(*I);
1199 curIvar = *I;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001200 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001201
1202 if (!layout.empty()) {
1203 // Order synthesized ivars by their size.
1204 std::stable_sort(layout.begin(), layout.end());
1205 unsigned Ix = 0, EIx = layout.size();
1206 if (!data().IvarList) {
1207 data().IvarList = layout[0].Ivar; Ix++;
1208 curIvar = data().IvarList;
1209 }
1210 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1211 curIvar->setNextIvar(layout[Ix].Ivar);
1212 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001213 }
1214 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001215 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001216}
Chris Lattnerab351632009-02-20 20:59:54 +00001217
1218/// FindCategoryDeclaration - Finds category declaration in the list of
1219/// categories for this class and returns it. Name of the category is passed
1220/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001221///
Chris Lattnerab351632009-02-20 20:59:54 +00001222ObjCCategoryDecl *
1223ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001224 // FIXME: Should make sure no callers ever do this.
1225 if (!hasDefinition())
1226 return 0;
1227
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001228 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001229 LoadExternalDefinition();
1230
Douglas Gregord3297242013-01-16 23:00:23 +00001231 for (visible_categories_iterator Cat = visible_categories_begin(),
1232 CatEnd = visible_categories_end();
1233 Cat != CatEnd;
1234 ++Cat) {
1235 if (Cat->getIdentifier() == CategoryId)
1236 return *Cat;
1237 }
1238
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001239 return 0;
1240}
1241
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001242ObjCMethodDecl *
1243ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001244 for (visible_categories_iterator Cat = visible_categories_begin(),
1245 CatEnd = visible_categories_end();
1246 Cat != CatEnd;
1247 ++Cat) {
1248 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001249 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1250 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001251 }
1252
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001253 return 0;
1254}
1255
1256ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001257 for (visible_categories_iterator Cat = visible_categories_begin(),
1258 CatEnd = visible_categories_end();
1259 Cat != CatEnd;
1260 ++Cat) {
1261 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001262 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1263 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001264 }
1265
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001266 return 0;
1267}
1268
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001269/// ClassImplementsProtocol - Checks that 'lProto' protocol
1270/// has been implemented in IDecl class, its super class or categories (if
1271/// lookupCategory is true).
1272bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1273 bool lookupCategory,
1274 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001275 if (!hasDefinition())
1276 return false;
1277
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001278 ObjCInterfaceDecl *IDecl = this;
1279 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001280 for (ObjCInterfaceDecl::protocol_iterator
1281 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001282 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1283 return true;
1284 // This is dubious and is added to be compatible with gcc. In gcc, it is
1285 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1286 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1287 // object. This IMO, should be a bug.
1288 // FIXME: Treat this as an extension, and flag this as an error when GCC
1289 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001290 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001291 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1292 return true;
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001295 // 2nd, look up the category.
1296 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001297 for (visible_categories_iterator Cat = visible_categories_begin(),
1298 CatEnd = visible_categories_end();
1299 Cat != CatEnd;
1300 ++Cat) {
1301 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1302 E = Cat->protocol_end();
1303 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001304 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1305 return true;
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001308 // 3rd, look up the super class(s)
1309 if (IDecl->getSuperClass())
1310 return
1311 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1312 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001314 return false;
1315}
1316
Chris Lattnerab351632009-02-20 20:59:54 +00001317//===----------------------------------------------------------------------===//
1318// ObjCIvarDecl
1319//===----------------------------------------------------------------------===//
1320
David Blaikie99ba9e32011-12-20 02:48:34 +00001321void ObjCIvarDecl::anchor() { }
1322
Daniel Dunbara0654922010-04-02 20:10:03 +00001323ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001324 SourceLocation StartLoc,
1325 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001326 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001327 AccessControl ac, Expr *BW,
1328 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001329 if (DC) {
1330 // Ivar's can only appear in interfaces, implementations (via synthesized
1331 // properties), and class extensions (via direct declaration, or synthesized
1332 // properties).
1333 //
1334 // FIXME: This should really be asserting this:
1335 // (isa<ObjCCategoryDecl>(DC) &&
1336 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1337 // but unfortunately we sometimes place ivars into non-class extension
1338 // categories on error. This breaks an AST invariant, and should not be
1339 // fixed.
1340 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1341 isa<ObjCCategoryDecl>(DC)) &&
1342 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001343 // Once a new ivar is created in any of class/class-extension/implementation
1344 // decl contexts, the previously built IvarList must be rebuilt.
1345 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1346 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001347 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001348 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001349 else
1350 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001351 }
1352 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001353 }
1354
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001355 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1356 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001357}
1358
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001359ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1360 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1361 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1362 QualType(), 0, ObjCIvarDecl::None, 0, false);
1363}
1364
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001365const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1366 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001367
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001368 switch (DC->getKind()) {
1369 default:
1370 case ObjCCategoryImpl:
1371 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001372 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001373
1374 // Ivars can only appear in class extension categories.
1375 case ObjCCategory: {
1376 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1377 assert(CD->IsClassExtension() && "invalid container for ivar!");
1378 return CD->getClassInterface();
1379 }
1380
1381 case ObjCImplementation:
1382 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1383
1384 case ObjCInterface:
1385 return cast<ObjCInterfaceDecl>(DC);
1386 }
1387}
Chris Lattnerab351632009-02-20 20:59:54 +00001388
1389//===----------------------------------------------------------------------===//
1390// ObjCAtDefsFieldDecl
1391//===----------------------------------------------------------------------===//
1392
David Blaikie99ba9e32011-12-20 02:48:34 +00001393void ObjCAtDefsFieldDecl::anchor() { }
1394
Chris Lattnerab351632009-02-20 20:59:54 +00001395ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001396*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1397 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001398 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001399 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001400}
1401
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001402ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1403 unsigned ID) {
1404 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1405 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1406 0, QualType(), 0);
1407}
1408
Chris Lattnerab351632009-02-20 20:59:54 +00001409//===----------------------------------------------------------------------===//
1410// ObjCProtocolDecl
1411//===----------------------------------------------------------------------===//
1412
David Blaikie99ba9e32011-12-20 02:48:34 +00001413void ObjCProtocolDecl::anchor() { }
1414
Douglas Gregor27c6da22012-01-01 20:30:41 +00001415ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1416 SourceLocation nameLoc,
1417 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001418 ObjCProtocolDecl *PrevDecl)
1419 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001420{
1421 setPreviousDeclaration(PrevDecl);
1422 if (PrevDecl)
1423 Data = PrevDecl->Data;
1424}
1425
Chris Lattnerab351632009-02-20 20:59:54 +00001426ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001427 IdentifierInfo *Id,
1428 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001429 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001430 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001431 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001432 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001433 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001434 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001435}
1436
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001437ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1438 unsigned ID) {
1439 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001440 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1441 SourceLocation(), 0);
1442 Result->Data.setInt(!C.getLangOpts().Modules);
1443 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001444}
1445
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001446ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1447 ObjCProtocolDecl *PDecl = this;
1448
1449 if (Name == getIdentifier())
1450 return PDecl;
1451
1452 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1453 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1454 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001456 return NULL;
1457}
1458
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001459// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001460// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001461ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1462 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001463 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001465 // If there is no definition or the definition is hidden, we don't find
1466 // anything.
1467 const ObjCProtocolDecl *Def = getDefinition();
1468 if (!Def || Def->isHidden())
1469 return NULL;
1470
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001471 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001472 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattnerab351632009-02-20 20:59:54 +00001474 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001475 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001476 return MethodDecl;
1477 return NULL;
1478}
1479
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001480void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001481 assert(!Data.getPointer() && "Protocol already has a definition!");
1482 Data.setPointer(new (getASTContext()) DefinitionData);
1483 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001484}
1485
1486void ObjCProtocolDecl::startDefinition() {
1487 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001488
1489 // Update all of the declarations with a pointer to the definition.
1490 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1491 RD != RDEnd; ++RD)
1492 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001493}
1494
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001495void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1496 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001497
1498 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1499 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1500 E = PDecl->prop_end(); P != E; ++P) {
1501 ObjCPropertyDecl *Prop = *P;
1502 // Insert into PM if not there already.
1503 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001504 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001505 }
1506 // Scan through protocol's protocols.
1507 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1508 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001509 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001510 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001511}
1512
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001513
1514void ObjCProtocolDecl::collectInheritedProtocolProperties(
1515 const ObjCPropertyDecl *Property,
1516 ProtocolPropertyMap &PM) const {
1517 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1518 bool MatchFound = false;
1519 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1520 E = PDecl->prop_end(); P != E; ++P) {
1521 ObjCPropertyDecl *Prop = *P;
1522 if (Prop == Property)
1523 continue;
1524 if (Prop->getIdentifier() == Property->getIdentifier()) {
1525 PM[PDecl] = Prop;
1526 MatchFound = true;
1527 break;
1528 }
1529 }
1530 // Scan through protocol's protocols which did not have a matching property.
1531 if (!MatchFound)
1532 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1533 E = PDecl->protocol_end(); PI != E; ++PI)
1534 (*PI)->collectInheritedProtocolProperties(Property, PM);
1535 }
1536}
Anna Zaksb36ea372012-10-18 19:17:53 +00001537
Chris Lattnerab351632009-02-20 20:59:54 +00001538//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001539// ObjCCategoryDecl
1540//===----------------------------------------------------------------------===//
1541
David Blaikie99ba9e32011-12-20 02:48:34 +00001542void ObjCCategoryDecl::anchor() { }
1543
Chris Lattnerab351632009-02-20 20:59:54 +00001544ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001545 SourceLocation AtLoc,
1546 SourceLocation ClassNameLoc,
1547 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001548 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001549 ObjCInterfaceDecl *IDecl,
1550 SourceLocation IvarLBraceLoc,
1551 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001552 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1553 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001554 IDecl,
1555 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001556 if (IDecl) {
1557 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001558 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001559 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001560 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001561 if (ASTMutationListener *L = C.getASTMutationListener())
1562 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1563 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001564 }
1565
1566 return CatDecl;
1567}
1568
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001569ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1570 unsigned ID) {
1571 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1572 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1573 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001574}
1575
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001576ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1577 return getASTContext().getObjCImplementation(
1578 const_cast<ObjCCategoryDecl*>(this));
1579}
1580
1581void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1582 getASTContext().setObjCImplementation(this, ImplD);
1583}
1584
1585
Chris Lattnerab351632009-02-20 20:59:54 +00001586//===----------------------------------------------------------------------===//
1587// ObjCCategoryImplDecl
1588//===----------------------------------------------------------------------===//
1589
David Blaikie99ba9e32011-12-20 02:48:34 +00001590void ObjCCategoryImplDecl::anchor() { }
1591
Chris Lattnerab351632009-02-20 20:59:54 +00001592ObjCCategoryImplDecl *
1593ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001594 IdentifierInfo *Id,
1595 ObjCInterfaceDecl *ClassInterface,
1596 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001597 SourceLocation atStartLoc,
1598 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001599 if (ClassInterface && ClassInterface->hasDefinition())
1600 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001601 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001602 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001603}
1604
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001605ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1606 unsigned ID) {
1607 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1608 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1609 SourceLocation(), SourceLocation());
1610}
1611
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001612ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001613 // The class interface might be NULL if we are working with invalid code.
1614 if (const ObjCInterfaceDecl *ID = getClassInterface())
1615 return ID->FindCategoryDeclaration(getIdentifier());
1616 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001617}
1618
Chris Lattnerab351632009-02-20 20:59:54 +00001619
David Blaikie99ba9e32011-12-20 02:48:34 +00001620void ObjCImplDecl::anchor() { }
1621
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001622void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001623 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001624 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001625 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001626}
1627
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001628void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1629 ASTContext &Ctx = getASTContext();
1630
1631 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001632 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001633 if (IFace)
1634 Ctx.setObjCImplementation(IFace, ImplD);
1635
Duncan Sands98f2cca2009-07-21 07:56:29 +00001636 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001637 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1638 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1639 Ctx.setObjCImplementation(CD, ImplD);
1640 }
1641
1642 ClassInterface = IFace;
1643}
1644
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001645/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001646/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001647/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001648///
Chris Lattner3aa18612009-02-28 18:42:10 +00001649ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001650FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1651 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001652 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001653 if (PID->getPropertyIvarDecl() &&
1654 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1655 return PID;
1656 }
1657 return 0;
1658}
1659
1660/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001661/// added to the list of those properties \@synthesized/\@dynamic in this
1662/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001663///
Chris Lattner3aa18612009-02-28 18:42:10 +00001664ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001665FindPropertyImplDecl(IdentifierInfo *Id) const {
1666 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001667 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001668 if (PID->getPropertyDecl()->getIdentifier() == Id)
1669 return PID;
1670 }
1671 return 0;
1672}
1673
Chris Lattner5f9e2722011-07-23 10:55:15 +00001674raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001675 const ObjCCategoryImplDecl &CID) {
1676 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001677 return OS;
1678}
1679
Chris Lattnerab351632009-02-20 20:59:54 +00001680//===----------------------------------------------------------------------===//
1681// ObjCImplementationDecl
1682//===----------------------------------------------------------------------===//
1683
David Blaikie99ba9e32011-12-20 02:48:34 +00001684void ObjCImplementationDecl::anchor() { }
1685
Chris Lattnerab351632009-02-20 20:59:54 +00001686ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001687ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001688 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001689 ObjCInterfaceDecl *SuperDecl,
1690 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001691 SourceLocation atStartLoc,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001692 SourceLocation superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001693 SourceLocation IvarLBraceLoc,
1694 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001695 if (ClassInterface && ClassInterface->hasDefinition())
1696 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001697 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001698 nameLoc, atStartLoc, superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001699 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001700}
1701
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001702ObjCImplementationDecl *
1703ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1704 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1705 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1706 SourceLocation());
1707}
1708
John McCallda6d9762011-07-22 04:15:06 +00001709void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1710 CXXCtorInitializer ** initializers,
1711 unsigned numInitializers) {
1712 if (numInitializers > 0) {
1713 NumIvarInitializers = numInitializers;
1714 CXXCtorInitializer **ivarInitializers =
1715 new (C) CXXCtorInitializer*[NumIvarInitializers];
1716 memcpy(ivarInitializers, initializers,
1717 numInitializers * sizeof(CXXCtorInitializer*));
1718 IvarInitializers = ivarInitializers;
1719 }
1720}
1721
Chris Lattner5f9e2722011-07-23 10:55:15 +00001722raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001723 const ObjCImplementationDecl &ID) {
1724 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001725 return OS;
1726}
1727
Chris Lattnerab351632009-02-20 20:59:54 +00001728//===----------------------------------------------------------------------===//
1729// ObjCCompatibleAliasDecl
1730//===----------------------------------------------------------------------===//
1731
David Blaikie99ba9e32011-12-20 02:48:34 +00001732void ObjCCompatibleAliasDecl::anchor() { }
1733
Chris Lattnerab351632009-02-20 20:59:54 +00001734ObjCCompatibleAliasDecl *
1735ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1736 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001737 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001738 ObjCInterfaceDecl* AliasedClass) {
1739 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1740}
1741
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001742ObjCCompatibleAliasDecl *
1743ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1744 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1745 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1746}
1747
Chris Lattnerab351632009-02-20 20:59:54 +00001748//===----------------------------------------------------------------------===//
1749// ObjCPropertyDecl
1750//===----------------------------------------------------------------------===//
1751
David Blaikie99ba9e32011-12-20 02:48:34 +00001752void ObjCPropertyDecl::anchor() { }
1753
Chris Lattnerab351632009-02-20 20:59:54 +00001754ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1755 SourceLocation L,
1756 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001757 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001758 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001759 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001760 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001761 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001762}
1763
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001764ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1765 unsigned ID) {
1766 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1767 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001768 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001769 0);
1770}
1771
Chris Lattnerab351632009-02-20 20:59:54 +00001772//===----------------------------------------------------------------------===//
1773// ObjCPropertyImplDecl
1774//===----------------------------------------------------------------------===//
1775
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001776ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001777 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001778 SourceLocation atLoc,
1779 SourceLocation L,
1780 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001781 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001782 ObjCIvarDecl *ivar,
1783 SourceLocation ivarLoc) {
1784 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1785 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001786}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001787
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001788ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1789 unsigned ID) {
1790 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1791 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1792 0, Dynamic, 0, SourceLocation());
1793}
1794
Douglas Gregora4ffd852010-11-17 01:03:52 +00001795SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1796 SourceLocation EndLoc = getLocation();
1797 if (IvarLoc.isValid())
1798 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001799
Douglas Gregora4ffd852010-11-17 01:03:52 +00001800 return SourceRange(AtLoc, EndLoc);
1801}