blob: 43f9c785926ae6f87048f27508e5b401e34de8b7 [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;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000289
290 case OMF_performSelector:
291 // we don't annotate performSelector's
292 return true;
293
John McCallf85e1932011-06-15 23:02:42 +0000294 }
295
296 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
297 S.Context));
298 return false;
299}
300
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000301static void DiagnoseObjCImplementedDeprecations(Sema &S,
302 NamedDecl *ND,
303 SourceLocation ImplLoc,
304 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000305 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000306 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000307 if (select == 0)
308 S.Diag(ND->getLocation(), diag::note_method_declared_at);
309 else
310 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
311 }
312}
313
Steve Naroffebf64432009-02-28 16:59:13 +0000314/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000315/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000316void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000317 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000318 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Steve Naroff394f3f42008-07-25 17:57:26 +0000320 // If we don't have a valid method decl, simply return.
321 if (!MDecl)
322 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000323
324 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000325 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000326 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000327 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000328 AddFactoryMethodToGlobalPool(MDecl, true);
329
Chris Lattner4d391482007-12-12 07:09:47 +0000330 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000331 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000332 PushFunctionScope();
333
Chris Lattner4d391482007-12-12 07:09:47 +0000334 // Create Decl objects for each parameter, entrring them in the scope for
335 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000336
337 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000338 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar451318c2008-08-26 06:07:48 +0000340 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
341 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000342
Chris Lattner8123a952008-04-10 02:22:51 +0000343 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000344 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000345 E = MDecl->param_end(); PI != E; ++PI) {
346 ParmVarDecl *Param = (*PI);
347 if (!Param->isInvalidDecl() &&
348 RequireCompleteType(Param->getLocation(), Param->getType(),
349 diag::err_typecheck_decl_incomplete_type))
350 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000351 if ((*PI)->getIdentifier())
352 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000353 }
John McCallf85e1932011-06-15 23:02:42 +0000354
355 // In ARC, disallow definition of retain/release/autorelease/retainCount
356 if (getLangOptions().ObjCAutoRefCount) {
357 switch (MDecl->getMethodFamily()) {
358 case OMF_retain:
359 case OMF_retainCount:
360 case OMF_release:
361 case OMF_autorelease:
362 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
363 << MDecl->getSelector();
364 break;
365
366 case OMF_None:
367 case OMF_dealloc:
368 case OMF_alloc:
369 case OMF_init:
370 case OMF_mutableCopy:
371 case OMF_copy:
372 case OMF_new:
373 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000374 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000375 break;
376 }
377 }
378
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000379 // Warn on implementating deprecated methods under
380 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000381 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
382 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000383 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000384 DiagnoseObjCImplementedDeprecations(*this,
385 dyn_cast<NamedDecl>(IMD),
386 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +0000387}
388
John McCalld226f652010-08-21 09:40:31 +0000389Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000390ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
391 IdentifierInfo *ClassName, SourceLocation ClassLoc,
392 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000393 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000394 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000395 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000396 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattner4d391482007-12-12 07:09:47 +0000398 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000399 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000400 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000401
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000403 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000404 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000407 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
408 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000409 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000410 if (!IDecl->isForwardDecl()) {
411 IDecl->setInvalidDecl();
412 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
413 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000414
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000415 // Return the previous class interface.
416 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000417 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000418 } else {
419 IDecl->setLocation(AtInterfaceLoc);
420 IDecl->setForwardDecl(false);
421 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000422 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000423 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000424 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000425
426 // Since this ObjCInterfaceDecl was created by a forward declaration,
427 // we now add it to the DeclContext since it wasn't added before
428 // (see ActOnForwardClassDeclaration).
429 IDecl->setLexicalDeclContext(CurContext);
430 CurContext->addDecl(IDecl);
431
432 if (AttrList)
433 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000434 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000435 } else {
436 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
437 ClassName, ClassLoc);
438 if (AttrList)
439 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
440
441 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner4d391482007-12-12 07:09:47 +0000444 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000445 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000446 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
447 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000448
449 if (!PrevDecl) {
450 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000451 TypoCorrection Corrected = CorrectTypo(
452 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
453 NULL, NULL, false, CTC_NoKeywords);
454 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000455 Diag(SuperLoc, diag::err_undef_superclass_suggest)
456 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
458 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000459 }
460 }
461
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000462 if (PrevDecl == IDecl) {
463 Diag(SuperLoc, diag::err_recursive_superclass)
464 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
465 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000466 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000467 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000468 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000469
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000470 // Diagnose classes that inherit from deprecated classes.
471 if (SuperClassDecl)
472 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000474 if (PrevDecl && SuperClassDecl == 0) {
475 // The previous declaration was not a class decl. Check if we have a
476 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000477 if (const TypedefNameDecl *TDecl =
478 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000479 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000480 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000481 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
482 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000483 }
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000486 // This handles the following case:
487 //
488 // typedef int SuperClass;
489 // @interface MyClass : SuperClass {} @end
490 //
491 if (!SuperClassDecl) {
492 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
493 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000494 }
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Richard Smith162e1c12011-04-15 14:24:37 +0000497 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000498 if (!SuperClassDecl)
499 Diag(SuperLoc, diag::err_undef_superclass)
500 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000501 else if (SuperClassDecl->isForwardDecl()) {
502 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000503 << SuperClassDecl->getDeclName() << ClassName
504 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000505 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
506 SuperClassDecl = 0;
507 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000508 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000509 IDecl->setSuperClass(SuperClassDecl);
510 IDecl->setSuperClassLoc(SuperLoc);
511 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000512 }
Chris Lattner4d391482007-12-12 07:09:47 +0000513 } else { // we have a root class.
514 IDecl->setLocEnd(ClassLoc);
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Sebastian Redl0b17c612010-08-13 00:28:03 +0000517 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000518 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000519 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000520 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000521 IDecl->setLocEnd(EndProtoLoc);
522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Anders Carlsson15281452008-11-04 16:57:32 +0000524 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000525 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000526}
527
528/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000529/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000530Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
531 IdentifierInfo *AliasName,
532 SourceLocation AliasLocation,
533 IdentifierInfo *ClassName,
534 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000535 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000536 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000537 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000538 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000539 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000540 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000541 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000542 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000543 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000544 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000545 }
546 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000547 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000548 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000549 if (const TypedefNameDecl *TDecl =
550 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000551 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000552 if (T->isObjCObjectType()) {
553 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000554 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000555 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000556 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000557 }
558 }
559 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000560 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
561 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000563 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000564 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000565 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000568 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000569 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000570 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Anders Carlsson15281452008-11-04 16:57:32 +0000572 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000573 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000574
John McCalld226f652010-08-21 09:40:31 +0000575 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000576}
577
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000578bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000579 IdentifierInfo *PName,
580 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000581 const ObjCList<ObjCProtocolDecl> &PList) {
582
583 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000584 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
585 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000586 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
587 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000588 if (PDecl->getIdentifier() == PName) {
589 Diag(Ploc, diag::err_protocol_has_circular_dependency);
590 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000591 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000592 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000593 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
594 PDecl->getLocation(), PDecl->getReferencedProtocols()))
595 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000596 }
597 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000598 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000599}
600
John McCalld226f652010-08-21 09:40:31 +0000601Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000602Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
603 IdentifierInfo *ProtocolName,
604 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000605 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000606 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000607 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000608 SourceLocation EndProtoLoc,
609 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000610 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000611 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000612 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000613 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000614 if (PDecl) {
615 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000616 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000617 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000618 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000619 // Just return the protocol we already had.
620 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000621 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000622 }
Steve Naroff61d68522009-03-05 15:22:01 +0000623 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000624 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000625 err = CheckForwardProtocolDeclarationForCircularDependency(
626 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Steve Narofff11b5082008-08-13 16:39:22 +0000628 // Make sure the cached decl gets a valid start location.
629 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000630 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000631 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000632 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000633 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000634 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000635 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000636 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000637 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000638 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000639 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000640 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000641 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000642 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000643 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000644 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
645 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000646 PDecl->setLocEnd(EndProtoLoc);
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
649 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000650 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000651}
652
653/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000654/// issues an error if they are not declared. It returns list of
655/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000656void
Chris Lattnere13b9592008-07-26 04:03:38 +0000657Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000658 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000659 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000660 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000661 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000662 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
663 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000664 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000665 TypoCorrection Corrected = CorrectTypo(
666 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
667 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
668 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000669 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000670 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000671 Diag(PDecl->getLocation(), diag::note_previous_decl)
672 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000673 }
674 }
675
676 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000677 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000678 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000679 continue;
680 }
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000682 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000683
684 // If this is a forward declaration and we are supposed to warn in this
685 // case, do it.
686 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000687 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000689 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000690 }
691}
692
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000693/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000694/// a class method in its extension.
695///
Mike Stump1eb44332009-09-09 15:08:12 +0000696void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000697 ObjCInterfaceDecl *ID) {
698 if (!ID)
699 return; // Possibly due to previous error
700
701 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000702 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
703 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000704 ObjCMethodDecl *MD = *i;
705 MethodMap[MD->getSelector()] = MD;
706 }
707
708 if (MethodMap.empty())
709 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000710 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
711 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000712 ObjCMethodDecl *Method = *i;
713 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
714 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
715 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
716 << Method->getDeclName();
717 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
718 }
719 }
720}
721
Chris Lattner58fe03b2009-04-12 08:43:13 +0000722/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000723Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000724Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000725 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000726 unsigned NumElts,
727 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000729 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner4d391482007-12-12 07:09:47 +0000731 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000732 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000733 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000734 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000735 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000736 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000737 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000738 PushOnScopeChains(PDecl, TUScope, false);
739 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000740 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000741 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000742 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000743 if (!isNew)
744 PDecl->setChangedSinceDeserialization(true);
745 }
Chris Lattner4d391482007-12-12 07:09:47 +0000746 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000747 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
750 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000751 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000752 Protocols.data(), Protocols.size(),
753 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000754 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000755 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000756 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000757}
758
John McCalld226f652010-08-21 09:40:31 +0000759Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000760ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
761 IdentifierInfo *ClassName, SourceLocation ClassLoc,
762 IdentifierInfo *CategoryName,
763 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000764 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000765 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000766 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000767 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000768 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000769 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000770
771 /// Check that class of this category is already completely declared.
772 if (!IDecl || IDecl->isForwardDecl()) {
773 // Create an invalid ObjCCategoryDecl to serve as context for
774 // the enclosing method declarations. We mark the decl invalid
775 // to make it clear that this isn't a valid AST.
776 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
777 ClassLoc, CategoryLoc, CategoryName);
778 CDecl->setInvalidDecl();
779 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000780 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000781 }
782
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000783 if (!CategoryName && IDecl->getImplementation()) {
784 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
785 Diag(IDecl->getImplementation()->getLocation(),
786 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000787 }
788
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000789 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
790 ClassLoc, CategoryLoc, CategoryName);
791 // FIXME: PushOnScopeChains?
792 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000793
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000794 CDecl->setClassInterface(IDecl);
795 // Insert class extension to the list of class's categories.
796 if (!CategoryName)
797 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattner16b34b42009-02-16 21:30:01 +0000799 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000800 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000801
Fariborz Jahanian25760612010-02-15 21:55:26 +0000802 if (CategoryName) {
803 /// Check for duplicate interface declaration for this category
804 ObjCCategoryDecl *CDeclChain;
805 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
806 CDeclChain = CDeclChain->getNextClassCategory()) {
807 if (CDeclChain->getIdentifier() == CategoryName) {
808 // Class extensions can be declared multiple times.
809 Diag(CategoryLoc, diag::warn_dup_category_def)
810 << ClassName << CategoryName;
811 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
812 break;
813 }
Chris Lattner70f19542009-02-16 21:26:43 +0000814 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000815 if (!CDeclChain)
816 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000817 }
Chris Lattner70f19542009-02-16 21:26:43 +0000818
Chris Lattner4d391482007-12-12 07:09:47 +0000819 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000820 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000821 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000822 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000823 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000824 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000825 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Anders Carlsson15281452008-11-04 16:57:32 +0000828 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000829 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000830}
831
832/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000833/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000834/// object.
John McCalld226f652010-08-21 09:40:31 +0000835Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000836 SourceLocation AtCatImplLoc,
837 IdentifierInfo *ClassName, SourceLocation ClassLoc,
838 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000839 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000840 ObjCCategoryDecl *CatIDecl = 0;
841 if (IDecl) {
842 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
843 if (!CatIDecl) {
844 // Category @implementation with no corresponding @interface.
845 // Create and install one.
846 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000847 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000848 CatName);
849 CatIDecl->setClassInterface(IDecl);
850 CatIDecl->insertNextClassCategory();
851 }
852 }
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000855 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
856 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000857 /// Check that class of this category is already completely declared.
858 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000860
Douglas Gregord0434102009-01-09 00:49:46 +0000861 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000862 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000863
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000864 /// Check that CatName, category name, is not used in another implementation.
865 if (CatIDecl) {
866 if (CatIDecl->getImplementation()) {
867 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
868 << CatName;
869 Diag(CatIDecl->getImplementation()->getLocation(),
870 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000871 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000872 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000873 // Warn on implementating category of deprecated class under
874 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000875 DiagnoseObjCImplementedDeprecations(*this,
876 dyn_cast<NamedDecl>(IDecl),
877 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000878 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Anders Carlsson15281452008-11-04 16:57:32 +0000881 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000882 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000883}
884
John McCalld226f652010-08-21 09:40:31 +0000885Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000886 SourceLocation AtClassImplLoc,
887 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000888 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000889 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000890 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000891 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000892 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000893 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
894 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000896 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000897 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000898 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
899 // If this is a forward declaration of an interface, warn.
900 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000901 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000902 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000903 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000904 } else {
905 // We did not find anything with the name ClassName; try to correct for
906 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000907 TypoCorrection Corrected = CorrectTypo(
908 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
909 NULL, NULL, false, CTC_NoKeywords);
910 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000911 // Suggest the (potentially) correct interface name. However, put the
912 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000913 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000914 // provide a code-modification hint or use the typo name for recovery,
915 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000916 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000917 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000918 << ClassName << CorrectedName;
919 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
920 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000921 IDecl = 0;
922 } else {
923 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
924 }
Chris Lattner4d391482007-12-12 07:09:47 +0000925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattner4d391482007-12-12 07:09:47 +0000927 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000928 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000929 if (SuperClassname) {
930 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000931 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
932 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000934 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
935 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000936 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000937 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000938 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000939 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 Diag(SuperClassLoc, diag::err_undef_superclass)
941 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000942 else if (IDecl && IDecl->getSuperClass() != SDecl) {
943 // This implementation and its interface do not have the same
944 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000945 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000946 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000947 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000948 }
949 }
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattner4d391482007-12-12 07:09:47 +0000952 if (!IDecl) {
953 // Legacy case of @implementation with no corresponding @interface.
954 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000955
Mike Stump390b4cc2009-05-16 07:39:55 +0000956 // FIXME: Do we support attributes on the @implementation? If so we should
957 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000958 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000959 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000960 IDecl->setSuperClass(SDecl);
961 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000962
963 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000964 } else {
965 // Mark the interface as being completed, even if it was just as
966 // @class ....;
967 // declaration; the user cannot reopen it.
968 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
971 ObjCImplementationDecl* IMPDecl =
972 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000973 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Anders Carlsson15281452008-11-04 16:57:32 +0000975 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000976 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Chris Lattner4d391482007-12-12 07:09:47 +0000978 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000979 if (IDecl->getImplementation()) {
980 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000982 Diag(IDecl->getImplementation()->getLocation(),
983 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000984 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000985 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000986 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000987 // Warn on implementating deprecated class under
988 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000989 DiagnoseObjCImplementedDeprecations(*this,
990 dyn_cast<NamedDecl>(IDecl),
991 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000992 }
John McCalld226f652010-08-21 09:40:31 +0000993 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000994}
995
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
997 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000998 SourceLocation RBrace) {
999 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001000 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001001 if (!IDecl)
1002 return;
1003 /// Check case of non-existing @interface decl.
1004 /// (legacy objective-c @implementation decl without an @interface decl).
1005 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001006 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +00001007 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001008 // Add ivar's to class's DeclContext.
1009 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001010 ivars[i]->setLexicalDeclContext(ImpDecl);
1011 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001012 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001013 }
1014
Chris Lattner4d391482007-12-12 07:09:47 +00001015 return;
1016 }
1017 // If implementation has empty ivar list, just return.
1018 if (numIvars == 0)
1019 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattner4d391482007-12-12 07:09:47 +00001021 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001022 if (LangOpts.ObjCNonFragileABI2) {
1023 if (ImpDecl->getSuperClass())
1024 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1025 for (unsigned i = 0; i < numIvars; i++) {
1026 ObjCIvarDecl* ImplIvar = ivars[i];
1027 if (const ObjCIvarDecl *ClsIvar =
1028 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1029 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1030 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1031 continue;
1032 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001033 // Instance ivar to Implementation's DeclContext.
1034 ImplIvar->setLexicalDeclContext(ImpDecl);
1035 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1036 ImpDecl->addDecl(ImplIvar);
1037 }
1038 return;
1039 }
Chris Lattner4d391482007-12-12 07:09:47 +00001040 // Check interface's Ivar list against those in the implementation.
1041 // names and types must match.
1042 //
Chris Lattner4d391482007-12-12 07:09:47 +00001043 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001044 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001045 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1046 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047 ObjCIvarDecl* ImplIvar = ivars[j++];
1048 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001049 assert (ImplIvar && "missing implementation ivar");
1050 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Steve Naroffca331292009-03-03 14:49:36 +00001052 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001053 if (Context.getCanonicalType(ImplIvar->getType()) !=
1054 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001055 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001056 << ImplIvar->getIdentifier()
1057 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001058 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001059 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1060 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1061 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001062 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1063 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001064 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1065 << ImplIvar->getIdentifier();
1066 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068 }
Steve Naroffca331292009-03-03 14:49:36 +00001069 // Make sure the names are identical.
1070 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001071 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001072 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001073 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001074 }
1075 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattner609e4c72007-12-12 18:11:49 +00001078 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001079 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001080 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001081 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001082}
1083
Steve Naroff3c2eb662008-02-10 21:38:56 +00001084void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001085 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001086 // No point warning no definition of method which is 'unavailable'.
1087 if (method->hasAttr<UnavailableAttr>())
1088 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001089 if (!IncompleteImpl) {
1090 Diag(ImpLoc, diag::warn_incomplete_impl);
1091 IncompleteImpl = true;
1092 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001093 if (DiagID == diag::warn_unimplemented_protocol_method)
1094 Diag(ImpLoc, DiagID) << method->getDeclName();
1095 else
1096 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001097}
1098
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001099/// Determines if type B can be substituted for type A. Returns true if we can
1100/// guarantee that anything that the user will do to an object of type A can
1101/// also be done to an object of type B. This is trivially true if the two
1102/// types are the same, or if B is a subclass of A. It becomes more complex
1103/// in cases where protocols are involved.
1104///
1105/// Object types in Objective-C describe the minimum requirements for an
1106/// object, rather than providing a complete description of a type. For
1107/// example, if A is a subclass of B, then B* may refer to an instance of A.
1108/// The principle of substitutability means that we may use an instance of A
1109/// anywhere that we may use an instance of B - it will implement all of the
1110/// ivars of B and all of the methods of B.
1111///
1112/// This substitutability is important when type checking methods, because
1113/// the implementation may have stricter type definitions than the interface.
1114/// The interface specifies minimum requirements, but the implementation may
1115/// have more accurate ones. For example, a method may privately accept
1116/// instances of B, but only publish that it accepts instances of A. Any
1117/// object passed to it will be type checked against B, and so will implicitly
1118/// by a valid A*. Similarly, a method may return a subclass of the class that
1119/// it is declared as returning.
1120///
1121/// This is most important when considering subclassing. A method in a
1122/// subclass must accept any object as an argument that its superclass's
1123/// implementation accepts. It may, however, accept a more general type
1124/// without breaking substitutability (i.e. you can still use the subclass
1125/// anywhere that you can use the superclass, but not vice versa). The
1126/// converse requirement applies to return types: the return type for a
1127/// subclass method must be a valid object of the kind that the superclass
1128/// advertises, but it may be specified more accurately. This avoids the need
1129/// for explicit down-casting by callers.
1130///
1131/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001132static bool isObjCTypeSubstitutable(ASTContext &Context,
1133 const ObjCObjectPointerType *A,
1134 const ObjCObjectPointerType *B,
1135 bool rejectId) {
1136 // Reject a protocol-unqualified id.
1137 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001138
1139 // If B is a qualified id, then A must also be a qualified id and it must
1140 // implement all of the protocols in B. It may not be a qualified class.
1141 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1142 // stricter definition so it is not substitutable for id<A>.
1143 if (B->isObjCQualifiedIdType()) {
1144 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001145 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1146 QualType(B,0),
1147 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001148 }
1149
1150 /*
1151 // id is a special type that bypasses type checking completely. We want a
1152 // warning when it is used in one place but not another.
1153 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1154
1155
1156 // If B is a qualified id, then A must also be a qualified id (which it isn't
1157 // if we've got this far)
1158 if (B->isObjCQualifiedIdType()) return false;
1159 */
1160
1161 // Now we know that A and B are (potentially-qualified) class types. The
1162 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001163 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001164}
1165
John McCall10302c02010-10-28 02:34:38 +00001166static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1167 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1168}
1169
1170static void CheckMethodOverrideReturn(Sema &S,
1171 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001172 ObjCMethodDecl *MethodDecl,
1173 bool IsProtocolMethodDecl) {
1174 if (IsProtocolMethodDecl &&
1175 (MethodDecl->getObjCDeclQualifier() !=
1176 MethodImpl->getObjCDeclQualifier())) {
1177 S.Diag(MethodImpl->getLocation(),
1178 diag::warn_conflicting_ret_type_modifiers)
1179 << MethodImpl->getDeclName()
1180 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1181 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1182 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1183 }
1184
John McCall10302c02010-10-28 02:34:38 +00001185 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001186 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001187 return;
1188
1189 unsigned DiagID = diag::warn_conflicting_ret_types;
1190
1191 // Mismatches between ObjC pointers go into a different warning
1192 // category, and sometimes they're even completely whitelisted.
1193 if (const ObjCObjectPointerType *ImplPtrTy =
1194 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1195 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001196 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001197 // Allow non-matching return types as long as they don't violate
1198 // the principle of substitutability. Specifically, we permit
1199 // return types that are subclasses of the declared return type,
1200 // or that are more-qualified versions of the declared type.
1201 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1202 return;
1203
1204 DiagID = diag::warn_non_covariant_ret_types;
1205 }
1206 }
1207
1208 S.Diag(MethodImpl->getLocation(), DiagID)
1209 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001210 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001211 << MethodImpl->getResultType()
1212 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001213 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1214 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001215}
1216
1217static void CheckMethodOverrideParam(Sema &S,
1218 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001219 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001220 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001221 ParmVarDecl *IfaceVar,
1222 bool IsProtocolMethodDecl) {
1223 if (IsProtocolMethodDecl &&
1224 (ImplVar->getObjCDeclQualifier() !=
1225 IfaceVar->getObjCDeclQualifier())) {
1226 S.Diag(ImplVar->getLocation(),
1227 diag::warn_conflicting_param_modifiers)
1228 << getTypeRange(ImplVar->getTypeSourceInfo())
1229 << MethodImpl->getDeclName();
1230 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1231 << getTypeRange(IfaceVar->getTypeSourceInfo());
1232 }
1233
John McCall10302c02010-10-28 02:34:38 +00001234 QualType ImplTy = ImplVar->getType();
1235 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001236
John McCall10302c02010-10-28 02:34:38 +00001237 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1238 return;
1239
1240 unsigned DiagID = diag::warn_conflicting_param_types;
1241
1242 // Mismatches between ObjC pointers go into a different warning
1243 // category, and sometimes they're even completely whitelisted.
1244 if (const ObjCObjectPointerType *ImplPtrTy =
1245 ImplTy->getAs<ObjCObjectPointerType>()) {
1246 if (const ObjCObjectPointerType *IfacePtrTy =
1247 IfaceTy->getAs<ObjCObjectPointerType>()) {
1248 // Allow non-matching argument types as long as they don't
1249 // violate the principle of substitutability. Specifically, the
1250 // implementation must accept any objects that the superclass
1251 // accepts, however it may also accept others.
1252 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1253 return;
1254
1255 DiagID = diag::warn_non_contravariant_param_types;
1256 }
1257 }
1258
1259 S.Diag(ImplVar->getLocation(), DiagID)
1260 << getTypeRange(ImplVar->getTypeSourceInfo())
1261 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1262 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1263 << getTypeRange(IfaceVar->getTypeSourceInfo());
1264}
John McCallf85e1932011-06-15 23:02:42 +00001265
1266/// In ARC, check whether the conventional meanings of the two methods
1267/// match. If they don't, it's a hard error.
1268static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1269 ObjCMethodDecl *decl) {
1270 ObjCMethodFamily implFamily = impl->getMethodFamily();
1271 ObjCMethodFamily declFamily = decl->getMethodFamily();
1272 if (implFamily == declFamily) return false;
1273
1274 // Since conventions are sorted by selector, the only possibility is
1275 // that the types differ enough to cause one selector or the other
1276 // to fall out of the family.
1277 assert(implFamily == OMF_None || declFamily == OMF_None);
1278
1279 // No further diagnostics required on invalid declarations.
1280 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1281
1282 const ObjCMethodDecl *unmatched = impl;
1283 ObjCMethodFamily family = declFamily;
1284 unsigned errorID = diag::err_arc_lost_method_convention;
1285 unsigned noteID = diag::note_arc_lost_method_convention;
1286 if (declFamily == OMF_None) {
1287 unmatched = decl;
1288 family = implFamily;
1289 errorID = diag::err_arc_gained_method_convention;
1290 noteID = diag::note_arc_gained_method_convention;
1291 }
1292
1293 // Indexes into a %select clause in the diagnostic.
1294 enum FamilySelector {
1295 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1296 };
1297 FamilySelector familySelector = FamilySelector();
1298
1299 switch (family) {
1300 case OMF_None: llvm_unreachable("logic error, no method convention");
1301 case OMF_retain:
1302 case OMF_release:
1303 case OMF_autorelease:
1304 case OMF_dealloc:
1305 case OMF_retainCount:
1306 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001307 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001308 // Mismatches for these methods don't change ownership
1309 // conventions, so we don't care.
1310 return false;
1311
1312 case OMF_init: familySelector = F_init; break;
1313 case OMF_alloc: familySelector = F_alloc; break;
1314 case OMF_copy: familySelector = F_copy; break;
1315 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1316 case OMF_new: familySelector = F_new; break;
1317 }
1318
1319 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1320 ReasonSelector reasonSelector;
1321
1322 // The only reason these methods don't fall within their families is
1323 // due to unusual result types.
1324 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1325 reasonSelector = R_UnrelatedReturn;
1326 } else {
1327 reasonSelector = R_NonObjectReturn;
1328 }
1329
1330 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1331 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1332
1333 return true;
1334}
John McCall10302c02010-10-28 02:34:38 +00001335
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001336void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001337 ObjCMethodDecl *MethodDecl,
1338 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001339 if (getLangOptions().ObjCAutoRefCount &&
1340 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1341 return;
1342
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001343 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1344 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Chris Lattner3aff9192009-04-11 19:58:42 +00001346 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001347 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001348 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001349 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1350 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001352 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001353 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001354 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001355 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001356}
1357
Mike Stump390b4cc2009-05-16 07:39:55 +00001358/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1359/// improve the efficiency of selector lookups and type checking by associating
1360/// with each protocol / interface / category the flattened instance tables. If
1361/// we used an immutable set to keep the table then it wouldn't add significant
1362/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001363
Steve Naroffefe7f362008-02-08 22:06:17 +00001364/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001365/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001366void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1367 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001368 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001369 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001370 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001371 ObjCContainerDecl *CDecl) {
1372 ObjCInterfaceDecl *IDecl;
1373 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1374 IDecl = C->getClassInterface();
1375 else
1376 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1377 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1378
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001379 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001380 ObjCInterfaceDecl *NSIDecl = 0;
1381 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001382 // check to see if class implements forwardInvocation method and objects
1383 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001384 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001385 // Under such conditions, which means that every method possible is
1386 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001387 // found" warnings.
1388 // FIXME: Use a general GetUnarySelector method for this.
1389 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1390 Selector fISelector = Context.Selectors.getSelector(1, &II);
1391 if (InsMap.count(fISelector))
1392 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1393 // need be implemented in the implementation.
1394 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001397 // If a method lookup fails locally we still need to look and see if
1398 // the method was implemented by a base class or an inherited
1399 // protocol. This lookup is slow, but occurs rarely in correct code
1400 // and otherwise would terminate in a warning.
1401
Chris Lattner4d391482007-12-12 07:09:47 +00001402 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001403 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001404 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001405 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001406 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001407 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001408 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001409 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001410 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001411 // Ugly, but necessary. Method declared in protcol might have
1412 // have been synthesized due to a property declared in the class which
1413 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001414 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001415 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001416 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001417 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001418 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1419 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001420 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001421 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001422 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1423 << PDecl->getDeclName();
1424 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001425 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001426 }
1427 }
Chris Lattner4d391482007-12-12 07:09:47 +00001428 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001429 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001430 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001431 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001432 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001433 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1434 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001435 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001436 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001437 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001438 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001439 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001440 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1441 PDecl->getDeclName();
1442 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001443 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001444 }
Chris Lattner780f3292008-07-21 21:32:27 +00001445 // Check on this protocols's referenced protocols, recursively.
1446 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1447 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001448 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001449}
1450
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001451/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001452/// or protocol against those declared in their implementations.
1453///
1454void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1455 const llvm::DenseSet<Selector> &ClsMap,
1456 llvm::DenseSet<Selector> &InsMapSeen,
1457 llvm::DenseSet<Selector> &ClsMapSeen,
1458 ObjCImplDecl* IMPDecl,
1459 ObjCContainerDecl* CDecl,
1460 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001461 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001462 // Check and see if instance methods in class interface have been
1463 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001464 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1465 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001466 if (InsMapSeen.count((*I)->getSelector()))
1467 continue;
1468 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001469 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001470 !InsMap.count((*I)->getSelector())) {
1471 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001472 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1473 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001474 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001475 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001476 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001477 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001478 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001479 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001480 assert(MethodDecl &&
1481 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001482 // ImpMethodDecl may be null as in a @dynamic property.
1483 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001484 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1485 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001486 }
1487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001489 // Check and see if class methods in class interface have been
1490 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001491 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001492 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001493 if (ClsMapSeen.count((*I)->getSelector()))
1494 continue;
1495 ClsMapSeen.insert((*I)->getSelector());
1496 if (!ClsMap.count((*I)->getSelector())) {
1497 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001498 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1499 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001500 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001501 ObjCMethodDecl *ImpMethodDecl =
1502 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001503 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001504 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001505 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1506 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001507 }
1508 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001509
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001510 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001511 // Also methods in class extensions need be looked at next.
1512 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1513 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1514 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1515 IMPDecl,
1516 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1517 IncompleteImpl, false);
1518
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001519 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001520 for (ObjCInterfaceDecl::all_protocol_iterator
1521 PI = I->all_referenced_protocol_begin(),
1522 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001523 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1524 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001525 (*PI), IncompleteImpl, false);
1526 if (I->getSuperClass())
1527 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001528 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001529 I->getSuperClass(), IncompleteImpl, false);
1530 }
1531}
1532
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001533void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001534 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001535 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001536 llvm::DenseSet<Selector> InsMap;
1537 // Check and see if instance methods in class interface have been
1538 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001539 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001541 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001543 // Check and see if properties declared in the interface have either 1)
1544 // an implementation or 2) there is a @synthesize/@dynamic implementation
1545 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001546 if (isa<ObjCInterfaceDecl>(CDecl) &&
1547 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001548 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001549
Chris Lattner4d391482007-12-12 07:09:47 +00001550 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001551 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001552 I = IMPDecl->classmeth_begin(),
1553 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001554 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001556 // Check for type conflict of methods declared in a class/protocol and
1557 // its implementation; if any.
1558 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001559 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1560 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001561 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Chris Lattner4d391482007-12-12 07:09:47 +00001563 // Check the protocol list for unimplemented methods in the @implementation
1564 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001565 // Check and see if class methods in class interface have been
1566 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Chris Lattnercddc8882009-03-01 00:56:52 +00001568 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001569 for (ObjCInterfaceDecl::all_protocol_iterator
1570 PI = I->all_referenced_protocol_begin(),
1571 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001572 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001573 InsMap, ClsMap, I);
1574 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001575 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1576 Categories; Categories = Categories->getNextClassExtension())
1577 ImplMethodsVsClassMethods(S, IMPDecl,
1578 const_cast<ObjCCategoryDecl*>(Categories),
1579 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001580 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001581 // For extended class, unimplemented methods in its protocols will
1582 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001583 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001584 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1585 E = C->protocol_end(); PI != E; ++PI)
1586 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001587 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001588 // Report unimplemented properties in the category as well.
1589 // When reporting on missing setter/getters, do not report when
1590 // setter/getter is implemented in category's primary class
1591 // implementation.
1592 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1593 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1594 for (ObjCImplementationDecl::instmeth_iterator
1595 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1596 InsMap.insert((*I)->getSelector());
1597 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001598 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001599 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001600 } else
1601 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001602}
1603
Mike Stump1eb44332009-09-09 15:08:12 +00001604/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001605Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001606Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001607 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001608 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001609 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Chris Lattner4d391482007-12-12 07:09:47 +00001612 for (unsigned i = 0; i != NumElts; ++i) {
1613 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001614 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001615 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001616 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001617 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001618 // Maybe we will complain about the shadowed template parameter.
1619 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1620 // Just pretend that we didn't see the previous declaration.
1621 PrevDecl = 0;
1622 }
1623
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001624 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001625 // GCC apparently allows the following idiom:
1626 //
1627 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1628 // @class XCElementToggler;
1629 //
Mike Stump1eb44332009-09-09 15:08:12 +00001630 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001631 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001632 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001633 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001634 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001635 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001636 // a forward class declaration matching a typedef name of a class refers
1637 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001638 if (const ObjCObjectType *OI =
1639 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1640 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001641 }
Chris Lattner4d391482007-12-12 07:09:47 +00001642 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001643 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1644 if (!IDecl) { // Not already seen? Make a forward decl.
1645 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1646 IdentList[i], IdentLocs[i], true);
1647
1648 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1649 // the current DeclContext. This prevents clients that walk DeclContext
1650 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1651 // declared later (if at all). We also take care to explicitly make
1652 // sure this declaration is visible for name lookup.
1653 PushOnScopeChains(IDecl, TUScope, false);
1654 CurContext->makeDeclVisibleInContext(IDecl, true);
1655 }
Chris Lattner4d391482007-12-12 07:09:47 +00001656
1657 Interfaces.push_back(IDecl);
1658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek321c22f2009-11-18 00:28:11 +00001660 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001661 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001662 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001663 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001664 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001665 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001666 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001667}
1668
John McCall0f4c4c42011-06-16 01:15:19 +00001669static bool tryMatchRecordTypes(ASTContext &Context,
1670 Sema::MethodMatchStrategy strategy,
1671 const Type *left, const Type *right);
1672
John McCallf85e1932011-06-15 23:02:42 +00001673static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1674 QualType leftQT, QualType rightQT) {
1675 const Type *left =
1676 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1677 const Type *right =
1678 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1679
1680 if (left == right) return true;
1681
1682 // If we're doing a strict match, the types have to match exactly.
1683 if (strategy == Sema::MMS_strict) return false;
1684
1685 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1686
1687 // Otherwise, use this absurdly complicated algorithm to try to
1688 // validate the basic, low-level compatibility of the two types.
1689
1690 // As a minimum, require the sizes and alignments to match.
1691 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1692 return false;
1693
1694 // Consider all the kinds of non-dependent canonical types:
1695 // - functions and arrays aren't possible as return and parameter types
1696
1697 // - vector types of equal size can be arbitrarily mixed
1698 if (isa<VectorType>(left)) return isa<VectorType>(right);
1699 if (isa<VectorType>(right)) return false;
1700
1701 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001702 // - structs, unions, and Objective-C objects must match more-or-less
1703 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001704 // - everything else should be a scalar
1705 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001706 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001707
1708 // Make scalars agree in kind, except count bools as chars.
1709 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1710 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1711 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1712 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1713
1714 // Note that data member pointers and function member pointers don't
1715 // intermix because of the size differences.
1716
1717 return (leftSK == rightSK);
1718}
Chris Lattner4d391482007-12-12 07:09:47 +00001719
John McCall0f4c4c42011-06-16 01:15:19 +00001720static bool tryMatchRecordTypes(ASTContext &Context,
1721 Sema::MethodMatchStrategy strategy,
1722 const Type *lt, const Type *rt) {
1723 assert(lt && rt && lt != rt);
1724
1725 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1726 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1727 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1728
1729 // Require union-hood to match.
1730 if (left->isUnion() != right->isUnion()) return false;
1731
1732 // Require an exact match if either is non-POD.
1733 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1734 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1735 return false;
1736
1737 // Require size and alignment to match.
1738 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1739
1740 // Require fields to match.
1741 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1742 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1743 for (; li != le && ri != re; ++li, ++ri) {
1744 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1745 return false;
1746 }
1747 return (li == le && ri == re);
1748}
1749
Chris Lattner4d391482007-12-12 07:09:47 +00001750/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1751/// returns true, or false, accordingly.
1752/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001753bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1754 const ObjCMethodDecl *right,
1755 MethodMatchStrategy strategy) {
1756 if (!matchTypes(Context, strategy,
1757 left->getResultType(), right->getResultType()))
1758 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
John McCallf85e1932011-06-15 23:02:42 +00001760 if (getLangOptions().ObjCAutoRefCount &&
1761 (left->hasAttr<NSReturnsRetainedAttr>()
1762 != right->hasAttr<NSReturnsRetainedAttr>() ||
1763 left->hasAttr<NSConsumesSelfAttr>()
1764 != right->hasAttr<NSConsumesSelfAttr>()))
1765 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
John McCallf85e1932011-06-15 23:02:42 +00001767 ObjCMethodDecl::param_iterator
1768 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001769
John McCallf85e1932011-06-15 23:02:42 +00001770 for (; li != le; ++li, ++ri) {
1771 assert(ri != right->param_end() && "Param mismatch");
1772 ParmVarDecl *lparm = *li, *rparm = *ri;
1773
1774 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1775 return false;
1776
1777 if (getLangOptions().ObjCAutoRefCount &&
1778 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1779 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001780 }
1781 return true;
1782}
1783
Sebastian Redldb9d2142010-08-02 23:18:59 +00001784/// \brief Read the contents of the method pool for a given selector from
1785/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001786///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001787/// This routine should only be called once, when the method pool has no entry
1788/// for this selector.
1789Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001790 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001791 assert(MethodPool.find(Sel) == MethodPool.end() &&
1792 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001793
1794 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001795 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Sebastian Redldb9d2142010-08-02 23:18:59 +00001797 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001798}
1799
Sebastian Redldb9d2142010-08-02 23:18:59 +00001800void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1801 bool instance) {
1802 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1803 if (Pos == MethodPool.end()) {
1804 if (ExternalSource)
1805 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001806 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001807 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1808 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001809 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001810 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001811 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001812 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001813 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001814 Entry.Method = Method;
1815 Entry.Next = 0;
1816 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Chris Lattnerb25df352009-03-04 05:16:45 +00001819 // We've seen a method with this name, see if we have already seen this type
1820 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001821 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1822 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1823
1824 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001825 ObjCMethodDecl *PrevObjCMethod = List->Method;
1826 PrevObjCMethod->setDefined(impl);
1827 // If a method is deprecated, push it in the global pool.
1828 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001829 if (Method->isDeprecated()) {
1830 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001831 List->Method = Method;
1832 }
1833 // If new method is unavailable, push it into global pool
1834 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001835 if (Method->isUnavailable()) {
1836 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001837 List->Method = Method;
1838 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001839 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001840 }
John McCallf85e1932011-06-15 23:02:42 +00001841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Chris Lattnerb25df352009-03-04 05:16:45 +00001843 // We have a new signature for an existing method - add it.
1844 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001845 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1846 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001847}
1848
John McCallf85e1932011-06-15 23:02:42 +00001849/// Determines if this is an "acceptable" loose mismatch in the global
1850/// method pool. This exists mostly as a hack to get around certain
1851/// global mismatches which we can't afford to make warnings / errors.
1852/// Really, what we want is a way to take a method out of the global
1853/// method pool.
1854static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1855 ObjCMethodDecl *other) {
1856 if (!chosen->isInstanceMethod())
1857 return false;
1858
1859 Selector sel = chosen->getSelector();
1860 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1861 return false;
1862
1863 // Don't complain about mismatches for -length if the method we
1864 // chose has an integral result type.
1865 return (chosen->getResultType()->isIntegerType());
1866}
1867
Sebastian Redldb9d2142010-08-02 23:18:59 +00001868ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001869 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001870 bool warn, bool instance) {
1871 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1872 if (Pos == MethodPool.end()) {
1873 if (ExternalSource)
1874 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001875 else
1876 return 0;
1877 }
1878
Sebastian Redldb9d2142010-08-02 23:18:59 +00001879 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Sebastian Redldb9d2142010-08-02 23:18:59 +00001881 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001882 bool issueDiagnostic = false, issueError = false;
1883
1884 // We support a warning which complains about *any* difference in
1885 // method signature.
1886 bool strictSelectorMatch =
1887 (receiverIdOrClass && warn &&
1888 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1889 R.getBegin()) !=
1890 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001891 if (strictSelectorMatch)
1892 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001893 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1894 MMS_strict)) {
1895 issueDiagnostic = true;
1896 break;
1897 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001898 }
1899
John McCallf85e1932011-06-15 23:02:42 +00001900 // If we didn't see any strict differences, we won't see any loose
1901 // differences. In ARC, however, we also need to check for loose
1902 // mismatches, because most of them are errors.
1903 if (!strictSelectorMatch ||
1904 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001905 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001906 // This checks if the methods differ in type mismatch.
1907 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1908 MMS_loose) &&
1909 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1910 issueDiagnostic = true;
1911 if (getLangOptions().ObjCAutoRefCount)
1912 issueError = true;
1913 break;
1914 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001915 }
1916
John McCallf85e1932011-06-15 23:02:42 +00001917 if (issueDiagnostic) {
1918 if (issueError)
1919 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1920 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001921 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1922 else
1923 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001924
1925 Diag(MethList.Method->getLocStart(),
1926 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001927 << MethList.Method->getSourceRange();
1928 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1929 Diag(Next->Method->getLocStart(), diag::note_also_found)
1930 << Next->Method->getSourceRange();
1931 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001932 }
1933 return MethList.Method;
1934}
1935
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001936ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001937 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1938 if (Pos == MethodPool.end())
1939 return 0;
1940
1941 GlobalMethods &Methods = Pos->second;
1942
1943 if (Methods.first.Method && Methods.first.Method->isDefined())
1944 return Methods.first.Method;
1945 if (Methods.second.Method && Methods.second.Method->isDefined())
1946 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001947 return 0;
1948}
1949
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001950/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1951/// identical selector names in current and its super classes and issues
1952/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001953void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1954 ObjCMethodDecl *Method,
1955 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001956 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1957 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001959 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001960 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001961 SD->lookupMethod(Method->getSelector(), IsInstance);
1962 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001963 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001964 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001965 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001966 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1967 E = Method->param_end();
1968 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1969 for (; ParamI != E; ++ParamI, ++PrevI) {
1970 // Number of parameters are the same and is guaranteed by selector match.
1971 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1972 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1973 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001974 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001975 // respective argument type in the super class method, issue warning;
1976 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001977 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001978 << T1 << T2;
1979 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1980 return;
1981 }
1982 }
1983 ID = SD;
1984 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001985}
1986
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001987/// DiagnoseDuplicateIvars -
1988/// Check for duplicate ivars in the entire class at the start of
1989/// @implementation. This becomes necesssary because class extension can
1990/// add ivars to a class in random order which will not be known until
1991/// class's @implementation is seen.
1992void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1993 ObjCInterfaceDecl *SID) {
1994 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1995 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1996 ObjCIvarDecl* Ivar = (*IVI);
1997 if (Ivar->isInvalidDecl())
1998 continue;
1999 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2000 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2001 if (prevIvar) {
2002 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2003 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2004 Ivar->setInvalidDecl();
2005 }
2006 }
2007 }
2008}
2009
Steve Naroffa56f6162007-12-18 01:30:32 +00002010// Note: For class/category implemenations, allMethods/allProperties is
2011// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002012void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002013 Decl *ClassDecl,
2014 Decl **allMethods, unsigned allNum,
2015 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002016 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00002017 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
2018 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00002019 // should be true.
2020 if (!ClassDecl)
2021 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002022
Mike Stump1eb44332009-09-09 15:08:12 +00002023 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002024 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2025 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002026 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002027
Ted Kremenek782f2f52010-01-07 01:20:12 +00002028 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2029 // FIXME: This is wrong. We shouldn't be pretending that there is
2030 // an '@end' in the declaration.
2031 SourceLocation L = ClassDecl->getLocation();
2032 AtEnd.setBegin(L);
2033 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002034 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002035 }
2036
Steve Naroff0701bbb2009-01-08 17:28:14 +00002037 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2038 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2039 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2040
Chris Lattner4d391482007-12-12 07:09:47 +00002041 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002042 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002043 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002044
2045 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002046 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002047 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002048 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002049 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002050 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002051 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002052 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002053 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002054 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002055 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002056 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002057 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002058 InsMap[Method->getSelector()] = Method;
2059 /// The following allows us to typecheck messages to "id".
2060 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002061 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002062 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002063 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002064 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002065 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002066 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002067 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002068 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002069 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002070 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002071 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002072 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002073 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002074 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002075 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002076 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002077 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002078 /// The following allows us to typecheck messages to "Class".
2079 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002080 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002081 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002082 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002083 }
2084 }
2085 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002086 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002087 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002088 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002089 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002090 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002091 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002092 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002093 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002094 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002095
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002096 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002097 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002098 if (C->IsClassExtension()) {
2099 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2100 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002101 }
Chris Lattner4d391482007-12-12 07:09:47 +00002102 }
Steve Naroff09c47192009-01-09 15:36:25 +00002103 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002104 if (CDecl->getIdentifier())
2105 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2106 // user-defined setter/getter. It also synthesizes setter/getter methods
2107 // and adds them to the DeclContext and global method pools.
2108 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2109 E = CDecl->prop_end();
2110 I != E; ++I)
2111 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002112 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002113 }
2114 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002115 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002116 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002117 // Any property declared in a class extension might have user
2118 // declared setter or getter in current class extension or one
2119 // of the other class extensions. Mark them as synthesized as
2120 // property will be synthesized when property with same name is
2121 // seen in the @implementation.
2122 for (const ObjCCategoryDecl *ClsExtDecl =
2123 IDecl->getFirstClassExtension();
2124 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2125 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2126 E = ClsExtDecl->prop_end(); I != E; ++I) {
2127 ObjCPropertyDecl *Property = (*I);
2128 // Skip over properties declared @dynamic
2129 if (const ObjCPropertyImplDecl *PIDecl
2130 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2131 if (PIDecl->getPropertyImplementation()
2132 == ObjCPropertyImplDecl::Dynamic)
2133 continue;
2134
2135 for (const ObjCCategoryDecl *CExtDecl =
2136 IDecl->getFirstClassExtension();
2137 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2138 if (ObjCMethodDecl *GetterMethod =
2139 CExtDecl->getInstanceMethod(Property->getGetterName()))
2140 GetterMethod->setSynthesized(true);
2141 if (!Property->isReadOnly())
2142 if (ObjCMethodDecl *SetterMethod =
2143 CExtDecl->getInstanceMethod(Property->getSetterName()))
2144 SetterMethod->setSynthesized(true);
2145 }
2146 }
2147 }
2148
Ted Kremenekc32647d2010-12-23 21:35:43 +00002149 if (LangOpts.ObjCDefaultSynthProperties &&
2150 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002151 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002152 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002153 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002154 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002155
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002156 if (LangOpts.ObjCNonFragileABI2)
2157 while (IDecl->getSuperClass()) {
2158 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2159 IDecl = IDecl->getSuperClass();
2160 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002161 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002162 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002163 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002164 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002165 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Chris Lattner4d391482007-12-12 07:09:47 +00002167 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002168 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002169 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002170 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002171 Categories; Categories = Categories->getNextClassCategory()) {
2172 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002173 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002174 break;
2175 }
2176 }
2177 }
2178 }
Chris Lattner682bf922009-03-29 16:50:03 +00002179 if (isInterfaceDeclKind) {
2180 // Reject invalid vardecls.
2181 for (unsigned i = 0; i != tuvNum; i++) {
2182 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2183 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2184 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002185 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002186 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002187 }
Chris Lattner682bf922009-03-29 16:50:03 +00002188 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002189 }
Chris Lattner4d391482007-12-12 07:09:47 +00002190}
2191
2192
2193/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2194/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002195static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002196CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002197 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002198}
2199
Ted Kremenek422bae72010-04-18 04:59:38 +00002200static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002201bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002202 // The 'ibaction' attribute is allowed on method definitions because of
2203 // how the IBAction macro is used on both method declarations and definitions.
2204 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002205 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2206 if ((*i)->getKind() != attr::IBAction)
2207 return true;
2208 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002209}
2210
Douglas Gregor926df6c2011-06-11 01:09:30 +00002211/// \brief Check whether the declared result type of the given Objective-C
2212/// method declaration is compatible with the method's class.
2213///
2214static bool
2215CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2216 ObjCInterfaceDecl *CurrentClass) {
2217 QualType ResultType = Method->getResultType();
2218 SourceRange ResultTypeRange;
2219 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2220 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2221
2222 // If an Objective-C method inherits its related result type, then its
2223 // declared result type must be compatible with its own class type. The
2224 // declared result type is compatible if:
2225 if (const ObjCObjectPointerType *ResultObjectType
2226 = ResultType->getAs<ObjCObjectPointerType>()) {
2227 // - it is id or qualified id, or
2228 if (ResultObjectType->isObjCIdType() ||
2229 ResultObjectType->isObjCQualifiedIdType())
2230 return false;
2231
2232 if (CurrentClass) {
2233 if (ObjCInterfaceDecl *ResultClass
2234 = ResultObjectType->getInterfaceDecl()) {
2235 // - it is the same as the method's class type, or
2236 if (CurrentClass == ResultClass)
2237 return false;
2238
2239 // - it is a superclass of the method's class type
2240 if (ResultClass->isSuperClassOf(CurrentClass))
2241 return false;
2242 }
2243 }
2244 }
2245
2246 return true;
2247}
2248
2249/// \brief Determine if any method in the global method pool has an inferred
2250/// result type.
2251static bool
2252anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
2253 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
2254 if (Pos == S.MethodPool.end()) {
2255 if (S.ExternalSource)
2256 Pos = S.ReadMethodPool(Sel);
2257 else
2258 return 0;
2259 }
2260
2261 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
2262 for (ObjCMethodList *M = &List; M; M = M->Next) {
2263 if (M->Method && M->Method->hasRelatedResultType())
2264 return true;
2265 }
2266
2267 return false;
2268}
2269
John McCalld226f652010-08-21 09:40:31 +00002270Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002271 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002272 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00002273 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00002274 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002275 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002276 Selector Sel,
2277 // optional arguments. The number of types/arguments is obtained
2278 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002279 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002280 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002281 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002282 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002283 // Make sure we can establish a context for the method.
2284 if (!ClassDecl) {
2285 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002286 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002287 }
Chris Lattner4d391482007-12-12 07:09:47 +00002288 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002290 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002291 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002292 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Steve Naroffccef3712009-02-20 22:59:16 +00002294 // Methods cannot return interface types. All ObjC objects are
2295 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002296 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002297 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2298 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002299 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002300 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002301 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002302 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002303 Diag(MethodLoc, diag::warn_missing_method_return_type)
2304 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002305 }
Mike Stump1eb44332009-09-09 15:08:12 +00002306
2307 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002308 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002309 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002310 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002311 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002312 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002313 MethodDeclKind == tok::objc_optional
2314 ? ObjCMethodDecl::Optional
2315 : ObjCMethodDecl::Required,
2316 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Chris Lattner0ed844b2008-04-04 06:12:32 +00002318 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Chris Lattner7db638d2009-04-11 19:42:43 +00002320 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002321 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002322 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattnere294d3f2009-04-11 18:57:04 +00002324 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002325 ArgType = Context.getObjCIdType();
2326 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002327 } else {
John McCall58e46772009-10-23 21:48:59 +00002328 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002329 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002330 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002331 }
Mike Stump1eb44332009-09-09 15:08:12 +00002332
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002333 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2334 LookupOrdinaryName, ForRedeclaration);
2335 LookupName(R, S);
2336 if (R.isSingleResult()) {
2337 NamedDecl *PrevDecl = R.getFoundDecl();
2338 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002339 Diag(ArgInfo[i].NameLoc,
2340 (MethodDefinition ? diag::warn_method_param_redefinition
2341 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002342 << ArgInfo[i].Name;
2343 Diag(PrevDecl->getLocation(),
2344 diag::note_previous_declaration);
2345 }
2346 }
2347
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002348 SourceLocation StartLoc = DI
2349 ? DI->getTypeLoc().getBeginLoc()
2350 : ArgInfo[i].NameLoc;
2351
John McCall81ef3e62011-04-23 02:46:06 +00002352 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2353 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2354 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002355
John McCall70798862011-05-02 00:30:12 +00002356 Param->setObjCMethodScopeInfo(i);
2357
Chris Lattner0ed844b2008-04-04 06:12:32 +00002358 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002359 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002361 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002362 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002364 S->AddDecl(Param);
2365 IdResolver.AddDecl(Param);
2366
Chris Lattner0ed844b2008-04-04 06:12:32 +00002367 Params.push_back(Param);
2368 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002369
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002370 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002371 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002372 QualType ArgType = Param->getType();
2373 if (ArgType.isNull())
2374 ArgType = Context.getObjCIdType();
2375 else
2376 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002377 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002378 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002379 Diag(Param->getLocation(),
2380 diag::err_object_cannot_be_passed_returned_by_value)
2381 << 1 << ArgType;
2382 Param->setInvalidDecl();
2383 }
2384 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002385
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002386 Params.push_back(Param);
2387 }
2388
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002389 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2390 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002391 ObjCMethod->setObjCDeclQualifier(
2392 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2393 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002394
2395 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002396 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002397
John McCall54abf7d2009-11-04 02:18:39 +00002398 const ObjCMethodDecl *InterfaceMD = 0;
2399
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002400 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002401 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002402 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002403 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002404 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2405 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002406 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002407 PrevMethod = ImpDecl->getClassMethod(Sel);
2408 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002409 }
John McCall54abf7d2009-11-04 02:18:39 +00002410 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2411 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002412
Sean Huntcf807c42010-08-18 23:23:40 +00002413 if (ObjCMethod->hasAttrs() &&
2414 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002415 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002416 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002417 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002418 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002419 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2420 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002421 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002422 PrevMethod = CatImpDecl->getClassMethod(Sel);
2423 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002424 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002425
2426 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2427 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2428
Sean Huntcf807c42010-08-18 23:23:40 +00002429 if (ObjCMethod->hasAttrs() &&
2430 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002431 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002432 } else {
2433 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002434 }
2435 if (PrevMethod) {
2436 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002437 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002438 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002439 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002440 }
John McCall54abf7d2009-11-04 02:18:39 +00002441
Douglas Gregor926df6c2011-06-11 01:09:30 +00002442 // If this Objective-C method does not have a related result type, but we
2443 // are allowed to infer related result types, try to do so based on the
2444 // method family.
2445 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2446 if (!CurrentClass) {
2447 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2448 CurrentClass = Cat->getClassInterface();
2449 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2450 CurrentClass = Impl->getClassInterface();
2451 else if (ObjCCategoryImplDecl *CatImpl
2452 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2453 CurrentClass = CatImpl->getClassInterface();
2454 }
2455
John McCalleca5d222011-03-02 04:00:57 +00002456 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002457 if (InterfaceMD) {
2458 // Inherit the related result type, if we can.
2459 if (InterfaceMD->hasRelatedResultType() &&
2460 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2461 ObjCMethod->SetRelatedResultType();
2462
John McCalleca5d222011-03-02 04:00:57 +00002463 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002464 }
2465
John McCallf85e1932011-06-15 23:02:42 +00002466 bool ARCError = false;
2467 if (getLangOptions().ObjCAutoRefCount)
2468 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2469
2470 if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
Douglas Gregor74da19f2011-06-14 23:20:43 +00002471 getLangOptions().ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002472 bool InferRelatedResultType = false;
2473 switch (ObjCMethod->getMethodFamily()) {
2474 case OMF_None:
2475 case OMF_copy:
2476 case OMF_dealloc:
2477 case OMF_mutableCopy:
2478 case OMF_release:
2479 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002480 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002481 break;
2482
2483 case OMF_alloc:
2484 case OMF_new:
2485 InferRelatedResultType = ObjCMethod->isClassMethod();
2486 break;
2487
2488 case OMF_init:
2489 case OMF_autorelease:
2490 case OMF_retain:
2491 case OMF_self:
2492 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2493 break;
2494 }
2495
2496 if (InferRelatedResultType &&
2497 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2498 ObjCMethod->SetRelatedResultType();
2499
2500 if (!InterfaceMD &&
2501 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2502 ObjCMethod->isInstanceMethod()))
2503 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2504 }
2505
John McCalld226f652010-08-21 09:40:31 +00002506 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002507}
2508
Chris Lattnercc98eac2008-12-17 07:13:27 +00002509bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002510 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002511 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002512
Anders Carlsson15281452008-11-04 16:57:32 +00002513 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2514 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Anders Carlsson15281452008-11-04 16:57:32 +00002516 return true;
2517}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002518
Chris Lattnercc98eac2008-12-17 07:13:27 +00002519/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2520/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002521void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002522 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002523 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002524 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002525 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002526 if (!Class) {
2527 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2528 return;
2529 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002530 if (LangOpts.ObjCNonFragileABI) {
2531 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2532 return;
2533 }
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Chris Lattnercc98eac2008-12-17 07:13:27 +00002535 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002536 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2537 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002538 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002539 for (unsigned i = 0; i < Ivars.size(); i++) {
2540 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002541 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002542 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2543 /*FIXME: StartL=*/ID->getLocation(),
2544 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002545 ID->getIdentifier(), ID->getType(),
2546 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002547 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002548 }
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Chris Lattnercc98eac2008-12-17 07:13:27 +00002550 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002551 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002552 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002553 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002554 if (getLangOptions().CPlusPlus)
2555 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002556 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002557 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002558 }
2559}
2560
Douglas Gregor160b5632010-04-26 17:32:49 +00002561/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002562VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2563 SourceLocation StartLoc,
2564 SourceLocation IdLoc,
2565 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002566 bool Invalid) {
2567 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2568 // duration shall not be qualified by an address-space qualifier."
2569 // Since all parameters have automatic store duration, they can not have
2570 // an address space.
2571 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002572 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002573 Invalid = true;
2574 }
2575
2576 // An @catch parameter must be an unqualified object pointer type;
2577 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2578 if (Invalid) {
2579 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002580 } else if (T->isDependentType()) {
2581 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002582 } else if (!T->isObjCObjectPointerType()) {
2583 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002584 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002585 } else if (T->isObjCQualifiedIdType()) {
2586 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002587 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002588 }
2589
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002590 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2591 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002592 New->setExceptionVariable(true);
2593
Douglas Gregor160b5632010-04-26 17:32:49 +00002594 if (Invalid)
2595 New->setInvalidDecl();
2596 return New;
2597}
2598
John McCalld226f652010-08-21 09:40:31 +00002599Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002600 const DeclSpec &DS = D.getDeclSpec();
2601
2602 // We allow the "register" storage class on exception variables because
2603 // GCC did, but we drop it completely. Any other storage class is an error.
2604 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2605 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2606 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2607 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2608 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2609 << DS.getStorageClassSpec();
2610 }
2611 if (D.getDeclSpec().isThreadSpecified())
2612 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2613 D.getMutableDeclSpec().ClearStorageClassSpecs();
2614
2615 DiagnoseFunctionSpecifiers(D);
2616
2617 // Check that there are no default arguments inside the type of this
2618 // exception object (C++ only).
2619 if (getLangOptions().CPlusPlus)
2620 CheckExtraCXXDefaultArguments(D);
2621
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002622 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002623 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002624
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002625 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2626 D.getSourceRange().getBegin(),
2627 D.getIdentifierLoc(),
2628 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002629 D.isInvalidType());
2630
2631 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2632 if (D.getCXXScopeSpec().isSet()) {
2633 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2634 << D.getCXXScopeSpec().getRange();
2635 New->setInvalidDecl();
2636 }
2637
2638 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002639 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002640 if (D.getIdentifier())
2641 IdResolver.AddDecl(New);
2642
2643 ProcessDeclAttributes(S, New, D);
2644
2645 if (New->hasAttr<BlocksAttr>())
2646 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002647 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002648}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002649
2650/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002651/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002652void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002653 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002654 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2655 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002656 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002657 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002658 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002659 }
2660}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002661
2662void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002663 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002664 unsigned numInitializers) {
2665 if (numInitializers > 0) {
2666 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002667 CXXCtorInitializer **ivarInitializers =
2668 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002669 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002670 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002671 IvarInitializers = ivarInitializers;
2672 }
2673}
2674
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002675void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002676 // Warning will be issued only when selector table is
2677 // generated (which means there is at lease one implementation
2678 // in the TU). This is to match gcc's behavior.
2679 if (ReferencedSelectors.empty() ||
2680 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002681 return;
2682 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2683 ReferencedSelectors.begin(),
2684 E = ReferencedSelectors.end(); S != E; ++S) {
2685 Selector Sel = (*S).first;
2686 if (!LookupImplementedMethodInGlobalPool(Sel))
2687 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2688 }
2689 return;
2690}