blob: 563de6fd15fe83a8bdcee143526bdeac393c0ee8 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
Douglas Gregor05a2e942011-06-13 16:07:18 +0000158/// \brief Check for consistency between a given method declaration and the
159/// methods it overrides within the class hierarchy.
160///
161/// This method walks the inheritance hierarchy starting at the given
162/// declaration context (\p DC), invoking Sema::CheckObjCMethodOverride() with
163/// the given new method (\p NewMethod) and any method it directly overrides
164/// in the hierarchy. Sema::CheckObjCMethodOverride() is responsible for
165/// checking consistency, e.g., among return types for methods that return a
166/// related result type.
Douglas Gregor926df6c2011-06-11 01:09:30 +0000167static bool CheckObjCMethodOverrides(Sema &S, ObjCMethodDecl *NewMethod,
168 DeclContext *DC,
169 bool SkipCurrent = true) {
170 if (!DC)
171 return false;
172
173 if (!SkipCurrent) {
174 // Look for this method. If we find it, we're done.
175 Selector Sel = NewMethod->getSelector();
176 bool IsInstance = NewMethod->isInstanceMethod();
177 DeclContext::lookup_const_iterator Meth, MethEnd;
178 for (llvm::tie(Meth, MethEnd) = DC->lookup(Sel); Meth != MethEnd; ++Meth) {
179 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
180 if (MD && MD->isInstanceMethod() == IsInstance)
181 return S.CheckObjCMethodOverride(NewMethod, MD, false);
182 }
183 }
184
185 if (ObjCInterfaceDecl *Class = llvm::dyn_cast<ObjCInterfaceDecl>(DC)) {
186 // Look through categories.
187 for (ObjCCategoryDecl *Category = Class->getCategoryList();
188 Category; Category = Category->getNextClassCategory()) {
189 if (CheckObjCMethodOverrides(S, NewMethod, Category, false))
190 return true;
191 }
John McCallf85e1932011-06-15 23:02:42 +0000192
Douglas Gregor926df6c2011-06-11 01:09:30 +0000193 // Look through protocols.
194 for (ObjCList<ObjCProtocolDecl>::iterator I = Class->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000195 IEnd = Class->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000196 I != IEnd; ++I)
197 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
198 return true;
199
200 // Look in our superclass.
201 return CheckObjCMethodOverrides(S, NewMethod, Class->getSuperClass(),
202 false);
203 }
204
205 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC)) {
206 // Look through protocols.
207 for (ObjCList<ObjCProtocolDecl>::iterator I = Category->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000208 IEnd = Category->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000209 I != IEnd; ++I)
210 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
211 return true;
212
213 return false;
214 }
215
216 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC)) {
217 // Look through protocols.
218 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocol->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000219 IEnd = Protocol->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000220 I != IEnd; ++I)
221 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
222 return true;
223
224 return false;
225 }
226
227 return false;
228}
229
230bool Sema::CheckObjCMethodOverrides(ObjCMethodDecl *NewMethod,
231 DeclContext *DC) {
232 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(DC))
233 return ::CheckObjCMethodOverrides(*this, NewMethod, Class);
John McCallf85e1932011-06-15 23:02:42 +0000234
Douglas Gregor926df6c2011-06-11 01:09:30 +0000235 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC))
236 return ::CheckObjCMethodOverrides(*this, NewMethod, Category);
John McCallf85e1932011-06-15 23:02:42 +0000237
Douglas Gregor926df6c2011-06-11 01:09:30 +0000238 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC))
239 return ::CheckObjCMethodOverrides(*this, NewMethod, Protocol);
John McCallf85e1932011-06-15 23:02:42 +0000240
Douglas Gregor926df6c2011-06-11 01:09:30 +0000241 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(DC))
242 return ::CheckObjCMethodOverrides(*this, NewMethod,
243 Impl->getClassInterface());
244
245 if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(DC))
246 return ::CheckObjCMethodOverrides(*this, NewMethod,
247 CatImpl->getClassInterface());
248
249 return ::CheckObjCMethodOverrides(*this, NewMethod, CurContext);
250}
251
John McCallf85e1932011-06-15 23:02:42 +0000252/// \brief Check a method declaration for compatibility with the Objective-C
253/// ARC conventions.
254static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
255 ObjCMethodFamily family = method->getMethodFamily();
256 switch (family) {
257 case OMF_None:
258 case OMF_dealloc:
259 case OMF_retain:
260 case OMF_release:
261 case OMF_autorelease:
262 case OMF_retainCount:
263 case OMF_self:
264 return false;
265
266 case OMF_init:
267 // If the method doesn't obey the init rules, don't bother annotating it.
268 if (S.checkInitMethod(method, QualType()))
269 return true;
270
271 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
272 S.Context));
273
274 // Don't add a second copy of this attribute, but otherwise don't
275 // let it be suppressed.
276 if (method->hasAttr<NSReturnsRetainedAttr>())
277 return false;
278 break;
279
280 case OMF_alloc:
281 case OMF_copy:
282 case OMF_mutableCopy:
283 case OMF_new:
284 if (method->hasAttr<NSReturnsRetainedAttr>() ||
285 method->hasAttr<NSReturnsNotRetainedAttr>() ||
286 method->hasAttr<NSReturnsAutoreleasedAttr>())
287 return false;
288 break;
289 }
290
291 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
292 S.Context));
293 return false;
294}
295
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000296static void DiagnoseObjCImplementedDeprecations(Sema &S,
297 NamedDecl *ND,
298 SourceLocation ImplLoc,
299 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000300 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000301 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000302 if (select == 0)
303 S.Diag(ND->getLocation(), diag::note_method_declared_at);
304 else
305 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
306 }
307}
308
Steve Naroffebf64432009-02-28 16:59:13 +0000309/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000310/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000311void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000312 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000313 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Steve Naroff394f3f42008-07-25 17:57:26 +0000315 // If we don't have a valid method decl, simply return.
316 if (!MDecl)
317 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000318
319 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000320 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000321 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000322 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000323 AddFactoryMethodToGlobalPool(MDecl, true);
324
Chris Lattner4d391482007-12-12 07:09:47 +0000325 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000326 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000327 PushFunctionScope();
328
Chris Lattner4d391482007-12-12 07:09:47 +0000329 // Create Decl objects for each parameter, entrring them in the scope for
330 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000331
332 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000333 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar451318c2008-08-26 06:07:48 +0000335 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
336 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000337
Chris Lattner8123a952008-04-10 02:22:51 +0000338 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000339 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000340 E = MDecl->param_end(); PI != E; ++PI) {
341 ParmVarDecl *Param = (*PI);
342 if (!Param->isInvalidDecl() &&
343 RequireCompleteType(Param->getLocation(), Param->getType(),
344 diag::err_typecheck_decl_incomplete_type))
345 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000346 if ((*PI)->getIdentifier())
347 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000348 }
John McCallf85e1932011-06-15 23:02:42 +0000349
350 // In ARC, disallow definition of retain/release/autorelease/retainCount
351 if (getLangOptions().ObjCAutoRefCount) {
352 switch (MDecl->getMethodFamily()) {
353 case OMF_retain:
354 case OMF_retainCount:
355 case OMF_release:
356 case OMF_autorelease:
357 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
358 << MDecl->getSelector();
359 break;
360
361 case OMF_None:
362 case OMF_dealloc:
363 case OMF_alloc:
364 case OMF_init:
365 case OMF_mutableCopy:
366 case OMF_copy:
367 case OMF_new:
368 case OMF_self:
369 break;
370 }
371 }
372
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000373 // Warn on implementating deprecated methods under
374 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000375 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
376 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000377 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000378 DiagnoseObjCImplementedDeprecations(*this,
379 dyn_cast<NamedDecl>(IMD),
380 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +0000381}
382
John McCalld226f652010-08-21 09:40:31 +0000383Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
385 IdentifierInfo *ClassName, SourceLocation ClassLoc,
386 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000387 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000388 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000389 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000390 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner4d391482007-12-12 07:09:47 +0000392 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000393 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000394 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000395
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000397 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000398 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000401 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
402 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000403 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000404 if (!IDecl->isForwardDecl()) {
405 IDecl->setInvalidDecl();
406 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
407 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000408
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000409 // Return the previous class interface.
410 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000411 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000412 } else {
413 IDecl->setLocation(AtInterfaceLoc);
414 IDecl->setForwardDecl(false);
415 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000416 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000417 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000418 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000419
420 // Since this ObjCInterfaceDecl was created by a forward declaration,
421 // we now add it to the DeclContext since it wasn't added before
422 // (see ActOnForwardClassDeclaration).
423 IDecl->setLexicalDeclContext(CurContext);
424 CurContext->addDecl(IDecl);
425
426 if (AttrList)
427 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000428 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000429 } else {
430 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
431 ClassName, ClassLoc);
432 if (AttrList)
433 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
434
435 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner4d391482007-12-12 07:09:47 +0000438 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000439 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000440 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
441 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000442
443 if (!PrevDecl) {
444 // Try to correct for a typo in the superclass name.
445 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000446 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000447 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
448 Diag(SuperLoc, diag::err_undef_superclass_suggest)
449 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000450 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
451 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000452 }
453 }
454
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000455 if (PrevDecl == IDecl) {
456 Diag(SuperLoc, diag::err_recursive_superclass)
457 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
458 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000459 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000460 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000461 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000462
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 // Diagnose classes that inherit from deprecated classes.
464 if (SuperClassDecl)
465 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000467 if (PrevDecl && SuperClassDecl == 0) {
468 // The previous declaration was not a class decl. Check if we have a
469 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000470 if (const TypedefNameDecl *TDecl =
471 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000472 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000473 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000474 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
475 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000476 }
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000479 // This handles the following case:
480 //
481 // typedef int SuperClass;
482 // @interface MyClass : SuperClass {} @end
483 //
484 if (!SuperClassDecl) {
485 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
486 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000487 }
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Richard Smith162e1c12011-04-15 14:24:37 +0000490 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000491 if (!SuperClassDecl)
492 Diag(SuperLoc, diag::err_undef_superclass)
493 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
494 else if (SuperClassDecl->isForwardDecl())
495 Diag(SuperLoc, diag::err_undef_superclass)
496 << SuperClassDecl->getDeclName() << ClassName
497 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000498 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000499 IDecl->setSuperClass(SuperClassDecl);
500 IDecl->setSuperClassLoc(SuperLoc);
501 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000502 }
Chris Lattner4d391482007-12-12 07:09:47 +0000503 } else { // we have a root class.
504 IDecl->setLocEnd(ClassLoc);
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Sebastian Redl0b17c612010-08-13 00:28:03 +0000507 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000508 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000509 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000510 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000511 IDecl->setLocEnd(EndProtoLoc);
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson15281452008-11-04 16:57:32 +0000514 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000515 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000516}
517
518/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000519/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000520Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
521 IdentifierInfo *AliasName,
522 SourceLocation AliasLocation,
523 IdentifierInfo *ClassName,
524 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000525 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000526 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000527 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000528 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000529 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000530 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000531 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000532 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000533 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000534 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000535 }
536 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000537 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000538 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000539 if (const TypedefNameDecl *TDecl =
540 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000541 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000542 if (T->isObjCObjectType()) {
543 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000544 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000545 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000546 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000547 }
548 }
549 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000550 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
551 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000552 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000553 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000554 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000555 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000558 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000559 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000560 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Anders Carlsson15281452008-11-04 16:57:32 +0000562 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000563 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000564
John McCalld226f652010-08-21 09:40:31 +0000565 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000566}
567
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000568bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000569 IdentifierInfo *PName,
570 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000571 const ObjCList<ObjCProtocolDecl> &PList) {
572
573 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000574 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
575 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000576 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
577 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000578 if (PDecl->getIdentifier() == PName) {
579 Diag(Ploc, diag::err_protocol_has_circular_dependency);
580 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000581 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000582 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000583 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
584 PDecl->getLocation(), PDecl->getReferencedProtocols()))
585 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000586 }
587 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000588 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000589}
590
John McCalld226f652010-08-21 09:40:31 +0000591Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000592Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
593 IdentifierInfo *ProtocolName,
594 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000595 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000596 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000597 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000598 SourceLocation EndProtoLoc,
599 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000600 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000601 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000602 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000603 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000604 if (PDecl) {
605 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000606 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000607 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000608 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000609 // Just return the protocol we already had.
610 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000611 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000612 }
Steve Naroff61d68522009-03-05 15:22:01 +0000613 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000614 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000615 err = CheckForwardProtocolDeclarationForCircularDependency(
616 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Steve Narofff11b5082008-08-13 16:39:22 +0000618 // Make sure the cached decl gets a valid start location.
619 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000620 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000621 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000622 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000623 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000624 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000625 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000626 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000627 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000628 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000629 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000630 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000631 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000632 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000633 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000634 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
635 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000636 PDecl->setLocEnd(EndProtoLoc);
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
639 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000640 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000641}
642
643/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000644/// issues an error if they are not declared. It returns list of
645/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000646void
Chris Lattnere13b9592008-07-26 04:03:38 +0000647Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000648 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000649 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000650 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000651 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
653 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000654 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000655 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
656 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000657 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000658 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
659 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
660 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000661 Diag(PDecl->getLocation(), diag::note_previous_decl)
662 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000663 }
664 }
665
666 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000667 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000668 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000669 continue;
670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000672 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000673
674 // If this is a forward declaration and we are supposed to warn in this
675 // case, do it.
676 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000677 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000678 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000679 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000680 }
681}
682
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000683/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000684/// a class method in its extension.
685///
Mike Stump1eb44332009-09-09 15:08:12 +0000686void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000687 ObjCInterfaceDecl *ID) {
688 if (!ID)
689 return; // Possibly due to previous error
690
691 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
693 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000694 ObjCMethodDecl *MD = *i;
695 MethodMap[MD->getSelector()] = MD;
696 }
697
698 if (MethodMap.empty())
699 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000700 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
701 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000702 ObjCMethodDecl *Method = *i;
703 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
704 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
705 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
706 << Method->getDeclName();
707 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
708 }
709 }
710}
711
Chris Lattner58fe03b2009-04-12 08:43:13 +0000712/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000713Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000714Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000715 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000716 unsigned NumElts,
717 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000719 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattner4d391482007-12-12 07:09:47 +0000721 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000722 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000723 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000724 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000725 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000726 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000727 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000728 PushOnScopeChains(PDecl, TUScope, false);
729 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000730 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000731 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000732 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000733 if (!isNew)
734 PDecl->setChangedSinceDeserialization(true);
735 }
Chris Lattner4d391482007-12-12 07:09:47 +0000736 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000737 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
740 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000741 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000742 Protocols.data(), Protocols.size(),
743 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000744 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000745 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000746 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000747}
748
John McCalld226f652010-08-21 09:40:31 +0000749Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000750ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
751 IdentifierInfo *ClassName, SourceLocation ClassLoc,
752 IdentifierInfo *CategoryName,
753 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000754 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000755 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000756 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000757 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000758 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000759 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000760
761 /// Check that class of this category is already completely declared.
762 if (!IDecl || IDecl->isForwardDecl()) {
763 // Create an invalid ObjCCategoryDecl to serve as context for
764 // the enclosing method declarations. We mark the decl invalid
765 // to make it clear that this isn't a valid AST.
766 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
767 ClassLoc, CategoryLoc, CategoryName);
768 CDecl->setInvalidDecl();
769 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000770 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000771 }
772
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000773 if (!CategoryName && IDecl->getImplementation()) {
774 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
775 Diag(IDecl->getImplementation()->getLocation(),
776 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000777 }
778
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000779 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
780 ClassLoc, CategoryLoc, CategoryName);
781 // FIXME: PushOnScopeChains?
782 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000783
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000784 CDecl->setClassInterface(IDecl);
785 // Insert class extension to the list of class's categories.
786 if (!CategoryName)
787 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattner16b34b42009-02-16 21:30:01 +0000789 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000790 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000791
Fariborz Jahanian25760612010-02-15 21:55:26 +0000792 if (CategoryName) {
793 /// Check for duplicate interface declaration for this category
794 ObjCCategoryDecl *CDeclChain;
795 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
796 CDeclChain = CDeclChain->getNextClassCategory()) {
797 if (CDeclChain->getIdentifier() == CategoryName) {
798 // Class extensions can be declared multiple times.
799 Diag(CategoryLoc, diag::warn_dup_category_def)
800 << ClassName << CategoryName;
801 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
802 break;
803 }
Chris Lattner70f19542009-02-16 21:26:43 +0000804 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000805 if (!CDeclChain)
806 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000807 }
Chris Lattner70f19542009-02-16 21:26:43 +0000808
Chris Lattner4d391482007-12-12 07:09:47 +0000809 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000810 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000811 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000812 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000813 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000814 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000815 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Anders Carlsson15281452008-11-04 16:57:32 +0000818 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000819 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000820}
821
822/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000823/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000824/// object.
John McCalld226f652010-08-21 09:40:31 +0000825Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000826 SourceLocation AtCatImplLoc,
827 IdentifierInfo *ClassName, SourceLocation ClassLoc,
828 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000829 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000830 ObjCCategoryDecl *CatIDecl = 0;
831 if (IDecl) {
832 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
833 if (!CatIDecl) {
834 // Category @implementation with no corresponding @interface.
835 // Create and install one.
836 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000837 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000838 CatName);
839 CatIDecl->setClassInterface(IDecl);
840 CatIDecl->insertNextClassCategory();
841 }
842 }
843
Mike Stump1eb44332009-09-09 15:08:12 +0000844 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000845 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
846 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000847 /// Check that class of this category is already completely declared.
848 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000849 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000850
Douglas Gregord0434102009-01-09 00:49:46 +0000851 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000852 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000853
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000854 /// Check that CatName, category name, is not used in another implementation.
855 if (CatIDecl) {
856 if (CatIDecl->getImplementation()) {
857 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
858 << CatName;
859 Diag(CatIDecl->getImplementation()->getLocation(),
860 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000861 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000862 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000863 // Warn on implementating category of deprecated class under
864 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000865 DiagnoseObjCImplementedDeprecations(*this,
866 dyn_cast<NamedDecl>(IDecl),
867 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000868 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Anders Carlsson15281452008-11-04 16:57:32 +0000871 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000872 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000873}
874
John McCalld226f652010-08-21 09:40:31 +0000875Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000876 SourceLocation AtClassImplLoc,
877 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000878 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000879 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000880 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000881 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000882 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000883 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
884 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000886 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000887 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000888 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
889 // If this is a forward declaration of an interface, warn.
890 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000891 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000892 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000893 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000894 } else {
895 // We did not find anything with the name ClassName; try to correct for
896 // typos in the class name.
897 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000898 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000899 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000900 // Suggest the (potentially) correct interface name. However, put the
901 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000902 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000903 // provide a code-modification hint or use the typo name for recovery,
904 // because this is just a warning. The program may actually be correct.
905 Diag(ClassLoc, diag::warn_undef_interface_suggest)
906 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000907 Diag(IDecl->getLocation(), diag::note_previous_decl)
908 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000909 << FixItHint::CreateReplacement(ClassLoc,
910 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000911 IDecl = 0;
912 } else {
913 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
914 }
Chris Lattner4d391482007-12-12 07:09:47 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner4d391482007-12-12 07:09:47 +0000917 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000919 if (SuperClassname) {
920 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000921 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
922 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000924 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
925 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000926 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000928 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000929 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 Diag(SuperClassLoc, diag::err_undef_superclass)
931 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000932 else if (IDecl && IDecl->getSuperClass() != SDecl) {
933 // This implementation and its interface do not have the same
934 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000935 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000936 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000937 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000938 }
939 }
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattner4d391482007-12-12 07:09:47 +0000942 if (!IDecl) {
943 // Legacy case of @implementation with no corresponding @interface.
944 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000945
Mike Stump390b4cc2009-05-16 07:39:55 +0000946 // FIXME: Do we support attributes on the @implementation? If so we should
947 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000948 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000949 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000950 IDecl->setSuperClass(SDecl);
951 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000952
953 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000954 } else {
955 // Mark the interface as being completed, even if it was just as
956 // @class ....;
957 // declaration; the user cannot reopen it.
958 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
961 ObjCImplementationDecl* IMPDecl =
962 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000963 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Anders Carlsson15281452008-11-04 16:57:32 +0000965 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000966 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000969 if (IDecl->getImplementation()) {
970 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000971 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000972 Diag(IDecl->getImplementation()->getLocation(),
973 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000974 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000975 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000976 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000977 // Warn on implementating deprecated class under
978 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000979 DiagnoseObjCImplementedDeprecations(*this,
980 dyn_cast<NamedDecl>(IDecl),
981 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000982 }
John McCalld226f652010-08-21 09:40:31 +0000983 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000984}
985
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
987 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000988 SourceLocation RBrace) {
989 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000990 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000991 if (!IDecl)
992 return;
993 /// Check case of non-existing @interface decl.
994 /// (legacy objective-c @implementation decl without an @interface decl).
995 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000996 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000997 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000998 // Add ivar's to class's DeclContext.
999 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001000 ivars[i]->setLexicalDeclContext(ImpDecl);
1001 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001002 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001003 }
1004
Chris Lattner4d391482007-12-12 07:09:47 +00001005 return;
1006 }
1007 // If implementation has empty ivar list, just return.
1008 if (numIvars == 0)
1009 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Chris Lattner4d391482007-12-12 07:09:47 +00001011 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001012 if (LangOpts.ObjCNonFragileABI2) {
1013 if (ImpDecl->getSuperClass())
1014 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1015 for (unsigned i = 0; i < numIvars; i++) {
1016 ObjCIvarDecl* ImplIvar = ivars[i];
1017 if (const ObjCIvarDecl *ClsIvar =
1018 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1019 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1020 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1021 continue;
1022 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001023 // Instance ivar to Implementation's DeclContext.
1024 ImplIvar->setLexicalDeclContext(ImpDecl);
1025 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1026 ImpDecl->addDecl(ImplIvar);
1027 }
1028 return;
1029 }
Chris Lattner4d391482007-12-12 07:09:47 +00001030 // Check interface's Ivar list against those in the implementation.
1031 // names and types must match.
1032 //
Chris Lattner4d391482007-12-12 07:09:47 +00001033 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001034 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001035 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1036 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001037 ObjCIvarDecl* ImplIvar = ivars[j++];
1038 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001039 assert (ImplIvar && "missing implementation ivar");
1040 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Steve Naroffca331292009-03-03 14:49:36 +00001042 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001043 if (Context.getCanonicalType(ImplIvar->getType()) !=
1044 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001045 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001046 << ImplIvar->getIdentifier()
1047 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001048 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001049 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1050 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1051 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001052 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1053 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001054 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1055 << ImplIvar->getIdentifier();
1056 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058 }
Steve Naroffca331292009-03-03 14:49:36 +00001059 // Make sure the names are identical.
1060 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001061 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001062 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001063 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001064 }
1065 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattner609e4c72007-12-12 18:11:49 +00001068 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001069 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001070 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001071 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001072}
1073
Steve Naroff3c2eb662008-02-10 21:38:56 +00001074void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001075 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +00001076 if (!IncompleteImpl) {
1077 Diag(ImpLoc, diag::warn_incomplete_impl);
1078 IncompleteImpl = true;
1079 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001080 if (DiagID == diag::warn_unimplemented_protocol_method)
1081 Diag(ImpLoc, DiagID) << method->getDeclName();
1082 else
1083 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001084}
1085
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001086/// Determines if type B can be substituted for type A. Returns true if we can
1087/// guarantee that anything that the user will do to an object of type A can
1088/// also be done to an object of type B. This is trivially true if the two
1089/// types are the same, or if B is a subclass of A. It becomes more complex
1090/// in cases where protocols are involved.
1091///
1092/// Object types in Objective-C describe the minimum requirements for an
1093/// object, rather than providing a complete description of a type. For
1094/// example, if A is a subclass of B, then B* may refer to an instance of A.
1095/// The principle of substitutability means that we may use an instance of A
1096/// anywhere that we may use an instance of B - it will implement all of the
1097/// ivars of B and all of the methods of B.
1098///
1099/// This substitutability is important when type checking methods, because
1100/// the implementation may have stricter type definitions than the interface.
1101/// The interface specifies minimum requirements, but the implementation may
1102/// have more accurate ones. For example, a method may privately accept
1103/// instances of B, but only publish that it accepts instances of A. Any
1104/// object passed to it will be type checked against B, and so will implicitly
1105/// by a valid A*. Similarly, a method may return a subclass of the class that
1106/// it is declared as returning.
1107///
1108/// This is most important when considering subclassing. A method in a
1109/// subclass must accept any object as an argument that its superclass's
1110/// implementation accepts. It may, however, accept a more general type
1111/// without breaking substitutability (i.e. you can still use the subclass
1112/// anywhere that you can use the superclass, but not vice versa). The
1113/// converse requirement applies to return types: the return type for a
1114/// subclass method must be a valid object of the kind that the superclass
1115/// advertises, but it may be specified more accurately. This avoids the need
1116/// for explicit down-casting by callers.
1117///
1118/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001119static bool isObjCTypeSubstitutable(ASTContext &Context,
1120 const ObjCObjectPointerType *A,
1121 const ObjCObjectPointerType *B,
1122 bool rejectId) {
1123 // Reject a protocol-unqualified id.
1124 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001125
1126 // If B is a qualified id, then A must also be a qualified id and it must
1127 // implement all of the protocols in B. It may not be a qualified class.
1128 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1129 // stricter definition so it is not substitutable for id<A>.
1130 if (B->isObjCQualifiedIdType()) {
1131 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001132 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1133 QualType(B,0),
1134 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001135 }
1136
1137 /*
1138 // id is a special type that bypasses type checking completely. We want a
1139 // warning when it is used in one place but not another.
1140 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1141
1142
1143 // If B is a qualified id, then A must also be a qualified id (which it isn't
1144 // if we've got this far)
1145 if (B->isObjCQualifiedIdType()) return false;
1146 */
1147
1148 // Now we know that A and B are (potentially-qualified) class types. The
1149 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001150 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001151}
1152
John McCall10302c02010-10-28 02:34:38 +00001153static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1154 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1155}
1156
1157static void CheckMethodOverrideReturn(Sema &S,
1158 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001159 ObjCMethodDecl *MethodDecl,
1160 bool IsProtocolMethodDecl) {
1161 if (IsProtocolMethodDecl &&
1162 (MethodDecl->getObjCDeclQualifier() !=
1163 MethodImpl->getObjCDeclQualifier())) {
1164 S.Diag(MethodImpl->getLocation(),
1165 diag::warn_conflicting_ret_type_modifiers)
1166 << MethodImpl->getDeclName()
1167 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1168 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1169 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1170 }
1171
John McCall10302c02010-10-28 02:34:38 +00001172 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001173 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001174 return;
1175
1176 unsigned DiagID = diag::warn_conflicting_ret_types;
1177
1178 // Mismatches between ObjC pointers go into a different warning
1179 // category, and sometimes they're even completely whitelisted.
1180 if (const ObjCObjectPointerType *ImplPtrTy =
1181 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1182 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001183 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001184 // Allow non-matching return types as long as they don't violate
1185 // the principle of substitutability. Specifically, we permit
1186 // return types that are subclasses of the declared return type,
1187 // or that are more-qualified versions of the declared type.
1188 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1189 return;
1190
1191 DiagID = diag::warn_non_covariant_ret_types;
1192 }
1193 }
1194
1195 S.Diag(MethodImpl->getLocation(), DiagID)
1196 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001197 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001198 << MethodImpl->getResultType()
1199 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001200 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1201 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001202}
1203
1204static void CheckMethodOverrideParam(Sema &S,
1205 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001206 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001207 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001208 ParmVarDecl *IfaceVar,
1209 bool IsProtocolMethodDecl) {
1210 if (IsProtocolMethodDecl &&
1211 (ImplVar->getObjCDeclQualifier() !=
1212 IfaceVar->getObjCDeclQualifier())) {
1213 S.Diag(ImplVar->getLocation(),
1214 diag::warn_conflicting_param_modifiers)
1215 << getTypeRange(ImplVar->getTypeSourceInfo())
1216 << MethodImpl->getDeclName();
1217 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1218 << getTypeRange(IfaceVar->getTypeSourceInfo());
1219 }
1220
John McCall10302c02010-10-28 02:34:38 +00001221 QualType ImplTy = ImplVar->getType();
1222 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001223
John McCall10302c02010-10-28 02:34:38 +00001224 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1225 return;
1226
1227 unsigned DiagID = diag::warn_conflicting_param_types;
1228
1229 // Mismatches between ObjC pointers go into a different warning
1230 // category, and sometimes they're even completely whitelisted.
1231 if (const ObjCObjectPointerType *ImplPtrTy =
1232 ImplTy->getAs<ObjCObjectPointerType>()) {
1233 if (const ObjCObjectPointerType *IfacePtrTy =
1234 IfaceTy->getAs<ObjCObjectPointerType>()) {
1235 // Allow non-matching argument types as long as they don't
1236 // violate the principle of substitutability. Specifically, the
1237 // implementation must accept any objects that the superclass
1238 // accepts, however it may also accept others.
1239 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1240 return;
1241
1242 DiagID = diag::warn_non_contravariant_param_types;
1243 }
1244 }
1245
1246 S.Diag(ImplVar->getLocation(), DiagID)
1247 << getTypeRange(ImplVar->getTypeSourceInfo())
1248 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1249 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1250 << getTypeRange(IfaceVar->getTypeSourceInfo());
1251}
John McCallf85e1932011-06-15 23:02:42 +00001252
1253/// In ARC, check whether the conventional meanings of the two methods
1254/// match. If they don't, it's a hard error.
1255static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1256 ObjCMethodDecl *decl) {
1257 ObjCMethodFamily implFamily = impl->getMethodFamily();
1258 ObjCMethodFamily declFamily = decl->getMethodFamily();
1259 if (implFamily == declFamily) return false;
1260
1261 // Since conventions are sorted by selector, the only possibility is
1262 // that the types differ enough to cause one selector or the other
1263 // to fall out of the family.
1264 assert(implFamily == OMF_None || declFamily == OMF_None);
1265
1266 // No further diagnostics required on invalid declarations.
1267 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1268
1269 const ObjCMethodDecl *unmatched = impl;
1270 ObjCMethodFamily family = declFamily;
1271 unsigned errorID = diag::err_arc_lost_method_convention;
1272 unsigned noteID = diag::note_arc_lost_method_convention;
1273 if (declFamily == OMF_None) {
1274 unmatched = decl;
1275 family = implFamily;
1276 errorID = diag::err_arc_gained_method_convention;
1277 noteID = diag::note_arc_gained_method_convention;
1278 }
1279
1280 // Indexes into a %select clause in the diagnostic.
1281 enum FamilySelector {
1282 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1283 };
1284 FamilySelector familySelector = FamilySelector();
1285
1286 switch (family) {
1287 case OMF_None: llvm_unreachable("logic error, no method convention");
1288 case OMF_retain:
1289 case OMF_release:
1290 case OMF_autorelease:
1291 case OMF_dealloc:
1292 case OMF_retainCount:
1293 case OMF_self:
1294 // Mismatches for these methods don't change ownership
1295 // conventions, so we don't care.
1296 return false;
1297
1298 case OMF_init: familySelector = F_init; break;
1299 case OMF_alloc: familySelector = F_alloc; break;
1300 case OMF_copy: familySelector = F_copy; break;
1301 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1302 case OMF_new: familySelector = F_new; break;
1303 }
1304
1305 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1306 ReasonSelector reasonSelector;
1307
1308 // The only reason these methods don't fall within their families is
1309 // due to unusual result types.
1310 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1311 reasonSelector = R_UnrelatedReturn;
1312 } else {
1313 reasonSelector = R_NonObjectReturn;
1314 }
1315
1316 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1317 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1318
1319 return true;
1320}
John McCall10302c02010-10-28 02:34:38 +00001321
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001322void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001323 ObjCMethodDecl *MethodDecl,
1324 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001325 if (getLangOptions().ObjCAutoRefCount &&
1326 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1327 return;
1328
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1330 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Chris Lattner3aff9192009-04-11 19:58:42 +00001332 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001333 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001334 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001335 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1336 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001338 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001339 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001340 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001341 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001342}
1343
Mike Stump390b4cc2009-05-16 07:39:55 +00001344/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1345/// improve the efficiency of selector lookups and type checking by associating
1346/// with each protocol / interface / category the flattened instance tables. If
1347/// we used an immutable set to keep the table then it wouldn't add significant
1348/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001349
Steve Naroffefe7f362008-02-08 22:06:17 +00001350/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001351/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001352void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1353 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001354 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001355 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001356 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001357 ObjCContainerDecl *CDecl) {
1358 ObjCInterfaceDecl *IDecl;
1359 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1360 IDecl = C->getClassInterface();
1361 else
1362 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1363 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1364
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001365 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001366 ObjCInterfaceDecl *NSIDecl = 0;
1367 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001368 // check to see if class implements forwardInvocation method and objects
1369 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001370 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001371 // Under such conditions, which means that every method possible is
1372 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001373 // found" warnings.
1374 // FIXME: Use a general GetUnarySelector method for this.
1375 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1376 Selector fISelector = Context.Selectors.getSelector(1, &II);
1377 if (InsMap.count(fISelector))
1378 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1379 // need be implemented in the implementation.
1380 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001383 // If a method lookup fails locally we still need to look and see if
1384 // the method was implemented by a base class or an inherited
1385 // protocol. This lookup is slow, but occurs rarely in correct code
1386 // and otherwise would terminate in a warning.
1387
Chris Lattner4d391482007-12-12 07:09:47 +00001388 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001389 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001390 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001391 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001392 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001393 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001394 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001395 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001396 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001397 // Ugly, but necessary. Method declared in protcol might have
1398 // have been synthesized due to a property declared in the class which
1399 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001400 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001401 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001402 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001403 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001404 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1405 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001406 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001407 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001408 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1409 << PDecl->getDeclName();
1410 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001411 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001412 }
1413 }
Chris Lattner4d391482007-12-12 07:09:47 +00001414 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001415 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001416 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001417 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001419 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1420 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001421 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001422 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001423 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001424 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001425 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001426 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1427 PDecl->getDeclName();
1428 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001429 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001430 }
Chris Lattner780f3292008-07-21 21:32:27 +00001431 // Check on this protocols's referenced protocols, recursively.
1432 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1433 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001434 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001435}
1436
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001437/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1438/// or protocol against those declared in their implementations.
1439///
1440void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1441 const llvm::DenseSet<Selector> &ClsMap,
1442 llvm::DenseSet<Selector> &InsMapSeen,
1443 llvm::DenseSet<Selector> &ClsMapSeen,
1444 ObjCImplDecl* IMPDecl,
1445 ObjCContainerDecl* CDecl,
1446 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001447 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001448 // Check and see if instance methods in class interface have been
1449 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001450 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1451 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001452 if (InsMapSeen.count((*I)->getSelector()))
1453 continue;
1454 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001455 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001456 !InsMap.count((*I)->getSelector())) {
1457 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001458 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1459 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001460 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001461 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001462 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001463 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001464 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001465 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001466 assert(MethodDecl &&
1467 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001468 // ImpMethodDecl may be null as in a @dynamic property.
1469 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001470 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1471 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001472 }
1473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001475 // Check and see if class methods in class interface have been
1476 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001477 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001478 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001479 if (ClsMapSeen.count((*I)->getSelector()))
1480 continue;
1481 ClsMapSeen.insert((*I)->getSelector());
1482 if (!ClsMap.count((*I)->getSelector())) {
1483 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001484 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1485 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001486 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001487 ObjCMethodDecl *ImpMethodDecl =
1488 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001489 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001491 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1492 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001493 }
1494 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001495
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001496 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001497 // Also methods in class extensions need be looked at next.
1498 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1499 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1500 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1501 IMPDecl,
1502 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1503 IncompleteImpl, false);
1504
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001505 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001506 for (ObjCInterfaceDecl::all_protocol_iterator
1507 PI = I->all_referenced_protocol_begin(),
1508 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001509 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1510 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001511 (*PI), IncompleteImpl, false);
1512 if (I->getSuperClass())
1513 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001514 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001515 I->getSuperClass(), IncompleteImpl, false);
1516 }
1517}
1518
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001519void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001520 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001521 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001522 llvm::DenseSet<Selector> InsMap;
1523 // Check and see if instance methods in class interface have been
1524 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001525 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001526 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001527 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001529 // Check and see if properties declared in the interface have either 1)
1530 // an implementation or 2) there is a @synthesize/@dynamic implementation
1531 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001532 if (isa<ObjCInterfaceDecl>(CDecl) &&
1533 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001534 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001535
Chris Lattner4d391482007-12-12 07:09:47 +00001536 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001537 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001538 I = IMPDecl->classmeth_begin(),
1539 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001540 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001542 // Check for type conflict of methods declared in a class/protocol and
1543 // its implementation; if any.
1544 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001545 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1546 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001547 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Chris Lattner4d391482007-12-12 07:09:47 +00001549 // Check the protocol list for unimplemented methods in the @implementation
1550 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001551 // Check and see if class methods in class interface have been
1552 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Chris Lattnercddc8882009-03-01 00:56:52 +00001554 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001555 for (ObjCInterfaceDecl::all_protocol_iterator
1556 PI = I->all_referenced_protocol_begin(),
1557 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001558 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001559 InsMap, ClsMap, I);
1560 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001561 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1562 Categories; Categories = Categories->getNextClassExtension())
1563 ImplMethodsVsClassMethods(S, IMPDecl,
1564 const_cast<ObjCCategoryDecl*>(Categories),
1565 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001566 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001567 // For extended class, unimplemented methods in its protocols will
1568 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001569 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001570 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1571 E = C->protocol_end(); PI != E; ++PI)
1572 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001573 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001574 // Report unimplemented properties in the category as well.
1575 // When reporting on missing setter/getters, do not report when
1576 // setter/getter is implemented in category's primary class
1577 // implementation.
1578 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1579 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1580 for (ObjCImplementationDecl::instmeth_iterator
1581 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1582 InsMap.insert((*I)->getSelector());
1583 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001584 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001585 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001586 } else
1587 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001588}
1589
Mike Stump1eb44332009-09-09 15:08:12 +00001590/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001591Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001592Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001593 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001594 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001595 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001596 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Chris Lattner4d391482007-12-12 07:09:47 +00001598 for (unsigned i = 0; i != NumElts; ++i) {
1599 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001600 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001601 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001602 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001603 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001604 // Maybe we will complain about the shadowed template parameter.
1605 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1606 // Just pretend that we didn't see the previous declaration.
1607 PrevDecl = 0;
1608 }
1609
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001611 // GCC apparently allows the following idiom:
1612 //
1613 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1614 // @class XCElementToggler;
1615 //
Mike Stump1eb44332009-09-09 15:08:12 +00001616 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001617 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001618 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001619 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001620 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001621 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001622 // a forward class declaration matching a typedef name of a class refers
1623 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001624 if (const ObjCObjectType *OI =
1625 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1626 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001627 }
Chris Lattner4d391482007-12-12 07:09:47 +00001628 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001629 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1630 if (!IDecl) { // Not already seen? Make a forward decl.
1631 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1632 IdentList[i], IdentLocs[i], true);
1633
1634 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1635 // the current DeclContext. This prevents clients that walk DeclContext
1636 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1637 // declared later (if at all). We also take care to explicitly make
1638 // sure this declaration is visible for name lookup.
1639 PushOnScopeChains(IDecl, TUScope, false);
1640 CurContext->makeDeclVisibleInContext(IDecl, true);
1641 }
Chris Lattner4d391482007-12-12 07:09:47 +00001642
1643 Interfaces.push_back(IDecl);
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Ted Kremenek321c22f2009-11-18 00:28:11 +00001646 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001647 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001648 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001649 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001650 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001651 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001652 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001653}
1654
John McCallf85e1932011-06-15 23:02:42 +00001655static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1656 QualType leftQT, QualType rightQT) {
1657 const Type *left =
1658 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1659 const Type *right =
1660 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1661
1662 if (left == right) return true;
1663
1664 // If we're doing a strict match, the types have to match exactly.
1665 if (strategy == Sema::MMS_strict) return false;
1666
1667 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1668
1669 // Otherwise, use this absurdly complicated algorithm to try to
1670 // validate the basic, low-level compatibility of the two types.
1671
1672 // As a minimum, require the sizes and alignments to match.
1673 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1674 return false;
1675
1676 // Consider all the kinds of non-dependent canonical types:
1677 // - functions and arrays aren't possible as return and parameter types
1678
1679 // - vector types of equal size can be arbitrarily mixed
1680 if (isa<VectorType>(left)) return isa<VectorType>(right);
1681 if (isa<VectorType>(right)) return false;
1682
1683 // - references should only match references of identical type
1684 // - structs, unions, and Objective-C objects must match exactly
1685 // - everything else should be a scalar
1686 if (!left->isScalarType() || !right->isScalarType())
1687 return false;
1688
1689 // Make scalars agree in kind, except count bools as chars.
1690 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1691 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1692 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1693 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1694
1695 // Note that data member pointers and function member pointers don't
1696 // intermix because of the size differences.
1697
1698 return (leftSK == rightSK);
1699}
Chris Lattner4d391482007-12-12 07:09:47 +00001700
1701/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1702/// returns true, or false, accordingly.
1703/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001704bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1705 const ObjCMethodDecl *right,
1706 MethodMatchStrategy strategy) {
1707 if (!matchTypes(Context, strategy,
1708 left->getResultType(), right->getResultType()))
1709 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001710
John McCallf85e1932011-06-15 23:02:42 +00001711 if (getLangOptions().ObjCAutoRefCount &&
1712 (left->hasAttr<NSReturnsRetainedAttr>()
1713 != right->hasAttr<NSReturnsRetainedAttr>() ||
1714 left->hasAttr<NSConsumesSelfAttr>()
1715 != right->hasAttr<NSConsumesSelfAttr>()))
1716 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001717
John McCallf85e1932011-06-15 23:02:42 +00001718 ObjCMethodDecl::param_iterator
1719 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001720
John McCallf85e1932011-06-15 23:02:42 +00001721 for (; li != le; ++li, ++ri) {
1722 assert(ri != right->param_end() && "Param mismatch");
1723 ParmVarDecl *lparm = *li, *rparm = *ri;
1724
1725 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1726 return false;
1727
1728 if (getLangOptions().ObjCAutoRefCount &&
1729 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1730 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001731 }
1732 return true;
1733}
1734
Sebastian Redldb9d2142010-08-02 23:18:59 +00001735/// \brief Read the contents of the method pool for a given selector from
1736/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001738/// This routine should only be called once, when the method pool has no entry
1739/// for this selector.
1740Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001741 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001742 assert(MethodPool.find(Sel) == MethodPool.end() &&
1743 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001744
1745 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001746 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Sebastian Redldb9d2142010-08-02 23:18:59 +00001748 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001749}
1750
Sebastian Redldb9d2142010-08-02 23:18:59 +00001751void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1752 bool instance) {
1753 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1754 if (Pos == MethodPool.end()) {
1755 if (ExternalSource)
1756 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001757 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001758 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1759 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001760 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001761 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001762 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001763 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001764 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001765 Entry.Method = Method;
1766 Entry.Next = 0;
1767 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Chris Lattnerb25df352009-03-04 05:16:45 +00001770 // We've seen a method with this name, see if we have already seen this type
1771 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001772 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1773 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1774
1775 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001776 ObjCMethodDecl *PrevObjCMethod = List->Method;
1777 PrevObjCMethod->setDefined(impl);
1778 // If a method is deprecated, push it in the global pool.
1779 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001780 if (Method->isDeprecated()) {
1781 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001782 List->Method = Method;
1783 }
1784 // If new method is unavailable, push it into global pool
1785 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001786 if (Method->isUnavailable()) {
1787 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001788 List->Method = Method;
1789 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001790 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001791 }
John McCallf85e1932011-06-15 23:02:42 +00001792 }
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Chris Lattnerb25df352009-03-04 05:16:45 +00001794 // We have a new signature for an existing method - add it.
1795 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001796 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1797 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001798}
1799
John McCallf85e1932011-06-15 23:02:42 +00001800/// Determines if this is an "acceptable" loose mismatch in the global
1801/// method pool. This exists mostly as a hack to get around certain
1802/// global mismatches which we can't afford to make warnings / errors.
1803/// Really, what we want is a way to take a method out of the global
1804/// method pool.
1805static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1806 ObjCMethodDecl *other) {
1807 if (!chosen->isInstanceMethod())
1808 return false;
1809
1810 Selector sel = chosen->getSelector();
1811 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1812 return false;
1813
1814 // Don't complain about mismatches for -length if the method we
1815 // chose has an integral result type.
1816 return (chosen->getResultType()->isIntegerType());
1817}
1818
Sebastian Redldb9d2142010-08-02 23:18:59 +00001819ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001820 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001821 bool warn, bool instance) {
1822 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1823 if (Pos == MethodPool.end()) {
1824 if (ExternalSource)
1825 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001826 else
1827 return 0;
1828 }
1829
Sebastian Redldb9d2142010-08-02 23:18:59 +00001830 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Sebastian Redldb9d2142010-08-02 23:18:59 +00001832 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001833 bool issueDiagnostic = false, issueError = false;
1834
1835 // We support a warning which complains about *any* difference in
1836 // method signature.
1837 bool strictSelectorMatch =
1838 (receiverIdOrClass && warn &&
1839 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1840 R.getBegin()) !=
1841 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001842 if (strictSelectorMatch)
1843 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001844 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1845 MMS_strict)) {
1846 issueDiagnostic = true;
1847 break;
1848 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001849 }
1850
John McCallf85e1932011-06-15 23:02:42 +00001851 // If we didn't see any strict differences, we won't see any loose
1852 // differences. In ARC, however, we also need to check for loose
1853 // mismatches, because most of them are errors.
1854 if (!strictSelectorMatch ||
1855 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001856 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001857 // This checks if the methods differ in type mismatch.
1858 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1859 MMS_loose) &&
1860 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1861 issueDiagnostic = true;
1862 if (getLangOptions().ObjCAutoRefCount)
1863 issueError = true;
1864 break;
1865 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001866 }
1867
John McCallf85e1932011-06-15 23:02:42 +00001868 if (issueDiagnostic) {
1869 if (issueError)
1870 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1871 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001872 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1873 else
1874 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001875
1876 Diag(MethList.Method->getLocStart(),
1877 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001878 << MethList.Method->getSourceRange();
1879 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1880 Diag(Next->Method->getLocStart(), diag::note_also_found)
1881 << Next->Method->getSourceRange();
1882 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001883 }
1884 return MethList.Method;
1885}
1886
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001887ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001888 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1889 if (Pos == MethodPool.end())
1890 return 0;
1891
1892 GlobalMethods &Methods = Pos->second;
1893
1894 if (Methods.first.Method && Methods.first.Method->isDefined())
1895 return Methods.first.Method;
1896 if (Methods.second.Method && Methods.second.Method->isDefined())
1897 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001898 return 0;
1899}
1900
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001901/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1902/// identical selector names in current and its super classes and issues
1903/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001904void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1905 ObjCMethodDecl *Method,
1906 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001907 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1908 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001910 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001911 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001912 SD->lookupMethod(Method->getSelector(), IsInstance);
1913 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001914 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001915 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001916 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001917 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1918 E = Method->param_end();
1919 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1920 for (; ParamI != E; ++ParamI, ++PrevI) {
1921 // Number of parameters are the same and is guaranteed by selector match.
1922 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1923 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1924 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001925 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001926 // respective argument type in the super class method, issue warning;
1927 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001928 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001929 << T1 << T2;
1930 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1931 return;
1932 }
1933 }
1934 ID = SD;
1935 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001936}
1937
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001938/// DiagnoseDuplicateIvars -
1939/// Check for duplicate ivars in the entire class at the start of
1940/// @implementation. This becomes necesssary because class extension can
1941/// add ivars to a class in random order which will not be known until
1942/// class's @implementation is seen.
1943void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1944 ObjCInterfaceDecl *SID) {
1945 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1946 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1947 ObjCIvarDecl* Ivar = (*IVI);
1948 if (Ivar->isInvalidDecl())
1949 continue;
1950 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1951 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1952 if (prevIvar) {
1953 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1954 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1955 Ivar->setInvalidDecl();
1956 }
1957 }
1958 }
1959}
1960
Steve Naroffa56f6162007-12-18 01:30:32 +00001961// Note: For class/category implemenations, allMethods/allProperties is
1962// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001963void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001964 Decl *ClassDecl,
1965 Decl **allMethods, unsigned allNum,
1966 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001967 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001968 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1969 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001970 // should be true.
1971 if (!ClassDecl)
1972 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001973
Mike Stump1eb44332009-09-09 15:08:12 +00001974 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001975 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1976 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001978
Ted Kremenek782f2f52010-01-07 01:20:12 +00001979 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1980 // FIXME: This is wrong. We shouldn't be pretending that there is
1981 // an '@end' in the declaration.
1982 SourceLocation L = ClassDecl->getLocation();
1983 AtEnd.setBegin(L);
1984 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001985 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001986 }
1987
Steve Naroff0701bbb2009-01-08 17:28:14 +00001988 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1989 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1990 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1991
Chris Lattner4d391482007-12-12 07:09:47 +00001992 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001993 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001994 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001995
1996 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001997 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001998 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001999 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002000 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002001 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002002 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002003 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002004 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002005 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002006 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002007 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002008 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002009 InsMap[Method->getSelector()] = Method;
2010 /// The following allows us to typecheck messages to "id".
2011 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002012 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002013 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002014 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002015 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002016 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002017 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002018 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002019 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002020 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002021 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002022 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002023 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002024 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002025 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002026 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002027 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002028 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002029 /// The following allows us to typecheck messages to "Class".
2030 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002031 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002032 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002033 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002034 }
2035 }
2036 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002037 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002038 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002039 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002040 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002041 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002042 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002043 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002044 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002045 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002046
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002047 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002048 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002049 if (C->IsClassExtension()) {
2050 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2051 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002052 }
Chris Lattner4d391482007-12-12 07:09:47 +00002053 }
Steve Naroff09c47192009-01-09 15:36:25 +00002054 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002055 if (CDecl->getIdentifier())
2056 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2057 // user-defined setter/getter. It also synthesizes setter/getter methods
2058 // and adds them to the DeclContext and global method pools.
2059 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2060 E = CDecl->prop_end();
2061 I != E; ++I)
2062 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002063 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002064 }
2065 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002066 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002067 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002068 // Any property declared in a class extension might have user
2069 // declared setter or getter in current class extension or one
2070 // of the other class extensions. Mark them as synthesized as
2071 // property will be synthesized when property with same name is
2072 // seen in the @implementation.
2073 for (const ObjCCategoryDecl *ClsExtDecl =
2074 IDecl->getFirstClassExtension();
2075 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2076 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2077 E = ClsExtDecl->prop_end(); I != E; ++I) {
2078 ObjCPropertyDecl *Property = (*I);
2079 // Skip over properties declared @dynamic
2080 if (const ObjCPropertyImplDecl *PIDecl
2081 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2082 if (PIDecl->getPropertyImplementation()
2083 == ObjCPropertyImplDecl::Dynamic)
2084 continue;
2085
2086 for (const ObjCCategoryDecl *CExtDecl =
2087 IDecl->getFirstClassExtension();
2088 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2089 if (ObjCMethodDecl *GetterMethod =
2090 CExtDecl->getInstanceMethod(Property->getGetterName()))
2091 GetterMethod->setSynthesized(true);
2092 if (!Property->isReadOnly())
2093 if (ObjCMethodDecl *SetterMethod =
2094 CExtDecl->getInstanceMethod(Property->getSetterName()))
2095 SetterMethod->setSynthesized(true);
2096 }
2097 }
2098 }
2099
Ted Kremenekc32647d2010-12-23 21:35:43 +00002100 if (LangOpts.ObjCDefaultSynthProperties &&
2101 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002102 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002103 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002104 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002105 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002106
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002107 if (LangOpts.ObjCNonFragileABI2)
2108 while (IDecl->getSuperClass()) {
2109 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2110 IDecl = IDecl->getSuperClass();
2111 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002112 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002113 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002114 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002115 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002116 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Chris Lattner4d391482007-12-12 07:09:47 +00002118 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002119 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002120 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002121 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002122 Categories; Categories = Categories->getNextClassCategory()) {
2123 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002124 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002125 break;
2126 }
2127 }
2128 }
2129 }
Chris Lattner682bf922009-03-29 16:50:03 +00002130 if (isInterfaceDeclKind) {
2131 // Reject invalid vardecls.
2132 for (unsigned i = 0; i != tuvNum; i++) {
2133 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2134 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2135 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002136 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002137 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002138 }
Chris Lattner682bf922009-03-29 16:50:03 +00002139 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002140 }
Chris Lattner4d391482007-12-12 07:09:47 +00002141}
2142
2143
2144/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2145/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002146static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002147CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002148 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002149}
2150
Ted Kremenek422bae72010-04-18 04:59:38 +00002151static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002152bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002153 // The 'ibaction' attribute is allowed on method definitions because of
2154 // how the IBAction macro is used on both method declarations and definitions.
2155 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002156 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2157 if ((*i)->getKind() != attr::IBAction)
2158 return true;
2159 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002160}
2161
Douglas Gregor926df6c2011-06-11 01:09:30 +00002162/// \brief Check whether the declared result type of the given Objective-C
2163/// method declaration is compatible with the method's class.
2164///
2165static bool
2166CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2167 ObjCInterfaceDecl *CurrentClass) {
2168 QualType ResultType = Method->getResultType();
2169 SourceRange ResultTypeRange;
2170 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2171 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2172
2173 // If an Objective-C method inherits its related result type, then its
2174 // declared result type must be compatible with its own class type. The
2175 // declared result type is compatible if:
2176 if (const ObjCObjectPointerType *ResultObjectType
2177 = ResultType->getAs<ObjCObjectPointerType>()) {
2178 // - it is id or qualified id, or
2179 if (ResultObjectType->isObjCIdType() ||
2180 ResultObjectType->isObjCQualifiedIdType())
2181 return false;
2182
2183 if (CurrentClass) {
2184 if (ObjCInterfaceDecl *ResultClass
2185 = ResultObjectType->getInterfaceDecl()) {
2186 // - it is the same as the method's class type, or
2187 if (CurrentClass == ResultClass)
2188 return false;
2189
2190 // - it is a superclass of the method's class type
2191 if (ResultClass->isSuperClassOf(CurrentClass))
2192 return false;
2193 }
2194 }
2195 }
2196
2197 return true;
2198}
2199
2200/// \brief Determine if any method in the global method pool has an inferred
2201/// result type.
2202static bool
2203anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
2204 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
2205 if (Pos == S.MethodPool.end()) {
2206 if (S.ExternalSource)
2207 Pos = S.ReadMethodPool(Sel);
2208 else
2209 return 0;
2210 }
2211
2212 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
2213 for (ObjCMethodList *M = &List; M; M = M->Next) {
2214 if (M->Method && M->Method->hasRelatedResultType())
2215 return true;
2216 }
2217
2218 return false;
2219}
2220
John McCalld226f652010-08-21 09:40:31 +00002221Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002222 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002223 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00002224 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00002225 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002226 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002227 Selector Sel,
2228 // optional arguments. The number of types/arguments is obtained
2229 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002230 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002231 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002232 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002233 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002234 // Make sure we can establish a context for the method.
2235 if (!ClassDecl) {
2236 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002237 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002238 }
Chris Lattner4d391482007-12-12 07:09:47 +00002239 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002241 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002242 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002243 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Steve Naroffccef3712009-02-20 22:59:16 +00002245 // Methods cannot return interface types. All ObjC objects are
2246 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002247 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002248 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2249 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002250 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002251 }
Steve Naroffccef3712009-02-20 22:59:16 +00002252 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002253 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002254
2255 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002256 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002257 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002258 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002259 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002260 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002261 MethodDeclKind == tok::objc_optional
2262 ? ObjCMethodDecl::Optional
2263 : ObjCMethodDecl::Required,
2264 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Chris Lattner0ed844b2008-04-04 06:12:32 +00002266 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Chris Lattner7db638d2009-04-11 19:42:43 +00002268 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002269 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002270 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Chris Lattnere294d3f2009-04-11 18:57:04 +00002272 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002273 ArgType = Context.getObjCIdType();
2274 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002275 } else {
John McCall58e46772009-10-23 21:48:59 +00002276 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002277 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002278 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002279 }
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002281 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2282 LookupOrdinaryName, ForRedeclaration);
2283 LookupName(R, S);
2284 if (R.isSingleResult()) {
2285 NamedDecl *PrevDecl = R.getFoundDecl();
2286 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002287 Diag(ArgInfo[i].NameLoc,
2288 (MethodDefinition ? diag::warn_method_param_redefinition
2289 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002290 << ArgInfo[i].Name;
2291 Diag(PrevDecl->getLocation(),
2292 diag::note_previous_declaration);
2293 }
2294 }
2295
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002296 SourceLocation StartLoc = DI
2297 ? DI->getTypeLoc().getBeginLoc()
2298 : ArgInfo[i].NameLoc;
2299
John McCall81ef3e62011-04-23 02:46:06 +00002300 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2301 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2302 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002303
John McCall70798862011-05-02 00:30:12 +00002304 Param->setObjCMethodScopeInfo(i);
2305
Chris Lattner0ed844b2008-04-04 06:12:32 +00002306 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002307 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002309 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002310 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002312 S->AddDecl(Param);
2313 IdResolver.AddDecl(Param);
2314
Chris Lattner0ed844b2008-04-04 06:12:32 +00002315 Params.push_back(Param);
2316 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002317
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002318 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002319 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002320 QualType ArgType = Param->getType();
2321 if (ArgType.isNull())
2322 ArgType = Context.getObjCIdType();
2323 else
2324 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2325 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002326 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002327 Diag(Param->getLocation(),
2328 diag::err_object_cannot_be_passed_returned_by_value)
2329 << 1 << ArgType;
2330 Param->setInvalidDecl();
2331 }
2332 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002333
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002334 Params.push_back(Param);
2335 }
2336
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002337 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2338 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002339 ObjCMethod->setObjCDeclQualifier(
2340 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2341 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002342
2343 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002344 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
John McCall54abf7d2009-11-04 02:18:39 +00002346 const ObjCMethodDecl *InterfaceMD = 0;
2347
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002348 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002349 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002350 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002351 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002352 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2353 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002354 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002355 PrevMethod = ImpDecl->getClassMethod(Sel);
2356 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002357 }
John McCall54abf7d2009-11-04 02:18:39 +00002358 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2359 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002360
Sean Huntcf807c42010-08-18 23:23:40 +00002361 if (ObjCMethod->hasAttrs() &&
2362 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002363 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002364 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002365 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002366 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002367 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2368 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002369 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002370 PrevMethod = CatImpDecl->getClassMethod(Sel);
2371 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002372 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002373
2374 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2375 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2376
Sean Huntcf807c42010-08-18 23:23:40 +00002377 if (ObjCMethod->hasAttrs() &&
2378 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002379 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002380 } else {
2381 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002382 }
2383 if (PrevMethod) {
2384 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002385 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002386 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002387 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002388 }
John McCall54abf7d2009-11-04 02:18:39 +00002389
Douglas Gregor926df6c2011-06-11 01:09:30 +00002390 // If this Objective-C method does not have a related result type, but we
2391 // are allowed to infer related result types, try to do so based on the
2392 // method family.
2393 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2394 if (!CurrentClass) {
2395 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2396 CurrentClass = Cat->getClassInterface();
2397 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2398 CurrentClass = Impl->getClassInterface();
2399 else if (ObjCCategoryImplDecl *CatImpl
2400 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2401 CurrentClass = CatImpl->getClassInterface();
2402 }
2403
John McCalleca5d222011-03-02 04:00:57 +00002404 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002405 if (InterfaceMD) {
2406 // Inherit the related result type, if we can.
2407 if (InterfaceMD->hasRelatedResultType() &&
2408 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2409 ObjCMethod->SetRelatedResultType();
2410
John McCalleca5d222011-03-02 04:00:57 +00002411 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002412 }
2413
John McCallf85e1932011-06-15 23:02:42 +00002414 bool ARCError = false;
2415 if (getLangOptions().ObjCAutoRefCount)
2416 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2417
2418 if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
Douglas Gregor74da19f2011-06-14 23:20:43 +00002419 getLangOptions().ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002420 bool InferRelatedResultType = false;
2421 switch (ObjCMethod->getMethodFamily()) {
2422 case OMF_None:
2423 case OMF_copy:
2424 case OMF_dealloc:
2425 case OMF_mutableCopy:
2426 case OMF_release:
2427 case OMF_retainCount:
2428 break;
2429
2430 case OMF_alloc:
2431 case OMF_new:
2432 InferRelatedResultType = ObjCMethod->isClassMethod();
2433 break;
2434
2435 case OMF_init:
2436 case OMF_autorelease:
2437 case OMF_retain:
2438 case OMF_self:
2439 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2440 break;
2441 }
2442
2443 if (InferRelatedResultType &&
2444 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2445 ObjCMethod->SetRelatedResultType();
2446
2447 if (!InterfaceMD &&
2448 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2449 ObjCMethod->isInstanceMethod()))
2450 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2451 }
2452
John McCalld226f652010-08-21 09:40:31 +00002453 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002454}
2455
Chris Lattnercc98eac2008-12-17 07:13:27 +00002456bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002457 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002458 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Anders Carlsson15281452008-11-04 16:57:32 +00002460 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2461 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002462
Anders Carlsson15281452008-11-04 16:57:32 +00002463 return true;
2464}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002465
Chris Lattnercc98eac2008-12-17 07:13:27 +00002466/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2467/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002468void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002469 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002470 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002471 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002472 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002473 if (!Class) {
2474 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2475 return;
2476 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002477 if (LangOpts.ObjCNonFragileABI) {
2478 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2479 return;
2480 }
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Chris Lattnercc98eac2008-12-17 07:13:27 +00002482 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002483 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2484 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002485 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002486 for (unsigned i = 0; i < Ivars.size(); i++) {
2487 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002488 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002489 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2490 /*FIXME: StartL=*/ID->getLocation(),
2491 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002492 ID->getIdentifier(), ID->getType(),
2493 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002494 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Chris Lattnercc98eac2008-12-17 07:13:27 +00002497 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002498 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002499 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002500 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002501 if (getLangOptions().CPlusPlus)
2502 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002503 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002504 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002505 }
2506}
2507
Douglas Gregor160b5632010-04-26 17:32:49 +00002508/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002509VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2510 SourceLocation StartLoc,
2511 SourceLocation IdLoc,
2512 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002513 bool Invalid) {
2514 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2515 // duration shall not be qualified by an address-space qualifier."
2516 // Since all parameters have automatic store duration, they can not have
2517 // an address space.
2518 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002519 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002520 Invalid = true;
2521 }
2522
2523 // An @catch parameter must be an unqualified object pointer type;
2524 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2525 if (Invalid) {
2526 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002527 } else if (T->isDependentType()) {
2528 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002529 } else if (!T->isObjCObjectPointerType()) {
2530 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002531 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002532 } else if (T->isObjCQualifiedIdType()) {
2533 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002534 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002535 }
2536
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002537 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2538 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002539 New->setExceptionVariable(true);
2540
Douglas Gregor160b5632010-04-26 17:32:49 +00002541 if (Invalid)
2542 New->setInvalidDecl();
2543 return New;
2544}
2545
John McCalld226f652010-08-21 09:40:31 +00002546Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002547 const DeclSpec &DS = D.getDeclSpec();
2548
2549 // We allow the "register" storage class on exception variables because
2550 // GCC did, but we drop it completely. Any other storage class is an error.
2551 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2552 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2553 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2554 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2555 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2556 << DS.getStorageClassSpec();
2557 }
2558 if (D.getDeclSpec().isThreadSpecified())
2559 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2560 D.getMutableDeclSpec().ClearStorageClassSpecs();
2561
2562 DiagnoseFunctionSpecifiers(D);
2563
2564 // Check that there are no default arguments inside the type of this
2565 // exception object (C++ only).
2566 if (getLangOptions().CPlusPlus)
2567 CheckExtraCXXDefaultArguments(D);
2568
Douglas Gregor160b5632010-04-26 17:32:49 +00002569 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002570 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2571 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002572
2573 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2574 // Objective-C++: Types shall not be defined in exception types.
2575 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2576 << Context.getTypeDeclType(OwnedDecl);
2577 }
2578
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002579 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2580 D.getSourceRange().getBegin(),
2581 D.getIdentifierLoc(),
2582 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002583 D.isInvalidType());
2584
2585 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2586 if (D.getCXXScopeSpec().isSet()) {
2587 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2588 << D.getCXXScopeSpec().getRange();
2589 New->setInvalidDecl();
2590 }
2591
2592 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002593 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002594 if (D.getIdentifier())
2595 IdResolver.AddDecl(New);
2596
2597 ProcessDeclAttributes(S, New, D);
2598
2599 if (New->hasAttr<BlocksAttr>())
2600 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002601 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002602}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002603
2604/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002605/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002606void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002607 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002608 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2609 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002610 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002611 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002612 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002613 }
2614}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002615
2616void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002617 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002618 unsigned numInitializers) {
2619 if (numInitializers > 0) {
2620 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002621 CXXCtorInitializer **ivarInitializers =
2622 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002623 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002624 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002625 IvarInitializers = ivarInitializers;
2626 }
2627}
2628
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002629void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002630 // Warning will be issued only when selector table is
2631 // generated (which means there is at lease one implementation
2632 // in the TU). This is to match gcc's behavior.
2633 if (ReferencedSelectors.empty() ||
2634 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002635 return;
2636 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2637 ReferencedSelectors.begin(),
2638 E = ReferencedSelectors.end(); S != E; ++S) {
2639 Selector Sel = (*S).first;
2640 if (!LookupImplementedMethodInGlobalPool(Sel))
2641 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2642 }
2643 return;
2644}