blob: a9f4f95c20b18a53c7668c3415abecf57d32307c [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.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000445 TypoCorrection Corrected = CorrectTypo(
446 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
447 NULL, NULL, false, CTC_NoKeywords);
448 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000449 Diag(SuperLoc, diag::err_undef_superclass_suggest)
450 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000451 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
452 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000453 }
454 }
455
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000456 if (PrevDecl == IDecl) {
457 Diag(SuperLoc, diag::err_recursive_superclass)
458 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
459 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000460 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000461 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000462 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000463
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000464 // Diagnose classes that inherit from deprecated classes.
465 if (SuperClassDecl)
466 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000468 if (PrevDecl && SuperClassDecl == 0) {
469 // The previous declaration was not a class decl. Check if we have a
470 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000471 if (const TypedefNameDecl *TDecl =
472 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000473 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000474 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000475 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
476 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000477 }
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000480 // This handles the following case:
481 //
482 // typedef int SuperClass;
483 // @interface MyClass : SuperClass {} @end
484 //
485 if (!SuperClassDecl) {
486 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
487 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000488 }
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Richard Smith162e1c12011-04-15 14:24:37 +0000491 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000492 if (!SuperClassDecl)
493 Diag(SuperLoc, diag::err_undef_superclass)
494 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000495 else if (SuperClassDecl->isForwardDecl()) {
496 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000497 << SuperClassDecl->getDeclName() << ClassName
498 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000499 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
500 SuperClassDecl = 0;
501 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000502 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000503 IDecl->setSuperClass(SuperClassDecl);
504 IDecl->setSuperClassLoc(SuperLoc);
505 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000506 }
Chris Lattner4d391482007-12-12 07:09:47 +0000507 } else { // we have a root class.
508 IDecl->setLocEnd(ClassLoc);
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Sebastian Redl0b17c612010-08-13 00:28:03 +0000511 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000512 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000513 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000514 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000515 IDecl->setLocEnd(EndProtoLoc);
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Anders Carlsson15281452008-11-04 16:57:32 +0000518 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000519 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000520}
521
522/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000523/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000524Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
525 IdentifierInfo *AliasName,
526 SourceLocation AliasLocation,
527 IdentifierInfo *ClassName,
528 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000529 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000530 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000531 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000532 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000533 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000534 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000535 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000536 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000537 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000538 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000539 }
540 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000541 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000542 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000543 if (const TypedefNameDecl *TDecl =
544 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000545 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000546 if (T->isObjCObjectType()) {
547 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000548 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000549 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000550 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000551 }
552 }
553 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000554 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
555 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000557 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000558 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000559 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000562 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000563 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000564 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anders Carlsson15281452008-11-04 16:57:32 +0000566 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000567 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000570}
571
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000572bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000573 IdentifierInfo *PName,
574 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000575 const ObjCList<ObjCProtocolDecl> &PList) {
576
577 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000578 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
579 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000580 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
581 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000582 if (PDecl->getIdentifier() == PName) {
583 Diag(Ploc, diag::err_protocol_has_circular_dependency);
584 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000585 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000586 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000587 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
588 PDecl->getLocation(), PDecl->getReferencedProtocols()))
589 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000590 }
591 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000592 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000593}
594
John McCalld226f652010-08-21 09:40:31 +0000595Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000596Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
597 IdentifierInfo *ProtocolName,
598 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000599 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000600 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000601 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000602 SourceLocation EndProtoLoc,
603 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000604 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000605 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000606 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000607 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000608 if (PDecl) {
609 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000610 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000611 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000612 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000613 // Just return the protocol we already had.
614 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000615 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
Steve Naroff61d68522009-03-05 15:22:01 +0000617 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000618 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000619 err = CheckForwardProtocolDeclarationForCircularDependency(
620 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Steve Narofff11b5082008-08-13 16:39:22 +0000622 // Make sure the cached decl gets a valid start location.
623 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000624 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000625 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000626 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000627 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000628 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000629 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000630 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000631 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000632 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000633 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000634 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000635 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000636 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000637 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000638 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
639 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000640 PDecl->setLocEnd(EndProtoLoc);
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
643 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000644 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000645}
646
647/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000648/// issues an error if they are not declared. It returns list of
649/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000650void
Chris Lattnere13b9592008-07-26 04:03:38 +0000651Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000652 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000653 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000654 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000655 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000656 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
657 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000658 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000659 TypoCorrection Corrected = CorrectTypo(
660 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
661 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
662 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000663 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000664 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000665 Diag(PDecl->getLocation(), diag::note_previous_decl)
666 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000667 }
668 }
669
670 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000671 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000673 continue;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000676 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000677
678 // If this is a forward declaration and we are supposed to warn in this
679 // case, do it.
680 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000681 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000682 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000683 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000684 }
685}
686
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000687/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000688/// a class method in its extension.
689///
Mike Stump1eb44332009-09-09 15:08:12 +0000690void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000691 ObjCInterfaceDecl *ID) {
692 if (!ID)
693 return; // Possibly due to previous error
694
695 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000696 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
697 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000698 ObjCMethodDecl *MD = *i;
699 MethodMap[MD->getSelector()] = MD;
700 }
701
702 if (MethodMap.empty())
703 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000704 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
705 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000706 ObjCMethodDecl *Method = *i;
707 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
708 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
709 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
710 << Method->getDeclName();
711 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
712 }
713 }
714}
715
Chris Lattner58fe03b2009-04-12 08:43:13 +0000716/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000717Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000718Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000719 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000720 unsigned NumElts,
721 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000723 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattner4d391482007-12-12 07:09:47 +0000725 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000726 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000727 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000728 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000729 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000730 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000731 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000732 PushOnScopeChains(PDecl, TUScope, false);
733 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000734 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000735 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000736 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000737 if (!isNew)
738 PDecl->setChangedSinceDeserialization(true);
739 }
Chris Lattner4d391482007-12-12 07:09:47 +0000740 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000741 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
744 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000745 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000746 Protocols.data(), Protocols.size(),
747 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000748 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000749 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000750 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000751}
752
John McCalld226f652010-08-21 09:40:31 +0000753Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000754ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
755 IdentifierInfo *ClassName, SourceLocation ClassLoc,
756 IdentifierInfo *CategoryName,
757 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000758 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000759 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000760 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000761 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000762 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000763 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000764
765 /// Check that class of this category is already completely declared.
766 if (!IDecl || IDecl->isForwardDecl()) {
767 // Create an invalid ObjCCategoryDecl to serve as context for
768 // the enclosing method declarations. We mark the decl invalid
769 // to make it clear that this isn't a valid AST.
770 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
771 ClassLoc, CategoryLoc, CategoryName);
772 CDecl->setInvalidDecl();
773 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000774 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000775 }
776
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000777 if (!CategoryName && IDecl->getImplementation()) {
778 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
779 Diag(IDecl->getImplementation()->getLocation(),
780 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000781 }
782
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000783 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
784 ClassLoc, CategoryLoc, CategoryName);
785 // FIXME: PushOnScopeChains?
786 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000787
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000788 CDecl->setClassInterface(IDecl);
789 // Insert class extension to the list of class's categories.
790 if (!CategoryName)
791 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattner16b34b42009-02-16 21:30:01 +0000793 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000794 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000795
Fariborz Jahanian25760612010-02-15 21:55:26 +0000796 if (CategoryName) {
797 /// Check for duplicate interface declaration for this category
798 ObjCCategoryDecl *CDeclChain;
799 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
800 CDeclChain = CDeclChain->getNextClassCategory()) {
801 if (CDeclChain->getIdentifier() == CategoryName) {
802 // Class extensions can be declared multiple times.
803 Diag(CategoryLoc, diag::warn_dup_category_def)
804 << ClassName << CategoryName;
805 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
806 break;
807 }
Chris Lattner70f19542009-02-16 21:26:43 +0000808 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000809 if (!CDeclChain)
810 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000811 }
Chris Lattner70f19542009-02-16 21:26:43 +0000812
Chris Lattner4d391482007-12-12 07:09:47 +0000813 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000814 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000815 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000816 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000817 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000818 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000819 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlsson15281452008-11-04 16:57:32 +0000822 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000823 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000824}
825
826/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000828/// object.
John McCalld226f652010-08-21 09:40:31 +0000829Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000830 SourceLocation AtCatImplLoc,
831 IdentifierInfo *ClassName, SourceLocation ClassLoc,
832 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000833 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000834 ObjCCategoryDecl *CatIDecl = 0;
835 if (IDecl) {
836 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
837 if (!CatIDecl) {
838 // Category @implementation with no corresponding @interface.
839 // Create and install one.
840 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000841 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000842 CatName);
843 CatIDecl->setClassInterface(IDecl);
844 CatIDecl->insertNextClassCategory();
845 }
846 }
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000849 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
850 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000851 /// Check that class of this category is already completely declared.
852 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000853 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000854
Douglas Gregord0434102009-01-09 00:49:46 +0000855 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000856 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000857
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000858 /// Check that CatName, category name, is not used in another implementation.
859 if (CatIDecl) {
860 if (CatIDecl->getImplementation()) {
861 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
862 << CatName;
863 Diag(CatIDecl->getImplementation()->getLocation(),
864 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000865 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000866 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000867 // Warn on implementating category of deprecated class under
868 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000869 DiagnoseObjCImplementedDeprecations(*this,
870 dyn_cast<NamedDecl>(IDecl),
871 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000872 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Anders Carlsson15281452008-11-04 16:57:32 +0000875 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000876 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000877}
878
John McCalld226f652010-08-21 09:40:31 +0000879Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000880 SourceLocation AtClassImplLoc,
881 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000882 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000883 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000884 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000885 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000886 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000887 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
888 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000889 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000890 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000891 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000892 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
893 // If this is a forward declaration of an interface, warn.
894 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000896 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000897 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000898 } else {
899 // We did not find anything with the name ClassName; try to correct for
900 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000901 TypoCorrection Corrected = CorrectTypo(
902 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
903 NULL, NULL, false, CTC_NoKeywords);
904 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000905 // Suggest the (potentially) correct interface name. However, put the
906 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000907 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000908 // provide a code-modification hint or use the typo name for recovery,
909 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000910 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000911 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000912 << ClassName << CorrectedName;
913 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
914 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000915 IDecl = 0;
916 } else {
917 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
918 }
Chris Lattner4d391482007-12-12 07:09:47 +0000919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattner4d391482007-12-12 07:09:47 +0000921 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000922 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000923 if (SuperClassname) {
924 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000925 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
926 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000928 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
929 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000930 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000931 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000933 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000934 Diag(SuperClassLoc, diag::err_undef_superclass)
935 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000936 else if (IDecl && IDecl->getSuperClass() != SDecl) {
937 // This implementation and its interface do not have the same
938 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000939 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000940 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000941 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000942 }
943 }
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chris Lattner4d391482007-12-12 07:09:47 +0000946 if (!IDecl) {
947 // Legacy case of @implementation with no corresponding @interface.
948 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000949
Mike Stump390b4cc2009-05-16 07:39:55 +0000950 // FIXME: Do we support attributes on the @implementation? If so we should
951 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000952 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000953 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000954 IDecl->setSuperClass(SDecl);
955 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000956
957 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000958 } else {
959 // Mark the interface as being completed, even if it was just as
960 // @class ....;
961 // declaration; the user cannot reopen it.
962 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000963 }
Mike Stump1eb44332009-09-09 15:08:12 +0000964
965 ObjCImplementationDecl* IMPDecl =
966 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000967 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Anders Carlsson15281452008-11-04 16:57:32 +0000969 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000970 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattner4d391482007-12-12 07:09:47 +0000972 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000973 if (IDecl->getImplementation()) {
974 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000976 Diag(IDecl->getImplementation()->getLocation(),
977 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000978 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000979 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000980 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000981 // Warn on implementating deprecated class under
982 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000983 DiagnoseObjCImplementedDeprecations(*this,
984 dyn_cast<NamedDecl>(IDecl),
985 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000986 }
John McCalld226f652010-08-21 09:40:31 +0000987 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000988}
989
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
991 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000992 SourceLocation RBrace) {
993 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000994 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000995 if (!IDecl)
996 return;
997 /// Check case of non-existing @interface decl.
998 /// (legacy objective-c @implementation decl without an @interface decl).
999 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001000 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +00001001 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001002 // Add ivar's to class's DeclContext.
1003 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001004 ivars[i]->setLexicalDeclContext(ImpDecl);
1005 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001006 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001007 }
1008
Chris Lattner4d391482007-12-12 07:09:47 +00001009 return;
1010 }
1011 // If implementation has empty ivar list, just return.
1012 if (numIvars == 0)
1013 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattner4d391482007-12-12 07:09:47 +00001015 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001016 if (LangOpts.ObjCNonFragileABI2) {
1017 if (ImpDecl->getSuperClass())
1018 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1019 for (unsigned i = 0; i < numIvars; i++) {
1020 ObjCIvarDecl* ImplIvar = ivars[i];
1021 if (const ObjCIvarDecl *ClsIvar =
1022 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1023 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1024 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1025 continue;
1026 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001027 // Instance ivar to Implementation's DeclContext.
1028 ImplIvar->setLexicalDeclContext(ImpDecl);
1029 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1030 ImpDecl->addDecl(ImplIvar);
1031 }
1032 return;
1033 }
Chris Lattner4d391482007-12-12 07:09:47 +00001034 // Check interface's Ivar list against those in the implementation.
1035 // names and types must match.
1036 //
Chris Lattner4d391482007-12-12 07:09:47 +00001037 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001038 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001039 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1040 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001041 ObjCIvarDecl* ImplIvar = ivars[j++];
1042 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001043 assert (ImplIvar && "missing implementation ivar");
1044 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Steve Naroffca331292009-03-03 14:49:36 +00001046 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001047 if (Context.getCanonicalType(ImplIvar->getType()) !=
1048 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001049 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001050 << ImplIvar->getIdentifier()
1051 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001052 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001053 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1054 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1055 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001056 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1057 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001058 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1059 << ImplIvar->getIdentifier();
1060 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062 }
Steve Naroffca331292009-03-03 14:49:36 +00001063 // Make sure the names are identical.
1064 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001065 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001066 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001067 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001068 }
1069 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner609e4c72007-12-12 18:11:49 +00001072 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001073 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001074 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001075 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001076}
1077
Steve Naroff3c2eb662008-02-10 21:38:56 +00001078void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001079 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001080 // No point warning no definition of method which is 'unavailable'.
1081 if (method->hasAttr<UnavailableAttr>())
1082 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001083 if (!IncompleteImpl) {
1084 Diag(ImpLoc, diag::warn_incomplete_impl);
1085 IncompleteImpl = true;
1086 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001087 if (DiagID == diag::warn_unimplemented_protocol_method)
1088 Diag(ImpLoc, DiagID) << method->getDeclName();
1089 else
1090 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001091}
1092
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001093/// Determines if type B can be substituted for type A. Returns true if we can
1094/// guarantee that anything that the user will do to an object of type A can
1095/// also be done to an object of type B. This is trivially true if the two
1096/// types are the same, or if B is a subclass of A. It becomes more complex
1097/// in cases where protocols are involved.
1098///
1099/// Object types in Objective-C describe the minimum requirements for an
1100/// object, rather than providing a complete description of a type. For
1101/// example, if A is a subclass of B, then B* may refer to an instance of A.
1102/// The principle of substitutability means that we may use an instance of A
1103/// anywhere that we may use an instance of B - it will implement all of the
1104/// ivars of B and all of the methods of B.
1105///
1106/// This substitutability is important when type checking methods, because
1107/// the implementation may have stricter type definitions than the interface.
1108/// The interface specifies minimum requirements, but the implementation may
1109/// have more accurate ones. For example, a method may privately accept
1110/// instances of B, but only publish that it accepts instances of A. Any
1111/// object passed to it will be type checked against B, and so will implicitly
1112/// by a valid A*. Similarly, a method may return a subclass of the class that
1113/// it is declared as returning.
1114///
1115/// This is most important when considering subclassing. A method in a
1116/// subclass must accept any object as an argument that its superclass's
1117/// implementation accepts. It may, however, accept a more general type
1118/// without breaking substitutability (i.e. you can still use the subclass
1119/// anywhere that you can use the superclass, but not vice versa). The
1120/// converse requirement applies to return types: the return type for a
1121/// subclass method must be a valid object of the kind that the superclass
1122/// advertises, but it may be specified more accurately. This avoids the need
1123/// for explicit down-casting by callers.
1124///
1125/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001126static bool isObjCTypeSubstitutable(ASTContext &Context,
1127 const ObjCObjectPointerType *A,
1128 const ObjCObjectPointerType *B,
1129 bool rejectId) {
1130 // Reject a protocol-unqualified id.
1131 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001132
1133 // If B is a qualified id, then A must also be a qualified id and it must
1134 // implement all of the protocols in B. It may not be a qualified class.
1135 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1136 // stricter definition so it is not substitutable for id<A>.
1137 if (B->isObjCQualifiedIdType()) {
1138 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001139 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1140 QualType(B,0),
1141 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001142 }
1143
1144 /*
1145 // id is a special type that bypasses type checking completely. We want a
1146 // warning when it is used in one place but not another.
1147 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1148
1149
1150 // If B is a qualified id, then A must also be a qualified id (which it isn't
1151 // if we've got this far)
1152 if (B->isObjCQualifiedIdType()) return false;
1153 */
1154
1155 // Now we know that A and B are (potentially-qualified) class types. The
1156 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001157 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001158}
1159
John McCall10302c02010-10-28 02:34:38 +00001160static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1161 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1162}
1163
1164static void CheckMethodOverrideReturn(Sema &S,
1165 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001166 ObjCMethodDecl *MethodDecl,
1167 bool IsProtocolMethodDecl) {
1168 if (IsProtocolMethodDecl &&
1169 (MethodDecl->getObjCDeclQualifier() !=
1170 MethodImpl->getObjCDeclQualifier())) {
1171 S.Diag(MethodImpl->getLocation(),
1172 diag::warn_conflicting_ret_type_modifiers)
1173 << MethodImpl->getDeclName()
1174 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1175 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1176 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1177 }
1178
John McCall10302c02010-10-28 02:34:38 +00001179 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001180 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001181 return;
1182
1183 unsigned DiagID = diag::warn_conflicting_ret_types;
1184
1185 // Mismatches between ObjC pointers go into a different warning
1186 // category, and sometimes they're even completely whitelisted.
1187 if (const ObjCObjectPointerType *ImplPtrTy =
1188 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1189 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001190 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001191 // Allow non-matching return types as long as they don't violate
1192 // the principle of substitutability. Specifically, we permit
1193 // return types that are subclasses of the declared return type,
1194 // or that are more-qualified versions of the declared type.
1195 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1196 return;
1197
1198 DiagID = diag::warn_non_covariant_ret_types;
1199 }
1200 }
1201
1202 S.Diag(MethodImpl->getLocation(), DiagID)
1203 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001204 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001205 << MethodImpl->getResultType()
1206 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001207 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1208 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001209}
1210
1211static void CheckMethodOverrideParam(Sema &S,
1212 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001213 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001214 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 ParmVarDecl *IfaceVar,
1216 bool IsProtocolMethodDecl) {
1217 if (IsProtocolMethodDecl &&
1218 (ImplVar->getObjCDeclQualifier() !=
1219 IfaceVar->getObjCDeclQualifier())) {
1220 S.Diag(ImplVar->getLocation(),
1221 diag::warn_conflicting_param_modifiers)
1222 << getTypeRange(ImplVar->getTypeSourceInfo())
1223 << MethodImpl->getDeclName();
1224 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1225 << getTypeRange(IfaceVar->getTypeSourceInfo());
1226 }
1227
John McCall10302c02010-10-28 02:34:38 +00001228 QualType ImplTy = ImplVar->getType();
1229 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001230
John McCall10302c02010-10-28 02:34:38 +00001231 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1232 return;
1233
1234 unsigned DiagID = diag::warn_conflicting_param_types;
1235
1236 // Mismatches between ObjC pointers go into a different warning
1237 // category, and sometimes they're even completely whitelisted.
1238 if (const ObjCObjectPointerType *ImplPtrTy =
1239 ImplTy->getAs<ObjCObjectPointerType>()) {
1240 if (const ObjCObjectPointerType *IfacePtrTy =
1241 IfaceTy->getAs<ObjCObjectPointerType>()) {
1242 // Allow non-matching argument types as long as they don't
1243 // violate the principle of substitutability. Specifically, the
1244 // implementation must accept any objects that the superclass
1245 // accepts, however it may also accept others.
1246 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1247 return;
1248
1249 DiagID = diag::warn_non_contravariant_param_types;
1250 }
1251 }
1252
1253 S.Diag(ImplVar->getLocation(), DiagID)
1254 << getTypeRange(ImplVar->getTypeSourceInfo())
1255 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1256 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1257 << getTypeRange(IfaceVar->getTypeSourceInfo());
1258}
John McCallf85e1932011-06-15 23:02:42 +00001259
1260/// In ARC, check whether the conventional meanings of the two methods
1261/// match. If they don't, it's a hard error.
1262static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1263 ObjCMethodDecl *decl) {
1264 ObjCMethodFamily implFamily = impl->getMethodFamily();
1265 ObjCMethodFamily declFamily = decl->getMethodFamily();
1266 if (implFamily == declFamily) return false;
1267
1268 // Since conventions are sorted by selector, the only possibility is
1269 // that the types differ enough to cause one selector or the other
1270 // to fall out of the family.
1271 assert(implFamily == OMF_None || declFamily == OMF_None);
1272
1273 // No further diagnostics required on invalid declarations.
1274 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1275
1276 const ObjCMethodDecl *unmatched = impl;
1277 ObjCMethodFamily family = declFamily;
1278 unsigned errorID = diag::err_arc_lost_method_convention;
1279 unsigned noteID = diag::note_arc_lost_method_convention;
1280 if (declFamily == OMF_None) {
1281 unmatched = decl;
1282 family = implFamily;
1283 errorID = diag::err_arc_gained_method_convention;
1284 noteID = diag::note_arc_gained_method_convention;
1285 }
1286
1287 // Indexes into a %select clause in the diagnostic.
1288 enum FamilySelector {
1289 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1290 };
1291 FamilySelector familySelector = FamilySelector();
1292
1293 switch (family) {
1294 case OMF_None: llvm_unreachable("logic error, no method convention");
1295 case OMF_retain:
1296 case OMF_release:
1297 case OMF_autorelease:
1298 case OMF_dealloc:
1299 case OMF_retainCount:
1300 case OMF_self:
1301 // Mismatches for these methods don't change ownership
1302 // conventions, so we don't care.
1303 return false;
1304
1305 case OMF_init: familySelector = F_init; break;
1306 case OMF_alloc: familySelector = F_alloc; break;
1307 case OMF_copy: familySelector = F_copy; break;
1308 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1309 case OMF_new: familySelector = F_new; break;
1310 }
1311
1312 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1313 ReasonSelector reasonSelector;
1314
1315 // The only reason these methods don't fall within their families is
1316 // due to unusual result types.
1317 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1318 reasonSelector = R_UnrelatedReturn;
1319 } else {
1320 reasonSelector = R_NonObjectReturn;
1321 }
1322
1323 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1324 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1325
1326 return true;
1327}
John McCall10302c02010-10-28 02:34:38 +00001328
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001329void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001330 ObjCMethodDecl *MethodDecl,
1331 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001332 if (getLangOptions().ObjCAutoRefCount &&
1333 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1334 return;
1335
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001336 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1337 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Chris Lattner3aff9192009-04-11 19:58:42 +00001339 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001340 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001341 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001342 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1343 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001345 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001346 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001347 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001348 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001349}
1350
Mike Stump390b4cc2009-05-16 07:39:55 +00001351/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1352/// improve the efficiency of selector lookups and type checking by associating
1353/// with each protocol / interface / category the flattened instance tables. If
1354/// we used an immutable set to keep the table then it wouldn't add significant
1355/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001356
Steve Naroffefe7f362008-02-08 22:06:17 +00001357/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001358/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001359void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1360 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001361 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001362 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001363 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001364 ObjCContainerDecl *CDecl) {
1365 ObjCInterfaceDecl *IDecl;
1366 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1367 IDecl = C->getClassInterface();
1368 else
1369 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1370 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1371
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001372 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001373 ObjCInterfaceDecl *NSIDecl = 0;
1374 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001375 // check to see if class implements forwardInvocation method and objects
1376 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001377 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001378 // Under such conditions, which means that every method possible is
1379 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001380 // found" warnings.
1381 // FIXME: Use a general GetUnarySelector method for this.
1382 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1383 Selector fISelector = Context.Selectors.getSelector(1, &II);
1384 if (InsMap.count(fISelector))
1385 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1386 // need be implemented in the implementation.
1387 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1388 }
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001390 // If a method lookup fails locally we still need to look and see if
1391 // the method was implemented by a base class or an inherited
1392 // protocol. This lookup is slow, but occurs rarely in correct code
1393 // and otherwise would terminate in a warning.
1394
Chris Lattner4d391482007-12-12 07:09:47 +00001395 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001396 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001397 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001398 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001399 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001400 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001401 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001402 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001403 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001404 // Ugly, but necessary. Method declared in protcol might have
1405 // have been synthesized due to a property declared in the class which
1406 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001407 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001408 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001409 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001410 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001411 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1412 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001413 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001414 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001415 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1416 << PDecl->getDeclName();
1417 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001418 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001419 }
1420 }
Chris Lattner4d391482007-12-12 07:09:47 +00001421 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001422 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001423 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001424 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001425 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001426 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1427 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001428 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001429 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001430 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001431 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001432 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001433 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1434 PDecl->getDeclName();
1435 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001436 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001437 }
Chris Lattner780f3292008-07-21 21:32:27 +00001438 // Check on this protocols's referenced protocols, recursively.
1439 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1440 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001441 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001442}
1443
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001444/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1445/// or protocol against those declared in their implementations.
1446///
1447void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1448 const llvm::DenseSet<Selector> &ClsMap,
1449 llvm::DenseSet<Selector> &InsMapSeen,
1450 llvm::DenseSet<Selector> &ClsMapSeen,
1451 ObjCImplDecl* IMPDecl,
1452 ObjCContainerDecl* CDecl,
1453 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001454 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001455 // Check and see if instance methods in class interface have been
1456 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1458 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001459 if (InsMapSeen.count((*I)->getSelector()))
1460 continue;
1461 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001462 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001463 !InsMap.count((*I)->getSelector())) {
1464 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001465 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1466 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001467 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001468 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001469 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001470 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001471 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001472 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001473 assert(MethodDecl &&
1474 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001475 // ImpMethodDecl may be null as in a @dynamic property.
1476 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001477 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1478 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001479 }
1480 }
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001482 // Check and see if class methods in class interface have been
1483 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001484 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001485 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001486 if (ClsMapSeen.count((*I)->getSelector()))
1487 continue;
1488 ClsMapSeen.insert((*I)->getSelector());
1489 if (!ClsMap.count((*I)->getSelector())) {
1490 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001491 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1492 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001493 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001494 ObjCMethodDecl *ImpMethodDecl =
1495 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001496 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001497 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001498 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1499 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001500 }
1501 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001502
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001503 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001504 // Also methods in class extensions need be looked at next.
1505 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1506 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1507 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1508 IMPDecl,
1509 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1510 IncompleteImpl, false);
1511
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001512 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001513 for (ObjCInterfaceDecl::all_protocol_iterator
1514 PI = I->all_referenced_protocol_begin(),
1515 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001516 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1517 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001518 (*PI), IncompleteImpl, false);
1519 if (I->getSuperClass())
1520 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001521 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001522 I->getSuperClass(), IncompleteImpl, false);
1523 }
1524}
1525
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001526void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001527 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001528 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001529 llvm::DenseSet<Selector> InsMap;
1530 // Check and see if instance methods in class interface have been
1531 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001532 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001533 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001534 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001536 // Check and see if properties declared in the interface have either 1)
1537 // an implementation or 2) there is a @synthesize/@dynamic implementation
1538 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001539 if (isa<ObjCInterfaceDecl>(CDecl) &&
1540 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001541 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001542
Chris Lattner4d391482007-12-12 07:09:47 +00001543 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001544 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001545 I = IMPDecl->classmeth_begin(),
1546 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001547 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001549 // Check for type conflict of methods declared in a class/protocol and
1550 // its implementation; if any.
1551 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001552 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1553 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Chris Lattner4d391482007-12-12 07:09:47 +00001556 // Check the protocol list for unimplemented methods in the @implementation
1557 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001558 // Check and see if class methods in class interface have been
1559 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Chris Lattnercddc8882009-03-01 00:56:52 +00001561 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001562 for (ObjCInterfaceDecl::all_protocol_iterator
1563 PI = I->all_referenced_protocol_begin(),
1564 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001565 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001566 InsMap, ClsMap, I);
1567 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001568 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1569 Categories; Categories = Categories->getNextClassExtension())
1570 ImplMethodsVsClassMethods(S, IMPDecl,
1571 const_cast<ObjCCategoryDecl*>(Categories),
1572 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001573 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001574 // For extended class, unimplemented methods in its protocols will
1575 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001576 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001577 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1578 E = C->protocol_end(); PI != E; ++PI)
1579 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001580 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001581 // Report unimplemented properties in the category as well.
1582 // When reporting on missing setter/getters, do not report when
1583 // setter/getter is implemented in category's primary class
1584 // implementation.
1585 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1586 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1587 for (ObjCImplementationDecl::instmeth_iterator
1588 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1589 InsMap.insert((*I)->getSelector());
1590 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001591 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001592 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001593 } else
1594 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001595}
1596
Mike Stump1eb44332009-09-09 15:08:12 +00001597/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001598Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001599Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001600 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001601 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001602 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001603 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Chris Lattner4d391482007-12-12 07:09:47 +00001605 for (unsigned i = 0; i != NumElts; ++i) {
1606 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001607 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001608 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001609 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001610 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001611 // Maybe we will complain about the shadowed template parameter.
1612 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1613 // Just pretend that we didn't see the previous declaration.
1614 PrevDecl = 0;
1615 }
1616
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001618 // GCC apparently allows the following idiom:
1619 //
1620 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1621 // @class XCElementToggler;
1622 //
Mike Stump1eb44332009-09-09 15:08:12 +00001623 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001624 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001625 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001626 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001627 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001628 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001629 // a forward class declaration matching a typedef name of a class refers
1630 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001631 if (const ObjCObjectType *OI =
1632 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1633 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001634 }
Chris Lattner4d391482007-12-12 07:09:47 +00001635 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001636 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1637 if (!IDecl) { // Not already seen? Make a forward decl.
1638 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1639 IdentList[i], IdentLocs[i], true);
1640
1641 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1642 // the current DeclContext. This prevents clients that walk DeclContext
1643 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1644 // declared later (if at all). We also take care to explicitly make
1645 // sure this declaration is visible for name lookup.
1646 PushOnScopeChains(IDecl, TUScope, false);
1647 CurContext->makeDeclVisibleInContext(IDecl, true);
1648 }
Chris Lattner4d391482007-12-12 07:09:47 +00001649
1650 Interfaces.push_back(IDecl);
1651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek321c22f2009-11-18 00:28:11 +00001653 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001654 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001655 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001656 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001657 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001658 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001659 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001660}
1661
John McCall0f4c4c42011-06-16 01:15:19 +00001662static bool tryMatchRecordTypes(ASTContext &Context,
1663 Sema::MethodMatchStrategy strategy,
1664 const Type *left, const Type *right);
1665
John McCallf85e1932011-06-15 23:02:42 +00001666static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1667 QualType leftQT, QualType rightQT) {
1668 const Type *left =
1669 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1670 const Type *right =
1671 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1672
1673 if (left == right) return true;
1674
1675 // If we're doing a strict match, the types have to match exactly.
1676 if (strategy == Sema::MMS_strict) return false;
1677
1678 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1679
1680 // Otherwise, use this absurdly complicated algorithm to try to
1681 // validate the basic, low-level compatibility of the two types.
1682
1683 // As a minimum, require the sizes and alignments to match.
1684 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1685 return false;
1686
1687 // Consider all the kinds of non-dependent canonical types:
1688 // - functions and arrays aren't possible as return and parameter types
1689
1690 // - vector types of equal size can be arbitrarily mixed
1691 if (isa<VectorType>(left)) return isa<VectorType>(right);
1692 if (isa<VectorType>(right)) return false;
1693
1694 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001695 // - structs, unions, and Objective-C objects must match more-or-less
1696 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001697 // - everything else should be a scalar
1698 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001699 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001700
1701 // Make scalars agree in kind, except count bools as chars.
1702 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1703 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1704 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1705 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1706
1707 // Note that data member pointers and function member pointers don't
1708 // intermix because of the size differences.
1709
1710 return (leftSK == rightSK);
1711}
Chris Lattner4d391482007-12-12 07:09:47 +00001712
John McCall0f4c4c42011-06-16 01:15:19 +00001713static bool tryMatchRecordTypes(ASTContext &Context,
1714 Sema::MethodMatchStrategy strategy,
1715 const Type *lt, const Type *rt) {
1716 assert(lt && rt && lt != rt);
1717
1718 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1719 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1720 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1721
1722 // Require union-hood to match.
1723 if (left->isUnion() != right->isUnion()) return false;
1724
1725 // Require an exact match if either is non-POD.
1726 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1727 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1728 return false;
1729
1730 // Require size and alignment to match.
1731 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1732
1733 // Require fields to match.
1734 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1735 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1736 for (; li != le && ri != re; ++li, ++ri) {
1737 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1738 return false;
1739 }
1740 return (li == le && ri == re);
1741}
1742
Chris Lattner4d391482007-12-12 07:09:47 +00001743/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1744/// returns true, or false, accordingly.
1745/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001746bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1747 const ObjCMethodDecl *right,
1748 MethodMatchStrategy strategy) {
1749 if (!matchTypes(Context, strategy,
1750 left->getResultType(), right->getResultType()))
1751 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001752
John McCallf85e1932011-06-15 23:02:42 +00001753 if (getLangOptions().ObjCAutoRefCount &&
1754 (left->hasAttr<NSReturnsRetainedAttr>()
1755 != right->hasAttr<NSReturnsRetainedAttr>() ||
1756 left->hasAttr<NSConsumesSelfAttr>()
1757 != right->hasAttr<NSConsumesSelfAttr>()))
1758 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
John McCallf85e1932011-06-15 23:02:42 +00001760 ObjCMethodDecl::param_iterator
1761 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001762
John McCallf85e1932011-06-15 23:02:42 +00001763 for (; li != le; ++li, ++ri) {
1764 assert(ri != right->param_end() && "Param mismatch");
1765 ParmVarDecl *lparm = *li, *rparm = *ri;
1766
1767 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1768 return false;
1769
1770 if (getLangOptions().ObjCAutoRefCount &&
1771 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1772 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001773 }
1774 return true;
1775}
1776
Sebastian Redldb9d2142010-08-02 23:18:59 +00001777/// \brief Read the contents of the method pool for a given selector from
1778/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001779///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001780/// This routine should only be called once, when the method pool has no entry
1781/// for this selector.
1782Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001783 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001784 assert(MethodPool.find(Sel) == MethodPool.end() &&
1785 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001786
1787 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001788 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Sebastian Redldb9d2142010-08-02 23:18:59 +00001790 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001791}
1792
Sebastian Redldb9d2142010-08-02 23:18:59 +00001793void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1794 bool instance) {
1795 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1796 if (Pos == MethodPool.end()) {
1797 if (ExternalSource)
1798 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001799 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001800 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1801 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001802 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001803 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001804 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001805 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001806 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001807 Entry.Method = Method;
1808 Entry.Next = 0;
1809 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001810 }
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Chris Lattnerb25df352009-03-04 05:16:45 +00001812 // We've seen a method with this name, see if we have already seen this type
1813 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001814 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1815 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1816
1817 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001818 ObjCMethodDecl *PrevObjCMethod = List->Method;
1819 PrevObjCMethod->setDefined(impl);
1820 // If a method is deprecated, push it in the global pool.
1821 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001822 if (Method->isDeprecated()) {
1823 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001824 List->Method = Method;
1825 }
1826 // If new method is unavailable, push it into global pool
1827 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001828 if (Method->isUnavailable()) {
1829 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001830 List->Method = Method;
1831 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001832 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001833 }
John McCallf85e1932011-06-15 23:02:42 +00001834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattnerb25df352009-03-04 05:16:45 +00001836 // We have a new signature for an existing method - add it.
1837 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001838 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1839 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001840}
1841
John McCallf85e1932011-06-15 23:02:42 +00001842/// Determines if this is an "acceptable" loose mismatch in the global
1843/// method pool. This exists mostly as a hack to get around certain
1844/// global mismatches which we can't afford to make warnings / errors.
1845/// Really, what we want is a way to take a method out of the global
1846/// method pool.
1847static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1848 ObjCMethodDecl *other) {
1849 if (!chosen->isInstanceMethod())
1850 return false;
1851
1852 Selector sel = chosen->getSelector();
1853 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1854 return false;
1855
1856 // Don't complain about mismatches for -length if the method we
1857 // chose has an integral result type.
1858 return (chosen->getResultType()->isIntegerType());
1859}
1860
Sebastian Redldb9d2142010-08-02 23:18:59 +00001861ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001862 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001863 bool warn, bool instance) {
1864 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1865 if (Pos == MethodPool.end()) {
1866 if (ExternalSource)
1867 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001868 else
1869 return 0;
1870 }
1871
Sebastian Redldb9d2142010-08-02 23:18:59 +00001872 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Sebastian Redldb9d2142010-08-02 23:18:59 +00001874 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001875 bool issueDiagnostic = false, issueError = false;
1876
1877 // We support a warning which complains about *any* difference in
1878 // method signature.
1879 bool strictSelectorMatch =
1880 (receiverIdOrClass && warn &&
1881 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1882 R.getBegin()) !=
1883 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001884 if (strictSelectorMatch)
1885 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001886 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1887 MMS_strict)) {
1888 issueDiagnostic = true;
1889 break;
1890 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001891 }
1892
John McCallf85e1932011-06-15 23:02:42 +00001893 // If we didn't see any strict differences, we won't see any loose
1894 // differences. In ARC, however, we also need to check for loose
1895 // mismatches, because most of them are errors.
1896 if (!strictSelectorMatch ||
1897 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001898 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001899 // This checks if the methods differ in type mismatch.
1900 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1901 MMS_loose) &&
1902 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1903 issueDiagnostic = true;
1904 if (getLangOptions().ObjCAutoRefCount)
1905 issueError = true;
1906 break;
1907 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001908 }
1909
John McCallf85e1932011-06-15 23:02:42 +00001910 if (issueDiagnostic) {
1911 if (issueError)
1912 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1913 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001914 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1915 else
1916 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001917
1918 Diag(MethList.Method->getLocStart(),
1919 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001920 << MethList.Method->getSourceRange();
1921 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1922 Diag(Next->Method->getLocStart(), diag::note_also_found)
1923 << Next->Method->getSourceRange();
1924 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001925 }
1926 return MethList.Method;
1927}
1928
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001929ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001930 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1931 if (Pos == MethodPool.end())
1932 return 0;
1933
1934 GlobalMethods &Methods = Pos->second;
1935
1936 if (Methods.first.Method && Methods.first.Method->isDefined())
1937 return Methods.first.Method;
1938 if (Methods.second.Method && Methods.second.Method->isDefined())
1939 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001940 return 0;
1941}
1942
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001943/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1944/// identical selector names in current and its super classes and issues
1945/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001946void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1947 ObjCMethodDecl *Method,
1948 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001949 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1950 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001952 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001953 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001954 SD->lookupMethod(Method->getSelector(), IsInstance);
1955 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001956 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001957 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001958 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001959 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1960 E = Method->param_end();
1961 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1962 for (; ParamI != E; ++ParamI, ++PrevI) {
1963 // Number of parameters are the same and is guaranteed by selector match.
1964 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1965 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1966 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001967 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001968 // respective argument type in the super class method, issue warning;
1969 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001970 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001971 << T1 << T2;
1972 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1973 return;
1974 }
1975 }
1976 ID = SD;
1977 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001978}
1979
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001980/// DiagnoseDuplicateIvars -
1981/// Check for duplicate ivars in the entire class at the start of
1982/// @implementation. This becomes necesssary because class extension can
1983/// add ivars to a class in random order which will not be known until
1984/// class's @implementation is seen.
1985void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1986 ObjCInterfaceDecl *SID) {
1987 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1988 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1989 ObjCIvarDecl* Ivar = (*IVI);
1990 if (Ivar->isInvalidDecl())
1991 continue;
1992 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1993 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1994 if (prevIvar) {
1995 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1996 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1997 Ivar->setInvalidDecl();
1998 }
1999 }
2000 }
2001}
2002
Steve Naroffa56f6162007-12-18 01:30:32 +00002003// Note: For class/category implemenations, allMethods/allProperties is
2004// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002005void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002006 Decl *ClassDecl,
2007 Decl **allMethods, unsigned allNum,
2008 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002009 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00002010 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
2011 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00002012 // should be true.
2013 if (!ClassDecl)
2014 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002015
Mike Stump1eb44332009-09-09 15:08:12 +00002016 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002017 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2018 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002019 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002020
Ted Kremenek782f2f52010-01-07 01:20:12 +00002021 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2022 // FIXME: This is wrong. We shouldn't be pretending that there is
2023 // an '@end' in the declaration.
2024 SourceLocation L = ClassDecl->getLocation();
2025 AtEnd.setBegin(L);
2026 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002027 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002028 }
2029
Steve Naroff0701bbb2009-01-08 17:28:14 +00002030 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2031 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2032 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2033
Chris Lattner4d391482007-12-12 07:09:47 +00002034 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002035 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002036 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002037
2038 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002039 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002040 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002041 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002042 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002043 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002044 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002045 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002046 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002047 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002048 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002049 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002050 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002051 InsMap[Method->getSelector()] = Method;
2052 /// The following allows us to typecheck messages to "id".
2053 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002054 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002055 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002056 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002057 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002058 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002059 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002060 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002061 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002062 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002063 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002064 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002065 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002066 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002067 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002068 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002069 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002070 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002071 /// The following allows us to typecheck messages to "Class".
2072 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002073 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002074 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002075 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002076 }
2077 }
2078 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002079 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002080 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002081 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002082 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002083 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002084 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002085 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002086 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002087 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002088
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002089 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002090 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002091 if (C->IsClassExtension()) {
2092 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2093 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002094 }
Chris Lattner4d391482007-12-12 07:09:47 +00002095 }
Steve Naroff09c47192009-01-09 15:36:25 +00002096 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002097 if (CDecl->getIdentifier())
2098 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2099 // user-defined setter/getter. It also synthesizes setter/getter methods
2100 // and adds them to the DeclContext and global method pools.
2101 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2102 E = CDecl->prop_end();
2103 I != E; ++I)
2104 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002105 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002106 }
2107 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002108 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002109 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002110 // Any property declared in a class extension might have user
2111 // declared setter or getter in current class extension or one
2112 // of the other class extensions. Mark them as synthesized as
2113 // property will be synthesized when property with same name is
2114 // seen in the @implementation.
2115 for (const ObjCCategoryDecl *ClsExtDecl =
2116 IDecl->getFirstClassExtension();
2117 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2118 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2119 E = ClsExtDecl->prop_end(); I != E; ++I) {
2120 ObjCPropertyDecl *Property = (*I);
2121 // Skip over properties declared @dynamic
2122 if (const ObjCPropertyImplDecl *PIDecl
2123 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2124 if (PIDecl->getPropertyImplementation()
2125 == ObjCPropertyImplDecl::Dynamic)
2126 continue;
2127
2128 for (const ObjCCategoryDecl *CExtDecl =
2129 IDecl->getFirstClassExtension();
2130 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2131 if (ObjCMethodDecl *GetterMethod =
2132 CExtDecl->getInstanceMethod(Property->getGetterName()))
2133 GetterMethod->setSynthesized(true);
2134 if (!Property->isReadOnly())
2135 if (ObjCMethodDecl *SetterMethod =
2136 CExtDecl->getInstanceMethod(Property->getSetterName()))
2137 SetterMethod->setSynthesized(true);
2138 }
2139 }
2140 }
2141
Ted Kremenekc32647d2010-12-23 21:35:43 +00002142 if (LangOpts.ObjCDefaultSynthProperties &&
2143 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002144 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002145 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002146 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002147 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002148
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002149 if (LangOpts.ObjCNonFragileABI2)
2150 while (IDecl->getSuperClass()) {
2151 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2152 IDecl = IDecl->getSuperClass();
2153 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002154 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002155 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002156 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002157 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002158 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Chris Lattner4d391482007-12-12 07:09:47 +00002160 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002161 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002162 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002163 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002164 Categories; Categories = Categories->getNextClassCategory()) {
2165 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002166 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002167 break;
2168 }
2169 }
2170 }
2171 }
Chris Lattner682bf922009-03-29 16:50:03 +00002172 if (isInterfaceDeclKind) {
2173 // Reject invalid vardecls.
2174 for (unsigned i = 0; i != tuvNum; i++) {
2175 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2176 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2177 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002178 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002179 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002180 }
Chris Lattner682bf922009-03-29 16:50:03 +00002181 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002182 }
Chris Lattner4d391482007-12-12 07:09:47 +00002183}
2184
2185
2186/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2187/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002188static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002189CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002190 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002191}
2192
Ted Kremenek422bae72010-04-18 04:59:38 +00002193static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002194bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002195 // The 'ibaction' attribute is allowed on method definitions because of
2196 // how the IBAction macro is used on both method declarations and definitions.
2197 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002198 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2199 if ((*i)->getKind() != attr::IBAction)
2200 return true;
2201 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002202}
2203
Douglas Gregor926df6c2011-06-11 01:09:30 +00002204/// \brief Check whether the declared result type of the given Objective-C
2205/// method declaration is compatible with the method's class.
2206///
2207static bool
2208CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2209 ObjCInterfaceDecl *CurrentClass) {
2210 QualType ResultType = Method->getResultType();
2211 SourceRange ResultTypeRange;
2212 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2213 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2214
2215 // If an Objective-C method inherits its related result type, then its
2216 // declared result type must be compatible with its own class type. The
2217 // declared result type is compatible if:
2218 if (const ObjCObjectPointerType *ResultObjectType
2219 = ResultType->getAs<ObjCObjectPointerType>()) {
2220 // - it is id or qualified id, or
2221 if (ResultObjectType->isObjCIdType() ||
2222 ResultObjectType->isObjCQualifiedIdType())
2223 return false;
2224
2225 if (CurrentClass) {
2226 if (ObjCInterfaceDecl *ResultClass
2227 = ResultObjectType->getInterfaceDecl()) {
2228 // - it is the same as the method's class type, or
2229 if (CurrentClass == ResultClass)
2230 return false;
2231
2232 // - it is a superclass of the method's class type
2233 if (ResultClass->isSuperClassOf(CurrentClass))
2234 return false;
2235 }
2236 }
2237 }
2238
2239 return true;
2240}
2241
2242/// \brief Determine if any method in the global method pool has an inferred
2243/// result type.
2244static bool
2245anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
2246 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
2247 if (Pos == S.MethodPool.end()) {
2248 if (S.ExternalSource)
2249 Pos = S.ReadMethodPool(Sel);
2250 else
2251 return 0;
2252 }
2253
2254 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
2255 for (ObjCMethodList *M = &List; M; M = M->Next) {
2256 if (M->Method && M->Method->hasRelatedResultType())
2257 return true;
2258 }
2259
2260 return false;
2261}
2262
John McCalld226f652010-08-21 09:40:31 +00002263Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002264 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002265 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00002266 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00002267 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002268 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002269 Selector Sel,
2270 // optional arguments. The number of types/arguments is obtained
2271 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002272 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002273 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002274 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002275 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002276 // Make sure we can establish a context for the method.
2277 if (!ClassDecl) {
2278 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002279 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002280 }
Chris Lattner4d391482007-12-12 07:09:47 +00002281 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002283 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002284 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002285 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Steve Naroffccef3712009-02-20 22:59:16 +00002287 // Methods cannot return interface types. All ObjC objects are
2288 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002289 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002290 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2291 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002292 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002293 }
Steve Naroffccef3712009-02-20 22:59:16 +00002294 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002295 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002296
2297 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002298 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002299 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002300 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002301 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002302 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002303 MethodDeclKind == tok::objc_optional
2304 ? ObjCMethodDecl::Optional
2305 : ObjCMethodDecl::Required,
2306 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Chris Lattner0ed844b2008-04-04 06:12:32 +00002308 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Chris Lattner7db638d2009-04-11 19:42:43 +00002310 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002311 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002312 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Chris Lattnere294d3f2009-04-11 18:57:04 +00002314 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002315 ArgType = Context.getObjCIdType();
2316 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002317 } else {
John McCall58e46772009-10-23 21:48:59 +00002318 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002319 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002320 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002321 }
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002323 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2324 LookupOrdinaryName, ForRedeclaration);
2325 LookupName(R, S);
2326 if (R.isSingleResult()) {
2327 NamedDecl *PrevDecl = R.getFoundDecl();
2328 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002329 Diag(ArgInfo[i].NameLoc,
2330 (MethodDefinition ? diag::warn_method_param_redefinition
2331 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002332 << ArgInfo[i].Name;
2333 Diag(PrevDecl->getLocation(),
2334 diag::note_previous_declaration);
2335 }
2336 }
2337
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002338 SourceLocation StartLoc = DI
2339 ? DI->getTypeLoc().getBeginLoc()
2340 : ArgInfo[i].NameLoc;
2341
John McCall81ef3e62011-04-23 02:46:06 +00002342 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2343 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2344 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
John McCall70798862011-05-02 00:30:12 +00002346 Param->setObjCMethodScopeInfo(i);
2347
Chris Lattner0ed844b2008-04-04 06:12:32 +00002348 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002349 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002350
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002351 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002352 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002354 S->AddDecl(Param);
2355 IdResolver.AddDecl(Param);
2356
Chris Lattner0ed844b2008-04-04 06:12:32 +00002357 Params.push_back(Param);
2358 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002359
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002360 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002361 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002362 QualType ArgType = Param->getType();
2363 if (ArgType.isNull())
2364 ArgType = Context.getObjCIdType();
2365 else
2366 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2367 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002368 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002369 Diag(Param->getLocation(),
2370 diag::err_object_cannot_be_passed_returned_by_value)
2371 << 1 << ArgType;
2372 Param->setInvalidDecl();
2373 }
2374 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002375
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002376 Params.push_back(Param);
2377 }
2378
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002379 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2380 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002381 ObjCMethod->setObjCDeclQualifier(
2382 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2383 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002384
2385 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002386 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002387
John McCall54abf7d2009-11-04 02:18:39 +00002388 const ObjCMethodDecl *InterfaceMD = 0;
2389
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002390 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002391 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002392 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002393 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002394 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2395 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002396 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002397 PrevMethod = ImpDecl->getClassMethod(Sel);
2398 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002399 }
John McCall54abf7d2009-11-04 02:18:39 +00002400 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2401 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002402
Sean Huntcf807c42010-08-18 23:23:40 +00002403 if (ObjCMethod->hasAttrs() &&
2404 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002405 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002406 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002407 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002408 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002409 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2410 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002411 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002412 PrevMethod = CatImpDecl->getClassMethod(Sel);
2413 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002414 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002415
2416 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2417 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2418
Sean Huntcf807c42010-08-18 23:23:40 +00002419 if (ObjCMethod->hasAttrs() &&
2420 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002421 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002422 } else {
2423 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002424 }
2425 if (PrevMethod) {
2426 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002427 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002428 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002429 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002430 }
John McCall54abf7d2009-11-04 02:18:39 +00002431
Douglas Gregor926df6c2011-06-11 01:09:30 +00002432 // If this Objective-C method does not have a related result type, but we
2433 // are allowed to infer related result types, try to do so based on the
2434 // method family.
2435 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2436 if (!CurrentClass) {
2437 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2438 CurrentClass = Cat->getClassInterface();
2439 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2440 CurrentClass = Impl->getClassInterface();
2441 else if (ObjCCategoryImplDecl *CatImpl
2442 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2443 CurrentClass = CatImpl->getClassInterface();
2444 }
2445
John McCalleca5d222011-03-02 04:00:57 +00002446 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002447 if (InterfaceMD) {
2448 // Inherit the related result type, if we can.
2449 if (InterfaceMD->hasRelatedResultType() &&
2450 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2451 ObjCMethod->SetRelatedResultType();
2452
John McCalleca5d222011-03-02 04:00:57 +00002453 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002454 }
2455
John McCallf85e1932011-06-15 23:02:42 +00002456 bool ARCError = false;
2457 if (getLangOptions().ObjCAutoRefCount)
2458 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2459
2460 if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
Douglas Gregor74da19f2011-06-14 23:20:43 +00002461 getLangOptions().ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002462 bool InferRelatedResultType = false;
2463 switch (ObjCMethod->getMethodFamily()) {
2464 case OMF_None:
2465 case OMF_copy:
2466 case OMF_dealloc:
2467 case OMF_mutableCopy:
2468 case OMF_release:
2469 case OMF_retainCount:
2470 break;
2471
2472 case OMF_alloc:
2473 case OMF_new:
2474 InferRelatedResultType = ObjCMethod->isClassMethod();
2475 break;
2476
2477 case OMF_init:
2478 case OMF_autorelease:
2479 case OMF_retain:
2480 case OMF_self:
2481 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2482 break;
2483 }
2484
2485 if (InferRelatedResultType &&
2486 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2487 ObjCMethod->SetRelatedResultType();
2488
2489 if (!InterfaceMD &&
2490 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2491 ObjCMethod->isInstanceMethod()))
2492 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2493 }
2494
John McCalld226f652010-08-21 09:40:31 +00002495 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002496}
2497
Chris Lattnercc98eac2008-12-17 07:13:27 +00002498bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002499 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002500 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002501
Anders Carlsson15281452008-11-04 16:57:32 +00002502 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2503 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002504
Anders Carlsson15281452008-11-04 16:57:32 +00002505 return true;
2506}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002507
Chris Lattnercc98eac2008-12-17 07:13:27 +00002508/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2509/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002510void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002511 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002512 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002513 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002514 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002515 if (!Class) {
2516 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2517 return;
2518 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002519 if (LangOpts.ObjCNonFragileABI) {
2520 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2521 return;
2522 }
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Chris Lattnercc98eac2008-12-17 07:13:27 +00002524 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002525 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2526 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002527 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002528 for (unsigned i = 0; i < Ivars.size(); i++) {
2529 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002530 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002531 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2532 /*FIXME: StartL=*/ID->getLocation(),
2533 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002534 ID->getIdentifier(), ID->getType(),
2535 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002536 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002537 }
Mike Stump1eb44332009-09-09 15:08:12 +00002538
Chris Lattnercc98eac2008-12-17 07:13:27 +00002539 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002540 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002541 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002542 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002543 if (getLangOptions().CPlusPlus)
2544 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002545 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002546 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002547 }
2548}
2549
Douglas Gregor160b5632010-04-26 17:32:49 +00002550/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002551VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2552 SourceLocation StartLoc,
2553 SourceLocation IdLoc,
2554 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002555 bool Invalid) {
2556 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2557 // duration shall not be qualified by an address-space qualifier."
2558 // Since all parameters have automatic store duration, they can not have
2559 // an address space.
2560 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002561 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002562 Invalid = true;
2563 }
2564
2565 // An @catch parameter must be an unqualified object pointer type;
2566 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2567 if (Invalid) {
2568 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002569 } else if (T->isDependentType()) {
2570 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002571 } else if (!T->isObjCObjectPointerType()) {
2572 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002573 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002574 } else if (T->isObjCQualifiedIdType()) {
2575 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002576 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002577 }
2578
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002579 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2580 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002581 New->setExceptionVariable(true);
2582
Douglas Gregor160b5632010-04-26 17:32:49 +00002583 if (Invalid)
2584 New->setInvalidDecl();
2585 return New;
2586}
2587
John McCalld226f652010-08-21 09:40:31 +00002588Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002589 const DeclSpec &DS = D.getDeclSpec();
2590
2591 // We allow the "register" storage class on exception variables because
2592 // GCC did, but we drop it completely. Any other storage class is an error.
2593 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2594 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2595 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2596 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2597 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2598 << DS.getStorageClassSpec();
2599 }
2600 if (D.getDeclSpec().isThreadSpecified())
2601 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2602 D.getMutableDeclSpec().ClearStorageClassSpecs();
2603
2604 DiagnoseFunctionSpecifiers(D);
2605
2606 // Check that there are no default arguments inside the type of this
2607 // exception object (C++ only).
2608 if (getLangOptions().CPlusPlus)
2609 CheckExtraCXXDefaultArguments(D);
2610
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002611 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002612 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002613
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002614 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2615 D.getSourceRange().getBegin(),
2616 D.getIdentifierLoc(),
2617 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002618 D.isInvalidType());
2619
2620 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2621 if (D.getCXXScopeSpec().isSet()) {
2622 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2623 << D.getCXXScopeSpec().getRange();
2624 New->setInvalidDecl();
2625 }
2626
2627 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002628 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002629 if (D.getIdentifier())
2630 IdResolver.AddDecl(New);
2631
2632 ProcessDeclAttributes(S, New, D);
2633
2634 if (New->hasAttr<BlocksAttr>())
2635 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002636 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002637}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002638
2639/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002640/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002641void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002642 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002643 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2644 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002645 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002646 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002647 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002648 }
2649}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002650
2651void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002652 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002653 unsigned numInitializers) {
2654 if (numInitializers > 0) {
2655 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002656 CXXCtorInitializer **ivarInitializers =
2657 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002658 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002659 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002660 IvarInitializers = ivarInitializers;
2661 }
2662}
2663
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002664void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002665 // Warning will be issued only when selector table is
2666 // generated (which means there is at lease one implementation
2667 // in the TU). This is to match gcc's behavior.
2668 if (ReferencedSelectors.empty() ||
2669 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002670 return;
2671 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2672 ReferencedSelectors.begin(),
2673 E = ReferencedSelectors.end(); S != E; ++S) {
2674 Selector Sel = (*S).first;
2675 if (!LookupImplementedMethodInGlobalPool(Sel))
2676 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2677 }
2678 return;
2679}