blob: 3052b826b65330fb65db7850cf01adce6bd09e37 [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) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001079 // No point warning no definition of method which is 'unavailable'.
1080 if (method->hasAttr<UnavailableAttr>())
1081 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001082 if (!IncompleteImpl) {
1083 Diag(ImpLoc, diag::warn_incomplete_impl);
1084 IncompleteImpl = true;
1085 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001086 if (DiagID == diag::warn_unimplemented_protocol_method)
1087 Diag(ImpLoc, DiagID) << method->getDeclName();
1088 else
1089 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001090}
1091
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001092/// Determines if type B can be substituted for type A. Returns true if we can
1093/// guarantee that anything that the user will do to an object of type A can
1094/// also be done to an object of type B. This is trivially true if the two
1095/// types are the same, or if B is a subclass of A. It becomes more complex
1096/// in cases where protocols are involved.
1097///
1098/// Object types in Objective-C describe the minimum requirements for an
1099/// object, rather than providing a complete description of a type. For
1100/// example, if A is a subclass of B, then B* may refer to an instance of A.
1101/// The principle of substitutability means that we may use an instance of A
1102/// anywhere that we may use an instance of B - it will implement all of the
1103/// ivars of B and all of the methods of B.
1104///
1105/// This substitutability is important when type checking methods, because
1106/// the implementation may have stricter type definitions than the interface.
1107/// The interface specifies minimum requirements, but the implementation may
1108/// have more accurate ones. For example, a method may privately accept
1109/// instances of B, but only publish that it accepts instances of A. Any
1110/// object passed to it will be type checked against B, and so will implicitly
1111/// by a valid A*. Similarly, a method may return a subclass of the class that
1112/// it is declared as returning.
1113///
1114/// This is most important when considering subclassing. A method in a
1115/// subclass must accept any object as an argument that its superclass's
1116/// implementation accepts. It may, however, accept a more general type
1117/// without breaking substitutability (i.e. you can still use the subclass
1118/// anywhere that you can use the superclass, but not vice versa). The
1119/// converse requirement applies to return types: the return type for a
1120/// subclass method must be a valid object of the kind that the superclass
1121/// advertises, but it may be specified more accurately. This avoids the need
1122/// for explicit down-casting by callers.
1123///
1124/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001125static bool isObjCTypeSubstitutable(ASTContext &Context,
1126 const ObjCObjectPointerType *A,
1127 const ObjCObjectPointerType *B,
1128 bool rejectId) {
1129 // Reject a protocol-unqualified id.
1130 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001131
1132 // If B is a qualified id, then A must also be a qualified id and it must
1133 // implement all of the protocols in B. It may not be a qualified class.
1134 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1135 // stricter definition so it is not substitutable for id<A>.
1136 if (B->isObjCQualifiedIdType()) {
1137 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001138 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1139 QualType(B,0),
1140 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001141 }
1142
1143 /*
1144 // id is a special type that bypasses type checking completely. We want a
1145 // warning when it is used in one place but not another.
1146 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1147
1148
1149 // If B is a qualified id, then A must also be a qualified id (which it isn't
1150 // if we've got this far)
1151 if (B->isObjCQualifiedIdType()) return false;
1152 */
1153
1154 // Now we know that A and B are (potentially-qualified) class types. The
1155 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001156 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001157}
1158
John McCall10302c02010-10-28 02:34:38 +00001159static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1160 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1161}
1162
1163static void CheckMethodOverrideReturn(Sema &S,
1164 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001165 ObjCMethodDecl *MethodDecl,
1166 bool IsProtocolMethodDecl) {
1167 if (IsProtocolMethodDecl &&
1168 (MethodDecl->getObjCDeclQualifier() !=
1169 MethodImpl->getObjCDeclQualifier())) {
1170 S.Diag(MethodImpl->getLocation(),
1171 diag::warn_conflicting_ret_type_modifiers)
1172 << MethodImpl->getDeclName()
1173 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1174 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1175 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1176 }
1177
John McCall10302c02010-10-28 02:34:38 +00001178 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001179 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001180 return;
1181
1182 unsigned DiagID = diag::warn_conflicting_ret_types;
1183
1184 // Mismatches between ObjC pointers go into a different warning
1185 // category, and sometimes they're even completely whitelisted.
1186 if (const ObjCObjectPointerType *ImplPtrTy =
1187 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1188 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001189 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001190 // Allow non-matching return types as long as they don't violate
1191 // the principle of substitutability. Specifically, we permit
1192 // return types that are subclasses of the declared return type,
1193 // or that are more-qualified versions of the declared type.
1194 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1195 return;
1196
1197 DiagID = diag::warn_non_covariant_ret_types;
1198 }
1199 }
1200
1201 S.Diag(MethodImpl->getLocation(), DiagID)
1202 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001203 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001204 << MethodImpl->getResultType()
1205 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001206 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1207 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001208}
1209
1210static void CheckMethodOverrideParam(Sema &S,
1211 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001212 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001213 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001214 ParmVarDecl *IfaceVar,
1215 bool IsProtocolMethodDecl) {
1216 if (IsProtocolMethodDecl &&
1217 (ImplVar->getObjCDeclQualifier() !=
1218 IfaceVar->getObjCDeclQualifier())) {
1219 S.Diag(ImplVar->getLocation(),
1220 diag::warn_conflicting_param_modifiers)
1221 << getTypeRange(ImplVar->getTypeSourceInfo())
1222 << MethodImpl->getDeclName();
1223 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1224 << getTypeRange(IfaceVar->getTypeSourceInfo());
1225 }
1226
John McCall10302c02010-10-28 02:34:38 +00001227 QualType ImplTy = ImplVar->getType();
1228 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001229
John McCall10302c02010-10-28 02:34:38 +00001230 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1231 return;
1232
1233 unsigned DiagID = diag::warn_conflicting_param_types;
1234
1235 // Mismatches between ObjC pointers go into a different warning
1236 // category, and sometimes they're even completely whitelisted.
1237 if (const ObjCObjectPointerType *ImplPtrTy =
1238 ImplTy->getAs<ObjCObjectPointerType>()) {
1239 if (const ObjCObjectPointerType *IfacePtrTy =
1240 IfaceTy->getAs<ObjCObjectPointerType>()) {
1241 // Allow non-matching argument types as long as they don't
1242 // violate the principle of substitutability. Specifically, the
1243 // implementation must accept any objects that the superclass
1244 // accepts, however it may also accept others.
1245 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1246 return;
1247
1248 DiagID = diag::warn_non_contravariant_param_types;
1249 }
1250 }
1251
1252 S.Diag(ImplVar->getLocation(), DiagID)
1253 << getTypeRange(ImplVar->getTypeSourceInfo())
1254 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1255 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1256 << getTypeRange(IfaceVar->getTypeSourceInfo());
1257}
John McCallf85e1932011-06-15 23:02:42 +00001258
1259/// In ARC, check whether the conventional meanings of the two methods
1260/// match. If they don't, it's a hard error.
1261static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1262 ObjCMethodDecl *decl) {
1263 ObjCMethodFamily implFamily = impl->getMethodFamily();
1264 ObjCMethodFamily declFamily = decl->getMethodFamily();
1265 if (implFamily == declFamily) return false;
1266
1267 // Since conventions are sorted by selector, the only possibility is
1268 // that the types differ enough to cause one selector or the other
1269 // to fall out of the family.
1270 assert(implFamily == OMF_None || declFamily == OMF_None);
1271
1272 // No further diagnostics required on invalid declarations.
1273 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1274
1275 const ObjCMethodDecl *unmatched = impl;
1276 ObjCMethodFamily family = declFamily;
1277 unsigned errorID = diag::err_arc_lost_method_convention;
1278 unsigned noteID = diag::note_arc_lost_method_convention;
1279 if (declFamily == OMF_None) {
1280 unmatched = decl;
1281 family = implFamily;
1282 errorID = diag::err_arc_gained_method_convention;
1283 noteID = diag::note_arc_gained_method_convention;
1284 }
1285
1286 // Indexes into a %select clause in the diagnostic.
1287 enum FamilySelector {
1288 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1289 };
1290 FamilySelector familySelector = FamilySelector();
1291
1292 switch (family) {
1293 case OMF_None: llvm_unreachable("logic error, no method convention");
1294 case OMF_retain:
1295 case OMF_release:
1296 case OMF_autorelease:
1297 case OMF_dealloc:
1298 case OMF_retainCount:
1299 case OMF_self:
1300 // Mismatches for these methods don't change ownership
1301 // conventions, so we don't care.
1302 return false;
1303
1304 case OMF_init: familySelector = F_init; break;
1305 case OMF_alloc: familySelector = F_alloc; break;
1306 case OMF_copy: familySelector = F_copy; break;
1307 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1308 case OMF_new: familySelector = F_new; break;
1309 }
1310
1311 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1312 ReasonSelector reasonSelector;
1313
1314 // The only reason these methods don't fall within their families is
1315 // due to unusual result types.
1316 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1317 reasonSelector = R_UnrelatedReturn;
1318 } else {
1319 reasonSelector = R_NonObjectReturn;
1320 }
1321
1322 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1323 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1324
1325 return true;
1326}
John McCall10302c02010-10-28 02:34:38 +00001327
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001328void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329 ObjCMethodDecl *MethodDecl,
1330 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001331 if (getLangOptions().ObjCAutoRefCount &&
1332 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1333 return;
1334
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001335 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1336 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattner3aff9192009-04-11 19:58:42 +00001338 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001339 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001340 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001341 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1342 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001344 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001345 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001346 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001347 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001348}
1349
Mike Stump390b4cc2009-05-16 07:39:55 +00001350/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1351/// improve the efficiency of selector lookups and type checking by associating
1352/// with each protocol / interface / category the flattened instance tables. If
1353/// we used an immutable set to keep the table then it wouldn't add significant
1354/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001355
Steve Naroffefe7f362008-02-08 22:06:17 +00001356/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001357/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001358void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1359 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001360 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001361 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001362 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001363 ObjCContainerDecl *CDecl) {
1364 ObjCInterfaceDecl *IDecl;
1365 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1366 IDecl = C->getClassInterface();
1367 else
1368 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1369 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1370
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001371 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001372 ObjCInterfaceDecl *NSIDecl = 0;
1373 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001374 // check to see if class implements forwardInvocation method and objects
1375 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001376 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // Under such conditions, which means that every method possible is
1378 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001379 // found" warnings.
1380 // FIXME: Use a general GetUnarySelector method for this.
1381 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1382 Selector fISelector = Context.Selectors.getSelector(1, &II);
1383 if (InsMap.count(fISelector))
1384 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1385 // need be implemented in the implementation.
1386 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001389 // If a method lookup fails locally we still need to look and see if
1390 // the method was implemented by a base class or an inherited
1391 // protocol. This lookup is slow, but occurs rarely in correct code
1392 // and otherwise would terminate in a warning.
1393
Chris Lattner4d391482007-12-12 07:09:47 +00001394 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001395 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001396 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001397 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001398 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001399 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001400 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001401 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001402 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001403 // Ugly, but necessary. Method declared in protcol might have
1404 // have been synthesized due to a property declared in the class which
1405 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001406 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001407 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001408 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001409 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001410 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1411 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001412 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001413 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001414 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1415 << PDecl->getDeclName();
1416 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001417 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001418 }
1419 }
Chris Lattner4d391482007-12-12 07:09:47 +00001420 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001421 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001422 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001423 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001424 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001425 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1426 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001427 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001428 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001429 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001430 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001431 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001432 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1433 PDecl->getDeclName();
1434 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001435 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001436 }
Chris Lattner780f3292008-07-21 21:32:27 +00001437 // Check on this protocols's referenced protocols, recursively.
1438 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1439 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001440 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001441}
1442
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001443/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1444/// or protocol against those declared in their implementations.
1445///
1446void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1447 const llvm::DenseSet<Selector> &ClsMap,
1448 llvm::DenseSet<Selector> &InsMapSeen,
1449 llvm::DenseSet<Selector> &ClsMapSeen,
1450 ObjCImplDecl* IMPDecl,
1451 ObjCContainerDecl* CDecl,
1452 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001453 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001454 // Check and see if instance methods in class interface have been
1455 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001456 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1457 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001458 if (InsMapSeen.count((*I)->getSelector()))
1459 continue;
1460 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001461 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001462 !InsMap.count((*I)->getSelector())) {
1463 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001464 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1465 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001466 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001467 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001468 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001469 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001470 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001472 assert(MethodDecl &&
1473 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001474 // ImpMethodDecl may be null as in a @dynamic property.
1475 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001476 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1477 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001478 }
1479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001481 // Check and see if class methods in class interface have been
1482 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001483 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001484 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001485 if (ClsMapSeen.count((*I)->getSelector()))
1486 continue;
1487 ClsMapSeen.insert((*I)->getSelector());
1488 if (!ClsMap.count((*I)->getSelector())) {
1489 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001490 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1491 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001492 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001493 ObjCMethodDecl *ImpMethodDecl =
1494 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001495 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001496 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001497 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1498 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001499 }
1500 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001501
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001502 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001503 // Also methods in class extensions need be looked at next.
1504 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1505 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1506 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1507 IMPDecl,
1508 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1509 IncompleteImpl, false);
1510
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001511 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001512 for (ObjCInterfaceDecl::all_protocol_iterator
1513 PI = I->all_referenced_protocol_begin(),
1514 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001515 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1516 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001517 (*PI), IncompleteImpl, false);
1518 if (I->getSuperClass())
1519 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001520 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001521 I->getSuperClass(), IncompleteImpl, false);
1522 }
1523}
1524
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001525void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001526 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001527 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001528 llvm::DenseSet<Selector> InsMap;
1529 // Check and see if instance methods in class interface have been
1530 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001531 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001532 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001533 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001535 // Check and see if properties declared in the interface have either 1)
1536 // an implementation or 2) there is a @synthesize/@dynamic implementation
1537 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001538 if (isa<ObjCInterfaceDecl>(CDecl) &&
1539 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001540 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001541
Chris Lattner4d391482007-12-12 07:09:47 +00001542 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001543 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001544 I = IMPDecl->classmeth_begin(),
1545 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001546 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001548 // Check for type conflict of methods declared in a class/protocol and
1549 // its implementation; if any.
1550 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001551 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1552 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001553 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Chris Lattner4d391482007-12-12 07:09:47 +00001555 // Check the protocol list for unimplemented methods in the @implementation
1556 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001557 // Check and see if class methods in class interface have been
1558 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Chris Lattnercddc8882009-03-01 00:56:52 +00001560 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001561 for (ObjCInterfaceDecl::all_protocol_iterator
1562 PI = I->all_referenced_protocol_begin(),
1563 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001564 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001565 InsMap, ClsMap, I);
1566 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001567 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1568 Categories; Categories = Categories->getNextClassExtension())
1569 ImplMethodsVsClassMethods(S, IMPDecl,
1570 const_cast<ObjCCategoryDecl*>(Categories),
1571 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001572 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001573 // For extended class, unimplemented methods in its protocols will
1574 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001575 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001576 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1577 E = C->protocol_end(); PI != E; ++PI)
1578 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001579 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001580 // Report unimplemented properties in the category as well.
1581 // When reporting on missing setter/getters, do not report when
1582 // setter/getter is implemented in category's primary class
1583 // implementation.
1584 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1585 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1586 for (ObjCImplementationDecl::instmeth_iterator
1587 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1588 InsMap.insert((*I)->getSelector());
1589 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001590 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001591 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001592 } else
1593 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001594}
1595
Mike Stump1eb44332009-09-09 15:08:12 +00001596/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001597Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001598Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001599 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001600 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001601 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001602 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Chris Lattner4d391482007-12-12 07:09:47 +00001604 for (unsigned i = 0; i != NumElts; ++i) {
1605 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001606 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001607 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001608 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001609 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001610 // Maybe we will complain about the shadowed template parameter.
1611 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1612 // Just pretend that we didn't see the previous declaration.
1613 PrevDecl = 0;
1614 }
1615
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001616 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001617 // GCC apparently allows the following idiom:
1618 //
1619 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1620 // @class XCElementToggler;
1621 //
Mike Stump1eb44332009-09-09 15:08:12 +00001622 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001623 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001624 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001625 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001626 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001627 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001628 // a forward class declaration matching a typedef name of a class refers
1629 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001630 if (const ObjCObjectType *OI =
1631 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1632 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001633 }
Chris Lattner4d391482007-12-12 07:09:47 +00001634 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001635 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1636 if (!IDecl) { // Not already seen? Make a forward decl.
1637 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1638 IdentList[i], IdentLocs[i], true);
1639
1640 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1641 // the current DeclContext. This prevents clients that walk DeclContext
1642 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1643 // declared later (if at all). We also take care to explicitly make
1644 // sure this declaration is visible for name lookup.
1645 PushOnScopeChains(IDecl, TUScope, false);
1646 CurContext->makeDeclVisibleInContext(IDecl, true);
1647 }
Chris Lattner4d391482007-12-12 07:09:47 +00001648
1649 Interfaces.push_back(IDecl);
1650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenek321c22f2009-11-18 00:28:11 +00001652 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001653 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001654 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001655 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001656 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001657 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001658 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001659}
1660
John McCall0f4c4c42011-06-16 01:15:19 +00001661static bool tryMatchRecordTypes(ASTContext &Context,
1662 Sema::MethodMatchStrategy strategy,
1663 const Type *left, const Type *right);
1664
John McCallf85e1932011-06-15 23:02:42 +00001665static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1666 QualType leftQT, QualType rightQT) {
1667 const Type *left =
1668 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1669 const Type *right =
1670 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1671
1672 if (left == right) return true;
1673
1674 // If we're doing a strict match, the types have to match exactly.
1675 if (strategy == Sema::MMS_strict) return false;
1676
1677 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1678
1679 // Otherwise, use this absurdly complicated algorithm to try to
1680 // validate the basic, low-level compatibility of the two types.
1681
1682 // As a minimum, require the sizes and alignments to match.
1683 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1684 return false;
1685
1686 // Consider all the kinds of non-dependent canonical types:
1687 // - functions and arrays aren't possible as return and parameter types
1688
1689 // - vector types of equal size can be arbitrarily mixed
1690 if (isa<VectorType>(left)) return isa<VectorType>(right);
1691 if (isa<VectorType>(right)) return false;
1692
1693 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001694 // - structs, unions, and Objective-C objects must match more-or-less
1695 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001696 // - everything else should be a scalar
1697 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001698 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001699
1700 // Make scalars agree in kind, except count bools as chars.
1701 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1702 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1703 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1704 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1705
1706 // Note that data member pointers and function member pointers don't
1707 // intermix because of the size differences.
1708
1709 return (leftSK == rightSK);
1710}
Chris Lattner4d391482007-12-12 07:09:47 +00001711
John McCall0f4c4c42011-06-16 01:15:19 +00001712static bool tryMatchRecordTypes(ASTContext &Context,
1713 Sema::MethodMatchStrategy strategy,
1714 const Type *lt, const Type *rt) {
1715 assert(lt && rt && lt != rt);
1716
1717 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1718 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1719 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1720
1721 // Require union-hood to match.
1722 if (left->isUnion() != right->isUnion()) return false;
1723
1724 // Require an exact match if either is non-POD.
1725 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1726 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1727 return false;
1728
1729 // Require size and alignment to match.
1730 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1731
1732 // Require fields to match.
1733 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1734 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1735 for (; li != le && ri != re; ++li, ++ri) {
1736 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1737 return false;
1738 }
1739 return (li == le && ri == re);
1740}
1741
Chris Lattner4d391482007-12-12 07:09:47 +00001742/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1743/// returns true, or false, accordingly.
1744/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001745bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1746 const ObjCMethodDecl *right,
1747 MethodMatchStrategy strategy) {
1748 if (!matchTypes(Context, strategy,
1749 left->getResultType(), right->getResultType()))
1750 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
John McCallf85e1932011-06-15 23:02:42 +00001752 if (getLangOptions().ObjCAutoRefCount &&
1753 (left->hasAttr<NSReturnsRetainedAttr>()
1754 != right->hasAttr<NSReturnsRetainedAttr>() ||
1755 left->hasAttr<NSConsumesSelfAttr>()
1756 != right->hasAttr<NSConsumesSelfAttr>()))
1757 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001758
John McCallf85e1932011-06-15 23:02:42 +00001759 ObjCMethodDecl::param_iterator
1760 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001761
John McCallf85e1932011-06-15 23:02:42 +00001762 for (; li != le; ++li, ++ri) {
1763 assert(ri != right->param_end() && "Param mismatch");
1764 ParmVarDecl *lparm = *li, *rparm = *ri;
1765
1766 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1767 return false;
1768
1769 if (getLangOptions().ObjCAutoRefCount &&
1770 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1771 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001772 }
1773 return true;
1774}
1775
Sebastian Redldb9d2142010-08-02 23:18:59 +00001776/// \brief Read the contents of the method pool for a given selector from
1777/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001778///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001779/// This routine should only be called once, when the method pool has no entry
1780/// for this selector.
1781Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001782 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001783 assert(MethodPool.find(Sel) == MethodPool.end() &&
1784 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001785
1786 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001787 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Sebastian Redldb9d2142010-08-02 23:18:59 +00001789 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001790}
1791
Sebastian Redldb9d2142010-08-02 23:18:59 +00001792void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1793 bool instance) {
1794 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1795 if (Pos == MethodPool.end()) {
1796 if (ExternalSource)
1797 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001798 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001799 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1800 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001801 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001802 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001803 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001804 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001805 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001806 Entry.Method = Method;
1807 Entry.Next = 0;
1808 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Chris Lattnerb25df352009-03-04 05:16:45 +00001811 // We've seen a method with this name, see if we have already seen this type
1812 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001813 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1814 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1815
1816 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001817 ObjCMethodDecl *PrevObjCMethod = List->Method;
1818 PrevObjCMethod->setDefined(impl);
1819 // If a method is deprecated, push it in the global pool.
1820 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001821 if (Method->isDeprecated()) {
1822 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001823 List->Method = Method;
1824 }
1825 // If new method is unavailable, push it into global pool
1826 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001827 if (Method->isUnavailable()) {
1828 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001829 List->Method = Method;
1830 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001831 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001832 }
John McCallf85e1932011-06-15 23:02:42 +00001833 }
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Chris Lattnerb25df352009-03-04 05:16:45 +00001835 // We have a new signature for an existing method - add it.
1836 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001837 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1838 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001839}
1840
John McCallf85e1932011-06-15 23:02:42 +00001841/// Determines if this is an "acceptable" loose mismatch in the global
1842/// method pool. This exists mostly as a hack to get around certain
1843/// global mismatches which we can't afford to make warnings / errors.
1844/// Really, what we want is a way to take a method out of the global
1845/// method pool.
1846static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1847 ObjCMethodDecl *other) {
1848 if (!chosen->isInstanceMethod())
1849 return false;
1850
1851 Selector sel = chosen->getSelector();
1852 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1853 return false;
1854
1855 // Don't complain about mismatches for -length if the method we
1856 // chose has an integral result type.
1857 return (chosen->getResultType()->isIntegerType());
1858}
1859
Sebastian Redldb9d2142010-08-02 23:18:59 +00001860ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001861 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001862 bool warn, bool instance) {
1863 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1864 if (Pos == MethodPool.end()) {
1865 if (ExternalSource)
1866 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001867 else
1868 return 0;
1869 }
1870
Sebastian Redldb9d2142010-08-02 23:18:59 +00001871 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Sebastian Redldb9d2142010-08-02 23:18:59 +00001873 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001874 bool issueDiagnostic = false, issueError = false;
1875
1876 // We support a warning which complains about *any* difference in
1877 // method signature.
1878 bool strictSelectorMatch =
1879 (receiverIdOrClass && warn &&
1880 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1881 R.getBegin()) !=
1882 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001883 if (strictSelectorMatch)
1884 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001885 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1886 MMS_strict)) {
1887 issueDiagnostic = true;
1888 break;
1889 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001890 }
1891
John McCallf85e1932011-06-15 23:02:42 +00001892 // If we didn't see any strict differences, we won't see any loose
1893 // differences. In ARC, however, we also need to check for loose
1894 // mismatches, because most of them are errors.
1895 if (!strictSelectorMatch ||
1896 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001897 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001898 // This checks if the methods differ in type mismatch.
1899 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1900 MMS_loose) &&
1901 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1902 issueDiagnostic = true;
1903 if (getLangOptions().ObjCAutoRefCount)
1904 issueError = true;
1905 break;
1906 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001907 }
1908
John McCallf85e1932011-06-15 23:02:42 +00001909 if (issueDiagnostic) {
1910 if (issueError)
1911 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1912 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001913 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1914 else
1915 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001916
1917 Diag(MethList.Method->getLocStart(),
1918 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001919 << MethList.Method->getSourceRange();
1920 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1921 Diag(Next->Method->getLocStart(), diag::note_also_found)
1922 << Next->Method->getSourceRange();
1923 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001924 }
1925 return MethList.Method;
1926}
1927
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001928ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001929 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1930 if (Pos == MethodPool.end())
1931 return 0;
1932
1933 GlobalMethods &Methods = Pos->second;
1934
1935 if (Methods.first.Method && Methods.first.Method->isDefined())
1936 return Methods.first.Method;
1937 if (Methods.second.Method && Methods.second.Method->isDefined())
1938 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001939 return 0;
1940}
1941
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001942/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1943/// identical selector names in current and its super classes and issues
1944/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001945void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1946 ObjCMethodDecl *Method,
1947 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001948 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1949 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001951 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001952 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001953 SD->lookupMethod(Method->getSelector(), IsInstance);
1954 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001955 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001956 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001957 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001958 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1959 E = Method->param_end();
1960 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1961 for (; ParamI != E; ++ParamI, ++PrevI) {
1962 // Number of parameters are the same and is guaranteed by selector match.
1963 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1964 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1965 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001966 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001967 // respective argument type in the super class method, issue warning;
1968 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001969 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001970 << T1 << T2;
1971 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1972 return;
1973 }
1974 }
1975 ID = SD;
1976 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001977}
1978
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001979/// DiagnoseDuplicateIvars -
1980/// Check for duplicate ivars in the entire class at the start of
1981/// @implementation. This becomes necesssary because class extension can
1982/// add ivars to a class in random order which will not be known until
1983/// class's @implementation is seen.
1984void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1985 ObjCInterfaceDecl *SID) {
1986 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1987 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1988 ObjCIvarDecl* Ivar = (*IVI);
1989 if (Ivar->isInvalidDecl())
1990 continue;
1991 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1992 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1993 if (prevIvar) {
1994 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1995 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1996 Ivar->setInvalidDecl();
1997 }
1998 }
1999 }
2000}
2001
Steve Naroffa56f6162007-12-18 01:30:32 +00002002// Note: For class/category implemenations, allMethods/allProperties is
2003// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002004void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002005 Decl *ClassDecl,
2006 Decl **allMethods, unsigned allNum,
2007 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002008 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00002009 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
2010 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00002011 // should be true.
2012 if (!ClassDecl)
2013 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002014
Mike Stump1eb44332009-09-09 15:08:12 +00002015 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002016 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2017 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002018 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002019
Ted Kremenek782f2f52010-01-07 01:20:12 +00002020 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2021 // FIXME: This is wrong. We shouldn't be pretending that there is
2022 // an '@end' in the declaration.
2023 SourceLocation L = ClassDecl->getLocation();
2024 AtEnd.setBegin(L);
2025 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002026 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002027 }
2028
Steve Naroff0701bbb2009-01-08 17:28:14 +00002029 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2030 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2031 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2032
Chris Lattner4d391482007-12-12 07:09:47 +00002033 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002034 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002035 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002036
2037 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002038 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002039 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002040 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002041 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002042 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002043 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002044 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002045 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002046 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002047 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002048 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002049 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002050 InsMap[Method->getSelector()] = Method;
2051 /// The following allows us to typecheck messages to "id".
2052 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002053 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002054 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002055 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002056 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002057 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002058 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002059 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002060 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002061 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002062 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002063 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002064 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002065 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002066 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002067 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002068 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002069 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002070 /// The following allows us to typecheck messages to "Class".
2071 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002072 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002073 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002074 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002075 }
2076 }
2077 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002078 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002079 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002080 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002081 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002082 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002083 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002084 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002085 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002086 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002087
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002088 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002089 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002090 if (C->IsClassExtension()) {
2091 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2092 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002093 }
Chris Lattner4d391482007-12-12 07:09:47 +00002094 }
Steve Naroff09c47192009-01-09 15:36:25 +00002095 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002096 if (CDecl->getIdentifier())
2097 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2098 // user-defined setter/getter. It also synthesizes setter/getter methods
2099 // and adds them to the DeclContext and global method pools.
2100 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2101 E = CDecl->prop_end();
2102 I != E; ++I)
2103 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002104 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002105 }
2106 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002107 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002108 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002109 // Any property declared in a class extension might have user
2110 // declared setter or getter in current class extension or one
2111 // of the other class extensions. Mark them as synthesized as
2112 // property will be synthesized when property with same name is
2113 // seen in the @implementation.
2114 for (const ObjCCategoryDecl *ClsExtDecl =
2115 IDecl->getFirstClassExtension();
2116 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2117 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2118 E = ClsExtDecl->prop_end(); I != E; ++I) {
2119 ObjCPropertyDecl *Property = (*I);
2120 // Skip over properties declared @dynamic
2121 if (const ObjCPropertyImplDecl *PIDecl
2122 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2123 if (PIDecl->getPropertyImplementation()
2124 == ObjCPropertyImplDecl::Dynamic)
2125 continue;
2126
2127 for (const ObjCCategoryDecl *CExtDecl =
2128 IDecl->getFirstClassExtension();
2129 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2130 if (ObjCMethodDecl *GetterMethod =
2131 CExtDecl->getInstanceMethod(Property->getGetterName()))
2132 GetterMethod->setSynthesized(true);
2133 if (!Property->isReadOnly())
2134 if (ObjCMethodDecl *SetterMethod =
2135 CExtDecl->getInstanceMethod(Property->getSetterName()))
2136 SetterMethod->setSynthesized(true);
2137 }
2138 }
2139 }
2140
Ted Kremenekc32647d2010-12-23 21:35:43 +00002141 if (LangOpts.ObjCDefaultSynthProperties &&
2142 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002143 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002144 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002145 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002146 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002147
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002148 if (LangOpts.ObjCNonFragileABI2)
2149 while (IDecl->getSuperClass()) {
2150 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2151 IDecl = IDecl->getSuperClass();
2152 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002153 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002154 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002155 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002156 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002157 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Chris Lattner4d391482007-12-12 07:09:47 +00002159 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002160 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002161 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002162 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002163 Categories; Categories = Categories->getNextClassCategory()) {
2164 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002165 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002166 break;
2167 }
2168 }
2169 }
2170 }
Chris Lattner682bf922009-03-29 16:50:03 +00002171 if (isInterfaceDeclKind) {
2172 // Reject invalid vardecls.
2173 for (unsigned i = 0; i != tuvNum; i++) {
2174 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2175 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2176 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002177 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002178 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002179 }
Chris Lattner682bf922009-03-29 16:50:03 +00002180 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002181 }
Chris Lattner4d391482007-12-12 07:09:47 +00002182}
2183
2184
2185/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2186/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002187static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002188CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002189 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002190}
2191
Ted Kremenek422bae72010-04-18 04:59:38 +00002192static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002193bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002194 // The 'ibaction' attribute is allowed on method definitions because of
2195 // how the IBAction macro is used on both method declarations and definitions.
2196 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002197 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2198 if ((*i)->getKind() != attr::IBAction)
2199 return true;
2200 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002201}
2202
Douglas Gregor926df6c2011-06-11 01:09:30 +00002203/// \brief Check whether the declared result type of the given Objective-C
2204/// method declaration is compatible with the method's class.
2205///
2206static bool
2207CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2208 ObjCInterfaceDecl *CurrentClass) {
2209 QualType ResultType = Method->getResultType();
2210 SourceRange ResultTypeRange;
2211 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2212 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2213
2214 // If an Objective-C method inherits its related result type, then its
2215 // declared result type must be compatible with its own class type. The
2216 // declared result type is compatible if:
2217 if (const ObjCObjectPointerType *ResultObjectType
2218 = ResultType->getAs<ObjCObjectPointerType>()) {
2219 // - it is id or qualified id, or
2220 if (ResultObjectType->isObjCIdType() ||
2221 ResultObjectType->isObjCQualifiedIdType())
2222 return false;
2223
2224 if (CurrentClass) {
2225 if (ObjCInterfaceDecl *ResultClass
2226 = ResultObjectType->getInterfaceDecl()) {
2227 // - it is the same as the method's class type, or
2228 if (CurrentClass == ResultClass)
2229 return false;
2230
2231 // - it is a superclass of the method's class type
2232 if (ResultClass->isSuperClassOf(CurrentClass))
2233 return false;
2234 }
2235 }
2236 }
2237
2238 return true;
2239}
2240
2241/// \brief Determine if any method in the global method pool has an inferred
2242/// result type.
2243static bool
2244anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
2245 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
2246 if (Pos == S.MethodPool.end()) {
2247 if (S.ExternalSource)
2248 Pos = S.ReadMethodPool(Sel);
2249 else
2250 return 0;
2251 }
2252
2253 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
2254 for (ObjCMethodList *M = &List; M; M = M->Next) {
2255 if (M->Method && M->Method->hasRelatedResultType())
2256 return true;
2257 }
2258
2259 return false;
2260}
2261
John McCalld226f652010-08-21 09:40:31 +00002262Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002263 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002264 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00002265 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00002266 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002267 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002268 Selector Sel,
2269 // optional arguments. The number of types/arguments is obtained
2270 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002271 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002272 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002273 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002274 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002275 // Make sure we can establish a context for the method.
2276 if (!ClassDecl) {
2277 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002278 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002279 }
Chris Lattner4d391482007-12-12 07:09:47 +00002280 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002282 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002283 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002284 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Steve Naroffccef3712009-02-20 22:59:16 +00002286 // Methods cannot return interface types. All ObjC objects are
2287 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002288 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002289 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2290 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002291 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002292 }
Steve Naroffccef3712009-02-20 22:59:16 +00002293 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002294 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002295
2296 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002297 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002298 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002299 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002300 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002301 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002302 MethodDeclKind == tok::objc_optional
2303 ? ObjCMethodDecl::Optional
2304 : ObjCMethodDecl::Required,
2305 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Chris Lattner0ed844b2008-04-04 06:12:32 +00002307 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Chris Lattner7db638d2009-04-11 19:42:43 +00002309 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002310 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002311 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Chris Lattnere294d3f2009-04-11 18:57:04 +00002313 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002314 ArgType = Context.getObjCIdType();
2315 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002316 } else {
John McCall58e46772009-10-23 21:48:59 +00002317 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002318 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002319 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002320 }
Mike Stump1eb44332009-09-09 15:08:12 +00002321
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002322 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2323 LookupOrdinaryName, ForRedeclaration);
2324 LookupName(R, S);
2325 if (R.isSingleResult()) {
2326 NamedDecl *PrevDecl = R.getFoundDecl();
2327 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002328 Diag(ArgInfo[i].NameLoc,
2329 (MethodDefinition ? diag::warn_method_param_redefinition
2330 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002331 << ArgInfo[i].Name;
2332 Diag(PrevDecl->getLocation(),
2333 diag::note_previous_declaration);
2334 }
2335 }
2336
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002337 SourceLocation StartLoc = DI
2338 ? DI->getTypeLoc().getBeginLoc()
2339 : ArgInfo[i].NameLoc;
2340
John McCall81ef3e62011-04-23 02:46:06 +00002341 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2342 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2343 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002344
John McCall70798862011-05-02 00:30:12 +00002345 Param->setObjCMethodScopeInfo(i);
2346
Chris Lattner0ed844b2008-04-04 06:12:32 +00002347 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002348 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002350 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002351 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002353 S->AddDecl(Param);
2354 IdResolver.AddDecl(Param);
2355
Chris Lattner0ed844b2008-04-04 06:12:32 +00002356 Params.push_back(Param);
2357 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002358
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002359 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002360 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002361 QualType ArgType = Param->getType();
2362 if (ArgType.isNull())
2363 ArgType = Context.getObjCIdType();
2364 else
2365 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2366 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002367 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002368 Diag(Param->getLocation(),
2369 diag::err_object_cannot_be_passed_returned_by_value)
2370 << 1 << ArgType;
2371 Param->setInvalidDecl();
2372 }
2373 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002374
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002375 Params.push_back(Param);
2376 }
2377
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002378 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2379 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002380 ObjCMethod->setObjCDeclQualifier(
2381 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2382 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002383
2384 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002385 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002386
John McCall54abf7d2009-11-04 02:18:39 +00002387 const ObjCMethodDecl *InterfaceMD = 0;
2388
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002389 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002390 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002391 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002392 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002393 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2394 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002395 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002396 PrevMethod = ImpDecl->getClassMethod(Sel);
2397 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002398 }
John McCall54abf7d2009-11-04 02:18:39 +00002399 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2400 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002401
Sean Huntcf807c42010-08-18 23:23:40 +00002402 if (ObjCMethod->hasAttrs() &&
2403 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002404 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002405 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002406 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002407 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002408 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2409 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002410 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002411 PrevMethod = CatImpDecl->getClassMethod(Sel);
2412 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002413 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002414
2415 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2416 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2417
Sean Huntcf807c42010-08-18 23:23:40 +00002418 if (ObjCMethod->hasAttrs() &&
2419 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002420 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002421 } else {
2422 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002423 }
2424 if (PrevMethod) {
2425 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002426 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002427 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002428 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002429 }
John McCall54abf7d2009-11-04 02:18:39 +00002430
Douglas Gregor926df6c2011-06-11 01:09:30 +00002431 // If this Objective-C method does not have a related result type, but we
2432 // are allowed to infer related result types, try to do so based on the
2433 // method family.
2434 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2435 if (!CurrentClass) {
2436 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2437 CurrentClass = Cat->getClassInterface();
2438 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2439 CurrentClass = Impl->getClassInterface();
2440 else if (ObjCCategoryImplDecl *CatImpl
2441 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2442 CurrentClass = CatImpl->getClassInterface();
2443 }
2444
John McCalleca5d222011-03-02 04:00:57 +00002445 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002446 if (InterfaceMD) {
2447 // Inherit the related result type, if we can.
2448 if (InterfaceMD->hasRelatedResultType() &&
2449 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2450 ObjCMethod->SetRelatedResultType();
2451
John McCalleca5d222011-03-02 04:00:57 +00002452 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002453 }
2454
John McCallf85e1932011-06-15 23:02:42 +00002455 bool ARCError = false;
2456 if (getLangOptions().ObjCAutoRefCount)
2457 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2458
2459 if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
Douglas Gregor74da19f2011-06-14 23:20:43 +00002460 getLangOptions().ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002461 bool InferRelatedResultType = false;
2462 switch (ObjCMethod->getMethodFamily()) {
2463 case OMF_None:
2464 case OMF_copy:
2465 case OMF_dealloc:
2466 case OMF_mutableCopy:
2467 case OMF_release:
2468 case OMF_retainCount:
2469 break;
2470
2471 case OMF_alloc:
2472 case OMF_new:
2473 InferRelatedResultType = ObjCMethod->isClassMethod();
2474 break;
2475
2476 case OMF_init:
2477 case OMF_autorelease:
2478 case OMF_retain:
2479 case OMF_self:
2480 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2481 break;
2482 }
2483
2484 if (InferRelatedResultType &&
2485 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2486 ObjCMethod->SetRelatedResultType();
2487
2488 if (!InterfaceMD &&
2489 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2490 ObjCMethod->isInstanceMethod()))
2491 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2492 }
2493
John McCalld226f652010-08-21 09:40:31 +00002494 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002495}
2496
Chris Lattnercc98eac2008-12-17 07:13:27 +00002497bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002498 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002499 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Anders Carlsson15281452008-11-04 16:57:32 +00002501 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2502 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Anders Carlsson15281452008-11-04 16:57:32 +00002504 return true;
2505}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002506
Chris Lattnercc98eac2008-12-17 07:13:27 +00002507/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2508/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002509void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002510 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002511 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002512 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002513 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002514 if (!Class) {
2515 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2516 return;
2517 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002518 if (LangOpts.ObjCNonFragileABI) {
2519 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2520 return;
2521 }
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Chris Lattnercc98eac2008-12-17 07:13:27 +00002523 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002524 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2525 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002526 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002527 for (unsigned i = 0; i < Ivars.size(); i++) {
2528 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002529 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002530 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2531 /*FIXME: StartL=*/ID->getLocation(),
2532 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002533 ID->getIdentifier(), ID->getType(),
2534 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002535 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002536 }
Mike Stump1eb44332009-09-09 15:08:12 +00002537
Chris Lattnercc98eac2008-12-17 07:13:27 +00002538 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002539 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002540 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002541 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002542 if (getLangOptions().CPlusPlus)
2543 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002544 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002545 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002546 }
2547}
2548
Douglas Gregor160b5632010-04-26 17:32:49 +00002549/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002550VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2551 SourceLocation StartLoc,
2552 SourceLocation IdLoc,
2553 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002554 bool Invalid) {
2555 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2556 // duration shall not be qualified by an address-space qualifier."
2557 // Since all parameters have automatic store duration, they can not have
2558 // an address space.
2559 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002560 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002561 Invalid = true;
2562 }
2563
2564 // An @catch parameter must be an unqualified object pointer type;
2565 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2566 if (Invalid) {
2567 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002568 } else if (T->isDependentType()) {
2569 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002570 } else if (!T->isObjCObjectPointerType()) {
2571 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002572 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002573 } else if (T->isObjCQualifiedIdType()) {
2574 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002575 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002576 }
2577
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002578 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2579 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002580 New->setExceptionVariable(true);
2581
Douglas Gregor160b5632010-04-26 17:32:49 +00002582 if (Invalid)
2583 New->setInvalidDecl();
2584 return New;
2585}
2586
John McCalld226f652010-08-21 09:40:31 +00002587Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002588 const DeclSpec &DS = D.getDeclSpec();
2589
2590 // We allow the "register" storage class on exception variables because
2591 // GCC did, but we drop it completely. Any other storage class is an error.
2592 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2593 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2594 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2595 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2596 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2597 << DS.getStorageClassSpec();
2598 }
2599 if (D.getDeclSpec().isThreadSpecified())
2600 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2601 D.getMutableDeclSpec().ClearStorageClassSpecs();
2602
2603 DiagnoseFunctionSpecifiers(D);
2604
2605 // Check that there are no default arguments inside the type of this
2606 // exception object (C++ only).
2607 if (getLangOptions().CPlusPlus)
2608 CheckExtraCXXDefaultArguments(D);
2609
Douglas Gregor160b5632010-04-26 17:32:49 +00002610 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002611 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2612 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002613
2614 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2615 // Objective-C++: Types shall not be defined in exception types.
2616 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2617 << Context.getTypeDeclType(OwnedDecl);
2618 }
2619
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002620 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2621 D.getSourceRange().getBegin(),
2622 D.getIdentifierLoc(),
2623 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002624 D.isInvalidType());
2625
2626 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2627 if (D.getCXXScopeSpec().isSet()) {
2628 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2629 << D.getCXXScopeSpec().getRange();
2630 New->setInvalidDecl();
2631 }
2632
2633 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002634 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002635 if (D.getIdentifier())
2636 IdResolver.AddDecl(New);
2637
2638 ProcessDeclAttributes(S, New, D);
2639
2640 if (New->hasAttr<BlocksAttr>())
2641 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002642 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002643}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002644
2645/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002646/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002647void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002648 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002649 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2650 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002651 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002652 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002653 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002654 }
2655}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002656
2657void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002658 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002659 unsigned numInitializers) {
2660 if (numInitializers > 0) {
2661 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002662 CXXCtorInitializer **ivarInitializers =
2663 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002664 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002665 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002666 IvarInitializers = ivarInitializers;
2667 }
2668}
2669
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002670void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002671 // Warning will be issued only when selector table is
2672 // generated (which means there is at lease one implementation
2673 // in the TU). This is to match gcc's behavior.
2674 if (ReferencedSelectors.empty() ||
2675 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002676 return;
2677 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2678 ReferencedSelectors.begin(),
2679 E = ReferencedSelectors.end(); S != E; ++S) {
2680 Selector Sel = (*S).first;
2681 if (!LookupImplementedMethodInGlobalPool(Sel))
2682 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2683 }
2684 return;
2685}