blob: 3bbe421cd144b2cfcc9a8cbfef55ad56faf58cc2 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
Douglas Gregor05a2e942011-06-13 16:07:18 +0000158/// \brief Check for consistency between a given method declaration and the
159/// methods it overrides within the class hierarchy.
160///
161/// This method walks the inheritance hierarchy starting at the given
162/// declaration context (\p DC), invoking Sema::CheckObjCMethodOverride() with
163/// the given new method (\p NewMethod) and any method it directly overrides
164/// in the hierarchy. Sema::CheckObjCMethodOverride() is responsible for
165/// checking consistency, e.g., among return types for methods that return a
166/// related result type.
Douglas Gregor926df6c2011-06-11 01:09:30 +0000167static bool CheckObjCMethodOverrides(Sema &S, ObjCMethodDecl *NewMethod,
168 DeclContext *DC,
169 bool SkipCurrent = true) {
170 if (!DC)
171 return false;
172
173 if (!SkipCurrent) {
174 // Look for this method. If we find it, we're done.
175 Selector Sel = NewMethod->getSelector();
176 bool IsInstance = NewMethod->isInstanceMethod();
177 DeclContext::lookup_const_iterator Meth, MethEnd;
178 for (llvm::tie(Meth, MethEnd) = DC->lookup(Sel); Meth != MethEnd; ++Meth) {
179 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
180 if (MD && MD->isInstanceMethod() == IsInstance)
181 return S.CheckObjCMethodOverride(NewMethod, MD, false);
182 }
183 }
184
185 if (ObjCInterfaceDecl *Class = llvm::dyn_cast<ObjCInterfaceDecl>(DC)) {
186 // Look through categories.
187 for (ObjCCategoryDecl *Category = Class->getCategoryList();
188 Category; Category = Category->getNextClassCategory()) {
189 if (CheckObjCMethodOverrides(S, NewMethod, Category, false))
190 return true;
191 }
John McCallf85e1932011-06-15 23:02:42 +0000192
Douglas Gregor926df6c2011-06-11 01:09:30 +0000193 // Look through protocols.
194 for (ObjCList<ObjCProtocolDecl>::iterator I = Class->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000195 IEnd = Class->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000196 I != IEnd; ++I)
197 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
198 return true;
199
200 // Look in our superclass.
201 return CheckObjCMethodOverrides(S, NewMethod, Class->getSuperClass(),
202 false);
203 }
204
205 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC)) {
206 // Look through protocols.
207 for (ObjCList<ObjCProtocolDecl>::iterator I = Category->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000208 IEnd = Category->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000209 I != IEnd; ++I)
210 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
211 return true;
212
213 return false;
214 }
215
216 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC)) {
217 // Look through protocols.
218 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocol->protocol_begin(),
John McCallf85e1932011-06-15 23:02:42 +0000219 IEnd = Protocol->protocol_end();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000220 I != IEnd; ++I)
221 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
222 return true;
223
224 return false;
225 }
226
227 return false;
228}
229
230bool Sema::CheckObjCMethodOverrides(ObjCMethodDecl *NewMethod,
231 DeclContext *DC) {
232 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(DC))
233 return ::CheckObjCMethodOverrides(*this, NewMethod, Class);
John McCallf85e1932011-06-15 23:02:42 +0000234
Douglas Gregor926df6c2011-06-11 01:09:30 +0000235 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC))
236 return ::CheckObjCMethodOverrides(*this, NewMethod, Category);
John McCallf85e1932011-06-15 23:02:42 +0000237
Douglas Gregor926df6c2011-06-11 01:09:30 +0000238 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC))
239 return ::CheckObjCMethodOverrides(*this, NewMethod, Protocol);
John McCallf85e1932011-06-15 23:02:42 +0000240
Douglas Gregor926df6c2011-06-11 01:09:30 +0000241 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(DC))
242 return ::CheckObjCMethodOverrides(*this, NewMethod,
243 Impl->getClassInterface());
244
245 if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(DC))
246 return ::CheckObjCMethodOverrides(*this, NewMethod,
247 CatImpl->getClassInterface());
248
249 return ::CheckObjCMethodOverrides(*this, NewMethod, CurContext);
250}
251
John McCallf85e1932011-06-15 23:02:42 +0000252/// \brief Check a method declaration for compatibility with the Objective-C
253/// ARC conventions.
254static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
255 ObjCMethodFamily family = method->getMethodFamily();
256 switch (family) {
257 case OMF_None:
258 case OMF_dealloc:
259 case OMF_retain:
260 case OMF_release:
261 case OMF_autorelease:
262 case OMF_retainCount:
263 case OMF_self:
264 return false;
265
266 case OMF_init:
267 // If the method doesn't obey the init rules, don't bother annotating it.
268 if (S.checkInitMethod(method, QualType()))
269 return true;
270
271 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
272 S.Context));
273
274 // Don't add a second copy of this attribute, but otherwise don't
275 // let it be suppressed.
276 if (method->hasAttr<NSReturnsRetainedAttr>())
277 return false;
278 break;
279
280 case OMF_alloc:
281 case OMF_copy:
282 case OMF_mutableCopy:
283 case OMF_new:
284 if (method->hasAttr<NSReturnsRetainedAttr>() ||
285 method->hasAttr<NSReturnsNotRetainedAttr>() ||
286 method->hasAttr<NSReturnsAutoreleasedAttr>())
287 return false;
288 break;
289 }
290
291 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
292 S.Context));
293 return false;
294}
295
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000296static void DiagnoseObjCImplementedDeprecations(Sema &S,
297 NamedDecl *ND,
298 SourceLocation ImplLoc,
299 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000300 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000301 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000302 if (select == 0)
303 S.Diag(ND->getLocation(), diag::note_method_declared_at);
304 else
305 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
306 }
307}
308
Steve Naroffebf64432009-02-28 16:59:13 +0000309/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000310/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000311void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000312 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000313 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Steve Naroff394f3f42008-07-25 17:57:26 +0000315 // If we don't have a valid method decl, simply return.
316 if (!MDecl)
317 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000318
319 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000320 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000321 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000322 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000323 AddFactoryMethodToGlobalPool(MDecl, true);
324
Chris Lattner4d391482007-12-12 07:09:47 +0000325 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000326 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000327 PushFunctionScope();
328
Chris Lattner4d391482007-12-12 07:09:47 +0000329 // Create Decl objects for each parameter, entrring them in the scope for
330 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000331
332 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000333 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar451318c2008-08-26 06:07:48 +0000335 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
336 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000337
Chris Lattner8123a952008-04-10 02:22:51 +0000338 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000339 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000340 E = MDecl->param_end(); PI != E; ++PI) {
341 ParmVarDecl *Param = (*PI);
342 if (!Param->isInvalidDecl() &&
343 RequireCompleteType(Param->getLocation(), Param->getType(),
344 diag::err_typecheck_decl_incomplete_type))
345 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000346 if ((*PI)->getIdentifier())
347 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000348 }
John McCallf85e1932011-06-15 23:02:42 +0000349
350 // In ARC, disallow definition of retain/release/autorelease/retainCount
351 if (getLangOptions().ObjCAutoRefCount) {
352 switch (MDecl->getMethodFamily()) {
353 case OMF_retain:
354 case OMF_retainCount:
355 case OMF_release:
356 case OMF_autorelease:
357 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
358 << MDecl->getSelector();
359 break;
360
361 case OMF_None:
362 case OMF_dealloc:
363 case OMF_alloc:
364 case OMF_init:
365 case OMF_mutableCopy:
366 case OMF_copy:
367 case OMF_new:
368 case OMF_self:
369 break;
370 }
371 }
372
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000373 // Warn on implementating deprecated methods under
374 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000375 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
376 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000377 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000378 DiagnoseObjCImplementedDeprecations(*this,
379 dyn_cast<NamedDecl>(IMD),
380 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +0000381}
382
John McCalld226f652010-08-21 09:40:31 +0000383Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
385 IdentifierInfo *ClassName, SourceLocation ClassLoc,
386 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000387 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000388 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000389 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000390 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner4d391482007-12-12 07:09:47 +0000392 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000393 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000394 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000395
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000397 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000398 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000401 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
402 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000403 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000404 if (!IDecl->isForwardDecl()) {
405 IDecl->setInvalidDecl();
406 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
407 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000408
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000409 // Return the previous class interface.
410 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000411 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000412 } else {
413 IDecl->setLocation(AtInterfaceLoc);
414 IDecl->setForwardDecl(false);
415 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000416 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000417 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000418 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000419
420 // Since this ObjCInterfaceDecl was created by a forward declaration,
421 // we now add it to the DeclContext since it wasn't added before
422 // (see ActOnForwardClassDeclaration).
423 IDecl->setLexicalDeclContext(CurContext);
424 CurContext->addDecl(IDecl);
425
426 if (AttrList)
427 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000428 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000429 } else {
430 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
431 ClassName, ClassLoc);
432 if (AttrList)
433 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
434
435 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner4d391482007-12-12 07:09:47 +0000438 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000439 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000440 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
441 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000442
443 if (!PrevDecl) {
444 // Try to correct for a typo in the superclass name.
445 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000446 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000447 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
448 Diag(SuperLoc, diag::err_undef_superclass_suggest)
449 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000450 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
451 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000452 }
453 }
454
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000455 if (PrevDecl == IDecl) {
456 Diag(SuperLoc, diag::err_recursive_superclass)
457 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
458 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000459 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000460 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000461 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000462
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 // Diagnose classes that inherit from deprecated classes.
464 if (SuperClassDecl)
465 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000467 if (PrevDecl && SuperClassDecl == 0) {
468 // The previous declaration was not a class decl. Check if we have a
469 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000470 if (const TypedefNameDecl *TDecl =
471 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000472 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000473 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000474 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
475 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000476 }
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000479 // This handles the following case:
480 //
481 // typedef int SuperClass;
482 // @interface MyClass : SuperClass {} @end
483 //
484 if (!SuperClassDecl) {
485 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
486 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000487 }
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Richard Smith162e1c12011-04-15 14:24:37 +0000490 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000491 if (!SuperClassDecl)
492 Diag(SuperLoc, diag::err_undef_superclass)
493 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000494 else if (SuperClassDecl->isForwardDecl()) {
495 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000496 << SuperClassDecl->getDeclName() << ClassName
497 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000498 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
499 SuperClassDecl = 0;
500 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000501 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000502 IDecl->setSuperClass(SuperClassDecl);
503 IDecl->setSuperClassLoc(SuperLoc);
504 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000505 }
Chris Lattner4d391482007-12-12 07:09:47 +0000506 } else { // we have a root class.
507 IDecl->setLocEnd(ClassLoc);
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Sebastian Redl0b17c612010-08-13 00:28:03 +0000510 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000511 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000512 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000513 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000514 IDecl->setLocEnd(EndProtoLoc);
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Anders Carlsson15281452008-11-04 16:57:32 +0000517 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000518 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000519}
520
521/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000522/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000523Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
524 IdentifierInfo *AliasName,
525 SourceLocation AliasLocation,
526 IdentifierInfo *ClassName,
527 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000528 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000529 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000530 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000531 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000532 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000533 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000534 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000535 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000536 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000537 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000538 }
539 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000540 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000541 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000542 if (const TypedefNameDecl *TDecl =
543 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000544 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000545 if (T->isObjCObjectType()) {
546 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000547 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000548 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000549 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000550 }
551 }
552 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000553 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
554 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000556 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000557 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000558 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000561 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000562 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000563 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Anders Carlsson15281452008-11-04 16:57:32 +0000565 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000566 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000567
John McCalld226f652010-08-21 09:40:31 +0000568 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000569}
570
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000571bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000572 IdentifierInfo *PName,
573 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000574 const ObjCList<ObjCProtocolDecl> &PList) {
575
576 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000577 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
578 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000579 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
580 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000581 if (PDecl->getIdentifier() == PName) {
582 Diag(Ploc, diag::err_protocol_has_circular_dependency);
583 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000584 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000585 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000586 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
587 PDecl->getLocation(), PDecl->getReferencedProtocols()))
588 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000589 }
590 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000591 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000592}
593
John McCalld226f652010-08-21 09:40:31 +0000594Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000595Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
596 IdentifierInfo *ProtocolName,
597 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000598 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000599 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000600 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000601 SourceLocation EndProtoLoc,
602 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000603 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000604 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000605 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000606 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 if (PDecl) {
608 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000609 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000610 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000611 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000612 // Just return the protocol we already had.
613 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000614 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000615 }
Steve Naroff61d68522009-03-05 15:22:01 +0000616 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000617 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000618 err = CheckForwardProtocolDeclarationForCircularDependency(
619 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Steve Narofff11b5082008-08-13 16:39:22 +0000621 // Make sure the cached decl gets a valid start location.
622 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000623 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000624 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000625 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000626 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000627 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000628 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000629 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000630 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000631 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000632 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000633 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000634 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000635 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000636 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000637 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
638 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000639 PDecl->setLocEnd(EndProtoLoc);
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000643 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
646/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000647/// issues an error if they are not declared. It returns list of
648/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000649void
Chris Lattnere13b9592008-07-26 04:03:38 +0000650Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000651 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000652 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000653 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000654 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000655 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
656 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000657 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000658 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
659 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000660 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000661 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
662 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
663 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000664 Diag(PDecl->getLocation(), diag::note_previous_decl)
665 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000666 }
667 }
668
669 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000670 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000672 continue;
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000675 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000676
677 // If this is a forward declaration and we are supposed to warn in this
678 // case, do it.
679 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000680 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000681 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000682 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000683 }
684}
685
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000686/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000687/// a class method in its extension.
688///
Mike Stump1eb44332009-09-09 15:08:12 +0000689void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000690 ObjCInterfaceDecl *ID) {
691 if (!ID)
692 return; // Possibly due to previous error
693
694 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
696 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000697 ObjCMethodDecl *MD = *i;
698 MethodMap[MD->getSelector()] = MD;
699 }
700
701 if (MethodMap.empty())
702 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000703 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
704 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000705 ObjCMethodDecl *Method = *i;
706 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
707 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
708 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
709 << Method->getDeclName();
710 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
711 }
712 }
713}
714
Chris Lattner58fe03b2009-04-12 08:43:13 +0000715/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000716Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000717Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000718 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000719 unsigned NumElts,
720 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000722 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Chris Lattner4d391482007-12-12 07:09:47 +0000724 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000725 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000726 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000727 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000728 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000729 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000730 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000731 PushOnScopeChains(PDecl, TUScope, false);
732 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000733 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000734 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000735 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000736 if (!isNew)
737 PDecl->setChangedSinceDeserialization(true);
738 }
Chris Lattner4d391482007-12-12 07:09:47 +0000739 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000740 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
743 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000744 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000745 Protocols.data(), Protocols.size(),
746 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000748 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000749 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000750}
751
John McCalld226f652010-08-21 09:40:31 +0000752Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000753ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
754 IdentifierInfo *ClassName, SourceLocation ClassLoc,
755 IdentifierInfo *CategoryName,
756 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000757 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000758 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000759 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000760 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000761 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000762 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000763
764 /// Check that class of this category is already completely declared.
765 if (!IDecl || IDecl->isForwardDecl()) {
766 // Create an invalid ObjCCategoryDecl to serve as context for
767 // the enclosing method declarations. We mark the decl invalid
768 // to make it clear that this isn't a valid AST.
769 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
770 ClassLoc, CategoryLoc, CategoryName);
771 CDecl->setInvalidDecl();
772 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000773 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000774 }
775
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000776 if (!CategoryName && IDecl->getImplementation()) {
777 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
778 Diag(IDecl->getImplementation()->getLocation(),
779 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000780 }
781
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000782 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
783 ClassLoc, CategoryLoc, CategoryName);
784 // FIXME: PushOnScopeChains?
785 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000786
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000787 CDecl->setClassInterface(IDecl);
788 // Insert class extension to the list of class's categories.
789 if (!CategoryName)
790 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner16b34b42009-02-16 21:30:01 +0000792 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000793 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000794
Fariborz Jahanian25760612010-02-15 21:55:26 +0000795 if (CategoryName) {
796 /// Check for duplicate interface declaration for this category
797 ObjCCategoryDecl *CDeclChain;
798 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
799 CDeclChain = CDeclChain->getNextClassCategory()) {
800 if (CDeclChain->getIdentifier() == CategoryName) {
801 // Class extensions can be declared multiple times.
802 Diag(CategoryLoc, diag::warn_dup_category_def)
803 << ClassName << CategoryName;
804 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
805 break;
806 }
Chris Lattner70f19542009-02-16 21:26:43 +0000807 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000808 if (!CDeclChain)
809 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000810 }
Chris Lattner70f19542009-02-16 21:26:43 +0000811
Chris Lattner4d391482007-12-12 07:09:47 +0000812 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000813 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000814 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000815 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000816 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000817 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000818 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Anders Carlsson15281452008-11-04 16:57:32 +0000821 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000822 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000823}
824
825/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000826/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000827/// object.
John McCalld226f652010-08-21 09:40:31 +0000828Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000829 SourceLocation AtCatImplLoc,
830 IdentifierInfo *ClassName, SourceLocation ClassLoc,
831 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000832 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000833 ObjCCategoryDecl *CatIDecl = 0;
834 if (IDecl) {
835 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
836 if (!CatIDecl) {
837 // Category @implementation with no corresponding @interface.
838 // Create and install one.
839 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000840 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000841 CatName);
842 CatIDecl->setClassInterface(IDecl);
843 CatIDecl->insertNextClassCategory();
844 }
845 }
846
Mike Stump1eb44332009-09-09 15:08:12 +0000847 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000848 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
849 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000850 /// Check that class of this category is already completely declared.
851 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000852 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000853
Douglas Gregord0434102009-01-09 00:49:46 +0000854 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000855 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000856
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000857 /// Check that CatName, category name, is not used in another implementation.
858 if (CatIDecl) {
859 if (CatIDecl->getImplementation()) {
860 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
861 << CatName;
862 Diag(CatIDecl->getImplementation()->getLocation(),
863 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000864 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000865 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000866 // Warn on implementating category of deprecated class under
867 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000868 DiagnoseObjCImplementedDeprecations(*this,
869 dyn_cast<NamedDecl>(IDecl),
870 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000871 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Anders Carlsson15281452008-11-04 16:57:32 +0000874 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000875 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000876}
877
John McCalld226f652010-08-21 09:40:31 +0000878Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000879 SourceLocation AtClassImplLoc,
880 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000881 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000882 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000883 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000884 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000885 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000886 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
887 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000889 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000890 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000891 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
892 // If this is a forward declaration of an interface, warn.
893 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000894 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000895 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000896 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000897 } else {
898 // We did not find anything with the name ClassName; try to correct for
899 // typos in the class name.
900 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000901 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000902 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000903 // Suggest the (potentially) correct interface name. However, put the
904 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000905 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000906 // provide a code-modification hint or use the typo name for recovery,
907 // because this is just a warning. The program may actually be correct.
908 Diag(ClassLoc, diag::warn_undef_interface_suggest)
909 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000910 Diag(IDecl->getLocation(), diag::note_previous_decl)
911 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000912 << FixItHint::CreateReplacement(ClassLoc,
913 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000914 IDecl = 0;
915 } else {
916 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
917 }
Chris Lattner4d391482007-12-12 07:09:47 +0000918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner4d391482007-12-12 07:09:47 +0000920 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000922 if (SuperClassname) {
923 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000924 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
925 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
928 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000929 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000931 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000932 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 Diag(SuperClassLoc, diag::err_undef_superclass)
934 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000935 else if (IDecl && IDecl->getSuperClass() != SDecl) {
936 // This implementation and its interface do not have the same
937 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000938 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000939 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000940 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000941 }
942 }
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattner4d391482007-12-12 07:09:47 +0000945 if (!IDecl) {
946 // Legacy case of @implementation with no corresponding @interface.
947 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000948
Mike Stump390b4cc2009-05-16 07:39:55 +0000949 // FIXME: Do we support attributes on the @implementation? If so we should
950 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000951 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000952 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000953 IDecl->setSuperClass(SDecl);
954 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000955
956 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000957 } else {
958 // Mark the interface as being completed, even if it was just as
959 // @class ....;
960 // declaration; the user cannot reopen it.
961 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
964 ObjCImplementationDecl* IMPDecl =
965 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000966 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Anders Carlsson15281452008-11-04 16:57:32 +0000968 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000969 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Chris Lattner4d391482007-12-12 07:09:47 +0000971 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000972 if (IDecl->getImplementation()) {
973 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000974 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000975 Diag(IDecl->getImplementation()->getLocation(),
976 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000977 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000978 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000979 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000980 // Warn on implementating deprecated class under
981 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000982 DiagnoseObjCImplementedDeprecations(*this,
983 dyn_cast<NamedDecl>(IDecl),
984 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000985 }
John McCalld226f652010-08-21 09:40:31 +0000986 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000987}
988
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
990 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000991 SourceLocation RBrace) {
992 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000993 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000994 if (!IDecl)
995 return;
996 /// Check case of non-existing @interface decl.
997 /// (legacy objective-c @implementation decl without an @interface decl).
998 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000999 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +00001000 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001001 // Add ivar's to class's DeclContext.
1002 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001003 ivars[i]->setLexicalDeclContext(ImpDecl);
1004 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001005 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001006 }
1007
Chris Lattner4d391482007-12-12 07:09:47 +00001008 return;
1009 }
1010 // If implementation has empty ivar list, just return.
1011 if (numIvars == 0)
1012 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Chris Lattner4d391482007-12-12 07:09:47 +00001014 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001015 if (LangOpts.ObjCNonFragileABI2) {
1016 if (ImpDecl->getSuperClass())
1017 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1018 for (unsigned i = 0; i < numIvars; i++) {
1019 ObjCIvarDecl* ImplIvar = ivars[i];
1020 if (const ObjCIvarDecl *ClsIvar =
1021 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1022 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1023 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1024 continue;
1025 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001026 // Instance ivar to Implementation's DeclContext.
1027 ImplIvar->setLexicalDeclContext(ImpDecl);
1028 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1029 ImpDecl->addDecl(ImplIvar);
1030 }
1031 return;
1032 }
Chris Lattner4d391482007-12-12 07:09:47 +00001033 // Check interface's Ivar list against those in the implementation.
1034 // names and types must match.
1035 //
Chris Lattner4d391482007-12-12 07:09:47 +00001036 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001037 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001038 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1039 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040 ObjCIvarDecl* ImplIvar = ivars[j++];
1041 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001042 assert (ImplIvar && "missing implementation ivar");
1043 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Steve Naroffca331292009-03-03 14:49:36 +00001045 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001046 if (Context.getCanonicalType(ImplIvar->getType()) !=
1047 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001048 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001049 << ImplIvar->getIdentifier()
1050 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001051 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001052 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1053 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1054 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001055 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1056 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001057 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1058 << ImplIvar->getIdentifier();
1059 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061 }
Steve Naroffca331292009-03-03 14:49:36 +00001062 // Make sure the names are identical.
1063 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001064 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001065 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001066 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001067 }
1068 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner609e4c72007-12-12 18:11:49 +00001071 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001072 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001073 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001074 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001075}
1076
Steve Naroff3c2eb662008-02-10 21:38:56 +00001077void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001078 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +00001079 if (!IncompleteImpl) {
1080 Diag(ImpLoc, diag::warn_incomplete_impl);
1081 IncompleteImpl = true;
1082 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001083 if (DiagID == diag::warn_unimplemented_protocol_method)
1084 Diag(ImpLoc, DiagID) << method->getDeclName();
1085 else
1086 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001087}
1088
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001089/// Determines if type B can be substituted for type A. Returns true if we can
1090/// guarantee that anything that the user will do to an object of type A can
1091/// also be done to an object of type B. This is trivially true if the two
1092/// types are the same, or if B is a subclass of A. It becomes more complex
1093/// in cases where protocols are involved.
1094///
1095/// Object types in Objective-C describe the minimum requirements for an
1096/// object, rather than providing a complete description of a type. For
1097/// example, if A is a subclass of B, then B* may refer to an instance of A.
1098/// The principle of substitutability means that we may use an instance of A
1099/// anywhere that we may use an instance of B - it will implement all of the
1100/// ivars of B and all of the methods of B.
1101///
1102/// This substitutability is important when type checking methods, because
1103/// the implementation may have stricter type definitions than the interface.
1104/// The interface specifies minimum requirements, but the implementation may
1105/// have more accurate ones. For example, a method may privately accept
1106/// instances of B, but only publish that it accepts instances of A. Any
1107/// object passed to it will be type checked against B, and so will implicitly
1108/// by a valid A*. Similarly, a method may return a subclass of the class that
1109/// it is declared as returning.
1110///
1111/// This is most important when considering subclassing. A method in a
1112/// subclass must accept any object as an argument that its superclass's
1113/// implementation accepts. It may, however, accept a more general type
1114/// without breaking substitutability (i.e. you can still use the subclass
1115/// anywhere that you can use the superclass, but not vice versa). The
1116/// converse requirement applies to return types: the return type for a
1117/// subclass method must be a valid object of the kind that the superclass
1118/// advertises, but it may be specified more accurately. This avoids the need
1119/// for explicit down-casting by callers.
1120///
1121/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001122static bool isObjCTypeSubstitutable(ASTContext &Context,
1123 const ObjCObjectPointerType *A,
1124 const ObjCObjectPointerType *B,
1125 bool rejectId) {
1126 // Reject a protocol-unqualified id.
1127 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001128
1129 // If B is a qualified id, then A must also be a qualified id and it must
1130 // implement all of the protocols in B. It may not be a qualified class.
1131 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1132 // stricter definition so it is not substitutable for id<A>.
1133 if (B->isObjCQualifiedIdType()) {
1134 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001135 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1136 QualType(B,0),
1137 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001138 }
1139
1140 /*
1141 // id is a special type that bypasses type checking completely. We want a
1142 // warning when it is used in one place but not another.
1143 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1144
1145
1146 // If B is a qualified id, then A must also be a qualified id (which it isn't
1147 // if we've got this far)
1148 if (B->isObjCQualifiedIdType()) return false;
1149 */
1150
1151 // Now we know that A and B are (potentially-qualified) class types. The
1152 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001153 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001154}
1155
John McCall10302c02010-10-28 02:34:38 +00001156static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1157 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1158}
1159
1160static void CheckMethodOverrideReturn(Sema &S,
1161 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001162 ObjCMethodDecl *MethodDecl,
1163 bool IsProtocolMethodDecl) {
1164 if (IsProtocolMethodDecl &&
1165 (MethodDecl->getObjCDeclQualifier() !=
1166 MethodImpl->getObjCDeclQualifier())) {
1167 S.Diag(MethodImpl->getLocation(),
1168 diag::warn_conflicting_ret_type_modifiers)
1169 << MethodImpl->getDeclName()
1170 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1171 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1172 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1173 }
1174
John McCall10302c02010-10-28 02:34:38 +00001175 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001176 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001177 return;
1178
1179 unsigned DiagID = diag::warn_conflicting_ret_types;
1180
1181 // Mismatches between ObjC pointers go into a different warning
1182 // category, and sometimes they're even completely whitelisted.
1183 if (const ObjCObjectPointerType *ImplPtrTy =
1184 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1185 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001186 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001187 // Allow non-matching return types as long as they don't violate
1188 // the principle of substitutability. Specifically, we permit
1189 // return types that are subclasses of the declared return type,
1190 // or that are more-qualified versions of the declared type.
1191 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1192 return;
1193
1194 DiagID = diag::warn_non_covariant_ret_types;
1195 }
1196 }
1197
1198 S.Diag(MethodImpl->getLocation(), DiagID)
1199 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001200 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001201 << MethodImpl->getResultType()
1202 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001203 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1204 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001205}
1206
1207static void CheckMethodOverrideParam(Sema &S,
1208 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001209 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001210 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001211 ParmVarDecl *IfaceVar,
1212 bool IsProtocolMethodDecl) {
1213 if (IsProtocolMethodDecl &&
1214 (ImplVar->getObjCDeclQualifier() !=
1215 IfaceVar->getObjCDeclQualifier())) {
1216 S.Diag(ImplVar->getLocation(),
1217 diag::warn_conflicting_param_modifiers)
1218 << getTypeRange(ImplVar->getTypeSourceInfo())
1219 << MethodImpl->getDeclName();
1220 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1221 << getTypeRange(IfaceVar->getTypeSourceInfo());
1222 }
1223
John McCall10302c02010-10-28 02:34:38 +00001224 QualType ImplTy = ImplVar->getType();
1225 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001226
John McCall10302c02010-10-28 02:34:38 +00001227 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1228 return;
1229
1230 unsigned DiagID = diag::warn_conflicting_param_types;
1231
1232 // Mismatches between ObjC pointers go into a different warning
1233 // category, and sometimes they're even completely whitelisted.
1234 if (const ObjCObjectPointerType *ImplPtrTy =
1235 ImplTy->getAs<ObjCObjectPointerType>()) {
1236 if (const ObjCObjectPointerType *IfacePtrTy =
1237 IfaceTy->getAs<ObjCObjectPointerType>()) {
1238 // Allow non-matching argument types as long as they don't
1239 // violate the principle of substitutability. Specifically, the
1240 // implementation must accept any objects that the superclass
1241 // accepts, however it may also accept others.
1242 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1243 return;
1244
1245 DiagID = diag::warn_non_contravariant_param_types;
1246 }
1247 }
1248
1249 S.Diag(ImplVar->getLocation(), DiagID)
1250 << getTypeRange(ImplVar->getTypeSourceInfo())
1251 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1252 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1253 << getTypeRange(IfaceVar->getTypeSourceInfo());
1254}
John McCallf85e1932011-06-15 23:02:42 +00001255
1256/// In ARC, check whether the conventional meanings of the two methods
1257/// match. If they don't, it's a hard error.
1258static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1259 ObjCMethodDecl *decl) {
1260 ObjCMethodFamily implFamily = impl->getMethodFamily();
1261 ObjCMethodFamily declFamily = decl->getMethodFamily();
1262 if (implFamily == declFamily) return false;
1263
1264 // Since conventions are sorted by selector, the only possibility is
1265 // that the types differ enough to cause one selector or the other
1266 // to fall out of the family.
1267 assert(implFamily == OMF_None || declFamily == OMF_None);
1268
1269 // No further diagnostics required on invalid declarations.
1270 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1271
1272 const ObjCMethodDecl *unmatched = impl;
1273 ObjCMethodFamily family = declFamily;
1274 unsigned errorID = diag::err_arc_lost_method_convention;
1275 unsigned noteID = diag::note_arc_lost_method_convention;
1276 if (declFamily == OMF_None) {
1277 unmatched = decl;
1278 family = implFamily;
1279 errorID = diag::err_arc_gained_method_convention;
1280 noteID = diag::note_arc_gained_method_convention;
1281 }
1282
1283 // Indexes into a %select clause in the diagnostic.
1284 enum FamilySelector {
1285 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1286 };
1287 FamilySelector familySelector = FamilySelector();
1288
1289 switch (family) {
1290 case OMF_None: llvm_unreachable("logic error, no method convention");
1291 case OMF_retain:
1292 case OMF_release:
1293 case OMF_autorelease:
1294 case OMF_dealloc:
1295 case OMF_retainCount:
1296 case OMF_self:
1297 // Mismatches for these methods don't change ownership
1298 // conventions, so we don't care.
1299 return false;
1300
1301 case OMF_init: familySelector = F_init; break;
1302 case OMF_alloc: familySelector = F_alloc; break;
1303 case OMF_copy: familySelector = F_copy; break;
1304 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1305 case OMF_new: familySelector = F_new; break;
1306 }
1307
1308 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1309 ReasonSelector reasonSelector;
1310
1311 // The only reason these methods don't fall within their families is
1312 // due to unusual result types.
1313 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1314 reasonSelector = R_UnrelatedReturn;
1315 } else {
1316 reasonSelector = R_NonObjectReturn;
1317 }
1318
1319 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1320 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1321
1322 return true;
1323}
John McCall10302c02010-10-28 02:34:38 +00001324
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001325void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001326 ObjCMethodDecl *MethodDecl,
1327 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001328 if (getLangOptions().ObjCAutoRefCount &&
1329 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1330 return;
1331
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001332 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1333 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Chris Lattner3aff9192009-04-11 19:58:42 +00001335 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001336 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001337 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001338 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1339 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001341 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001342 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001343 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001344 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001345}
1346
Mike Stump390b4cc2009-05-16 07:39:55 +00001347/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1348/// improve the efficiency of selector lookups and type checking by associating
1349/// with each protocol / interface / category the flattened instance tables. If
1350/// we used an immutable set to keep the table then it wouldn't add significant
1351/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001352
Steve Naroffefe7f362008-02-08 22:06:17 +00001353/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001354/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001355void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1356 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001357 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001358 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001359 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001360 ObjCContainerDecl *CDecl) {
1361 ObjCInterfaceDecl *IDecl;
1362 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1363 IDecl = C->getClassInterface();
1364 else
1365 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1366 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1367
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001368 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001369 ObjCInterfaceDecl *NSIDecl = 0;
1370 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371 // check to see if class implements forwardInvocation method and objects
1372 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001373 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001374 // Under such conditions, which means that every method possible is
1375 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001376 // found" warnings.
1377 // FIXME: Use a general GetUnarySelector method for this.
1378 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1379 Selector fISelector = Context.Selectors.getSelector(1, &II);
1380 if (InsMap.count(fISelector))
1381 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1382 // need be implemented in the implementation.
1383 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1384 }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001386 // If a method lookup fails locally we still need to look and see if
1387 // the method was implemented by a base class or an inherited
1388 // protocol. This lookup is slow, but occurs rarely in correct code
1389 // and otherwise would terminate in a warning.
1390
Chris Lattner4d391482007-12-12 07:09:47 +00001391 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001392 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001393 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001394 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001395 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001396 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001397 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001398 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001399 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001400 // Ugly, but necessary. Method declared in protcol might have
1401 // have been synthesized due to a property declared in the class which
1402 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001403 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001404 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001405 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001406 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001407 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1408 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001409 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001410 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001411 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1412 << PDecl->getDeclName();
1413 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001414 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001415 }
1416 }
Chris Lattner4d391482007-12-12 07:09:47 +00001417 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001418 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001419 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001420 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001421 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001422 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1423 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001424 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001425 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001426 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001427 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001428 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001429 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1430 PDecl->getDeclName();
1431 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001432 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001433 }
Chris Lattner780f3292008-07-21 21:32:27 +00001434 // Check on this protocols's referenced protocols, recursively.
1435 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1436 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001437 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001438}
1439
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001440/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1441/// or protocol against those declared in their implementations.
1442///
1443void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1444 const llvm::DenseSet<Selector> &ClsMap,
1445 llvm::DenseSet<Selector> &InsMapSeen,
1446 llvm::DenseSet<Selector> &ClsMapSeen,
1447 ObjCImplDecl* IMPDecl,
1448 ObjCContainerDecl* CDecl,
1449 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001450 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001451 // Check and see if instance methods in class interface have been
1452 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001453 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1454 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001455 if (InsMapSeen.count((*I)->getSelector()))
1456 continue;
1457 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001458 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001459 !InsMap.count((*I)->getSelector())) {
1460 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001461 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1462 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001463 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001464 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001465 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001466 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001467 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001468 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001469 assert(MethodDecl &&
1470 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001471 // ImpMethodDecl may be null as in a @dynamic property.
1472 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001473 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1474 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001475 }
1476 }
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001478 // Check and see if class methods in class interface have been
1479 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001480 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001481 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001482 if (ClsMapSeen.count((*I)->getSelector()))
1483 continue;
1484 ClsMapSeen.insert((*I)->getSelector());
1485 if (!ClsMap.count((*I)->getSelector())) {
1486 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001487 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1488 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001489 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 ObjCMethodDecl *ImpMethodDecl =
1491 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001492 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001493 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001494 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1495 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001496 }
1497 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001498
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001499 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001500 // Also methods in class extensions need be looked at next.
1501 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1502 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1503 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1504 IMPDecl,
1505 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1506 IncompleteImpl, false);
1507
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001508 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001509 for (ObjCInterfaceDecl::all_protocol_iterator
1510 PI = I->all_referenced_protocol_begin(),
1511 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001512 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1513 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001514 (*PI), IncompleteImpl, false);
1515 if (I->getSuperClass())
1516 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001517 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001518 I->getSuperClass(), IncompleteImpl, false);
1519 }
1520}
1521
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001522void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001523 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001524 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001525 llvm::DenseSet<Selector> InsMap;
1526 // Check and see if instance methods in class interface have been
1527 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001528 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001529 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001530 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001532 // Check and see if properties declared in the interface have either 1)
1533 // an implementation or 2) there is a @synthesize/@dynamic implementation
1534 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001535 if (isa<ObjCInterfaceDecl>(CDecl) &&
1536 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001537 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001538
Chris Lattner4d391482007-12-12 07:09:47 +00001539 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001540 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001541 I = IMPDecl->classmeth_begin(),
1542 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001543 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001545 // Check for type conflict of methods declared in a class/protocol and
1546 // its implementation; if any.
1547 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1549 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001550 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattner4d391482007-12-12 07:09:47 +00001552 // Check the protocol list for unimplemented methods in the @implementation
1553 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 // Check and see if class methods in class interface have been
1555 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattnercddc8882009-03-01 00:56:52 +00001557 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001558 for (ObjCInterfaceDecl::all_protocol_iterator
1559 PI = I->all_referenced_protocol_begin(),
1560 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001561 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001562 InsMap, ClsMap, I);
1563 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001564 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1565 Categories; Categories = Categories->getNextClassExtension())
1566 ImplMethodsVsClassMethods(S, IMPDecl,
1567 const_cast<ObjCCategoryDecl*>(Categories),
1568 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001569 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001570 // For extended class, unimplemented methods in its protocols will
1571 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001572 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001573 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1574 E = C->protocol_end(); PI != E; ++PI)
1575 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001576 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001577 // Report unimplemented properties in the category as well.
1578 // When reporting on missing setter/getters, do not report when
1579 // setter/getter is implemented in category's primary class
1580 // implementation.
1581 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1582 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1583 for (ObjCImplementationDecl::instmeth_iterator
1584 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1585 InsMap.insert((*I)->getSelector());
1586 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001587 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001588 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001589 } else
1590 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001591}
1592
Mike Stump1eb44332009-09-09 15:08:12 +00001593/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001594Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001595Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001596 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001597 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001598 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001599 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Chris Lattner4d391482007-12-12 07:09:47 +00001601 for (unsigned i = 0; i != NumElts; ++i) {
1602 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001603 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001604 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001605 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001606 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001607 // Maybe we will complain about the shadowed template parameter.
1608 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1609 // Just pretend that we didn't see the previous declaration.
1610 PrevDecl = 0;
1611 }
1612
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001614 // GCC apparently allows the following idiom:
1615 //
1616 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1617 // @class XCElementToggler;
1618 //
Mike Stump1eb44332009-09-09 15:08:12 +00001619 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001620 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001621 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001622 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001623 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001624 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001625 // a forward class declaration matching a typedef name of a class refers
1626 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001627 if (const ObjCObjectType *OI =
1628 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1629 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001630 }
Chris Lattner4d391482007-12-12 07:09:47 +00001631 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001632 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1633 if (!IDecl) { // Not already seen? Make a forward decl.
1634 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1635 IdentList[i], IdentLocs[i], true);
1636
1637 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1638 // the current DeclContext. This prevents clients that walk DeclContext
1639 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1640 // declared later (if at all). We also take care to explicitly make
1641 // sure this declaration is visible for name lookup.
1642 PushOnScopeChains(IDecl, TUScope, false);
1643 CurContext->makeDeclVisibleInContext(IDecl, true);
1644 }
Chris Lattner4d391482007-12-12 07:09:47 +00001645
1646 Interfaces.push_back(IDecl);
1647 }
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Ted Kremenek321c22f2009-11-18 00:28:11 +00001649 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001650 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001651 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001652 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001653 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001654 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001655 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001656}
1657
John McCall0f4c4c42011-06-16 01:15:19 +00001658static bool tryMatchRecordTypes(ASTContext &Context,
1659 Sema::MethodMatchStrategy strategy,
1660 const Type *left, const Type *right);
1661
John McCallf85e1932011-06-15 23:02:42 +00001662static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1663 QualType leftQT, QualType rightQT) {
1664 const Type *left =
1665 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1666 const Type *right =
1667 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1668
1669 if (left == right) return true;
1670
1671 // If we're doing a strict match, the types have to match exactly.
1672 if (strategy == Sema::MMS_strict) return false;
1673
1674 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1675
1676 // Otherwise, use this absurdly complicated algorithm to try to
1677 // validate the basic, low-level compatibility of the two types.
1678
1679 // As a minimum, require the sizes and alignments to match.
1680 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1681 return false;
1682
1683 // Consider all the kinds of non-dependent canonical types:
1684 // - functions and arrays aren't possible as return and parameter types
1685
1686 // - vector types of equal size can be arbitrarily mixed
1687 if (isa<VectorType>(left)) return isa<VectorType>(right);
1688 if (isa<VectorType>(right)) return false;
1689
1690 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001691 // - structs, unions, and Objective-C objects must match more-or-less
1692 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001693 // - everything else should be a scalar
1694 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001695 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001696
1697 // Make scalars agree in kind, except count bools as chars.
1698 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1699 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1700 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1701 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1702
1703 // Note that data member pointers and function member pointers don't
1704 // intermix because of the size differences.
1705
1706 return (leftSK == rightSK);
1707}
Chris Lattner4d391482007-12-12 07:09:47 +00001708
John McCall0f4c4c42011-06-16 01:15:19 +00001709static bool tryMatchRecordTypes(ASTContext &Context,
1710 Sema::MethodMatchStrategy strategy,
1711 const Type *lt, const Type *rt) {
1712 assert(lt && rt && lt != rt);
1713
1714 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1715 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1716 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1717
1718 // Require union-hood to match.
1719 if (left->isUnion() != right->isUnion()) return false;
1720
1721 // Require an exact match if either is non-POD.
1722 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1723 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1724 return false;
1725
1726 // Require size and alignment to match.
1727 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1728
1729 // Require fields to match.
1730 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1731 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1732 for (; li != le && ri != re; ++li, ++ri) {
1733 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1734 return false;
1735 }
1736 return (li == le && ri == re);
1737}
1738
Chris Lattner4d391482007-12-12 07:09:47 +00001739/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1740/// returns true, or false, accordingly.
1741/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001742bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1743 const ObjCMethodDecl *right,
1744 MethodMatchStrategy strategy) {
1745 if (!matchTypes(Context, strategy,
1746 left->getResultType(), right->getResultType()))
1747 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001748
John McCallf85e1932011-06-15 23:02:42 +00001749 if (getLangOptions().ObjCAutoRefCount &&
1750 (left->hasAttr<NSReturnsRetainedAttr>()
1751 != right->hasAttr<NSReturnsRetainedAttr>() ||
1752 left->hasAttr<NSConsumesSelfAttr>()
1753 != right->hasAttr<NSConsumesSelfAttr>()))
1754 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001755
John McCallf85e1932011-06-15 23:02:42 +00001756 ObjCMethodDecl::param_iterator
1757 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001758
John McCallf85e1932011-06-15 23:02:42 +00001759 for (; li != le; ++li, ++ri) {
1760 assert(ri != right->param_end() && "Param mismatch");
1761 ParmVarDecl *lparm = *li, *rparm = *ri;
1762
1763 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1764 return false;
1765
1766 if (getLangOptions().ObjCAutoRefCount &&
1767 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1768 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001769 }
1770 return true;
1771}
1772
Sebastian Redldb9d2142010-08-02 23:18:59 +00001773/// \brief Read the contents of the method pool for a given selector from
1774/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001775///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001776/// This routine should only be called once, when the method pool has no entry
1777/// for this selector.
1778Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001779 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001780 assert(MethodPool.find(Sel) == MethodPool.end() &&
1781 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001782
1783 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001784 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Sebastian Redldb9d2142010-08-02 23:18:59 +00001786 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001787}
1788
Sebastian Redldb9d2142010-08-02 23:18:59 +00001789void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1790 bool instance) {
1791 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1792 if (Pos == MethodPool.end()) {
1793 if (ExternalSource)
1794 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001795 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001796 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1797 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001798 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001799 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001800 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001801 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001802 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001803 Entry.Method = Method;
1804 Entry.Next = 0;
1805 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001806 }
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Chris Lattnerb25df352009-03-04 05:16:45 +00001808 // We've seen a method with this name, see if we have already seen this type
1809 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001810 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1811 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1812
1813 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001814 ObjCMethodDecl *PrevObjCMethod = List->Method;
1815 PrevObjCMethod->setDefined(impl);
1816 // If a method is deprecated, push it in the global pool.
1817 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001818 if (Method->isDeprecated()) {
1819 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001820 List->Method = Method;
1821 }
1822 // If new method is unavailable, push it into global pool
1823 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001824 if (Method->isUnavailable()) {
1825 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001826 List->Method = Method;
1827 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001828 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001829 }
John McCallf85e1932011-06-15 23:02:42 +00001830 }
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Chris Lattnerb25df352009-03-04 05:16:45 +00001832 // We have a new signature for an existing method - add it.
1833 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001834 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1835 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001836}
1837
John McCallf85e1932011-06-15 23:02:42 +00001838/// Determines if this is an "acceptable" loose mismatch in the global
1839/// method pool. This exists mostly as a hack to get around certain
1840/// global mismatches which we can't afford to make warnings / errors.
1841/// Really, what we want is a way to take a method out of the global
1842/// method pool.
1843static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1844 ObjCMethodDecl *other) {
1845 if (!chosen->isInstanceMethod())
1846 return false;
1847
1848 Selector sel = chosen->getSelector();
1849 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1850 return false;
1851
1852 // Don't complain about mismatches for -length if the method we
1853 // chose has an integral result type.
1854 return (chosen->getResultType()->isIntegerType());
1855}
1856
Sebastian Redldb9d2142010-08-02 23:18:59 +00001857ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001858 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001859 bool warn, bool instance) {
1860 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1861 if (Pos == MethodPool.end()) {
1862 if (ExternalSource)
1863 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001864 else
1865 return 0;
1866 }
1867
Sebastian Redldb9d2142010-08-02 23:18:59 +00001868 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Sebastian Redldb9d2142010-08-02 23:18:59 +00001870 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001871 bool issueDiagnostic = false, issueError = false;
1872
1873 // We support a warning which complains about *any* difference in
1874 // method signature.
1875 bool strictSelectorMatch =
1876 (receiverIdOrClass && warn &&
1877 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1878 R.getBegin()) !=
1879 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001880 if (strictSelectorMatch)
1881 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001882 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1883 MMS_strict)) {
1884 issueDiagnostic = true;
1885 break;
1886 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001887 }
1888
John McCallf85e1932011-06-15 23:02:42 +00001889 // If we didn't see any strict differences, we won't see any loose
1890 // differences. In ARC, however, we also need to check for loose
1891 // mismatches, because most of them are errors.
1892 if (!strictSelectorMatch ||
1893 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001894 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001895 // This checks if the methods differ in type mismatch.
1896 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1897 MMS_loose) &&
1898 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1899 issueDiagnostic = true;
1900 if (getLangOptions().ObjCAutoRefCount)
1901 issueError = true;
1902 break;
1903 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001904 }
1905
John McCallf85e1932011-06-15 23:02:42 +00001906 if (issueDiagnostic) {
1907 if (issueError)
1908 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1909 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001910 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1911 else
1912 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001913
1914 Diag(MethList.Method->getLocStart(),
1915 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001916 << MethList.Method->getSourceRange();
1917 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1918 Diag(Next->Method->getLocStart(), diag::note_also_found)
1919 << Next->Method->getSourceRange();
1920 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001921 }
1922 return MethList.Method;
1923}
1924
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001925ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1927 if (Pos == MethodPool.end())
1928 return 0;
1929
1930 GlobalMethods &Methods = Pos->second;
1931
1932 if (Methods.first.Method && Methods.first.Method->isDefined())
1933 return Methods.first.Method;
1934 if (Methods.second.Method && Methods.second.Method->isDefined())
1935 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001936 return 0;
1937}
1938
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001939/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1940/// identical selector names in current and its super classes and issues
1941/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001942void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1943 ObjCMethodDecl *Method,
1944 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001945 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1946 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001948 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001949 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001950 SD->lookupMethod(Method->getSelector(), IsInstance);
1951 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001952 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001953 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001954 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001955 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1956 E = Method->param_end();
1957 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1958 for (; ParamI != E; ++ParamI, ++PrevI) {
1959 // Number of parameters are the same and is guaranteed by selector match.
1960 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1961 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1962 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001963 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001964 // respective argument type in the super class method, issue warning;
1965 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001966 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001967 << T1 << T2;
1968 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1969 return;
1970 }
1971 }
1972 ID = SD;
1973 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001974}
1975
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001976/// DiagnoseDuplicateIvars -
1977/// Check for duplicate ivars in the entire class at the start of
1978/// @implementation. This becomes necesssary because class extension can
1979/// add ivars to a class in random order which will not be known until
1980/// class's @implementation is seen.
1981void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1982 ObjCInterfaceDecl *SID) {
1983 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1984 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1985 ObjCIvarDecl* Ivar = (*IVI);
1986 if (Ivar->isInvalidDecl())
1987 continue;
1988 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1989 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1990 if (prevIvar) {
1991 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1992 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1993 Ivar->setInvalidDecl();
1994 }
1995 }
1996 }
1997}
1998
Steve Naroffa56f6162007-12-18 01:30:32 +00001999// Note: For class/category implemenations, allMethods/allProperties is
2000// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002001void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002002 Decl *ClassDecl,
2003 Decl **allMethods, unsigned allNum,
2004 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002005 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00002006 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
2007 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00002008 // should be true.
2009 if (!ClassDecl)
2010 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002011
Mike Stump1eb44332009-09-09 15:08:12 +00002012 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002013 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2014 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002015 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002016
Ted Kremenek782f2f52010-01-07 01:20:12 +00002017 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2018 // FIXME: This is wrong. We shouldn't be pretending that there is
2019 // an '@end' in the declaration.
2020 SourceLocation L = ClassDecl->getLocation();
2021 AtEnd.setBegin(L);
2022 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002023 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002024 }
2025
Steve Naroff0701bbb2009-01-08 17:28:14 +00002026 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2027 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2028 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2029
Chris Lattner4d391482007-12-12 07:09:47 +00002030 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002031 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002032 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002033
2034 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002035 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002036 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002037 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002038 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002039 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002040 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002041 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002042 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002043 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002044 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002045 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002046 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002047 InsMap[Method->getSelector()] = Method;
2048 /// The following allows us to typecheck messages to "id".
2049 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002050 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002051 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002052 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002053 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002054 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002055 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002056 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002057 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002058 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002059 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002060 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002061 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002062 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002063 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002064 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002065 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002066 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002067 /// The following allows us to typecheck messages to "Class".
2068 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002069 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002070 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002071 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002072 }
2073 }
2074 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002075 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002076 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002077 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002078 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002079 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002080 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002081 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002082 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002083 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002084
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002085 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002086 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002087 if (C->IsClassExtension()) {
2088 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2089 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002090 }
Chris Lattner4d391482007-12-12 07:09:47 +00002091 }
Steve Naroff09c47192009-01-09 15:36:25 +00002092 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002093 if (CDecl->getIdentifier())
2094 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2095 // user-defined setter/getter. It also synthesizes setter/getter methods
2096 // and adds them to the DeclContext and global method pools.
2097 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2098 E = CDecl->prop_end();
2099 I != E; ++I)
2100 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002101 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002102 }
2103 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002104 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002105 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002106 // Any property declared in a class extension might have user
2107 // declared setter or getter in current class extension or one
2108 // of the other class extensions. Mark them as synthesized as
2109 // property will be synthesized when property with same name is
2110 // seen in the @implementation.
2111 for (const ObjCCategoryDecl *ClsExtDecl =
2112 IDecl->getFirstClassExtension();
2113 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2114 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2115 E = ClsExtDecl->prop_end(); I != E; ++I) {
2116 ObjCPropertyDecl *Property = (*I);
2117 // Skip over properties declared @dynamic
2118 if (const ObjCPropertyImplDecl *PIDecl
2119 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2120 if (PIDecl->getPropertyImplementation()
2121 == ObjCPropertyImplDecl::Dynamic)
2122 continue;
2123
2124 for (const ObjCCategoryDecl *CExtDecl =
2125 IDecl->getFirstClassExtension();
2126 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2127 if (ObjCMethodDecl *GetterMethod =
2128 CExtDecl->getInstanceMethod(Property->getGetterName()))
2129 GetterMethod->setSynthesized(true);
2130 if (!Property->isReadOnly())
2131 if (ObjCMethodDecl *SetterMethod =
2132 CExtDecl->getInstanceMethod(Property->getSetterName()))
2133 SetterMethod->setSynthesized(true);
2134 }
2135 }
2136 }
2137
Ted Kremenekc32647d2010-12-23 21:35:43 +00002138 if (LangOpts.ObjCDefaultSynthProperties &&
2139 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002140 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002141 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002142 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002143 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002144
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002145 if (LangOpts.ObjCNonFragileABI2)
2146 while (IDecl->getSuperClass()) {
2147 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2148 IDecl = IDecl->getSuperClass();
2149 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002150 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002151 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002152 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002153 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002154 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Chris Lattner4d391482007-12-12 07:09:47 +00002156 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002157 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002158 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002159 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002160 Categories; Categories = Categories->getNextClassCategory()) {
2161 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002162 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002163 break;
2164 }
2165 }
2166 }
2167 }
Chris Lattner682bf922009-03-29 16:50:03 +00002168 if (isInterfaceDeclKind) {
2169 // Reject invalid vardecls.
2170 for (unsigned i = 0; i != tuvNum; i++) {
2171 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2172 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2173 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002174 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002175 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002176 }
Chris Lattner682bf922009-03-29 16:50:03 +00002177 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002178 }
Chris Lattner4d391482007-12-12 07:09:47 +00002179}
2180
2181
2182/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2183/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002184static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002185CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002186 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002187}
2188
Ted Kremenek422bae72010-04-18 04:59:38 +00002189static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002190bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002191 // The 'ibaction' attribute is allowed on method definitions because of
2192 // how the IBAction macro is used on both method declarations and definitions.
2193 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002194 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2195 if ((*i)->getKind() != attr::IBAction)
2196 return true;
2197 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002198}
2199
Douglas Gregor926df6c2011-06-11 01:09:30 +00002200/// \brief Check whether the declared result type of the given Objective-C
2201/// method declaration is compatible with the method's class.
2202///
2203static bool
2204CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2205 ObjCInterfaceDecl *CurrentClass) {
2206 QualType ResultType = Method->getResultType();
2207 SourceRange ResultTypeRange;
2208 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2209 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2210
2211 // If an Objective-C method inherits its related result type, then its
2212 // declared result type must be compatible with its own class type. The
2213 // declared result type is compatible if:
2214 if (const ObjCObjectPointerType *ResultObjectType
2215 = ResultType->getAs<ObjCObjectPointerType>()) {
2216 // - it is id or qualified id, or
2217 if (ResultObjectType->isObjCIdType() ||
2218 ResultObjectType->isObjCQualifiedIdType())
2219 return false;
2220
2221 if (CurrentClass) {
2222 if (ObjCInterfaceDecl *ResultClass
2223 = ResultObjectType->getInterfaceDecl()) {
2224 // - it is the same as the method's class type, or
2225 if (CurrentClass == ResultClass)
2226 return false;
2227
2228 // - it is a superclass of the method's class type
2229 if (ResultClass->isSuperClassOf(CurrentClass))
2230 return false;
2231 }
2232 }
2233 }
2234
2235 return true;
2236}
2237
2238/// \brief Determine if any method in the global method pool has an inferred
2239/// result type.
2240static bool
2241anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
2242 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
2243 if (Pos == S.MethodPool.end()) {
2244 if (S.ExternalSource)
2245 Pos = S.ReadMethodPool(Sel);
2246 else
2247 return 0;
2248 }
2249
2250 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
2251 for (ObjCMethodList *M = &List; M; M = M->Next) {
2252 if (M->Method && M->Method->hasRelatedResultType())
2253 return true;
2254 }
2255
2256 return false;
2257}
2258
John McCalld226f652010-08-21 09:40:31 +00002259Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002260 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002261 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00002262 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00002263 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002264 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002265 Selector Sel,
2266 // optional arguments. The number of types/arguments is obtained
2267 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002268 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002269 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002270 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002271 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002272 // Make sure we can establish a context for the method.
2273 if (!ClassDecl) {
2274 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002275 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002276 }
Chris Lattner4d391482007-12-12 07:09:47 +00002277 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002279 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002280 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002281 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Steve Naroffccef3712009-02-20 22:59:16 +00002283 // Methods cannot return interface types. All ObjC objects are
2284 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002285 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002286 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2287 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002288 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002289 }
Steve Naroffccef3712009-02-20 22:59:16 +00002290 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002291 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002292
2293 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002294 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002295 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002296 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002297 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002298 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002299 MethodDeclKind == tok::objc_optional
2300 ? ObjCMethodDecl::Optional
2301 : ObjCMethodDecl::Required,
2302 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002303
Chris Lattner0ed844b2008-04-04 06:12:32 +00002304 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Chris Lattner7db638d2009-04-11 19:42:43 +00002306 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002307 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002308 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Chris Lattnere294d3f2009-04-11 18:57:04 +00002310 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002311 ArgType = Context.getObjCIdType();
2312 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002313 } else {
John McCall58e46772009-10-23 21:48:59 +00002314 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002315 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002316 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002317 }
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002319 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2320 LookupOrdinaryName, ForRedeclaration);
2321 LookupName(R, S);
2322 if (R.isSingleResult()) {
2323 NamedDecl *PrevDecl = R.getFoundDecl();
2324 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002325 Diag(ArgInfo[i].NameLoc,
2326 (MethodDefinition ? diag::warn_method_param_redefinition
2327 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002328 << ArgInfo[i].Name;
2329 Diag(PrevDecl->getLocation(),
2330 diag::note_previous_declaration);
2331 }
2332 }
2333
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002334 SourceLocation StartLoc = DI
2335 ? DI->getTypeLoc().getBeginLoc()
2336 : ArgInfo[i].NameLoc;
2337
John McCall81ef3e62011-04-23 02:46:06 +00002338 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2339 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2340 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002341
John McCall70798862011-05-02 00:30:12 +00002342 Param->setObjCMethodScopeInfo(i);
2343
Chris Lattner0ed844b2008-04-04 06:12:32 +00002344 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002345 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002347 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002348 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002350 S->AddDecl(Param);
2351 IdResolver.AddDecl(Param);
2352
Chris Lattner0ed844b2008-04-04 06:12:32 +00002353 Params.push_back(Param);
2354 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002355
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002356 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002357 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002358 QualType ArgType = Param->getType();
2359 if (ArgType.isNull())
2360 ArgType = Context.getObjCIdType();
2361 else
2362 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2363 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002364 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002365 Diag(Param->getLocation(),
2366 diag::err_object_cannot_be_passed_returned_by_value)
2367 << 1 << ArgType;
2368 Param->setInvalidDecl();
2369 }
2370 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002371
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002372 Params.push_back(Param);
2373 }
2374
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002375 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2376 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002377 ObjCMethod->setObjCDeclQualifier(
2378 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2379 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002380
2381 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002382 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002383
John McCall54abf7d2009-11-04 02:18:39 +00002384 const ObjCMethodDecl *InterfaceMD = 0;
2385
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002386 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002387 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002388 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002389 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002390 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2391 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002392 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002393 PrevMethod = ImpDecl->getClassMethod(Sel);
2394 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002395 }
John McCall54abf7d2009-11-04 02:18:39 +00002396 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2397 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002398
Sean Huntcf807c42010-08-18 23:23:40 +00002399 if (ObjCMethod->hasAttrs() &&
2400 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002401 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002402 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002403 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002404 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002405 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2406 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002407 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002408 PrevMethod = CatImpDecl->getClassMethod(Sel);
2409 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002410 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002411
2412 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2413 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2414
Sean Huntcf807c42010-08-18 23:23:40 +00002415 if (ObjCMethod->hasAttrs() &&
2416 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002417 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002418 } else {
2419 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002420 }
2421 if (PrevMethod) {
2422 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002423 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002424 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002425 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002426 }
John McCall54abf7d2009-11-04 02:18:39 +00002427
Douglas Gregor926df6c2011-06-11 01:09:30 +00002428 // If this Objective-C method does not have a related result type, but we
2429 // are allowed to infer related result types, try to do so based on the
2430 // method family.
2431 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2432 if (!CurrentClass) {
2433 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2434 CurrentClass = Cat->getClassInterface();
2435 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2436 CurrentClass = Impl->getClassInterface();
2437 else if (ObjCCategoryImplDecl *CatImpl
2438 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2439 CurrentClass = CatImpl->getClassInterface();
2440 }
2441
John McCalleca5d222011-03-02 04:00:57 +00002442 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002443 if (InterfaceMD) {
2444 // Inherit the related result type, if we can.
2445 if (InterfaceMD->hasRelatedResultType() &&
2446 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2447 ObjCMethod->SetRelatedResultType();
2448
John McCalleca5d222011-03-02 04:00:57 +00002449 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002450 }
2451
John McCallf85e1932011-06-15 23:02:42 +00002452 bool ARCError = false;
2453 if (getLangOptions().ObjCAutoRefCount)
2454 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2455
2456 if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
Douglas Gregor74da19f2011-06-14 23:20:43 +00002457 getLangOptions().ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002458 bool InferRelatedResultType = false;
2459 switch (ObjCMethod->getMethodFamily()) {
2460 case OMF_None:
2461 case OMF_copy:
2462 case OMF_dealloc:
2463 case OMF_mutableCopy:
2464 case OMF_release:
2465 case OMF_retainCount:
2466 break;
2467
2468 case OMF_alloc:
2469 case OMF_new:
2470 InferRelatedResultType = ObjCMethod->isClassMethod();
2471 break;
2472
2473 case OMF_init:
2474 case OMF_autorelease:
2475 case OMF_retain:
2476 case OMF_self:
2477 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2478 break;
2479 }
2480
2481 if (InferRelatedResultType &&
2482 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2483 ObjCMethod->SetRelatedResultType();
2484
2485 if (!InterfaceMD &&
2486 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2487 ObjCMethod->isInstanceMethod()))
2488 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2489 }
2490
John McCalld226f652010-08-21 09:40:31 +00002491 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002492}
2493
Chris Lattnercc98eac2008-12-17 07:13:27 +00002494bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002495 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002496 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Anders Carlsson15281452008-11-04 16:57:32 +00002498 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2499 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Anders Carlsson15281452008-11-04 16:57:32 +00002501 return true;
2502}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002503
Chris Lattnercc98eac2008-12-17 07:13:27 +00002504/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2505/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002506void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002507 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002508 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002509 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002510 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002511 if (!Class) {
2512 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2513 return;
2514 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002515 if (LangOpts.ObjCNonFragileABI) {
2516 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2517 return;
2518 }
Mike Stump1eb44332009-09-09 15:08:12 +00002519
Chris Lattnercc98eac2008-12-17 07:13:27 +00002520 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002521 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2522 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002523 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002524 for (unsigned i = 0; i < Ivars.size(); i++) {
2525 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002526 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002527 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2528 /*FIXME: StartL=*/ID->getLocation(),
2529 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002530 ID->getIdentifier(), ID->getType(),
2531 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002532 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002533 }
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Chris Lattnercc98eac2008-12-17 07:13:27 +00002535 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002536 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002537 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002538 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002539 if (getLangOptions().CPlusPlus)
2540 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002541 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002542 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002543 }
2544}
2545
Douglas Gregor160b5632010-04-26 17:32:49 +00002546/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002547VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2548 SourceLocation StartLoc,
2549 SourceLocation IdLoc,
2550 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002551 bool Invalid) {
2552 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2553 // duration shall not be qualified by an address-space qualifier."
2554 // Since all parameters have automatic store duration, they can not have
2555 // an address space.
2556 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002557 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002558 Invalid = true;
2559 }
2560
2561 // An @catch parameter must be an unqualified object pointer type;
2562 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2563 if (Invalid) {
2564 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002565 } else if (T->isDependentType()) {
2566 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002567 } else if (!T->isObjCObjectPointerType()) {
2568 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002569 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002570 } else if (T->isObjCQualifiedIdType()) {
2571 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002572 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002573 }
2574
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002575 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2576 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002577 New->setExceptionVariable(true);
2578
Douglas Gregor160b5632010-04-26 17:32:49 +00002579 if (Invalid)
2580 New->setInvalidDecl();
2581 return New;
2582}
2583
John McCalld226f652010-08-21 09:40:31 +00002584Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002585 const DeclSpec &DS = D.getDeclSpec();
2586
2587 // We allow the "register" storage class on exception variables because
2588 // GCC did, but we drop it completely. Any other storage class is an error.
2589 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2590 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2591 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2592 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2593 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2594 << DS.getStorageClassSpec();
2595 }
2596 if (D.getDeclSpec().isThreadSpecified())
2597 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2598 D.getMutableDeclSpec().ClearStorageClassSpecs();
2599
2600 DiagnoseFunctionSpecifiers(D);
2601
2602 // Check that there are no default arguments inside the type of this
2603 // exception object (C++ only).
2604 if (getLangOptions().CPlusPlus)
2605 CheckExtraCXXDefaultArguments(D);
2606
Douglas Gregor160b5632010-04-26 17:32:49 +00002607 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002608 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2609 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002610
2611 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2612 // Objective-C++: Types shall not be defined in exception types.
2613 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2614 << Context.getTypeDeclType(OwnedDecl);
2615 }
2616
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002617 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2618 D.getSourceRange().getBegin(),
2619 D.getIdentifierLoc(),
2620 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002621 D.isInvalidType());
2622
2623 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2624 if (D.getCXXScopeSpec().isSet()) {
2625 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2626 << D.getCXXScopeSpec().getRange();
2627 New->setInvalidDecl();
2628 }
2629
2630 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002631 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002632 if (D.getIdentifier())
2633 IdResolver.AddDecl(New);
2634
2635 ProcessDeclAttributes(S, New, D);
2636
2637 if (New->hasAttr<BlocksAttr>())
2638 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002639 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002640}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002641
2642/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002643/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002644void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002645 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002646 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2647 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002648 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002649 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002650 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002651 }
2652}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002653
2654void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002655 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002656 unsigned numInitializers) {
2657 if (numInitializers > 0) {
2658 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002659 CXXCtorInitializer **ivarInitializers =
2660 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002661 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002662 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002663 IvarInitializers = ivarInitializers;
2664 }
2665}
2666
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002667void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002668 // Warning will be issued only when selector table is
2669 // generated (which means there is at lease one implementation
2670 // in the TU). This is to match gcc's behavior.
2671 if (ReferencedSelectors.empty() ||
2672 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002673 return;
2674 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2675 ReferencedSelectors.begin(),
2676 E = ReferencedSelectors.end(); S != E; ++S) {
2677 Selector Sel = (*S).first;
2678 if (!LookupImplementedMethodInGlobalPool(Sel))
2679 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2680 }
2681 return;
2682}