blob: cc3459476adc7dbc7a4ede2ff60695c59b1cccaf [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
Ted Kremenek33f50432013-11-23 22:29:06 +0000259bool ObjCInterfaceDecl::shouldSuppressProtocol(const ObjCProtocolDecl *P) const{
260 if (!hasAttrs())
261 return false;
Ted Kremenek33f50432013-11-23 22:29:06 +0000262 const IdentifierInfo *PI = P->getIdentifier();
Ted Kremenek5ec76a02013-11-23 22:51:36 +0000263 for (specific_attr_iterator<ObjCSuppressProtocolAttr>
264 I = specific_attr_begin<ObjCSuppressProtocolAttr>(),
265 E = specific_attr_end<ObjCSuppressProtocolAttr>(); I != E; ++I)
266 if ((*I)->getProtocol() == PI)
Ted Kremenek33f50432013-11-23 22:29:06 +0000267 return true;
Ted Kremenek33f50432013-11-23 22:29:06 +0000268 return false;
269}
270
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000271/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
272/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000273/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000274///
275ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000276ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000277 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000278 // FIXME: Should make sure no callers ever do this.
279 if (!hasDefinition())
280 return 0;
281
282 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000283 LoadExternalDefinition();
284
Ted Kremenekd133a862010-03-15 20:30:07 +0000285 if (ObjCPropertyDecl *PD =
286 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
287 return PD;
288
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000289 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000290 for (ObjCInterfaceDecl::all_protocol_iterator
291 I = all_referenced_protocol_begin(),
292 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000293 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
294 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000295
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000296 return 0;
297}
298
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000299void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
300 PropertyDeclOrder &PO) const {
Anna Zaks673d76b2012-10-18 19:17:53 +0000301 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
302 E = prop_end(); P != E; ++P) {
303 ObjCPropertyDecl *Prop = *P;
304 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000305 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000306 }
307 for (ObjCInterfaceDecl::all_protocol_iterator
308 PI = all_referenced_protocol_begin(),
309 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000310 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000311 // Note, the properties declared only in class extensions are still copied
312 // into the main @interface's property list, and therefore we don't
313 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000314}
315
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000316bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
317 const ObjCInterfaceDecl *Class = this;
318 while (Class) {
319 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
320 return true;
321 Class = Class->getSuperClass();
322 }
323 return false;
324}
325
326const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
327 const ObjCInterfaceDecl *Class = this;
328 while (Class) {
329 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
330 return Class;
331 Class = Class->getSuperClass();
332 }
333 return 0;
334}
335
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000336void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
337 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
338 ASTContext &C)
339{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000341 LoadExternalDefinition();
342
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000343 if (data().AllReferencedProtocols.empty() &&
344 data().ReferencedProtocols.empty()) {
345 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000346 return;
347 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000348
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000349 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000350 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000351 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000352 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000353 for (unsigned i = 0; i < ExtNum; i++) {
354 bool protocolExists = false;
355 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000356 for (all_protocol_iterator
357 p = all_referenced_protocol_begin(),
358 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000359 ObjCProtocolDecl *Proto = (*p);
360 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
361 protocolExists = true;
362 break;
363 }
364 }
365 // Do we want to warn on a protocol in extension class which
366 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000367 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000368 ProtocolRefs.push_back(ProtoInExtension);
369 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000370
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000371 if (ProtocolRefs.empty())
372 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000373
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000374 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000375 for (all_protocol_iterator p = all_referenced_protocol_begin(),
376 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000377 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000378 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000379
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000380 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000381}
382
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000383void ObjCInterfaceDecl::allocateDefinitionData() {
384 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000385 Data.setPointer(new (getASTContext()) DefinitionData());
386 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000387
388 // Make the type point at the definition, now that we have one.
389 if (TypeForDecl)
390 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000391}
392
393void ObjCInterfaceDecl::startDefinition() {
394 allocateDefinitionData();
395
Douglas Gregor66b310c2011-12-15 18:03:09 +0000396 // Update all of the declarations with a pointer to the definition.
397 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
398 RD != RDEnd; ++RD) {
399 if (*RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000400 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000401 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000402}
403
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000404ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
405 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000406 // FIXME: Should make sure no callers ever do this.
407 if (!hasDefinition())
408 return 0;
409
410 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000411 LoadExternalDefinition();
412
Chris Lattner89375192008-03-16 00:19:01 +0000413 ObjCInterfaceDecl* ClassDecl = this;
414 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000415 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000416 clsDeclared = ClassDecl;
417 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000418 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000419
420 for (ObjCInterfaceDecl::visible_extensions_iterator
421 Ext = ClassDecl->visible_extensions_begin(),
422 ExtEnd = ClassDecl->visible_extensions_end();
423 Ext != ExtEnd; ++Ext) {
424 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000425 clsDeclared = ClassDecl;
426 return I;
427 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000428 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000429
Chris Lattner89375192008-03-16 00:19:01 +0000430 ClassDecl = ClassDecl->getSuperClass();
431 }
432 return NULL;
433}
434
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000435/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
436/// class whose name is passed as argument. If it is not one of the super classes
437/// the it returns NULL.
438ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
439 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000440 // FIXME: Should make sure no callers ever do this.
441 if (!hasDefinition())
442 return 0;
443
444 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000445 LoadExternalDefinition();
446
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000447 ObjCInterfaceDecl* ClassDecl = this;
448 while (ClassDecl != NULL) {
449 if (ClassDecl->getIdentifier() == ICName)
450 return ClassDecl;
451 ClassDecl = ClassDecl->getSuperClass();
452 }
453 return NULL;
454}
455
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000456ObjCProtocolDecl *
457ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
458 for (ObjCInterfaceDecl::all_protocol_iterator P =
459 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
460 P != PE; ++P)
461 if ((*P)->lookupProtocolNamed(Name))
462 return (*P);
463 ObjCInterfaceDecl *SuperClass = getSuperClass();
464 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
465}
466
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000467/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000468/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000469/// When argument category "C" is specified, any implicit method found
470/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000471ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000472 bool isInstance,
473 bool shallowCategoryLookup,
474 bool followSuper,
Ted Kremenek28eace62013-11-23 01:01:34 +0000475 const ObjCCategoryDecl *C,
476 const ObjCProtocolDecl *P) const
Ted Kremenek00781502013-11-23 01:01:29 +0000477{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000478 // FIXME: Should make sure no callers ever do this.
479 if (!hasDefinition())
480 return 0;
481
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000482 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000483 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000485 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000486 LoadExternalDefinition();
487
Ted Kremenek00781502013-11-23 01:01:29 +0000488 while (ClassDecl) {
Ted Kremenek28eace62013-11-23 01:01:34 +0000489 // If we are looking for a method that is part of protocol conformance,
490 // check if the superclass has been marked to suppress conformance
491 // of that protocol.
Ted Kremenek33f50432013-11-23 22:29:06 +0000492 if (P && ClassDecl->shouldSuppressProtocol(P))
493 return 0;
Ted Kremenek28eace62013-11-23 01:01:34 +0000494
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000495 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000496 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner89375192008-03-16 00:19:01 +0000498 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000499 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
500 E = ClassDecl->protocol_end();
501 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000502 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000503 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000504
505 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000506 for (ObjCInterfaceDecl::visible_categories_iterator
507 Cat = ClassDecl->visible_categories_begin(),
508 CatEnd = ClassDecl->visible_categories_end();
509 Cat != CatEnd; ++Cat) {
510 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
511 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000512 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000513
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000514 if (!shallowCategoryLookup) {
515 // Didn't find one yet - look through protocols.
516 const ObjCList<ObjCProtocolDecl> &Protocols =
517 Cat->getReferencedProtocols();
518 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
519 E = Protocols.end(); I != E; ++I)
520 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
521 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000522 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000523 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000524 }
Ted Kremenek00781502013-11-23 01:01:29 +0000525
526 if (!followSuper)
527 return NULL;
528
529 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000530 ClassDecl = ClassDecl->getSuperClass();
531 }
532 return NULL;
533}
534
Anna Zaksc77a3b12012-07-27 19:07:44 +0000535// Will search "local" class/category implementations for a method decl.
536// If failed, then we search in class's root for an instance method.
537// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000538ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
539 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000540 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000541 // FIXME: Should make sure no callers ever do this.
542 if (!hasDefinition())
543 return 0;
544
545 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000546 LoadExternalDefinition();
547
Steve Naroffbb69c942009-10-01 23:46:04 +0000548 ObjCMethodDecl *Method = 0;
549 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000550 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
551 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000552
553 // Look through local category implementations associated with the class.
554 if (!Method)
555 Method = Instance ? getCategoryInstanceMethod(Sel)
556 : getCategoryClassMethod(Sel);
557
558 // Before we give up, check if the selector is an instance method.
559 // But only in the root. This matches gcc's behavior and what the
560 // runtime expects.
561 if (!Instance && !Method && !getSuperClass()) {
562 Method = lookupInstanceMethod(Sel);
563 // Look through local category implementations associated
564 // with the root class.
565 if (!Method)
566 Method = lookupPrivateMethod(Sel, true);
567 }
568
Steve Naroffbb69c942009-10-01 23:46:04 +0000569 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000570 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000571 return Method;
572}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000573
574//===----------------------------------------------------------------------===//
575// ObjCMethodDecl
576//===----------------------------------------------------------------------===//
577
578ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000579 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000580 SourceLocation endLoc,
581 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000582 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000583 DeclContext *contextDecl,
584 bool isInstance,
585 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000586 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000587 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000588 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000589 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000590 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000591 return new (C, contextDecl) ObjCMethodDecl(
592 beginLoc, endLoc, SelInfo, T, ResultTInfo, contextDecl, isInstance,
593 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
594 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000595}
596
Douglas Gregor72172e92012-01-05 21:55:30 +0000597ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000598 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
599 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000600}
601
Douglas Gregora6017bb2012-10-09 17:21:28 +0000602Stmt *ObjCMethodDecl::getBody() const {
603 return Body.get(getASTContext().getExternalSource());
604}
605
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000606void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
607 assert(PrevMethod);
608 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
609 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000610 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000611}
612
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000613void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
614 ArrayRef<ParmVarDecl*> Params,
615 ArrayRef<SourceLocation> SelLocs) {
616 ParamsAndSelLocs = 0;
617 NumParams = Params.size();
618 if (Params.empty() && SelLocs.empty())
619 return;
620
621 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
622 sizeof(SourceLocation) * SelLocs.size();
623 ParamsAndSelLocs = C.Allocate(Size);
624 std::copy(Params.begin(), Params.end(), getParams());
625 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
626}
627
628void ObjCMethodDecl::getSelectorLocs(
629 SmallVectorImpl<SourceLocation> &SelLocs) const {
630 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
631 SelLocs.push_back(getSelectorLoc(i));
632}
633
634void ObjCMethodDecl::setMethodParams(ASTContext &C,
635 ArrayRef<ParmVarDecl*> Params,
636 ArrayRef<SourceLocation> SelLocs) {
637 assert((!SelLocs.empty() || isImplicit()) &&
638 "No selector locs for non-implicit method");
639 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000640 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000641
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000642 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
643 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000644 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000645 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000646
647 setParamsAndSelLocs(C, Params, SelLocs);
648}
649
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000650/// \brief A definition will return its interface declaration.
651/// An interface declaration will return its definition.
652/// Otherwise it will return itself.
653ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
654 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000655 ObjCMethodDecl *Redecl = 0;
656 if (HasRedeclaration)
657 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000658 if (Redecl)
659 return Redecl;
660
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000661 Decl *CtxD = cast<Decl>(getDeclContext());
662
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000663 if (!CtxD->isInvalidDecl()) {
664 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
665 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
666 if (!ImplD->isInvalidDecl())
667 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000668
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000669 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
670 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
671 if (!ImplD->isInvalidDecl())
672 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000673
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000674 } else if (ObjCImplementationDecl *ImplD =
675 dyn_cast<ObjCImplementationDecl>(CtxD)) {
676 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
677 if (!IFD->isInvalidDecl())
678 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000679
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000680 } else if (ObjCCategoryImplDecl *CImplD =
681 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
682 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
683 if (!CatD->isInvalidDecl())
684 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
685 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000686 }
687
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000688 if (!Redecl && isRedeclaration()) {
689 // This is the last redeclaration, go back to the first method.
690 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
691 isInstanceMethod());
692 }
693
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000694 return Redecl ? Redecl : this;
695}
696
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000697ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
698 Decl *CtxD = cast<Decl>(getDeclContext());
699
700 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
701 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
702 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
703 isInstanceMethod()))
704 return MD;
705
706 } else if (ObjCCategoryImplDecl *CImplD =
707 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000708 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000709 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
710 isInstanceMethod()))
711 return MD;
712 }
713
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000714 if (isRedeclaration())
715 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
716 isInstanceMethod());
717
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000718 return this;
719}
720
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000721SourceLocation ObjCMethodDecl::getLocEnd() const {
722 if (Stmt *Body = getBody())
723 return Body->getLocEnd();
724 return DeclEndLoc;
725}
726
John McCallb4526252011-03-02 01:50:55 +0000727ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
728 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000729 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000730 return family;
731
John McCall86bc21f2011-03-02 11:33:24 +0000732 // Check for an explicit attribute.
733 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
734 // The unfortunate necessity of mapping between enums here is due
735 // to the attributes framework.
736 switch (attr->getFamily()) {
737 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
738 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
739 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
740 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
741 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
742 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
743 }
744 Family = static_cast<unsigned>(family);
745 return family;
746 }
747
John McCallb4526252011-03-02 01:50:55 +0000748 family = getSelector().getMethodFamily();
749 switch (family) {
750 case OMF_None: break;
751
752 // init only has a conventional meaning for an instance method, and
753 // it has to return an object.
754 case OMF_init:
755 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
756 family = OMF_None;
757 break;
758
759 // alloc/copy/new have a conventional meaning for both class and
760 // instance methods, but they require an object return.
761 case OMF_alloc:
762 case OMF_copy:
763 case OMF_mutableCopy:
764 case OMF_new:
765 if (!getResultType()->isObjCObjectPointerType())
766 family = OMF_None;
767 break;
768
769 // These selectors have a conventional meaning only for instance methods.
770 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000771 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000772 case OMF_retain:
773 case OMF_release:
774 case OMF_autorelease:
775 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000776 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000777 if (!isInstanceMethod())
778 family = OMF_None;
779 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000780
781 case OMF_performSelector:
782 if (!isInstanceMethod() ||
783 !getResultType()->isObjCIdType())
784 family = OMF_None;
785 else {
786 unsigned noParams = param_size();
787 if (noParams < 1 || noParams > 3)
788 family = OMF_None;
789 else {
790 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
791 QualType ArgT = (*it);
792 if (!ArgT->isObjCSelType()) {
793 family = OMF_None;
794 break;
795 }
796 while (--noParams) {
797 it++;
798 ArgT = (*it);
799 if (!ArgT->isObjCIdType()) {
800 family = OMF_None;
801 break;
802 }
803 }
804 }
805 }
806 break;
807
John McCallb4526252011-03-02 01:50:55 +0000808 }
809
810 // Cache the result.
811 Family = static_cast<unsigned>(family);
812 return family;
813}
814
Mike Stump11289f42009-09-09 15:08:12 +0000815void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000816 const ObjCInterfaceDecl *OID) {
817 QualType selfTy;
818 if (isInstanceMethod()) {
819 // There may be no interface context due to error in declaration
820 // of the interface (which has been reported). Recover gracefully.
821 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000822 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000823 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000824 } else {
825 selfTy = Context.getObjCIdType();
826 }
827 } else // we have a factory method.
828 selfTy = Context.getObjCClassType();
829
John McCalld4631322011-06-17 06:42:21 +0000830 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000831 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000832
David Blaikiebbafb8a2012-03-11 07:00:24 +0000833 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000834 if (isInstanceMethod()) {
835 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000836
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000837 // 'self' is always __strong. It's actually pseudo-strong except
838 // in init methods (or methods labeled ns_consumes_self), though.
839 Qualifiers qs;
840 qs.setObjCLifetime(Qualifiers::OCL_Strong);
841 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000842
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000843 // In addition, 'self' is const unless this is an init method.
844 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
845 selfTy = selfTy.withConst();
846 selfIsPseudoStrong = true;
847 }
848 }
849 else {
850 assert(isClassMethod());
851 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000852 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000853 selfIsPseudoStrong = true;
854 }
John McCall31168b02011-06-15 23:02:42 +0000855 }
856
857 ImplicitParamDecl *self
858 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
859 &Context.Idents.get("self"), selfTy);
860 setSelfDecl(self);
861
862 if (selfIsConsumed)
863 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000864
John McCalld4631322011-06-17 06:42:21 +0000865 if (selfIsPseudoStrong)
866 self->setARCPseudoStrong(true);
867
Mike Stump11289f42009-09-09 15:08:12 +0000868 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
869 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000870 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000871}
872
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000873ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
874 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
875 return ID;
876 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
877 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000878 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000879 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000880
881 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000882 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000883}
884
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000885static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
886 const ObjCMethodDecl *Method,
887 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
888 bool MovedToSuper) {
889 if (!Container)
890 return;
891
892 // In categories look for overriden methods from protocols. A method from
893 // category is not "overriden" since it is considered as the "same" method
894 // (same USR) as the one from the interface.
895 if (const ObjCCategoryDecl *
896 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
897 // Check whether we have a matching method at this category but only if we
898 // are at the super class level.
899 if (MovedToSuper)
900 if (ObjCMethodDecl *
901 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000902 Method->isInstanceMethod(),
903 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000904 if (Method != Overridden) {
905 // We found an override at this category; there is no need to look
906 // into its protocols.
907 Methods.push_back(Overridden);
908 return;
909 }
910
911 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
912 PEnd = Category->protocol_end();
913 P != PEnd; ++P)
914 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
915 return;
916 }
917
918 // Check whether we have a matching method at this level.
919 if (const ObjCMethodDecl *
920 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000921 Method->isInstanceMethod(),
922 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000923 if (Method != Overridden) {
924 // We found an override at this level; there is no need to look
925 // into other protocols or categories.
926 Methods.push_back(Overridden);
927 return;
928 }
929
930 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
931 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
932 PEnd = Protocol->protocol_end();
933 P != PEnd; ++P)
934 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
935 }
936
937 if (const ObjCInterfaceDecl *
938 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
939 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
940 PEnd = Interface->protocol_end();
941 P != PEnd; ++P)
942 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
943
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000944 for (ObjCInterfaceDecl::known_categories_iterator
945 Cat = Interface->known_categories_begin(),
946 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000947 Cat != CatEnd; ++Cat) {
948 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000949 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000950 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000951
952 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
953 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
954 /*MovedToSuper=*/true);
955 }
956}
957
958static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
959 const ObjCMethodDecl *Method,
960 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
961 CollectOverriddenMethodsRecurse(Container, Method, Methods,
962 /*MovedToSuper=*/false);
963}
964
965static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
966 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
967 assert(Method->isOverriding());
968
969 if (const ObjCProtocolDecl *
970 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
971 CollectOverriddenMethods(ProtD, Method, overridden);
972
973 } else if (const ObjCImplDecl *
974 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
975 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
976 if (!ID)
977 return;
978 // Start searching for overridden methods using the method from the
979 // interface as starting point.
980 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000981 Method->isInstanceMethod(),
982 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000983 Method = IFaceMeth;
984 CollectOverriddenMethods(ID, Method, overridden);
985
986 } else if (const ObjCCategoryDecl *
987 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
988 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
989 if (!ID)
990 return;
991 // Start searching for overridden methods using the method from the
992 // interface as starting point.
993 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000994 Method->isInstanceMethod(),
995 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000996 Method = IFaceMeth;
997 CollectOverriddenMethods(ID, Method, overridden);
998
999 } else {
1000 CollectOverriddenMethods(
1001 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1002 Method, overridden);
1003 }
1004}
1005
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001006void ObjCMethodDecl::getOverriddenMethods(
1007 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1008 const ObjCMethodDecl *Method = this;
1009
1010 if (Method->isRedeclaration()) {
1011 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1012 getMethod(Method->getSelector(), Method->isInstanceMethod());
1013 }
1014
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001015 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001016 collectOverriddenMethodsSlow(Method, Overridden);
1017 assert(!Overridden.empty() &&
1018 "ObjCMethodDecl's overriding bit is not as expected");
1019 }
1020}
1021
Jordan Rose2bd991a2012-10-10 16:42:54 +00001022const ObjCPropertyDecl *
1023ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1024 Selector Sel = getSelector();
1025 unsigned NumArgs = Sel.getNumArgs();
1026 if (NumArgs > 1)
1027 return 0;
1028
Jordan Rose59e34ec2012-10-11 16:02:02 +00001029 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001030 return 0;
1031
1032 if (isPropertyAccessor()) {
1033 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001034 // If container is class extension, find its primary class.
1035 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1036 if (CatDecl->IsClassExtension())
1037 Container = CatDecl->getClassInterface();
1038
Jordan Rose2bd991a2012-10-10 16:42:54 +00001039 bool IsGetter = (NumArgs == 0);
1040
1041 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1042 E = Container->prop_end();
1043 I != E; ++I) {
1044 Selector NextSel = IsGetter ? (*I)->getGetterName()
1045 : (*I)->getSetterName();
1046 if (NextSel == Sel)
1047 return *I;
1048 }
1049
1050 llvm_unreachable("Marked as a property accessor but no property found!");
1051 }
1052
1053 if (!CheckOverrides)
1054 return 0;
1055
1056 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1057 OverridesTy Overrides;
1058 getOverriddenMethods(Overrides);
1059 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1060 I != E; ++I) {
1061 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1062 return Prop;
1063 }
1064
1065 return 0;
1066
1067}
1068
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001069//===----------------------------------------------------------------------===//
1070// ObjCInterfaceDecl
1071//===----------------------------------------------------------------------===//
1072
Douglas Gregord53ae832012-01-17 18:09:05 +00001073ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001074 DeclContext *DC,
1075 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001076 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001077 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001078 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001079 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001080 ObjCInterfaceDecl *Result = new (C, DC)
1081 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001082 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001083 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001084 return Result;
1085}
1086
Richard Smithf7981722013-11-22 09:01:48 +00001087ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001088 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001089 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1090 0, SourceLocation(),
1091 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001092 Result->Data.setInt(!C.getLangOpts().Modules);
1093 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001094}
1095
1096ObjCInterfaceDecl::
1097ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001098 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1099 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001100 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001101 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001102{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001103 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001104
1105 // Copy the 'data' pointer over.
1106 if (PrevDecl)
1107 Data = PrevDecl->Data;
1108
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001109 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001110}
1111
Douglas Gregor73693022010-12-01 23:49:52 +00001112void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001113 assert(data().ExternallyCompleted && "Class is not externally completed");
1114 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001115 getASTContext().getExternalSource()->CompleteType(
1116 const_cast<ObjCInterfaceDecl *>(this));
1117}
1118
1119void ObjCInterfaceDecl::setExternallyCompleted() {
1120 assert(getASTContext().getExternalSource() &&
1121 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001123 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001124 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001125}
1126
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001127ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001128 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1129 if (data().ExternallyCompleted)
1130 LoadExternalDefinition();
1131
1132 return getASTContext().getObjCImplementation(
1133 const_cast<ObjCInterfaceDecl*>(Def));
1134 }
1135
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001136 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001137 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001138}
1139
1140void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001141 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001142}
1143
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001144namespace {
1145 struct SynthesizeIvarChunk {
1146 uint64_t Size;
1147 ObjCIvarDecl *Ivar;
1148 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1149 : Size(size), Ivar(ivar) {}
1150 };
1151
1152 bool operator<(const SynthesizeIvarChunk & LHS,
1153 const SynthesizeIvarChunk &RHS) {
1154 return LHS.Size < RHS.Size;
1155 }
1156}
1157
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001158/// all_declared_ivar_begin - return first ivar declared in this class,
1159/// its extensions and its implementation. Lazily build the list on first
1160/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001161///
1162/// Caveat: The list returned by this method reflects the current
1163/// state of the parser. The cache will be updated for every ivar
1164/// added by an extension or the implementation when they are
1165/// encountered.
1166/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001167ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001168 // FIXME: Should make sure no callers ever do this.
1169 if (!hasDefinition())
1170 return 0;
1171
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001172 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001173 if (!data().IvarList) {
1174 if (!ivar_empty()) {
1175 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1176 data().IvarList = *I; ++I;
1177 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001178 curIvar->setNextIvar(*I);
1179 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001180
1181 for (ObjCInterfaceDecl::known_extensions_iterator
1182 Ext = known_extensions_begin(),
1183 ExtEnd = known_extensions_end();
1184 Ext != ExtEnd; ++Ext) {
1185 if (!Ext->ivar_empty()) {
1186 ObjCCategoryDecl::ivar_iterator
1187 I = Ext->ivar_begin(),
1188 E = Ext->ivar_end();
1189 if (!data().IvarList) {
1190 data().IvarList = *I; ++I;
1191 curIvar = data().IvarList;
1192 }
1193 for ( ;I != E; curIvar = *I, ++I)
1194 curIvar->setNextIvar(*I);
1195 }
1196 }
1197 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001198 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001199
1200 // cached and complete!
1201 if (!data().IvarListMissingImplementation)
1202 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001203
1204 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001205 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001206 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001207 SmallVector<SynthesizeIvarChunk, 16> layout;
1208 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1209 E = ImplDecl->ivar_end(); I != E; ++I) {
1210 ObjCIvarDecl *IV = *I;
1211 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1212 layout.push_back(SynthesizeIvarChunk(
1213 IV->getASTContext().getTypeSize(IV->getType()), IV));
1214 continue;
1215 }
1216 if (!data().IvarList)
1217 data().IvarList = *I;
1218 else
1219 curIvar->setNextIvar(*I);
1220 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001221 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001222
1223 if (!layout.empty()) {
1224 // Order synthesized ivars by their size.
1225 std::stable_sort(layout.begin(), layout.end());
1226 unsigned Ix = 0, EIx = layout.size();
1227 if (!data().IvarList) {
1228 data().IvarList = layout[0].Ivar; Ix++;
1229 curIvar = data().IvarList;
1230 }
1231 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1232 curIvar->setNextIvar(layout[Ix].Ivar);
1233 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001234 }
1235 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001236 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001237}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001238
1239/// FindCategoryDeclaration - Finds category declaration in the list of
1240/// categories for this class and returns it. Name of the category is passed
1241/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001242///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001243ObjCCategoryDecl *
1244ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001245 // FIXME: Should make sure no callers ever do this.
1246 if (!hasDefinition())
1247 return 0;
1248
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001249 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001250 LoadExternalDefinition();
1251
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001252 for (visible_categories_iterator Cat = visible_categories_begin(),
1253 CatEnd = visible_categories_end();
1254 Cat != CatEnd;
1255 ++Cat) {
1256 if (Cat->getIdentifier() == CategoryId)
1257 return *Cat;
1258 }
1259
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001260 return 0;
1261}
1262
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001263ObjCMethodDecl *
1264ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001265 for (visible_categories_iterator Cat = visible_categories_begin(),
1266 CatEnd = visible_categories_end();
1267 Cat != CatEnd;
1268 ++Cat) {
1269 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001270 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1271 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001272 }
1273
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001274 return 0;
1275}
1276
1277ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001278 for (visible_categories_iterator Cat = visible_categories_begin(),
1279 CatEnd = visible_categories_end();
1280 Cat != CatEnd;
1281 ++Cat) {
1282 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001283 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1284 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001285 }
1286
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001287 return 0;
1288}
1289
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001290/// ClassImplementsProtocol - Checks that 'lProto' protocol
1291/// has been implemented in IDecl class, its super class or categories (if
1292/// lookupCategory is true).
1293bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1294 bool lookupCategory,
1295 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001296 if (!hasDefinition())
1297 return false;
1298
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001299 ObjCInterfaceDecl *IDecl = this;
1300 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001301 for (ObjCInterfaceDecl::protocol_iterator
1302 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001303 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1304 return true;
1305 // This is dubious and is added to be compatible with gcc. In gcc, it is
1306 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1307 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1308 // object. This IMO, should be a bug.
1309 // FIXME: Treat this as an extension, and flag this as an error when GCC
1310 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001311 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001312 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1313 return true;
1314 }
Mike Stump11289f42009-09-09 15:08:12 +00001315
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001316 // 2nd, look up the category.
1317 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001318 for (visible_categories_iterator Cat = visible_categories_begin(),
1319 CatEnd = visible_categories_end();
1320 Cat != CatEnd;
1321 ++Cat) {
1322 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1323 E = Cat->protocol_end();
1324 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001325 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1326 return true;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001329 // 3rd, look up the super class(s)
1330 if (IDecl->getSuperClass())
1331 return
1332 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1333 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001334
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001335 return false;
1336}
1337
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001338//===----------------------------------------------------------------------===//
1339// ObjCIvarDecl
1340//===----------------------------------------------------------------------===//
1341
David Blaikie68e081d2011-12-20 02:48:34 +00001342void ObjCIvarDecl::anchor() { }
1343
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001344ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001345 SourceLocation StartLoc,
1346 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001347 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001348 AccessControl ac, Expr *BW,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001349 bool synthesized,
1350 bool backingIvarReferencedInAccessor) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001351 if (DC) {
1352 // Ivar's can only appear in interfaces, implementations (via synthesized
1353 // properties), and class extensions (via direct declaration, or synthesized
1354 // properties).
1355 //
1356 // FIXME: This should really be asserting this:
1357 // (isa<ObjCCategoryDecl>(DC) &&
1358 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1359 // but unfortunately we sometimes place ivars into non-class extension
1360 // categories on error. This breaks an AST invariant, and should not be
1361 // fixed.
1362 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1363 isa<ObjCCategoryDecl>(DC)) &&
1364 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001365 // Once a new ivar is created in any of class/class-extension/implementation
1366 // decl contexts, the previously built IvarList must be rebuilt.
1367 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1368 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001369 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001370 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001371 else
1372 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001373 }
1374 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001375 }
1376
Richard Smithf7981722013-11-22 09:01:48 +00001377 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1378 synthesized, backingIvarReferencedInAccessor);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001379}
1380
Douglas Gregor72172e92012-01-05 21:55:30 +00001381ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001382 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1383 QualType(), 0, ObjCIvarDecl::None, 0, false,
1384 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001385}
1386
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001387const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1388 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001389
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001390 switch (DC->getKind()) {
1391 default:
1392 case ObjCCategoryImpl:
1393 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001394 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001395
1396 // Ivars can only appear in class extension categories.
1397 case ObjCCategory: {
1398 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1399 assert(CD->IsClassExtension() && "invalid container for ivar!");
1400 return CD->getClassInterface();
1401 }
1402
1403 case ObjCImplementation:
1404 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1405
1406 case ObjCInterface:
1407 return cast<ObjCInterfaceDecl>(DC);
1408 }
1409}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001410
1411//===----------------------------------------------------------------------===//
1412// ObjCAtDefsFieldDecl
1413//===----------------------------------------------------------------------===//
1414
David Blaikie68e081d2011-12-20 02:48:34 +00001415void ObjCAtDefsFieldDecl::anchor() { }
1416
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001417ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001418*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1419 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001420 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001421 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001422}
1423
Richard Smithf7981722013-11-22 09:01:48 +00001424ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001425 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001426 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1427 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001428}
1429
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001430//===----------------------------------------------------------------------===//
1431// ObjCProtocolDecl
1432//===----------------------------------------------------------------------===//
1433
David Blaikie68e081d2011-12-20 02:48:34 +00001434void ObjCProtocolDecl::anchor() { }
1435
Douglas Gregor32c17572012-01-01 20:30:41 +00001436ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1437 SourceLocation nameLoc,
1438 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001439 ObjCProtocolDecl *PrevDecl)
1440 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001441{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001442 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001443 if (PrevDecl)
1444 Data = PrevDecl->Data;
1445}
1446
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001447ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001448 IdentifierInfo *Id,
1449 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001450 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001451 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001452 ObjCProtocolDecl *Result =
1453 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001454 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001455 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001456}
1457
Richard Smithf7981722013-11-22 09:01:48 +00001458ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001459 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001460 ObjCProtocolDecl *Result =
1461 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001462 Result->Data.setInt(!C.getLangOpts().Modules);
1463 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001464}
1465
Steve Naroff114aecb2009-03-01 16:12:44 +00001466ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1467 ObjCProtocolDecl *PDecl = this;
1468
1469 if (Name == getIdentifier())
1470 return PDecl;
1471
1472 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1473 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1474 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001475
Steve Naroff114aecb2009-03-01 16:12:44 +00001476 return NULL;
1477}
1478
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001479// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001480// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001481ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1482 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001483 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001484
Douglas Gregoreed49792013-01-17 00:38:46 +00001485 // If there is no definition or the definition is hidden, we don't find
1486 // anything.
1487 const ObjCProtocolDecl *Def = getDefinition();
1488 if (!Def || Def->isHidden())
1489 return NULL;
1490
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001491 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001492 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001493
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001494 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001495 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001496 return MethodDecl;
1497 return NULL;
1498}
1499
Douglas Gregore6e48b12012-01-01 19:29:29 +00001500void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001501 assert(!Data.getPointer() && "Protocol already has a definition!");
1502 Data.setPointer(new (getASTContext()) DefinitionData);
1503 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001504}
1505
1506void ObjCProtocolDecl::startDefinition() {
1507 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001508
1509 // Update all of the declarations with a pointer to the definition.
1510 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1511 RD != RDEnd; ++RD)
1512 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001513}
1514
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001515void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1516 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001517
1518 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1519 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1520 E = PDecl->prop_end(); P != E; ++P) {
1521 ObjCPropertyDecl *Prop = *P;
1522 // Insert into PM if not there already.
1523 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001524 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001525 }
1526 // Scan through protocol's protocols.
1527 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1528 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001529 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001530 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001531}
1532
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001533
1534void ObjCProtocolDecl::collectInheritedProtocolProperties(
1535 const ObjCPropertyDecl *Property,
1536 ProtocolPropertyMap &PM) const {
1537 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1538 bool MatchFound = false;
1539 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1540 E = PDecl->prop_end(); P != E; ++P) {
1541 ObjCPropertyDecl *Prop = *P;
1542 if (Prop == Property)
1543 continue;
1544 if (Prop->getIdentifier() == Property->getIdentifier()) {
1545 PM[PDecl] = Prop;
1546 MatchFound = true;
1547 break;
1548 }
1549 }
1550 // Scan through protocol's protocols which did not have a matching property.
1551 if (!MatchFound)
1552 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1553 E = PDecl->protocol_end(); PI != E; ++PI)
1554 (*PI)->collectInheritedProtocolProperties(Property, PM);
1555 }
1556}
Anna Zaks673d76b2012-10-18 19:17:53 +00001557
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001558//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001559// ObjCCategoryDecl
1560//===----------------------------------------------------------------------===//
1561
David Blaikie68e081d2011-12-20 02:48:34 +00001562void ObjCCategoryDecl::anchor() { }
1563
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001564ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001565 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001566 SourceLocation ClassNameLoc,
1567 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001568 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001569 ObjCInterfaceDecl *IDecl,
1570 SourceLocation IvarLBraceLoc,
1571 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001572 ObjCCategoryDecl *CatDecl =
1573 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1574 IDecl, 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
Richard Smithf7981722013-11-22 09:01:48 +00001588ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001589 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001590 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1591 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001592}
1593
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001594ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1595 return getASTContext().getObjCImplementation(
1596 const_cast<ObjCCategoryDecl*>(this));
1597}
1598
1599void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1600 getASTContext().setObjCImplementation(this, ImplD);
1601}
1602
1603
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001604//===----------------------------------------------------------------------===//
1605// ObjCCategoryImplDecl
1606//===----------------------------------------------------------------------===//
1607
David Blaikie68e081d2011-12-20 02:48:34 +00001608void ObjCCategoryImplDecl::anchor() { }
1609
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001610ObjCCategoryImplDecl *
1611ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001612 IdentifierInfo *Id,
1613 ObjCInterfaceDecl *ClassInterface,
1614 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001615 SourceLocation atStartLoc,
1616 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001617 if (ClassInterface && ClassInterface->hasDefinition())
1618 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001619 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1620 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001621}
1622
Douglas Gregor72172e92012-01-05 21:55:30 +00001623ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1624 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001625 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1626 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001627}
1628
Steve Narofff406f4d2009-10-29 21:11:04 +00001629ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001630 // The class interface might be NULL if we are working with invalid code.
1631 if (const ObjCInterfaceDecl *ID = getClassInterface())
1632 return ID->FindCategoryDeclaration(getIdentifier());
1633 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001634}
1635
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001636
David Blaikie68e081d2011-12-20 02:48:34 +00001637void ObjCImplDecl::anchor() { }
1638
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001639void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001640 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001641 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001642 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001643}
1644
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001645void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1646 ASTContext &Ctx = getASTContext();
1647
1648 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001649 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001650 if (IFace)
1651 Ctx.setObjCImplementation(IFace, ImplD);
1652
Duncan Sands49c29ee2009-07-21 07:56:29 +00001653 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001654 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1655 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1656 Ctx.setObjCImplementation(CD, ImplD);
1657 }
1658
1659 ClassInterface = IFace;
1660}
1661
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001662/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001663/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001664/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001665///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001666ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001667FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1668 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001669 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001670 if (PID->getPropertyIvarDecl() &&
1671 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1672 return PID;
1673 }
1674 return 0;
1675}
1676
1677/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001678/// added to the list of those properties \@synthesized/\@dynamic in this
1679/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001680///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001681ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001682FindPropertyImplDecl(IdentifierInfo *Id) const {
1683 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001684 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001685 if (PID->getPropertyDecl()->getIdentifier() == Id)
1686 return PID;
1687 }
1688 return 0;
1689}
1690
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001691raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001692 const ObjCCategoryImplDecl &CID) {
1693 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001694 return OS;
1695}
1696
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001697//===----------------------------------------------------------------------===//
1698// ObjCImplementationDecl
1699//===----------------------------------------------------------------------===//
1700
David Blaikie68e081d2011-12-20 02:48:34 +00001701void ObjCImplementationDecl::anchor() { }
1702
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001703ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001704ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001705 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001706 ObjCInterfaceDecl *SuperDecl,
1707 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001708 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001709 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001710 SourceLocation IvarLBraceLoc,
1711 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001712 if (ClassInterface && ClassInterface->hasDefinition())
1713 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001714 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1715 nameLoc, atStartLoc, superLoc,
1716 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001717}
1718
Douglas Gregor72172e92012-01-05 21:55:30 +00001719ObjCImplementationDecl *
1720ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001721 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1722 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001723}
1724
John McCall0410e572011-07-22 04:15:06 +00001725void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1726 CXXCtorInitializer ** initializers,
1727 unsigned numInitializers) {
1728 if (numInitializers > 0) {
1729 NumIvarInitializers = numInitializers;
1730 CXXCtorInitializer **ivarInitializers =
1731 new (C) CXXCtorInitializer*[NumIvarInitializers];
1732 memcpy(ivarInitializers, initializers,
1733 numInitializers * sizeof(CXXCtorInitializer*));
1734 IvarInitializers = ivarInitializers;
1735 }
1736}
1737
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001738raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001739 const ObjCImplementationDecl &ID) {
1740 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001741 return OS;
1742}
1743
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001744//===----------------------------------------------------------------------===//
1745// ObjCCompatibleAliasDecl
1746//===----------------------------------------------------------------------===//
1747
David Blaikie68e081d2011-12-20 02:48:34 +00001748void ObjCCompatibleAliasDecl::anchor() { }
1749
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001750ObjCCompatibleAliasDecl *
1751ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1752 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001753 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001754 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001755 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001756}
1757
Douglas Gregor72172e92012-01-05 21:55:30 +00001758ObjCCompatibleAliasDecl *
1759ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001760 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001761}
1762
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001763//===----------------------------------------------------------------------===//
1764// ObjCPropertyDecl
1765//===----------------------------------------------------------------------===//
1766
David Blaikie68e081d2011-12-20 02:48:34 +00001767void ObjCPropertyDecl::anchor() { }
1768
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001769ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1770 SourceLocation L,
1771 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001772 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001773 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001774 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001775 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001776 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001777}
1778
Richard Smithf7981722013-11-22 09:01:48 +00001779ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001780 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001781 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1782 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001783}
1784
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001785//===----------------------------------------------------------------------===//
1786// ObjCPropertyImplDecl
1787//===----------------------------------------------------------------------===//
1788
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001789ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001790 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001791 SourceLocation atLoc,
1792 SourceLocation L,
1793 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001794 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001795 ObjCIvarDecl *ivar,
1796 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001797 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1798 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001799}
Chris Lattnered0e1642008-03-17 01:19:02 +00001800
Richard Smithf7981722013-11-22 09:01:48 +00001801ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001802 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001803 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1804 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001805}
1806
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001807SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1808 SourceLocation EndLoc = getLocation();
1809 if (IvarLoc.isValid())
1810 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001811
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001812 return SourceRange(AtLoc, EndLoc);
1813}