blob: a4857b607315661ec9119986285066c5db8d258d [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 Kremenek00781502013-11-23 01:01:29 +0000460 bool isInstance,
461 bool shallowCategoryLookup,
462 bool followSuper,
Ted Kremenek28eace62013-11-23 01:01:34 +0000463 const ObjCCategoryDecl *C,
464 const ObjCProtocolDecl *P) const
Ted Kremenek00781502013-11-23 01:01:29 +0000465{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000466 // FIXME: Should make sure no callers ever do this.
467 if (!hasDefinition())
468 return 0;
469
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000470 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000471 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000473 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000474 LoadExternalDefinition();
475
Ted Kremenek00781502013-11-23 01:01:29 +0000476 while (ClassDecl) {
Ted Kremenek28eace62013-11-23 01:01:34 +0000477 // If we are looking for a method that is part of protocol conformance,
478 // check if the superclass has been marked to suppress conformance
479 // of that protocol.
480 if (P && ClassDecl->hasAttrs()) {
481 const AttrVec &V = ClassDecl->getAttrs();
482 const IdentifierInfo *PI = P->getIdentifier();
483 for (AttrVec::const_iterator I = V.begin(), E = V.end(); I != E; ++I) {
484 if (const ObjCSuppressProtocolAttr *A =
485 dyn_cast<ObjCSuppressProtocolAttr>(*I)){
486 if (A->getProtocol() == PI) {
487 return 0;
488 }
489 }
490 }
491 }
492
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000493 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000494 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner89375192008-03-16 00:19:01 +0000496 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000497 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
498 E = ClassDecl->protocol_end();
499 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000500 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000501 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000502
503 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000504 for (ObjCInterfaceDecl::visible_categories_iterator
505 Cat = ClassDecl->visible_categories_begin(),
506 CatEnd = ClassDecl->visible_categories_end();
507 Cat != CatEnd; ++Cat) {
508 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
509 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000510 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000511
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000512 if (!shallowCategoryLookup) {
513 // Didn't find one yet - look through protocols.
514 const ObjCList<ObjCProtocolDecl> &Protocols =
515 Cat->getReferencedProtocols();
516 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
517 E = Protocols.end(); I != E; ++I)
518 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
519 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000520 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000521 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000522 }
Ted Kremenek00781502013-11-23 01:01:29 +0000523
524 if (!followSuper)
525 return NULL;
526
527 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000528 ClassDecl = ClassDecl->getSuperClass();
529 }
530 return NULL;
531}
532
Anna Zaksc77a3b12012-07-27 19:07:44 +0000533// Will search "local" class/category implementations for a method decl.
534// If failed, then we search in class's root for an instance method.
535// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000536ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
537 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000538 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000539 // FIXME: Should make sure no callers ever do this.
540 if (!hasDefinition())
541 return 0;
542
543 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000544 LoadExternalDefinition();
545
Steve Naroffbb69c942009-10-01 23:46:04 +0000546 ObjCMethodDecl *Method = 0;
547 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000548 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
549 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000550
551 // Look through local category implementations associated with the class.
552 if (!Method)
553 Method = Instance ? getCategoryInstanceMethod(Sel)
554 : getCategoryClassMethod(Sel);
555
556 // Before we give up, check if the selector is an instance method.
557 // But only in the root. This matches gcc's behavior and what the
558 // runtime expects.
559 if (!Instance && !Method && !getSuperClass()) {
560 Method = lookupInstanceMethod(Sel);
561 // Look through local category implementations associated
562 // with the root class.
563 if (!Method)
564 Method = lookupPrivateMethod(Sel, true);
565 }
566
Steve Naroffbb69c942009-10-01 23:46:04 +0000567 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000568 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000569 return Method;
570}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000571
572//===----------------------------------------------------------------------===//
573// ObjCMethodDecl
574//===----------------------------------------------------------------------===//
575
576ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000577 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000578 SourceLocation endLoc,
579 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000580 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000581 DeclContext *contextDecl,
582 bool isInstance,
583 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000584 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000585 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000586 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000587 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000588 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000589 return new (C, contextDecl) ObjCMethodDecl(
590 beginLoc, endLoc, SelInfo, T, ResultTInfo, contextDecl, isInstance,
591 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
592 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000593}
594
Douglas Gregor72172e92012-01-05 21:55:30 +0000595ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000596 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
597 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000598}
599
Douglas Gregora6017bb2012-10-09 17:21:28 +0000600Stmt *ObjCMethodDecl::getBody() const {
601 return Body.get(getASTContext().getExternalSource());
602}
603
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000604void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
605 assert(PrevMethod);
606 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
607 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000608 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000609}
610
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000611void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
612 ArrayRef<ParmVarDecl*> Params,
613 ArrayRef<SourceLocation> SelLocs) {
614 ParamsAndSelLocs = 0;
615 NumParams = Params.size();
616 if (Params.empty() && SelLocs.empty())
617 return;
618
619 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
620 sizeof(SourceLocation) * SelLocs.size();
621 ParamsAndSelLocs = C.Allocate(Size);
622 std::copy(Params.begin(), Params.end(), getParams());
623 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
624}
625
626void ObjCMethodDecl::getSelectorLocs(
627 SmallVectorImpl<SourceLocation> &SelLocs) const {
628 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
629 SelLocs.push_back(getSelectorLoc(i));
630}
631
632void ObjCMethodDecl::setMethodParams(ASTContext &C,
633 ArrayRef<ParmVarDecl*> Params,
634 ArrayRef<SourceLocation> SelLocs) {
635 assert((!SelLocs.empty() || isImplicit()) &&
636 "No selector locs for non-implicit method");
637 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000638 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000639
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000640 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
641 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000642 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000643 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000644
645 setParamsAndSelLocs(C, Params, SelLocs);
646}
647
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000648/// \brief A definition will return its interface declaration.
649/// An interface declaration will return its definition.
650/// Otherwise it will return itself.
651ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
652 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000653 ObjCMethodDecl *Redecl = 0;
654 if (HasRedeclaration)
655 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000656 if (Redecl)
657 return Redecl;
658
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000659 Decl *CtxD = cast<Decl>(getDeclContext());
660
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000661 if (!CtxD->isInvalidDecl()) {
662 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
663 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
664 if (!ImplD->isInvalidDecl())
665 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000666
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000667 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
668 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
669 if (!ImplD->isInvalidDecl())
670 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000671
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000672 } else if (ObjCImplementationDecl *ImplD =
673 dyn_cast<ObjCImplementationDecl>(CtxD)) {
674 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
675 if (!IFD->isInvalidDecl())
676 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000677
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000678 } else if (ObjCCategoryImplDecl *CImplD =
679 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
680 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
681 if (!CatD->isInvalidDecl())
682 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
683 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000684 }
685
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000686 if (!Redecl && isRedeclaration()) {
687 // This is the last redeclaration, go back to the first method.
688 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
689 isInstanceMethod());
690 }
691
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000692 return Redecl ? Redecl : this;
693}
694
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000695ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
696 Decl *CtxD = cast<Decl>(getDeclContext());
697
698 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
699 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
700 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
701 isInstanceMethod()))
702 return MD;
703
704 } else if (ObjCCategoryImplDecl *CImplD =
705 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000706 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000707 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
708 isInstanceMethod()))
709 return MD;
710 }
711
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000712 if (isRedeclaration())
713 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
714 isInstanceMethod());
715
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000716 return this;
717}
718
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000719SourceLocation ObjCMethodDecl::getLocEnd() const {
720 if (Stmt *Body = getBody())
721 return Body->getLocEnd();
722 return DeclEndLoc;
723}
724
John McCallb4526252011-03-02 01:50:55 +0000725ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
726 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000727 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000728 return family;
729
John McCall86bc21f2011-03-02 11:33:24 +0000730 // Check for an explicit attribute.
731 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
732 // The unfortunate necessity of mapping between enums here is due
733 // to the attributes framework.
734 switch (attr->getFamily()) {
735 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
736 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
737 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
738 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
739 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
740 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
741 }
742 Family = static_cast<unsigned>(family);
743 return family;
744 }
745
John McCallb4526252011-03-02 01:50:55 +0000746 family = getSelector().getMethodFamily();
747 switch (family) {
748 case OMF_None: break;
749
750 // init only has a conventional meaning for an instance method, and
751 // it has to return an object.
752 case OMF_init:
753 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
754 family = OMF_None;
755 break;
756
757 // alloc/copy/new have a conventional meaning for both class and
758 // instance methods, but they require an object return.
759 case OMF_alloc:
760 case OMF_copy:
761 case OMF_mutableCopy:
762 case OMF_new:
763 if (!getResultType()->isObjCObjectPointerType())
764 family = OMF_None;
765 break;
766
767 // These selectors have a conventional meaning only for instance methods.
768 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000769 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000770 case OMF_retain:
771 case OMF_release:
772 case OMF_autorelease:
773 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000774 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000775 if (!isInstanceMethod())
776 family = OMF_None;
777 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000778
779 case OMF_performSelector:
780 if (!isInstanceMethod() ||
781 !getResultType()->isObjCIdType())
782 family = OMF_None;
783 else {
784 unsigned noParams = param_size();
785 if (noParams < 1 || noParams > 3)
786 family = OMF_None;
787 else {
788 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
789 QualType ArgT = (*it);
790 if (!ArgT->isObjCSelType()) {
791 family = OMF_None;
792 break;
793 }
794 while (--noParams) {
795 it++;
796 ArgT = (*it);
797 if (!ArgT->isObjCIdType()) {
798 family = OMF_None;
799 break;
800 }
801 }
802 }
803 }
804 break;
805
John McCallb4526252011-03-02 01:50:55 +0000806 }
807
808 // Cache the result.
809 Family = static_cast<unsigned>(family);
810 return family;
811}
812
Mike Stump11289f42009-09-09 15:08:12 +0000813void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000814 const ObjCInterfaceDecl *OID) {
815 QualType selfTy;
816 if (isInstanceMethod()) {
817 // There may be no interface context due to error in declaration
818 // of the interface (which has been reported). Recover gracefully.
819 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000820 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000821 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000822 } else {
823 selfTy = Context.getObjCIdType();
824 }
825 } else // we have a factory method.
826 selfTy = Context.getObjCClassType();
827
John McCalld4631322011-06-17 06:42:21 +0000828 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000829 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000830
David Blaikiebbafb8a2012-03-11 07:00:24 +0000831 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000832 if (isInstanceMethod()) {
833 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000834
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000835 // 'self' is always __strong. It's actually pseudo-strong except
836 // in init methods (or methods labeled ns_consumes_self), though.
837 Qualifiers qs;
838 qs.setObjCLifetime(Qualifiers::OCL_Strong);
839 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000840
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000841 // In addition, 'self' is const unless this is an init method.
842 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
843 selfTy = selfTy.withConst();
844 selfIsPseudoStrong = true;
845 }
846 }
847 else {
848 assert(isClassMethod());
849 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000850 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000851 selfIsPseudoStrong = true;
852 }
John McCall31168b02011-06-15 23:02:42 +0000853 }
854
855 ImplicitParamDecl *self
856 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
857 &Context.Idents.get("self"), selfTy);
858 setSelfDecl(self);
859
860 if (selfIsConsumed)
861 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000862
John McCalld4631322011-06-17 06:42:21 +0000863 if (selfIsPseudoStrong)
864 self->setARCPseudoStrong(true);
865
Mike Stump11289f42009-09-09 15:08:12 +0000866 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
867 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000868 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000869}
870
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000871ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
872 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
873 return ID;
874 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
875 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000876 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000877 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000878
879 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000880 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000881}
882
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000883static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
884 const ObjCMethodDecl *Method,
885 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
886 bool MovedToSuper) {
887 if (!Container)
888 return;
889
890 // In categories look for overriden methods from protocols. A method from
891 // category is not "overriden" since it is considered as the "same" method
892 // (same USR) as the one from the interface.
893 if (const ObjCCategoryDecl *
894 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
895 // Check whether we have a matching method at this category but only if we
896 // are at the super class level.
897 if (MovedToSuper)
898 if (ObjCMethodDecl *
899 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000900 Method->isInstanceMethod(),
901 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000902 if (Method != Overridden) {
903 // We found an override at this category; there is no need to look
904 // into its protocols.
905 Methods.push_back(Overridden);
906 return;
907 }
908
909 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
910 PEnd = Category->protocol_end();
911 P != PEnd; ++P)
912 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
913 return;
914 }
915
916 // Check whether we have a matching method at this level.
917 if (const ObjCMethodDecl *
918 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000919 Method->isInstanceMethod(),
920 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000921 if (Method != Overridden) {
922 // We found an override at this level; there is no need to look
923 // into other protocols or categories.
924 Methods.push_back(Overridden);
925 return;
926 }
927
928 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
929 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
930 PEnd = Protocol->protocol_end();
931 P != PEnd; ++P)
932 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
933 }
934
935 if (const ObjCInterfaceDecl *
936 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
937 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
938 PEnd = Interface->protocol_end();
939 P != PEnd; ++P)
940 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
941
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000942 for (ObjCInterfaceDecl::known_categories_iterator
943 Cat = Interface->known_categories_begin(),
944 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000945 Cat != CatEnd; ++Cat) {
946 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000947 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000948 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000949
950 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
951 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
952 /*MovedToSuper=*/true);
953 }
954}
955
956static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
957 const ObjCMethodDecl *Method,
958 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
959 CollectOverriddenMethodsRecurse(Container, Method, Methods,
960 /*MovedToSuper=*/false);
961}
962
963static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
964 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
965 assert(Method->isOverriding());
966
967 if (const ObjCProtocolDecl *
968 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
969 CollectOverriddenMethods(ProtD, Method, overridden);
970
971 } else if (const ObjCImplDecl *
972 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
973 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
974 if (!ID)
975 return;
976 // Start searching for overridden methods using the method from the
977 // interface as starting point.
978 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000979 Method->isInstanceMethod(),
980 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000981 Method = IFaceMeth;
982 CollectOverriddenMethods(ID, Method, overridden);
983
984 } else if (const ObjCCategoryDecl *
985 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
986 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
987 if (!ID)
988 return;
989 // Start searching for overridden methods using the method from the
990 // interface as starting point.
991 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000992 Method->isInstanceMethod(),
993 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000994 Method = IFaceMeth;
995 CollectOverriddenMethods(ID, Method, overridden);
996
997 } else {
998 CollectOverriddenMethods(
999 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1000 Method, overridden);
1001 }
1002}
1003
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001004void ObjCMethodDecl::getOverriddenMethods(
1005 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1006 const ObjCMethodDecl *Method = this;
1007
1008 if (Method->isRedeclaration()) {
1009 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1010 getMethod(Method->getSelector(), Method->isInstanceMethod());
1011 }
1012
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001013 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001014 collectOverriddenMethodsSlow(Method, Overridden);
1015 assert(!Overridden.empty() &&
1016 "ObjCMethodDecl's overriding bit is not as expected");
1017 }
1018}
1019
Jordan Rose2bd991a2012-10-10 16:42:54 +00001020const ObjCPropertyDecl *
1021ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1022 Selector Sel = getSelector();
1023 unsigned NumArgs = Sel.getNumArgs();
1024 if (NumArgs > 1)
1025 return 0;
1026
Jordan Rose59e34ec2012-10-11 16:02:02 +00001027 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001028 return 0;
1029
1030 if (isPropertyAccessor()) {
1031 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001032 // If container is class extension, find its primary class.
1033 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1034 if (CatDecl->IsClassExtension())
1035 Container = CatDecl->getClassInterface();
1036
Jordan Rose2bd991a2012-10-10 16:42:54 +00001037 bool IsGetter = (NumArgs == 0);
1038
1039 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1040 E = Container->prop_end();
1041 I != E; ++I) {
1042 Selector NextSel = IsGetter ? (*I)->getGetterName()
1043 : (*I)->getSetterName();
1044 if (NextSel == Sel)
1045 return *I;
1046 }
1047
1048 llvm_unreachable("Marked as a property accessor but no property found!");
1049 }
1050
1051 if (!CheckOverrides)
1052 return 0;
1053
1054 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1055 OverridesTy Overrides;
1056 getOverriddenMethods(Overrides);
1057 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1058 I != E; ++I) {
1059 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1060 return Prop;
1061 }
1062
1063 return 0;
1064
1065}
1066
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001067//===----------------------------------------------------------------------===//
1068// ObjCInterfaceDecl
1069//===----------------------------------------------------------------------===//
1070
Douglas Gregord53ae832012-01-17 18:09:05 +00001071ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001072 DeclContext *DC,
1073 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001074 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001075 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001076 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001077 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001078 ObjCInterfaceDecl *Result = new (C, DC)
1079 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001080 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001081 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001082 return Result;
1083}
1084
Richard Smithf7981722013-11-22 09:01:48 +00001085ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001086 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001087 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1088 0, SourceLocation(),
1089 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001090 Result->Data.setInt(!C.getLangOpts().Modules);
1091 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001092}
1093
1094ObjCInterfaceDecl::
1095ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001096 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1097 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001098 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001099 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001100{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001101 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001102
1103 // Copy the 'data' pointer over.
1104 if (PrevDecl)
1105 Data = PrevDecl->Data;
1106
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001107 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001108}
1109
Douglas Gregor73693022010-12-01 23:49:52 +00001110void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001111 assert(data().ExternallyCompleted && "Class is not externally completed");
1112 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001113 getASTContext().getExternalSource()->CompleteType(
1114 const_cast<ObjCInterfaceDecl *>(this));
1115}
1116
1117void ObjCInterfaceDecl::setExternallyCompleted() {
1118 assert(getASTContext().getExternalSource() &&
1119 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001120 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001121 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001123}
1124
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001125ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001126 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1127 if (data().ExternallyCompleted)
1128 LoadExternalDefinition();
1129
1130 return getASTContext().getObjCImplementation(
1131 const_cast<ObjCInterfaceDecl*>(Def));
1132 }
1133
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001134 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001135 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001136}
1137
1138void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001139 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001140}
1141
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001142namespace {
1143 struct SynthesizeIvarChunk {
1144 uint64_t Size;
1145 ObjCIvarDecl *Ivar;
1146 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1147 : Size(size), Ivar(ivar) {}
1148 };
1149
1150 bool operator<(const SynthesizeIvarChunk & LHS,
1151 const SynthesizeIvarChunk &RHS) {
1152 return LHS.Size < RHS.Size;
1153 }
1154}
1155
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001156/// all_declared_ivar_begin - return first ivar declared in this class,
1157/// its extensions and its implementation. Lazily build the list on first
1158/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001159///
1160/// Caveat: The list returned by this method reflects the current
1161/// state of the parser. The cache will be updated for every ivar
1162/// added by an extension or the implementation when they are
1163/// encountered.
1164/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001165ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001166 // FIXME: Should make sure no callers ever do this.
1167 if (!hasDefinition())
1168 return 0;
1169
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001170 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001171 if (!data().IvarList) {
1172 if (!ivar_empty()) {
1173 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1174 data().IvarList = *I; ++I;
1175 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001176 curIvar->setNextIvar(*I);
1177 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001178
1179 for (ObjCInterfaceDecl::known_extensions_iterator
1180 Ext = known_extensions_begin(),
1181 ExtEnd = known_extensions_end();
1182 Ext != ExtEnd; ++Ext) {
1183 if (!Ext->ivar_empty()) {
1184 ObjCCategoryDecl::ivar_iterator
1185 I = Ext->ivar_begin(),
1186 E = Ext->ivar_end();
1187 if (!data().IvarList) {
1188 data().IvarList = *I; ++I;
1189 curIvar = data().IvarList;
1190 }
1191 for ( ;I != E; curIvar = *I, ++I)
1192 curIvar->setNextIvar(*I);
1193 }
1194 }
1195 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001196 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001197
1198 // cached and complete!
1199 if (!data().IvarListMissingImplementation)
1200 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001201
1202 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001203 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001204 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001205 SmallVector<SynthesizeIvarChunk, 16> layout;
1206 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1207 E = ImplDecl->ivar_end(); I != E; ++I) {
1208 ObjCIvarDecl *IV = *I;
1209 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1210 layout.push_back(SynthesizeIvarChunk(
1211 IV->getASTContext().getTypeSize(IV->getType()), IV));
1212 continue;
1213 }
1214 if (!data().IvarList)
1215 data().IvarList = *I;
1216 else
1217 curIvar->setNextIvar(*I);
1218 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001219 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001220
1221 if (!layout.empty()) {
1222 // Order synthesized ivars by their size.
1223 std::stable_sort(layout.begin(), layout.end());
1224 unsigned Ix = 0, EIx = layout.size();
1225 if (!data().IvarList) {
1226 data().IvarList = layout[0].Ivar; Ix++;
1227 curIvar = data().IvarList;
1228 }
1229 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1230 curIvar->setNextIvar(layout[Ix].Ivar);
1231 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001232 }
1233 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001234 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001235}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001236
1237/// FindCategoryDeclaration - Finds category declaration in the list of
1238/// categories for this class and returns it. Name of the category is passed
1239/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001240///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001241ObjCCategoryDecl *
1242ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001243 // FIXME: Should make sure no callers ever do this.
1244 if (!hasDefinition())
1245 return 0;
1246
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001247 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001248 LoadExternalDefinition();
1249
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001250 for (visible_categories_iterator Cat = visible_categories_begin(),
1251 CatEnd = visible_categories_end();
1252 Cat != CatEnd;
1253 ++Cat) {
1254 if (Cat->getIdentifier() == CategoryId)
1255 return *Cat;
1256 }
1257
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001258 return 0;
1259}
1260
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001261ObjCMethodDecl *
1262ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001263 for (visible_categories_iterator Cat = visible_categories_begin(),
1264 CatEnd = visible_categories_end();
1265 Cat != CatEnd;
1266 ++Cat) {
1267 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001268 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1269 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001270 }
1271
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001272 return 0;
1273}
1274
1275ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001276 for (visible_categories_iterator Cat = visible_categories_begin(),
1277 CatEnd = visible_categories_end();
1278 Cat != CatEnd;
1279 ++Cat) {
1280 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001281 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1282 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001283 }
1284
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001285 return 0;
1286}
1287
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001288/// ClassImplementsProtocol - Checks that 'lProto' protocol
1289/// has been implemented in IDecl class, its super class or categories (if
1290/// lookupCategory is true).
1291bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1292 bool lookupCategory,
1293 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001294 if (!hasDefinition())
1295 return false;
1296
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001297 ObjCInterfaceDecl *IDecl = this;
1298 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001299 for (ObjCInterfaceDecl::protocol_iterator
1300 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001301 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1302 return true;
1303 // This is dubious and is added to be compatible with gcc. In gcc, it is
1304 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1305 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1306 // object. This IMO, should be a bug.
1307 // FIXME: Treat this as an extension, and flag this as an error when GCC
1308 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001309 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001310 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1311 return true;
1312 }
Mike Stump11289f42009-09-09 15:08:12 +00001313
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001314 // 2nd, look up the category.
1315 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001316 for (visible_categories_iterator Cat = visible_categories_begin(),
1317 CatEnd = visible_categories_end();
1318 Cat != CatEnd;
1319 ++Cat) {
1320 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1321 E = Cat->protocol_end();
1322 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001323 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1324 return true;
1325 }
Mike Stump11289f42009-09-09 15:08:12 +00001326
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001327 // 3rd, look up the super class(s)
1328 if (IDecl->getSuperClass())
1329 return
1330 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1331 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001332
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001333 return false;
1334}
1335
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001336//===----------------------------------------------------------------------===//
1337// ObjCIvarDecl
1338//===----------------------------------------------------------------------===//
1339
David Blaikie68e081d2011-12-20 02:48:34 +00001340void ObjCIvarDecl::anchor() { }
1341
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001342ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001343 SourceLocation StartLoc,
1344 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001345 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001346 AccessControl ac, Expr *BW,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001347 bool synthesized,
1348 bool backingIvarReferencedInAccessor) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001349 if (DC) {
1350 // Ivar's can only appear in interfaces, implementations (via synthesized
1351 // properties), and class extensions (via direct declaration, or synthesized
1352 // properties).
1353 //
1354 // FIXME: This should really be asserting this:
1355 // (isa<ObjCCategoryDecl>(DC) &&
1356 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1357 // but unfortunately we sometimes place ivars into non-class extension
1358 // categories on error. This breaks an AST invariant, and should not be
1359 // fixed.
1360 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1361 isa<ObjCCategoryDecl>(DC)) &&
1362 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001363 // Once a new ivar is created in any of class/class-extension/implementation
1364 // decl contexts, the previously built IvarList must be rebuilt.
1365 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1366 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001367 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001368 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001369 else
1370 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001371 }
1372 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001373 }
1374
Richard Smithf7981722013-11-22 09:01:48 +00001375 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1376 synthesized, backingIvarReferencedInAccessor);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001377}
1378
Douglas Gregor72172e92012-01-05 21:55:30 +00001379ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001380 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1381 QualType(), 0, ObjCIvarDecl::None, 0, false,
1382 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001383}
1384
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001385const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1386 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001387
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001388 switch (DC->getKind()) {
1389 default:
1390 case ObjCCategoryImpl:
1391 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001392 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001393
1394 // Ivars can only appear in class extension categories.
1395 case ObjCCategory: {
1396 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1397 assert(CD->IsClassExtension() && "invalid container for ivar!");
1398 return CD->getClassInterface();
1399 }
1400
1401 case ObjCImplementation:
1402 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1403
1404 case ObjCInterface:
1405 return cast<ObjCInterfaceDecl>(DC);
1406 }
1407}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001408
1409//===----------------------------------------------------------------------===//
1410// ObjCAtDefsFieldDecl
1411//===----------------------------------------------------------------------===//
1412
David Blaikie68e081d2011-12-20 02:48:34 +00001413void ObjCAtDefsFieldDecl::anchor() { }
1414
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001415ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001416*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1417 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001418 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001419 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001420}
1421
Richard Smithf7981722013-11-22 09:01:48 +00001422ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001423 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001424 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1425 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001426}
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) {
Richard Smithf7981722013-11-22 09:01:48 +00001450 ObjCProtocolDecl *Result =
1451 new (C, DC) 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
Richard Smithf7981722013-11-22 09:01:48 +00001456ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001457 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001458 ObjCProtocolDecl *Result =
1459 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001460 Result->Data.setInt(!C.getLangOpts().Modules);
1461 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001462}
1463
Steve Naroff114aecb2009-03-01 16:12:44 +00001464ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1465 ObjCProtocolDecl *PDecl = this;
1466
1467 if (Name == getIdentifier())
1468 return PDecl;
1469
1470 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1471 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1472 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001473
Steve Naroff114aecb2009-03-01 16:12:44 +00001474 return NULL;
1475}
1476
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001477// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001478// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001479ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1480 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001481 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregoreed49792013-01-17 00:38:46 +00001483 // If there is no definition or the definition is hidden, we don't find
1484 // anything.
1485 const ObjCProtocolDecl *Def = getDefinition();
1486 if (!Def || Def->isHidden())
1487 return NULL;
1488
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001489 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001490 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001491
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001492 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001493 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001494 return MethodDecl;
1495 return NULL;
1496}
1497
Douglas Gregore6e48b12012-01-01 19:29:29 +00001498void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001499 assert(!Data.getPointer() && "Protocol already has a definition!");
1500 Data.setPointer(new (getASTContext()) DefinitionData);
1501 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001502}
1503
1504void ObjCProtocolDecl::startDefinition() {
1505 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001506
1507 // Update all of the declarations with a pointer to the definition.
1508 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1509 RD != RDEnd; ++RD)
1510 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001511}
1512
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001513void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1514 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001515
1516 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1517 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1518 E = PDecl->prop_end(); P != E; ++P) {
1519 ObjCPropertyDecl *Prop = *P;
1520 // Insert into PM if not there already.
1521 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001522 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001523 }
1524 // Scan through protocol's protocols.
1525 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1526 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001527 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001528 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001529}
1530
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001531
1532void ObjCProtocolDecl::collectInheritedProtocolProperties(
1533 const ObjCPropertyDecl *Property,
1534 ProtocolPropertyMap &PM) const {
1535 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1536 bool MatchFound = false;
1537 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1538 E = PDecl->prop_end(); P != E; ++P) {
1539 ObjCPropertyDecl *Prop = *P;
1540 if (Prop == Property)
1541 continue;
1542 if (Prop->getIdentifier() == Property->getIdentifier()) {
1543 PM[PDecl] = Prop;
1544 MatchFound = true;
1545 break;
1546 }
1547 }
1548 // Scan through protocol's protocols which did not have a matching property.
1549 if (!MatchFound)
1550 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1551 E = PDecl->protocol_end(); PI != E; ++PI)
1552 (*PI)->collectInheritedProtocolProperties(Property, PM);
1553 }
1554}
Anna Zaks673d76b2012-10-18 19:17:53 +00001555
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001556//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001557// ObjCCategoryDecl
1558//===----------------------------------------------------------------------===//
1559
David Blaikie68e081d2011-12-20 02:48:34 +00001560void ObjCCategoryDecl::anchor() { }
1561
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001562ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001563 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001564 SourceLocation ClassNameLoc,
1565 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001566 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001567 ObjCInterfaceDecl *IDecl,
1568 SourceLocation IvarLBraceLoc,
1569 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001570 ObjCCategoryDecl *CatDecl =
1571 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1572 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001573 if (IDecl) {
1574 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001575 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001576 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001577 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001578 if (ASTMutationListener *L = C.getASTMutationListener())
1579 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1580 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001581 }
1582
1583 return CatDecl;
1584}
1585
Richard Smithf7981722013-11-22 09:01:48 +00001586ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001587 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001588 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1589 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001590}
1591
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001592ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1593 return getASTContext().getObjCImplementation(
1594 const_cast<ObjCCategoryDecl*>(this));
1595}
1596
1597void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1598 getASTContext().setObjCImplementation(this, ImplD);
1599}
1600
1601
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001602//===----------------------------------------------------------------------===//
1603// ObjCCategoryImplDecl
1604//===----------------------------------------------------------------------===//
1605
David Blaikie68e081d2011-12-20 02:48:34 +00001606void ObjCCategoryImplDecl::anchor() { }
1607
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001608ObjCCategoryImplDecl *
1609ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001610 IdentifierInfo *Id,
1611 ObjCInterfaceDecl *ClassInterface,
1612 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001613 SourceLocation atStartLoc,
1614 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001615 if (ClassInterface && ClassInterface->hasDefinition())
1616 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001617 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1618 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001619}
1620
Douglas Gregor72172e92012-01-05 21:55:30 +00001621ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1622 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001623 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1624 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001625}
1626
Steve Narofff406f4d2009-10-29 21:11:04 +00001627ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001628 // The class interface might be NULL if we are working with invalid code.
1629 if (const ObjCInterfaceDecl *ID = getClassInterface())
1630 return ID->FindCategoryDeclaration(getIdentifier());
1631 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001632}
1633
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001634
David Blaikie68e081d2011-12-20 02:48:34 +00001635void ObjCImplDecl::anchor() { }
1636
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001637void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001638 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001639 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001640 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001641}
1642
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001643void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1644 ASTContext &Ctx = getASTContext();
1645
1646 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001647 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001648 if (IFace)
1649 Ctx.setObjCImplementation(IFace, ImplD);
1650
Duncan Sands49c29ee2009-07-21 07:56:29 +00001651 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001652 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1653 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1654 Ctx.setObjCImplementation(CD, ImplD);
1655 }
1656
1657 ClassInterface = IFace;
1658}
1659
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001660/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001661/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001662/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001663///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001664ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001665FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1666 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001667 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001668 if (PID->getPropertyIvarDecl() &&
1669 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1670 return PID;
1671 }
1672 return 0;
1673}
1674
1675/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001676/// added to the list of those properties \@synthesized/\@dynamic in this
1677/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001678///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001679ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001680FindPropertyImplDecl(IdentifierInfo *Id) const {
1681 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001682 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001683 if (PID->getPropertyDecl()->getIdentifier() == Id)
1684 return PID;
1685 }
1686 return 0;
1687}
1688
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001689raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001690 const ObjCCategoryImplDecl &CID) {
1691 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001692 return OS;
1693}
1694
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001695//===----------------------------------------------------------------------===//
1696// ObjCImplementationDecl
1697//===----------------------------------------------------------------------===//
1698
David Blaikie68e081d2011-12-20 02:48:34 +00001699void ObjCImplementationDecl::anchor() { }
1700
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001701ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001702ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001703 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001704 ObjCInterfaceDecl *SuperDecl,
1705 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001706 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001707 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001708 SourceLocation IvarLBraceLoc,
1709 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001710 if (ClassInterface && ClassInterface->hasDefinition())
1711 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001712 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1713 nameLoc, atStartLoc, superLoc,
1714 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001715}
1716
Douglas Gregor72172e92012-01-05 21:55:30 +00001717ObjCImplementationDecl *
1718ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001719 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1720 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001721}
1722
John McCall0410e572011-07-22 04:15:06 +00001723void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1724 CXXCtorInitializer ** initializers,
1725 unsigned numInitializers) {
1726 if (numInitializers > 0) {
1727 NumIvarInitializers = numInitializers;
1728 CXXCtorInitializer **ivarInitializers =
1729 new (C) CXXCtorInitializer*[NumIvarInitializers];
1730 memcpy(ivarInitializers, initializers,
1731 numInitializers * sizeof(CXXCtorInitializer*));
1732 IvarInitializers = ivarInitializers;
1733 }
1734}
1735
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001736raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001737 const ObjCImplementationDecl &ID) {
1738 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001739 return OS;
1740}
1741
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001742//===----------------------------------------------------------------------===//
1743// ObjCCompatibleAliasDecl
1744//===----------------------------------------------------------------------===//
1745
David Blaikie68e081d2011-12-20 02:48:34 +00001746void ObjCCompatibleAliasDecl::anchor() { }
1747
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001748ObjCCompatibleAliasDecl *
1749ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1750 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001751 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001752 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001753 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001754}
1755
Douglas Gregor72172e92012-01-05 21:55:30 +00001756ObjCCompatibleAliasDecl *
1757ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001758 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001759}
1760
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001761//===----------------------------------------------------------------------===//
1762// ObjCPropertyDecl
1763//===----------------------------------------------------------------------===//
1764
David Blaikie68e081d2011-12-20 02:48:34 +00001765void ObjCPropertyDecl::anchor() { }
1766
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001767ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1768 SourceLocation L,
1769 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001770 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001771 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001772 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001774 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001775}
1776
Richard Smithf7981722013-11-22 09:01:48 +00001777ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001778 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001779 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1780 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001781}
1782
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001783//===----------------------------------------------------------------------===//
1784// ObjCPropertyImplDecl
1785//===----------------------------------------------------------------------===//
1786
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001787ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001788 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001789 SourceLocation atLoc,
1790 SourceLocation L,
1791 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001792 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001793 ObjCIvarDecl *ivar,
1794 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001795 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1796 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001797}
Chris Lattnered0e1642008-03-17 01:19:02 +00001798
Richard Smithf7981722013-11-22 09:01:48 +00001799ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001800 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001801 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1802 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001803}
1804
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001805SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1806 SourceLocation EndLoc = getLocation();
1807 if (IvarLoc.isValid())
1808 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001809
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001810 return SourceRange(AtLoc, EndLoc);
1811}