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