blob: d759c0795b8f3aa8dcb46eac6c1df98c933c5fd3 [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorb412e172010-07-25 18:17:45 +000028 List = 0;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-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 Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
David Blaikieff7d47a2012-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 Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-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 Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Douglas Gregoreed49792013-01-17 00:38:46 +000075 return 0;
76 }
77
Steve Naroffc4173fa2009-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 Blaikieff7d47a2012-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 Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000093 return 0;
94}
95
Fariborz Jahanian1446b342013-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 Kremenek4fb821e2010-03-15 20:11:46 +0000162ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000163ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000164 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-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 Kremenek4fb821e2010-03-15 20:11:46 +0000172
David Blaikieff7d47a2012-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 Kremenek4fb821e2010-03-15 20:11:46 +0000176 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
177 return PD;
178
179 return 0;
180}
181
Anna Zaks454477c2012-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 Jahaniana054e992008-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 Jahaniana054e992008-04-21 19:04:53 +0000194ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000195ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-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 Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenekddcd1092010-03-15 20:11:53 +0000203 if (ObjCPropertyDecl *PD =
204 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
205 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Ted Kremenekddcd1092010-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 Jahanian30a42922010-02-15 21:55:26 +0000214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216 break;
217 }
218 case Decl::ObjCInterface: {
219 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-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 Kremenekddcd1092010-03-15 20:11:53 +0000225 if (!Cat->IsClassExtension())
226 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
227 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000228 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000229
230 // Look through protocols.
Ted Kremenek0ef508d2010-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 Kremenekddcd1092010-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 Jahaniandab04842009-01-19 18:16:19 +0000252 }
253 }
Steve Narofff9c65242008-06-05 13:55:23 +0000254 return 0;
255}
256
David Blaikie68e081d2011-12-20 02:48:34 +0000257void ObjCInterfaceDecl::anchor() { }
258
Fariborz Jahaniande8db162009-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 Kremenekd133a862010-03-15 20:30:07 +0000261/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262///
263ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000264ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000265 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-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 Gregor73693022010-12-01 23:49:52 +0000271 LoadExternalDefinition();
272
Ted Kremenekd133a862010-03-15 20:30:07 +0000273 if (ObjCPropertyDecl *PD =
274 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
275 return PD;
276
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000277 // Look through protocols.
Ted Kremenek0ef508d2010-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 Jahaniande8db162009-11-02 22:45:15 +0000281 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
282 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000283
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000284 return 0;
285}
286
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000287void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
288 PropertyDeclOrder &PO) const {
Anna Zaks673d76b2012-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 Jahanianaedaaa42013-02-14 22:33:34 +0000293 PO.push_back(Prop);
Anna Zaks673d76b2012-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 Jahanianaedaaa42013-02-14 22:33:34 +0000298 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-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 Zaks673d76b2012-10-18 19:17:53 +0000302}
303
Benjamin Kramerea70eb32012-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 Jahanian092cd6e2009-10-05 20:41:32 +0000324void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
325 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
326 ASTContext &C)
327{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000328 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000329 LoadExternalDefinition();
330
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000331 if (data().AllReferencedProtocols.empty() &&
332 data().ReferencedProtocols.empty()) {
333 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 return;
335 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000336
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000337 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000338 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000339 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000340 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341 for (unsigned i = 0; i < ExtNum; i++) {
342 bool protocolExists = false;
343 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek0ef508d2010-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 Jahanian092cd6e2009-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 Kremenek0ef508d2010-09-01 01:21:15 +0000355 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000356 ProtocolRefs.push_back(ProtoInExtension);
357 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000358
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000359 if (ProtocolRefs.empty())
360 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000361
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000362 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-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 Jahanian092cd6e2009-10-05 20:41:32 +0000365 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000366 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000367
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000368 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000369}
370
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000371void ObjCInterfaceDecl::allocateDefinitionData() {
372 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000373 Data.setPointer(new (getASTContext()) DefinitionData());
374 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-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 Gregorab1ec82e2011-12-16 03:12:41 +0000379}
380
381void ObjCInterfaceDecl::startDefinition() {
382 allocateDefinitionData();
383
Douglas Gregor66b310c2011-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 Gregora323c4c2011-12-15 18:17:27 +0000388 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000389 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000390}
391
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000392ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
393 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-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 Kyrtzidis4e8b1362011-10-19 02:25:16 +0000399 LoadExternalDefinition();
400
Chris Lattner89375192008-03-16 00:19:01 +0000401 ObjCInterfaceDecl* ClassDecl = this;
402 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000403 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000404 clsDeclared = ClassDecl;
405 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000406 }
Douglas Gregor048fbfa2013-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 Jahanianafe13862010-02-23 01:26:30 +0000413 clsDeclared = ClassDecl;
414 return I;
415 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000416 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000417
Chris Lattner89375192008-03-16 00:19:01 +0000418 ClassDecl = ClassDecl->getSuperClass();
419 }
420 return NULL;
421}
422
Fariborz Jahaniandb3a4c12009-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 Gregorc0ac7d62011-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 Kyrtzidis4e8b1362011-10-19 02:25:16 +0000433 LoadExternalDefinition();
434
Fariborz Jahaniandb3a4c12009-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 Jahanian56f48d02013-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 Kyrtzidis553376b2009-07-25 22:15:51 +0000455/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000456/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000457/// When argument category "C" is specified, any implicit method found
458/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000459ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremeneka4bd9a02013-11-21 07:20:42 +0000460 bool isInstance,
461 bool shallowCategoryLookup,
462 const ObjCCategoryDecl *C,
463 const ObjCProtocolDecl *P) const
464{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000465 // FIXME: Should make sure no callers ever do this.
466 if (!hasDefinition())
467 return 0;
468
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000469 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000470 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000471
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000472 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000473 LoadExternalDefinition();
474
Ted Kremeneka4bd9a02013-11-21 07:20:42 +0000475 while (ClassDecl) {
476 // If we are looking for a method that is part of protocol conformance,
477 // check if the class has been marked to suppress conformance
478 // of that protocol.
479 if (P && ClassDecl->hasAttrs()) {
480 const AttrVec &V = ClassDecl->getAttrs();
481 const IdentifierInfo *PI = P->getIdentifier();
482 for (AttrVec::const_iterator I = V.begin(), E = V.end(); I != E; ++I) {
483 if (const ObjCSuppressProtocolAttr *A =
484 dyn_cast<ObjCSuppressProtocolAttr>(*I)){
485 if (A->getProtocol() == PI) {
486 return 0;
487 }
488 }
489 }
490 }
491
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000492 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000493 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattner89375192008-03-16 00:19:01 +0000495 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000496 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
497 E = ClassDecl->protocol_end();
498 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000499 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000500 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000501
502 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000503 for (ObjCInterfaceDecl::visible_categories_iterator
504 Cat = ClassDecl->visible_categories_begin(),
505 CatEnd = ClassDecl->visible_categories_end();
506 Cat != CatEnd; ++Cat) {
507 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
508 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000509 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000510
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000511 if (!shallowCategoryLookup) {
512 // Didn't find one yet - look through protocols.
513 const ObjCList<ObjCProtocolDecl> &Protocols =
514 Cat->getReferencedProtocols();
515 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
516 E = Protocols.end(); I != E; ++I)
517 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
518 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000519 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000520 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000521 }
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000522
Chris Lattner89375192008-03-16 00:19:01 +0000523 ClassDecl = ClassDecl->getSuperClass();
524 }
525 return NULL;
526}
527
Anna Zaksc77a3b12012-07-27 19:07:44 +0000528// Will search "local" class/category implementations for a method decl.
529// If failed, then we search in class's root for an instance method.
530// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000531ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
532 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000533 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000534 // FIXME: Should make sure no callers ever do this.
535 if (!hasDefinition())
536 return 0;
537
538 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000539 LoadExternalDefinition();
540
Steve Naroffbb69c942009-10-01 23:46:04 +0000541 ObjCMethodDecl *Method = 0;
542 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000543 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
544 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000545
546 // Look through local category implementations associated with the class.
547 if (!Method)
548 Method = Instance ? getCategoryInstanceMethod(Sel)
549 : getCategoryClassMethod(Sel);
550
551 // Before we give up, check if the selector is an instance method.
552 // But only in the root. This matches gcc's behavior and what the
553 // runtime expects.
554 if (!Instance && !Method && !getSuperClass()) {
555 Method = lookupInstanceMethod(Sel);
556 // Look through local category implementations associated
557 // with the root class.
558 if (!Method)
559 Method = lookupPrivateMethod(Sel, true);
560 }
561
Steve Naroffbb69c942009-10-01 23:46:04 +0000562 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000563 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000564 return Method;
565}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000566
567//===----------------------------------------------------------------------===//
568// ObjCMethodDecl
569//===----------------------------------------------------------------------===//
570
571ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000572 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000573 SourceLocation endLoc,
574 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000575 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000576 DeclContext *contextDecl,
577 bool isInstance,
578 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000579 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000580 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000581 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000582 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000583 bool HasRelatedResultType) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000584 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000585 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000586 isInstance, isVariadic, isPropertyAccessor,
587 isImplicitlyDeclared, isDefined,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000588 impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000589 HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000590}
591
Douglas Gregor72172e92012-01-05 21:55:30 +0000592ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
593 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
594 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
595 Selector(), QualType(), 0, 0);
596}
597
Douglas Gregora6017bb2012-10-09 17:21:28 +0000598Stmt *ObjCMethodDecl::getBody() const {
599 return Body.get(getASTContext().getExternalSource());
600}
601
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000602void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
603 assert(PrevMethod);
604 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
605 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000606 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000607}
608
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000609void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
610 ArrayRef<ParmVarDecl*> Params,
611 ArrayRef<SourceLocation> SelLocs) {
612 ParamsAndSelLocs = 0;
613 NumParams = Params.size();
614 if (Params.empty() && SelLocs.empty())
615 return;
616
617 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
618 sizeof(SourceLocation) * SelLocs.size();
619 ParamsAndSelLocs = C.Allocate(Size);
620 std::copy(Params.begin(), Params.end(), getParams());
621 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
622}
623
624void ObjCMethodDecl::getSelectorLocs(
625 SmallVectorImpl<SourceLocation> &SelLocs) const {
626 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
627 SelLocs.push_back(getSelectorLoc(i));
628}
629
630void ObjCMethodDecl::setMethodParams(ASTContext &C,
631 ArrayRef<ParmVarDecl*> Params,
632 ArrayRef<SourceLocation> SelLocs) {
633 assert((!SelLocs.empty() || isImplicit()) &&
634 "No selector locs for non-implicit method");
635 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000636 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000637
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000638 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
639 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000640 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000641 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000642
643 setParamsAndSelLocs(C, Params, SelLocs);
644}
645
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000646/// \brief A definition will return its interface declaration.
647/// An interface declaration will return its definition.
648/// Otherwise it will return itself.
649ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
650 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000651 ObjCMethodDecl *Redecl = 0;
652 if (HasRedeclaration)
653 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000654 if (Redecl)
655 return Redecl;
656
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000657 Decl *CtxD = cast<Decl>(getDeclContext());
658
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000659 if (!CtxD->isInvalidDecl()) {
660 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
661 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
662 if (!ImplD->isInvalidDecl())
663 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000664
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000665 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
666 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
667 if (!ImplD->isInvalidDecl())
668 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000669
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000670 } else if (ObjCImplementationDecl *ImplD =
671 dyn_cast<ObjCImplementationDecl>(CtxD)) {
672 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
673 if (!IFD->isInvalidDecl())
674 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000675
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000676 } else if (ObjCCategoryImplDecl *CImplD =
677 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
678 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
679 if (!CatD->isInvalidDecl())
680 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
681 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000682 }
683
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000684 if (!Redecl && isRedeclaration()) {
685 // This is the last redeclaration, go back to the first method.
686 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
687 isInstanceMethod());
688 }
689
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000690 return Redecl ? Redecl : this;
691}
692
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000693ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
694 Decl *CtxD = cast<Decl>(getDeclContext());
695
696 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
697 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
698 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
699 isInstanceMethod()))
700 return MD;
701
702 } else if (ObjCCategoryImplDecl *CImplD =
703 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000704 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000705 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
706 isInstanceMethod()))
707 return MD;
708 }
709
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000710 if (isRedeclaration())
711 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
712 isInstanceMethod());
713
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000714 return this;
715}
716
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000717SourceLocation ObjCMethodDecl::getLocEnd() const {
718 if (Stmt *Body = getBody())
719 return Body->getLocEnd();
720 return DeclEndLoc;
721}
722
John McCallb4526252011-03-02 01:50:55 +0000723ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
724 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000725 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000726 return family;
727
John McCall86bc21f2011-03-02 11:33:24 +0000728 // Check for an explicit attribute.
729 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
730 // The unfortunate necessity of mapping between enums here is due
731 // to the attributes framework.
732 switch (attr->getFamily()) {
733 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
734 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
735 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
736 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
737 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
738 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
739 }
740 Family = static_cast<unsigned>(family);
741 return family;
742 }
743
John McCallb4526252011-03-02 01:50:55 +0000744 family = getSelector().getMethodFamily();
745 switch (family) {
746 case OMF_None: break;
747
748 // init only has a conventional meaning for an instance method, and
749 // it has to return an object.
750 case OMF_init:
751 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
752 family = OMF_None;
753 break;
754
755 // alloc/copy/new have a conventional meaning for both class and
756 // instance methods, but they require an object return.
757 case OMF_alloc:
758 case OMF_copy:
759 case OMF_mutableCopy:
760 case OMF_new:
761 if (!getResultType()->isObjCObjectPointerType())
762 family = OMF_None;
763 break;
764
765 // These selectors have a conventional meaning only for instance methods.
766 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000767 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000768 case OMF_retain:
769 case OMF_release:
770 case OMF_autorelease:
771 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000772 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000773 if (!isInstanceMethod())
774 family = OMF_None;
775 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000776
777 case OMF_performSelector:
778 if (!isInstanceMethod() ||
779 !getResultType()->isObjCIdType())
780 family = OMF_None;
781 else {
782 unsigned noParams = param_size();
783 if (noParams < 1 || noParams > 3)
784 family = OMF_None;
785 else {
786 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
787 QualType ArgT = (*it);
788 if (!ArgT->isObjCSelType()) {
789 family = OMF_None;
790 break;
791 }
792 while (--noParams) {
793 it++;
794 ArgT = (*it);
795 if (!ArgT->isObjCIdType()) {
796 family = OMF_None;
797 break;
798 }
799 }
800 }
801 }
802 break;
803
John McCallb4526252011-03-02 01:50:55 +0000804 }
805
806 // Cache the result.
807 Family = static_cast<unsigned>(family);
808 return family;
809}
810
Mike Stump11289f42009-09-09 15:08:12 +0000811void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000812 const ObjCInterfaceDecl *OID) {
813 QualType selfTy;
814 if (isInstanceMethod()) {
815 // There may be no interface context due to error in declaration
816 // of the interface (which has been reported). Recover gracefully.
817 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000818 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000819 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000820 } else {
821 selfTy = Context.getObjCIdType();
822 }
823 } else // we have a factory method.
824 selfTy = Context.getObjCClassType();
825
John McCalld4631322011-06-17 06:42:21 +0000826 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000827 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000828
David Blaikiebbafb8a2012-03-11 07:00:24 +0000829 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000830 if (isInstanceMethod()) {
831 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000832
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000833 // 'self' is always __strong. It's actually pseudo-strong except
834 // in init methods (or methods labeled ns_consumes_self), though.
835 Qualifiers qs;
836 qs.setObjCLifetime(Qualifiers::OCL_Strong);
837 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000838
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000839 // In addition, 'self' is const unless this is an init method.
840 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
841 selfTy = selfTy.withConst();
842 selfIsPseudoStrong = true;
843 }
844 }
845 else {
846 assert(isClassMethod());
847 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000848 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000849 selfIsPseudoStrong = true;
850 }
John McCall31168b02011-06-15 23:02:42 +0000851 }
852
853 ImplicitParamDecl *self
854 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
855 &Context.Idents.get("self"), selfTy);
856 setSelfDecl(self);
857
858 if (selfIsConsumed)
859 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000860
John McCalld4631322011-06-17 06:42:21 +0000861 if (selfIsPseudoStrong)
862 self->setARCPseudoStrong(true);
863
Mike Stump11289f42009-09-09 15:08:12 +0000864 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
865 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000866 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000867}
868
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000869ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
870 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
871 return ID;
872 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
873 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000874 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000875 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000876
877 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000878 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000879}
880
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000881static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
882 const ObjCMethodDecl *Method,
883 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
884 bool MovedToSuper) {
885 if (!Container)
886 return;
887
888 // In categories look for overriden methods from protocols. A method from
889 // category is not "overriden" since it is considered as the "same" method
890 // (same USR) as the one from the interface.
891 if (const ObjCCategoryDecl *
892 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
893 // Check whether we have a matching method at this category but only if we
894 // are at the super class level.
895 if (MovedToSuper)
896 if (ObjCMethodDecl *
897 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000898 Method->isInstanceMethod(),
899 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000900 if (Method != Overridden) {
901 // We found an override at this category; there is no need to look
902 // into its protocols.
903 Methods.push_back(Overridden);
904 return;
905 }
906
907 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
908 PEnd = Category->protocol_end();
909 P != PEnd; ++P)
910 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
911 return;
912 }
913
914 // Check whether we have a matching method at this level.
915 if (const ObjCMethodDecl *
916 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000917 Method->isInstanceMethod(),
918 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000919 if (Method != Overridden) {
920 // We found an override at this level; there is no need to look
921 // into other protocols or categories.
922 Methods.push_back(Overridden);
923 return;
924 }
925
926 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
927 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
928 PEnd = Protocol->protocol_end();
929 P != PEnd; ++P)
930 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
931 }
932
933 if (const ObjCInterfaceDecl *
934 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
935 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
936 PEnd = Interface->protocol_end();
937 P != PEnd; ++P)
938 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
939
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000940 for (ObjCInterfaceDecl::known_categories_iterator
941 Cat = Interface->known_categories_begin(),
942 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000943 Cat != CatEnd; ++Cat) {
944 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000945 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000946 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000947
948 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
949 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
950 /*MovedToSuper=*/true);
951 }
952}
953
954static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
955 const ObjCMethodDecl *Method,
956 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
957 CollectOverriddenMethodsRecurse(Container, Method, Methods,
958 /*MovedToSuper=*/false);
959}
960
961static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
962 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
963 assert(Method->isOverriding());
964
965 if (const ObjCProtocolDecl *
966 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
967 CollectOverriddenMethods(ProtD, Method, overridden);
968
969 } else if (const ObjCImplDecl *
970 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
971 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
972 if (!ID)
973 return;
974 // Start searching for overridden methods using the method from the
975 // interface as starting point.
976 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000977 Method->isInstanceMethod(),
978 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000979 Method = IFaceMeth;
980 CollectOverriddenMethods(ID, Method, overridden);
981
982 } else if (const ObjCCategoryDecl *
983 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
984 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
985 if (!ID)
986 return;
987 // Start searching for overridden methods using the method from the
988 // interface as starting point.
989 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000990 Method->isInstanceMethod(),
991 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000992 Method = IFaceMeth;
993 CollectOverriddenMethods(ID, Method, overridden);
994
995 } else {
996 CollectOverriddenMethods(
997 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
998 Method, overridden);
999 }
1000}
1001
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001002void ObjCMethodDecl::getOverriddenMethods(
1003 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1004 const ObjCMethodDecl *Method = this;
1005
1006 if (Method->isRedeclaration()) {
1007 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1008 getMethod(Method->getSelector(), Method->isInstanceMethod());
1009 }
1010
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001011 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001012 collectOverriddenMethodsSlow(Method, Overridden);
1013 assert(!Overridden.empty() &&
1014 "ObjCMethodDecl's overriding bit is not as expected");
1015 }
1016}
1017
Jordan Rose2bd991a2012-10-10 16:42:54 +00001018const ObjCPropertyDecl *
1019ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1020 Selector Sel = getSelector();
1021 unsigned NumArgs = Sel.getNumArgs();
1022 if (NumArgs > 1)
1023 return 0;
1024
Jordan Rose59e34ec2012-10-11 16:02:02 +00001025 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001026 return 0;
1027
1028 if (isPropertyAccessor()) {
1029 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001030 // If container is class extension, find its primary class.
1031 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1032 if (CatDecl->IsClassExtension())
1033 Container = CatDecl->getClassInterface();
1034
Jordan Rose2bd991a2012-10-10 16:42:54 +00001035 bool IsGetter = (NumArgs == 0);
1036
1037 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1038 E = Container->prop_end();
1039 I != E; ++I) {
1040 Selector NextSel = IsGetter ? (*I)->getGetterName()
1041 : (*I)->getSetterName();
1042 if (NextSel == Sel)
1043 return *I;
1044 }
1045
1046 llvm_unreachable("Marked as a property accessor but no property found!");
1047 }
1048
1049 if (!CheckOverrides)
1050 return 0;
1051
1052 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1053 OverridesTy Overrides;
1054 getOverriddenMethods(Overrides);
1055 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1056 I != E; ++I) {
1057 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1058 return Prop;
1059 }
1060
1061 return 0;
1062
1063}
1064
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001065//===----------------------------------------------------------------------===//
1066// ObjCInterfaceDecl
1067//===----------------------------------------------------------------------===//
1068
Douglas Gregord53ae832012-01-17 18:09:05 +00001069ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001070 DeclContext *DC,
1071 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001072 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001073 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001074 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001075 bool isInternal){
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001076 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregor81252352011-12-16 22:37:11 +00001077 PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001078 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001079 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001080 return Result;
1081}
1082
Douglas Gregor72172e92012-01-05 21:55:30 +00001083ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1084 unsigned ID) {
1085 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001086 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1087 0, SourceLocation(),
1088 0, false);
1089 Result->Data.setInt(!C.getLangOpts().Modules);
1090 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001091}
1092
1093ObjCInterfaceDecl::
1094ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001095 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1096 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001097 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001098 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001099{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001100 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001101
1102 // Copy the 'data' pointer over.
1103 if (PrevDecl)
1104 Data = PrevDecl->Data;
1105
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001106 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001107}
1108
Douglas Gregor73693022010-12-01 23:49:52 +00001109void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001110 assert(data().ExternallyCompleted && "Class is not externally completed");
1111 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001112 getASTContext().getExternalSource()->CompleteType(
1113 const_cast<ObjCInterfaceDecl *>(this));
1114}
1115
1116void ObjCInterfaceDecl::setExternallyCompleted() {
1117 assert(getASTContext().getExternalSource() &&
1118 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001119 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001120 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001121 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001122}
1123
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001124ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001125 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1126 if (data().ExternallyCompleted)
1127 LoadExternalDefinition();
1128
1129 return getASTContext().getObjCImplementation(
1130 const_cast<ObjCInterfaceDecl*>(Def));
1131 }
1132
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001133 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001134 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001135}
1136
1137void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001138 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001139}
1140
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001141namespace {
1142 struct SynthesizeIvarChunk {
1143 uint64_t Size;
1144 ObjCIvarDecl *Ivar;
1145 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1146 : Size(size), Ivar(ivar) {}
1147 };
1148
1149 bool operator<(const SynthesizeIvarChunk & LHS,
1150 const SynthesizeIvarChunk &RHS) {
1151 return LHS.Size < RHS.Size;
1152 }
1153}
1154
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001155/// all_declared_ivar_begin - return first ivar declared in this class,
1156/// its extensions and its implementation. Lazily build the list on first
1157/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001158///
1159/// Caveat: The list returned by this method reflects the current
1160/// state of the parser. The cache will be updated for every ivar
1161/// added by an extension or the implementation when they are
1162/// encountered.
1163/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001164ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001165 // FIXME: Should make sure no callers ever do this.
1166 if (!hasDefinition())
1167 return 0;
1168
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001169 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001170 if (!data().IvarList) {
1171 if (!ivar_empty()) {
1172 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1173 data().IvarList = *I; ++I;
1174 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001175 curIvar->setNextIvar(*I);
1176 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001177
1178 for (ObjCInterfaceDecl::known_extensions_iterator
1179 Ext = known_extensions_begin(),
1180 ExtEnd = known_extensions_end();
1181 Ext != ExtEnd; ++Ext) {
1182 if (!Ext->ivar_empty()) {
1183 ObjCCategoryDecl::ivar_iterator
1184 I = Ext->ivar_begin(),
1185 E = Ext->ivar_end();
1186 if (!data().IvarList) {
1187 data().IvarList = *I; ++I;
1188 curIvar = data().IvarList;
1189 }
1190 for ( ;I != E; curIvar = *I, ++I)
1191 curIvar->setNextIvar(*I);
1192 }
1193 }
1194 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001195 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001196
1197 // cached and complete!
1198 if (!data().IvarListMissingImplementation)
1199 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001200
1201 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001202 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001203 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001204 SmallVector<SynthesizeIvarChunk, 16> layout;
1205 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1206 E = ImplDecl->ivar_end(); I != E; ++I) {
1207 ObjCIvarDecl *IV = *I;
1208 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1209 layout.push_back(SynthesizeIvarChunk(
1210 IV->getASTContext().getTypeSize(IV->getType()), IV));
1211 continue;
1212 }
1213 if (!data().IvarList)
1214 data().IvarList = *I;
1215 else
1216 curIvar->setNextIvar(*I);
1217 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001218 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001219
1220 if (!layout.empty()) {
1221 // Order synthesized ivars by their size.
1222 std::stable_sort(layout.begin(), layout.end());
1223 unsigned Ix = 0, EIx = layout.size();
1224 if (!data().IvarList) {
1225 data().IvarList = layout[0].Ivar; Ix++;
1226 curIvar = data().IvarList;
1227 }
1228 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1229 curIvar->setNextIvar(layout[Ix].Ivar);
1230 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001231 }
1232 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001233 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001234}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001235
1236/// FindCategoryDeclaration - Finds category declaration in the list of
1237/// categories for this class and returns it. Name of the category is passed
1238/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001239///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001240ObjCCategoryDecl *
1241ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001242 // FIXME: Should make sure no callers ever do this.
1243 if (!hasDefinition())
1244 return 0;
1245
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001246 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001247 LoadExternalDefinition();
1248
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001249 for (visible_categories_iterator Cat = visible_categories_begin(),
1250 CatEnd = visible_categories_end();
1251 Cat != CatEnd;
1252 ++Cat) {
1253 if (Cat->getIdentifier() == CategoryId)
1254 return *Cat;
1255 }
1256
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001257 return 0;
1258}
1259
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001260ObjCMethodDecl *
1261ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001262 for (visible_categories_iterator Cat = visible_categories_begin(),
1263 CatEnd = visible_categories_end();
1264 Cat != CatEnd;
1265 ++Cat) {
1266 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001267 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1268 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001269 }
1270
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001271 return 0;
1272}
1273
1274ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001275 for (visible_categories_iterator Cat = visible_categories_begin(),
1276 CatEnd = visible_categories_end();
1277 Cat != CatEnd;
1278 ++Cat) {
1279 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001280 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1281 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001282 }
1283
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001284 return 0;
1285}
1286
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001287/// ClassImplementsProtocol - Checks that 'lProto' protocol
1288/// has been implemented in IDecl class, its super class or categories (if
1289/// lookupCategory is true).
1290bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1291 bool lookupCategory,
1292 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001293 if (!hasDefinition())
1294 return false;
1295
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001296 ObjCInterfaceDecl *IDecl = this;
1297 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001298 for (ObjCInterfaceDecl::protocol_iterator
1299 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001300 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1301 return true;
1302 // This is dubious and is added to be compatible with gcc. In gcc, it is
1303 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1304 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1305 // object. This IMO, should be a bug.
1306 // FIXME: Treat this as an extension, and flag this as an error when GCC
1307 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001308 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001309 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1310 return true;
1311 }
Mike Stump11289f42009-09-09 15:08:12 +00001312
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001313 // 2nd, look up the category.
1314 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001315 for (visible_categories_iterator Cat = visible_categories_begin(),
1316 CatEnd = visible_categories_end();
1317 Cat != CatEnd;
1318 ++Cat) {
1319 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1320 E = Cat->protocol_end();
1321 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001322 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1323 return true;
1324 }
Mike Stump11289f42009-09-09 15:08:12 +00001325
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001326 // 3rd, look up the super class(s)
1327 if (IDecl->getSuperClass())
1328 return
1329 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1330 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001331
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001332 return false;
1333}
1334
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001335//===----------------------------------------------------------------------===//
1336// ObjCIvarDecl
1337//===----------------------------------------------------------------------===//
1338
David Blaikie68e081d2011-12-20 02:48:34 +00001339void ObjCIvarDecl::anchor() { }
1340
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001341ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001342 SourceLocation StartLoc,
1343 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001344 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001345 AccessControl ac, Expr *BW,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001346 bool synthesized,
1347 bool backingIvarReferencedInAccessor) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001348 if (DC) {
1349 // Ivar's can only appear in interfaces, implementations (via synthesized
1350 // properties), and class extensions (via direct declaration, or synthesized
1351 // properties).
1352 //
1353 // FIXME: This should really be asserting this:
1354 // (isa<ObjCCategoryDecl>(DC) &&
1355 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1356 // but unfortunately we sometimes place ivars into non-class extension
1357 // categories on error. This breaks an AST invariant, and should not be
1358 // fixed.
1359 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1360 isa<ObjCCategoryDecl>(DC)) &&
1361 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001362 // Once a new ivar is created in any of class/class-extension/implementation
1363 // decl contexts, the previously built IvarList must be rebuilt.
1364 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1365 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001366 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001367 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001368 else
1369 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001370 }
1371 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001372 }
1373
Abramo Bagnaradff19302011-03-08 08:55:46 +00001374 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001375 ac, BW, synthesized, backingIvarReferencedInAccessor);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001376}
1377
Douglas Gregor72172e92012-01-05 21:55:30 +00001378ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1379 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1380 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001381 QualType(), 0, ObjCIvarDecl::None, 0, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001382}
1383
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001384const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1385 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001386
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001387 switch (DC->getKind()) {
1388 default:
1389 case ObjCCategoryImpl:
1390 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001391 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001392
1393 // Ivars can only appear in class extension categories.
1394 case ObjCCategory: {
1395 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1396 assert(CD->IsClassExtension() && "invalid container for ivar!");
1397 return CD->getClassInterface();
1398 }
1399
1400 case ObjCImplementation:
1401 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1402
1403 case ObjCInterface:
1404 return cast<ObjCInterfaceDecl>(DC);
1405 }
1406}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001407
1408//===----------------------------------------------------------------------===//
1409// ObjCAtDefsFieldDecl
1410//===----------------------------------------------------------------------===//
1411
David Blaikie68e081d2011-12-20 02:48:34 +00001412void ObjCAtDefsFieldDecl::anchor() { }
1413
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001414ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001415*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1416 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001417 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001418 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001419}
1420
Douglas Gregor72172e92012-01-05 21:55:30 +00001421ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1422 unsigned ID) {
1423 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1424 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1425 0, QualType(), 0);
1426}
1427
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001428//===----------------------------------------------------------------------===//
1429// ObjCProtocolDecl
1430//===----------------------------------------------------------------------===//
1431
David Blaikie68e081d2011-12-20 02:48:34 +00001432void ObjCProtocolDecl::anchor() { }
1433
Douglas Gregor32c17572012-01-01 20:30:41 +00001434ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1435 SourceLocation nameLoc,
1436 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001437 ObjCProtocolDecl *PrevDecl)
1438 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001439{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001440 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001441 if (PrevDecl)
1442 Data = PrevDecl->Data;
1443}
1444
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001445ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001446 IdentifierInfo *Id,
1447 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001448 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001449 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor32c17572012-01-01 20:30:41 +00001450 ObjCProtocolDecl *Result
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001451 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001452 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001453 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001454}
1455
Douglas Gregor72172e92012-01-05 21:55:30 +00001456ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1457 unsigned ID) {
1458 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001459 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1460 SourceLocation(), 0);
1461 Result->Data.setInt(!C.getLangOpts().Modules);
1462 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001463}
1464
Steve Naroff114aecb2009-03-01 16:12:44 +00001465ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1466 ObjCProtocolDecl *PDecl = this;
1467
1468 if (Name == getIdentifier())
1469 return PDecl;
1470
1471 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1472 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1473 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001474
Steve Naroff114aecb2009-03-01 16:12:44 +00001475 return NULL;
1476}
1477
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001478// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001479// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001480ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1481 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001482 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001483
Douglas Gregoreed49792013-01-17 00:38:46 +00001484 // If there is no definition or the definition is hidden, we don't find
1485 // anything.
1486 const ObjCProtocolDecl *Def = getDefinition();
1487 if (!Def || Def->isHidden())
1488 return NULL;
1489
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001490 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001491 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001492
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001493 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001494 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001495 return MethodDecl;
1496 return NULL;
1497}
1498
Douglas Gregore6e48b12012-01-01 19:29:29 +00001499void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001500 assert(!Data.getPointer() && "Protocol already has a definition!");
1501 Data.setPointer(new (getASTContext()) DefinitionData);
1502 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001503}
1504
1505void ObjCProtocolDecl::startDefinition() {
1506 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001507
1508 // Update all of the declarations with a pointer to the definition.
1509 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1510 RD != RDEnd; ++RD)
1511 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001512}
1513
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001514void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1515 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001516
1517 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1518 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1519 E = PDecl->prop_end(); P != E; ++P) {
1520 ObjCPropertyDecl *Prop = *P;
1521 // Insert into PM if not there already.
1522 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001523 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001524 }
1525 // Scan through protocol's protocols.
1526 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1527 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001528 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001529 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001530}
1531
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001532
1533void ObjCProtocolDecl::collectInheritedProtocolProperties(
1534 const ObjCPropertyDecl *Property,
1535 ProtocolPropertyMap &PM) const {
1536 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1537 bool MatchFound = false;
1538 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1539 E = PDecl->prop_end(); P != E; ++P) {
1540 ObjCPropertyDecl *Prop = *P;
1541 if (Prop == Property)
1542 continue;
1543 if (Prop->getIdentifier() == Property->getIdentifier()) {
1544 PM[PDecl] = Prop;
1545 MatchFound = true;
1546 break;
1547 }
1548 }
1549 // Scan through protocol's protocols which did not have a matching property.
1550 if (!MatchFound)
1551 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1552 E = PDecl->protocol_end(); PI != E; ++PI)
1553 (*PI)->collectInheritedProtocolProperties(Property, PM);
1554 }
1555}
Anna Zaks673d76b2012-10-18 19:17:53 +00001556
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001557//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001558// ObjCCategoryDecl
1559//===----------------------------------------------------------------------===//
1560
David Blaikie68e081d2011-12-20 02:48:34 +00001561void ObjCCategoryDecl::anchor() { }
1562
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001563ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +00001564 SourceLocation AtLoc,
1565 SourceLocation ClassNameLoc,
1566 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001567 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001568 ObjCInterfaceDecl *IDecl,
1569 SourceLocation IvarLBraceLoc,
1570 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001571 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1572 CategoryNameLoc, Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001573 IDecl,
1574 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001575 if (IDecl) {
1576 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001577 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001578 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001579 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001580 if (ASTMutationListener *L = C.getASTMutationListener())
1581 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1582 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001583 }
1584
1585 return CatDecl;
1586}
1587
Douglas Gregor72172e92012-01-05 21:55:30 +00001588ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1589 unsigned ID) {
1590 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1591 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1592 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001593}
1594
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001595ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1596 return getASTContext().getObjCImplementation(
1597 const_cast<ObjCCategoryDecl*>(this));
1598}
1599
1600void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1601 getASTContext().setObjCImplementation(this, ImplD);
1602}
1603
1604
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001605//===----------------------------------------------------------------------===//
1606// ObjCCategoryImplDecl
1607//===----------------------------------------------------------------------===//
1608
David Blaikie68e081d2011-12-20 02:48:34 +00001609void ObjCCategoryImplDecl::anchor() { }
1610
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001611ObjCCategoryImplDecl *
1612ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001613 IdentifierInfo *Id,
1614 ObjCInterfaceDecl *ClassInterface,
1615 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001616 SourceLocation atStartLoc,
1617 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001618 if (ClassInterface && ClassInterface->hasDefinition())
1619 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001620 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001621 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001622}
1623
Douglas Gregor72172e92012-01-05 21:55:30 +00001624ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1625 unsigned ID) {
1626 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1627 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1628 SourceLocation(), SourceLocation());
1629}
1630
Steve Narofff406f4d2009-10-29 21:11:04 +00001631ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001632 // The class interface might be NULL if we are working with invalid code.
1633 if (const ObjCInterfaceDecl *ID = getClassInterface())
1634 return ID->FindCategoryDeclaration(getIdentifier());
1635 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001636}
1637
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001638
David Blaikie68e081d2011-12-20 02:48:34 +00001639void ObjCImplDecl::anchor() { }
1640
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001641void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001642 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001643 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001644 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001645}
1646
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001647void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1648 ASTContext &Ctx = getASTContext();
1649
1650 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001651 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001652 if (IFace)
1653 Ctx.setObjCImplementation(IFace, ImplD);
1654
Duncan Sands49c29ee2009-07-21 07:56:29 +00001655 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001656 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1657 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1658 Ctx.setObjCImplementation(CD, ImplD);
1659 }
1660
1661 ClassInterface = IFace;
1662}
1663
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001664/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001665/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001666/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001667///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001668ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001669FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1670 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001671 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001672 if (PID->getPropertyIvarDecl() &&
1673 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1674 return PID;
1675 }
1676 return 0;
1677}
1678
1679/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001680/// added to the list of those properties \@synthesized/\@dynamic in this
1681/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001682///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001683ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001684FindPropertyImplDecl(IdentifierInfo *Id) const {
1685 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001686 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001687 if (PID->getPropertyDecl()->getIdentifier() == Id)
1688 return PID;
1689 }
1690 return 0;
1691}
1692
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001693raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001694 const ObjCCategoryImplDecl &CID) {
1695 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001696 return OS;
1697}
1698
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001699//===----------------------------------------------------------------------===//
1700// ObjCImplementationDecl
1701//===----------------------------------------------------------------------===//
1702
David Blaikie68e081d2011-12-20 02:48:34 +00001703void ObjCImplementationDecl::anchor() { }
1704
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001705ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001706ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001707 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001708 ObjCInterfaceDecl *SuperDecl,
1709 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001710 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001711 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001712 SourceLocation IvarLBraceLoc,
1713 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001714 if (ClassInterface && ClassInterface->hasDefinition())
1715 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001716 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001717 nameLoc, atStartLoc, superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001718 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001719}
1720
Douglas Gregor72172e92012-01-05 21:55:30 +00001721ObjCImplementationDecl *
1722ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1723 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1724 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1725 SourceLocation());
1726}
1727
John McCall0410e572011-07-22 04:15:06 +00001728void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1729 CXXCtorInitializer ** initializers,
1730 unsigned numInitializers) {
1731 if (numInitializers > 0) {
1732 NumIvarInitializers = numInitializers;
1733 CXXCtorInitializer **ivarInitializers =
1734 new (C) CXXCtorInitializer*[NumIvarInitializers];
1735 memcpy(ivarInitializers, initializers,
1736 numInitializers * sizeof(CXXCtorInitializer*));
1737 IvarInitializers = ivarInitializers;
1738 }
1739}
1740
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001741raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001742 const ObjCImplementationDecl &ID) {
1743 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001744 return OS;
1745}
1746
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001747//===----------------------------------------------------------------------===//
1748// ObjCCompatibleAliasDecl
1749//===----------------------------------------------------------------------===//
1750
David Blaikie68e081d2011-12-20 02:48:34 +00001751void ObjCCompatibleAliasDecl::anchor() { }
1752
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001753ObjCCompatibleAliasDecl *
1754ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1755 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001756 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001757 ObjCInterfaceDecl* AliasedClass) {
1758 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1759}
1760
Douglas Gregor72172e92012-01-05 21:55:30 +00001761ObjCCompatibleAliasDecl *
1762ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1763 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1764 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1765}
1766
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001767//===----------------------------------------------------------------------===//
1768// ObjCPropertyDecl
1769//===----------------------------------------------------------------------===//
1770
David Blaikie68e081d2011-12-20 02:48:34 +00001771void ObjCPropertyDecl::anchor() { }
1772
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1774 SourceLocation L,
1775 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001776 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001777 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001778 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001779 PropertyControl propControl) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001780 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001781}
1782
Douglas Gregor72172e92012-01-05 21:55:30 +00001783ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1784 unsigned ID) {
1785 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1786 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001787 SourceLocation(),
Douglas Gregor72172e92012-01-05 21:55:30 +00001788 0);
1789}
1790
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001791//===----------------------------------------------------------------------===//
1792// ObjCPropertyImplDecl
1793//===----------------------------------------------------------------------===//
1794
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001795ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001796 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001797 SourceLocation atLoc,
1798 SourceLocation L,
1799 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001800 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001801 ObjCIvarDecl *ivar,
1802 SourceLocation ivarLoc) {
1803 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1804 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001805}
Chris Lattnered0e1642008-03-17 01:19:02 +00001806
Douglas Gregor72172e92012-01-05 21:55:30 +00001807ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1808 unsigned ID) {
1809 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1810 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1811 0, Dynamic, 0, SourceLocation());
1812}
1813
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001814SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1815 SourceLocation EndLoc = getLocation();
1816 if (IvarLoc.isValid())
1817 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001818
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001819 return SourceRange(AtLoc, EndLoc);
1820}