blob: 58eb0c99f2a21c1216b91e7cacde8af3f196df1e [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"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
24
Chris Lattner4d391482007-12-12 07:09:47 +000025using namespace clang;
26
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000027static void DiagnoseObjCImplementedDeprecations(Sema &S,
28 NamedDecl *ND,
29 SourceLocation ImplLoc,
30 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000031 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +000032 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000033 if (select == 0)
34 S.Diag(ND->getLocation(), diag::note_method_declared_at);
35 else
36 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
37 }
38}
39
Steve Naroffebf64432009-02-28 16:59:13 +000040/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000041/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +000042void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000043 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +000044 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +000045
Steve Naroff394f3f42008-07-25 17:57:26 +000046 // If we don't have a valid method decl, simply return.
47 if (!MDecl)
48 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000049
50 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000051 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000052 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000053 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000054 AddFactoryMethodToGlobalPool(MDecl, true);
55
Chris Lattner4d391482007-12-12 07:09:47 +000056 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000057 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000058 PushFunctionScope();
59
Chris Lattner4d391482007-12-12 07:09:47 +000060 // Create Decl objects for each parameter, entrring them in the scope for
61 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000062
63 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000064 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000065
Daniel Dunbar451318c2008-08-26 06:07:48 +000066 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
67 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000068
Chris Lattner8123a952008-04-10 02:22:51 +000069 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000070 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +000071 E = MDecl->param_end(); PI != E; ++PI) {
72 ParmVarDecl *Param = (*PI);
73 if (!Param->isInvalidDecl() &&
74 RequireCompleteType(Param->getLocation(), Param->getType(),
75 diag::err_typecheck_decl_incomplete_type))
76 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +000077 if ((*PI)->getIdentifier())
78 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +000079 }
Fariborz Jahanianb1224f62011-02-15 00:59:30 +000080 // Warn on implementating deprecated methods under
81 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000082 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
83 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +000084 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000085 DiagnoseObjCImplementedDeprecations(*this,
86 dyn_cast<NamedDecl>(IMD),
87 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +000088}
89
John McCalld226f652010-08-21 09:40:31 +000090Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000091ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
92 IdentifierInfo *ClassName, SourceLocation ClassLoc,
93 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +000094 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000095 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000096 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000097 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner4d391482007-12-12 07:09:47 +000099 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000100 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000101 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000102
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000104 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000105 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000108 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
109 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000110 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000111 if (!IDecl->isForwardDecl()) {
112 IDecl->setInvalidDecl();
113 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
114 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000115
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000116 // Return the previous class interface.
117 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000118 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000119 } else {
120 IDecl->setLocation(AtInterfaceLoc);
121 IDecl->setForwardDecl(false);
122 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000123 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000124 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000125 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000126
127 // Since this ObjCInterfaceDecl was created by a forward declaration,
128 // we now add it to the DeclContext since it wasn't added before
129 // (see ActOnForwardClassDeclaration).
130 IDecl->setLexicalDeclContext(CurContext);
131 CurContext->addDecl(IDecl);
132
133 if (AttrList)
134 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000135 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000136 } else {
137 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
138 ClassName, ClassLoc);
139 if (AttrList)
140 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
141
142 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattner4d391482007-12-12 07:09:47 +0000145 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000146 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000147 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
148 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000149
150 if (!PrevDecl) {
151 // Try to correct for a typo in the superclass name.
152 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000153 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000154 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
155 Diag(SuperLoc, diag::err_undef_superclass_suggest)
156 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000157 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
158 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000159 }
160 }
161
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000162 if (PrevDecl == IDecl) {
163 Diag(SuperLoc, diag::err_recursive_superclass)
164 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
165 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000166 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000167 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000168 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000169
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000170 // Diagnose classes that inherit from deprecated classes.
171 if (SuperClassDecl)
172 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 if (PrevDecl && SuperClassDecl == 0) {
175 // The previous declaration was not a class decl. Check if we have a
176 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000177 if (const TypedefNameDecl *TDecl =
178 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000179 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000180 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000181 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
182 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000183 }
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000186 // This handles the following case:
187 //
188 // typedef int SuperClass;
189 // @interface MyClass : SuperClass {} @end
190 //
191 if (!SuperClassDecl) {
192 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
193 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000194 }
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Richard Smith162e1c12011-04-15 14:24:37 +0000197 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000198 if (!SuperClassDecl)
199 Diag(SuperLoc, diag::err_undef_superclass)
200 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
201 else if (SuperClassDecl->isForwardDecl())
202 Diag(SuperLoc, diag::err_undef_superclass)
203 << SuperClassDecl->getDeclName() << ClassName
204 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000205 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000206 IDecl->setSuperClass(SuperClassDecl);
207 IDecl->setSuperClassLoc(SuperLoc);
208 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000209 }
Chris Lattner4d391482007-12-12 07:09:47 +0000210 } else { // we have a root class.
211 IDecl->setLocEnd(ClassLoc);
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Sebastian Redl0b17c612010-08-13 00:28:03 +0000214 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000215 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000216 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000217 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000218 IDecl->setLocEnd(EndProtoLoc);
219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Anders Carlsson15281452008-11-04 16:57:32 +0000221 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000222 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000223}
224
225/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000226/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000227Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
228 IdentifierInfo *AliasName,
229 SourceLocation AliasLocation,
230 IdentifierInfo *ClassName,
231 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000232 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000233 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000234 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000235 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000236 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000237 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000238 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000239 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000240 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000241 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000242 }
243 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000244 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000245 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000246 if (const TypedefNameDecl *TDecl =
247 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000248 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000249 if (T->isObjCObjectType()) {
250 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000251 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000252 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000253 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000254 }
255 }
256 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000257 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
258 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000259 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000260 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000261 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000262 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000265 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000266 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000267 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlsson15281452008-11-04 16:57:32 +0000269 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000270 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000271
John McCalld226f652010-08-21 09:40:31 +0000272 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000273}
274
Steve Naroff61d68522009-03-05 15:22:01 +0000275void Sema::CheckForwardProtocolDeclarationForCircularDependency(
276 IdentifierInfo *PName,
277 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000278 const ObjCList<ObjCProtocolDecl> &PList, bool &err) {
Steve Naroff61d68522009-03-05 15:22:01 +0000279 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
280 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Douglas Gregorc83c6872010-04-15 22:33:43 +0000282 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
283 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000284 if (PDecl->getIdentifier() == PName) {
285 Diag(Ploc, diag::err_protocol_has_circular_dependency);
286 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000287 err = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000290 PDecl->getLocation(), PDecl->getReferencedProtocols(), err);
Steve Naroff61d68522009-03-05 15:22:01 +0000291 }
292 }
293}
294
John McCalld226f652010-08-21 09:40:31 +0000295Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000296Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
297 IdentifierInfo *ProtocolName,
298 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000299 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000300 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000301 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000302 SourceLocation EndProtoLoc,
303 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000304 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000305 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000306 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000307 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000308 if (PDecl) {
309 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000310 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000311 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000312 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000313 // Just return the protocol we already had.
314 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000315 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000316 }
Steve Naroff61d68522009-03-05 15:22:01 +0000317 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000318 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000319 CheckForwardProtocolDeclarationForCircularDependency(
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000320 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList, err);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Steve Narofff11b5082008-08-13 16:39:22 +0000322 // Make sure the cached decl gets a valid start location.
323 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000324 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000325 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000326 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000327 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000328 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000329 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000330 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000331 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000332 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000333 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000334 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000335 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000336 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000337 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000338 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
339 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000340 PDecl->setLocEnd(EndProtoLoc);
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
343 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000344 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000345}
346
347/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000348/// issues an error if they are not declared. It returns list of
349/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000350void
Chris Lattnere13b9592008-07-26 04:03:38 +0000351Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000352 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000353 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000354 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000355 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000356 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
357 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000358 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000359 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
360 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000361 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000362 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
363 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
364 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000365 Diag(PDecl->getLocation(), diag::note_previous_decl)
366 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000367 }
368 }
369
370 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000371 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000372 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000373 continue;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000376 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000377
378 // If this is a forward declaration and we are supposed to warn in this
379 // case, do it.
380 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000381 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000382 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000383 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000384 }
385}
386
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000387/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000388/// a class method in its extension.
389///
Mike Stump1eb44332009-09-09 15:08:12 +0000390void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000391 ObjCInterfaceDecl *ID) {
392 if (!ID)
393 return; // Possibly due to previous error
394
395 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000396 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
397 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000398 ObjCMethodDecl *MD = *i;
399 MethodMap[MD->getSelector()] = MD;
400 }
401
402 if (MethodMap.empty())
403 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000404 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
405 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000406 ObjCMethodDecl *Method = *i;
407 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
408 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
409 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
410 << Method->getDeclName();
411 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
412 }
413 }
414}
415
Chris Lattner58fe03b2009-04-12 08:43:13 +0000416/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000417Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000418Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000419 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000420 unsigned NumElts,
421 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000422 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000423 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner4d391482007-12-12 07:09:47 +0000425 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000426 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000427 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000428 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000429 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000430 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000431 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000432 PushOnScopeChains(PDecl, TUScope, false);
433 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000434 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000435 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000436 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000437 if (!isNew)
438 PDecl->setChangedSinceDeserialization(true);
439 }
Chris Lattner4d391482007-12-12 07:09:47 +0000440 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000441 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
444 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000445 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000446 Protocols.data(), Protocols.size(),
447 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000448 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000449 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000450 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000451}
452
John McCalld226f652010-08-21 09:40:31 +0000453Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000454ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
455 IdentifierInfo *ClassName, SourceLocation ClassLoc,
456 IdentifierInfo *CategoryName,
457 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000458 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000459 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000460 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000461 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000462 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000463 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000464
465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl()) {
467 // Create an invalid ObjCCategoryDecl to serve as context for
468 // the enclosing method declarations. We mark the decl invalid
469 // to make it clear that this isn't a valid AST.
470 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
471 ClassLoc, CategoryLoc, CategoryName);
472 CDecl->setInvalidDecl();
473 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000474 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000475 }
476
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000477 if (!CategoryName && IDecl->getImplementation()) {
478 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
479 Diag(IDecl->getImplementation()->getLocation(),
480 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000481 }
482
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000483 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
484 ClassLoc, CategoryLoc, CategoryName);
485 // FIXME: PushOnScopeChains?
486 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000487
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000488 CDecl->setClassInterface(IDecl);
489 // Insert class extension to the list of class's categories.
490 if (!CategoryName)
491 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner16b34b42009-02-16 21:30:01 +0000493 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000494 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000495
Fariborz Jahanian25760612010-02-15 21:55:26 +0000496 if (CategoryName) {
497 /// Check for duplicate interface declaration for this category
498 ObjCCategoryDecl *CDeclChain;
499 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
500 CDeclChain = CDeclChain->getNextClassCategory()) {
501 if (CDeclChain->getIdentifier() == CategoryName) {
502 // Class extensions can be declared multiple times.
503 Diag(CategoryLoc, diag::warn_dup_category_def)
504 << ClassName << CategoryName;
505 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
506 break;
507 }
Chris Lattner70f19542009-02-16 21:26:43 +0000508 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000509 if (!CDeclChain)
510 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000511 }
Chris Lattner70f19542009-02-16 21:26:43 +0000512
Chris Lattner4d391482007-12-12 07:09:47 +0000513 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000514 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000515 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000516 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000517 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000518 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000519 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Anders Carlsson15281452008-11-04 16:57:32 +0000522 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000523 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000524}
525
526/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000528/// object.
John McCalld226f652010-08-21 09:40:31 +0000529Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000530 SourceLocation AtCatImplLoc,
531 IdentifierInfo *ClassName, SourceLocation ClassLoc,
532 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000533 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000534 ObjCCategoryDecl *CatIDecl = 0;
535 if (IDecl) {
536 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
537 if (!CatIDecl) {
538 // Category @implementation with no corresponding @interface.
539 // Create and install one.
540 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000541 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000542 CatName);
543 CatIDecl->setClassInterface(IDecl);
544 CatIDecl->insertNextClassCategory();
545 }
546 }
547
Mike Stump1eb44332009-09-09 15:08:12 +0000548 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000549 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
550 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000551 /// Check that class of this category is already completely declared.
552 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000553 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000554
Douglas Gregord0434102009-01-09 00:49:46 +0000555 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000556 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000557
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000558 /// Check that CatName, category name, is not used in another implementation.
559 if (CatIDecl) {
560 if (CatIDecl->getImplementation()) {
561 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
562 << CatName;
563 Diag(CatIDecl->getImplementation()->getLocation(),
564 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000565 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000566 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000567 // Warn on implementating category of deprecated class under
568 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000569 DiagnoseObjCImplementedDeprecations(*this,
570 dyn_cast<NamedDecl>(IDecl),
571 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000572 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Anders Carlsson15281452008-11-04 16:57:32 +0000575 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000576 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000577}
578
John McCalld226f652010-08-21 09:40:31 +0000579Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000580 SourceLocation AtClassImplLoc,
581 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000582 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000583 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000584 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000585 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000586 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000587 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
588 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000591 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000592 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
593 // If this is a forward declaration of an interface, warn.
594 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000595 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000596 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000597 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000598 } else {
599 // We did not find anything with the name ClassName; try to correct for
600 // typos in the class name.
601 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000602 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000603 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000604 // Suggest the (potentially) correct interface name. However, put the
605 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000606 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000607 // provide a code-modification hint or use the typo name for recovery,
608 // because this is just a warning. The program may actually be correct.
609 Diag(ClassLoc, diag::warn_undef_interface_suggest)
610 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000611 Diag(IDecl->getLocation(), diag::note_previous_decl)
612 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000613 << FixItHint::CreateReplacement(ClassLoc,
614 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000615 IDecl = 0;
616 } else {
617 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
618 }
Chris Lattner4d391482007-12-12 07:09:47 +0000619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner4d391482007-12-12 07:09:47 +0000621 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000623 if (SuperClassname) {
624 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000625 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
626 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
629 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000630 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000632 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 Diag(SuperClassLoc, diag::err_undef_superclass)
635 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000636 else if (IDecl && IDecl->getSuperClass() != SDecl) {
637 // This implementation and its interface do not have the same
638 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000640 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000641 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000642 }
643 }
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattner4d391482007-12-12 07:09:47 +0000646 if (!IDecl) {
647 // Legacy case of @implementation with no corresponding @interface.
648 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000649
Mike Stump390b4cc2009-05-16 07:39:55 +0000650 // FIXME: Do we support attributes on the @implementation? If so we should
651 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000652 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000653 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000654 IDecl->setSuperClass(SDecl);
655 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000656
657 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000658 } else {
659 // Mark the interface as being completed, even if it was just as
660 // @class ....;
661 // declaration; the user cannot reopen it.
662 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
665 ObjCImplementationDecl* IMPDecl =
666 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000667 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Anders Carlsson15281452008-11-04 16:57:32 +0000669 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000670 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner4d391482007-12-12 07:09:47 +0000672 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000673 if (IDecl->getImplementation()) {
674 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000676 Diag(IDecl->getImplementation()->getLocation(),
677 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000678 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000679 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000680 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000681 // Warn on implementating deprecated class under
682 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000683 DiagnoseObjCImplementedDeprecations(*this,
684 dyn_cast<NamedDecl>(IDecl),
685 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000686 }
John McCalld226f652010-08-21 09:40:31 +0000687 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000688}
689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
691 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000692 SourceLocation RBrace) {
693 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000694 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000695 if (!IDecl)
696 return;
697 /// Check case of non-existing @interface decl.
698 /// (legacy objective-c @implementation decl without an @interface decl).
699 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000700 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000701 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000702 // Add ivar's to class's DeclContext.
703 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000704 ivars[i]->setLexicalDeclContext(ImpDecl);
705 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000706 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000707 }
708
Chris Lattner4d391482007-12-12 07:09:47 +0000709 return;
710 }
711 // If implementation has empty ivar list, just return.
712 if (numIvars == 0)
713 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner4d391482007-12-12 07:09:47 +0000715 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000716 if (LangOpts.ObjCNonFragileABI2) {
717 if (ImpDecl->getSuperClass())
718 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
719 for (unsigned i = 0; i < numIvars; i++) {
720 ObjCIvarDecl* ImplIvar = ivars[i];
721 if (const ObjCIvarDecl *ClsIvar =
722 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
723 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
724 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
725 continue;
726 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000727 // Instance ivar to Implementation's DeclContext.
728 ImplIvar->setLexicalDeclContext(ImpDecl);
729 IDecl->makeDeclVisibleInContext(ImplIvar, false);
730 ImpDecl->addDecl(ImplIvar);
731 }
732 return;
733 }
Chris Lattner4d391482007-12-12 07:09:47 +0000734 // Check interface's Ivar list against those in the implementation.
735 // names and types must match.
736 //
Chris Lattner4d391482007-12-12 07:09:47 +0000737 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000738 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000739 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
740 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 ObjCIvarDecl* ImplIvar = ivars[j++];
742 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000743 assert (ImplIvar && "missing implementation ivar");
744 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Steve Naroffca331292009-03-03 14:49:36 +0000746 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000747 if (Context.getCanonicalType(ImplIvar->getType()) !=
748 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000750 << ImplIvar->getIdentifier()
751 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000752 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000753 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
754 Expr *ImplBitWidth = ImplIvar->getBitWidth();
755 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000756 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
757 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000758 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
759 << ImplIvar->getIdentifier();
760 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762 }
Steve Naroffca331292009-03-03 14:49:36 +0000763 // Make sure the names are identical.
764 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000765 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000766 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000767 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000768 }
769 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattner609e4c72007-12-12 18:11:49 +0000772 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000773 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000774 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000775 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000776}
777
Steve Naroff3c2eb662008-02-10 21:38:56 +0000778void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000779 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000780 if (!IncompleteImpl) {
781 Diag(ImpLoc, diag::warn_incomplete_impl);
782 IncompleteImpl = true;
783 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000784 if (DiagID == diag::warn_unimplemented_protocol_method)
785 Diag(ImpLoc, DiagID) << method->getDeclName();
786 else
787 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000788}
789
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000790/// Determines if type B can be substituted for type A. Returns true if we can
791/// guarantee that anything that the user will do to an object of type A can
792/// also be done to an object of type B. This is trivially true if the two
793/// types are the same, or if B is a subclass of A. It becomes more complex
794/// in cases where protocols are involved.
795///
796/// Object types in Objective-C describe the minimum requirements for an
797/// object, rather than providing a complete description of a type. For
798/// example, if A is a subclass of B, then B* may refer to an instance of A.
799/// The principle of substitutability means that we may use an instance of A
800/// anywhere that we may use an instance of B - it will implement all of the
801/// ivars of B and all of the methods of B.
802///
803/// This substitutability is important when type checking methods, because
804/// the implementation may have stricter type definitions than the interface.
805/// The interface specifies minimum requirements, but the implementation may
806/// have more accurate ones. For example, a method may privately accept
807/// instances of B, but only publish that it accepts instances of A. Any
808/// object passed to it will be type checked against B, and so will implicitly
809/// by a valid A*. Similarly, a method may return a subclass of the class that
810/// it is declared as returning.
811///
812/// This is most important when considering subclassing. A method in a
813/// subclass must accept any object as an argument that its superclass's
814/// implementation accepts. It may, however, accept a more general type
815/// without breaking substitutability (i.e. you can still use the subclass
816/// anywhere that you can use the superclass, but not vice versa). The
817/// converse requirement applies to return types: the return type for a
818/// subclass method must be a valid object of the kind that the superclass
819/// advertises, but it may be specified more accurately. This avoids the need
820/// for explicit down-casting by callers.
821///
822/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000823static bool isObjCTypeSubstitutable(ASTContext &Context,
824 const ObjCObjectPointerType *A,
825 const ObjCObjectPointerType *B,
826 bool rejectId) {
827 // Reject a protocol-unqualified id.
828 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000829
830 // If B is a qualified id, then A must also be a qualified id and it must
831 // implement all of the protocols in B. It may not be a qualified class.
832 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
833 // stricter definition so it is not substitutable for id<A>.
834 if (B->isObjCQualifiedIdType()) {
835 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000836 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
837 QualType(B,0),
838 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000839 }
840
841 /*
842 // id is a special type that bypasses type checking completely. We want a
843 // warning when it is used in one place but not another.
844 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
845
846
847 // If B is a qualified id, then A must also be a qualified id (which it isn't
848 // if we've got this far)
849 if (B->isObjCQualifiedIdType()) return false;
850 */
851
852 // Now we know that A and B are (potentially-qualified) class types. The
853 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000854 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000855}
856
John McCall10302c02010-10-28 02:34:38 +0000857static SourceRange getTypeRange(TypeSourceInfo *TSI) {
858 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
859}
860
861static void CheckMethodOverrideReturn(Sema &S,
862 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000863 ObjCMethodDecl *MethodDecl,
864 bool IsProtocolMethodDecl) {
865 if (IsProtocolMethodDecl &&
866 (MethodDecl->getObjCDeclQualifier() !=
867 MethodImpl->getObjCDeclQualifier())) {
868 S.Diag(MethodImpl->getLocation(),
869 diag::warn_conflicting_ret_type_modifiers)
870 << MethodImpl->getDeclName()
871 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
872 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
873 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
874 }
875
John McCall10302c02010-10-28 02:34:38 +0000876 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000877 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +0000878 return;
879
880 unsigned DiagID = diag::warn_conflicting_ret_types;
881
882 // Mismatches between ObjC pointers go into a different warning
883 // category, and sometimes they're even completely whitelisted.
884 if (const ObjCObjectPointerType *ImplPtrTy =
885 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
886 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000887 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +0000888 // Allow non-matching return types as long as they don't violate
889 // the principle of substitutability. Specifically, we permit
890 // return types that are subclasses of the declared return type,
891 // or that are more-qualified versions of the declared type.
892 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
893 return;
894
895 DiagID = diag::warn_non_covariant_ret_types;
896 }
897 }
898
899 S.Diag(MethodImpl->getLocation(), DiagID)
900 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000901 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +0000902 << MethodImpl->getResultType()
903 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000904 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
905 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +0000906}
907
908static void CheckMethodOverrideParam(Sema &S,
909 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000910 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +0000911 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000912 ParmVarDecl *IfaceVar,
913 bool IsProtocolMethodDecl) {
914 if (IsProtocolMethodDecl &&
915 (ImplVar->getObjCDeclQualifier() !=
916 IfaceVar->getObjCDeclQualifier())) {
917 S.Diag(ImplVar->getLocation(),
918 diag::warn_conflicting_param_modifiers)
919 << getTypeRange(ImplVar->getTypeSourceInfo())
920 << MethodImpl->getDeclName();
921 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
922 << getTypeRange(IfaceVar->getTypeSourceInfo());
923 }
924
John McCall10302c02010-10-28 02:34:38 +0000925 QualType ImplTy = ImplVar->getType();
926 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000927
John McCall10302c02010-10-28 02:34:38 +0000928 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
929 return;
930
931 unsigned DiagID = diag::warn_conflicting_param_types;
932
933 // Mismatches between ObjC pointers go into a different warning
934 // category, and sometimes they're even completely whitelisted.
935 if (const ObjCObjectPointerType *ImplPtrTy =
936 ImplTy->getAs<ObjCObjectPointerType>()) {
937 if (const ObjCObjectPointerType *IfacePtrTy =
938 IfaceTy->getAs<ObjCObjectPointerType>()) {
939 // Allow non-matching argument types as long as they don't
940 // violate the principle of substitutability. Specifically, the
941 // implementation must accept any objects that the superclass
942 // accepts, however it may also accept others.
943 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
944 return;
945
946 DiagID = diag::warn_non_contravariant_param_types;
947 }
948 }
949
950 S.Diag(ImplVar->getLocation(), DiagID)
951 << getTypeRange(ImplVar->getTypeSourceInfo())
952 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
953 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
954 << getTypeRange(IfaceVar->getTypeSourceInfo());
955}
956
957
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000958void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000959 ObjCMethodDecl *MethodDecl,
960 bool IsProtocolMethodDecl) {
961 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
962 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattner3aff9192009-04-11 19:58:42 +0000964 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000965 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +0000966 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000967 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
968 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000970 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000971 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000972 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000973 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000974}
975
Mike Stump390b4cc2009-05-16 07:39:55 +0000976/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
977/// improve the efficiency of selector lookups and type checking by associating
978/// with each protocol / interface / category the flattened instance tables. If
979/// we used an immutable set to keep the table then it wouldn't add significant
980/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000981
Steve Naroffefe7f362008-02-08 22:06:17 +0000982/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000983/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000984void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
985 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000986 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000987 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000988 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000989 ObjCContainerDecl *CDecl) {
990 ObjCInterfaceDecl *IDecl;
991 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
992 IDecl = C->getClassInterface();
993 else
994 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
995 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
996
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000997 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000998 ObjCInterfaceDecl *NSIDecl = 0;
999 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001000 // check to see if class implements forwardInvocation method and objects
1001 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001002 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001003 // Under such conditions, which means that every method possible is
1004 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001005 // found" warnings.
1006 // FIXME: Use a general GetUnarySelector method for this.
1007 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1008 Selector fISelector = Context.Selectors.getSelector(1, &II);
1009 if (InsMap.count(fISelector))
1010 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1011 // need be implemented in the implementation.
1012 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001015 // If a method lookup fails locally we still need to look and see if
1016 // the method was implemented by a base class or an inherited
1017 // protocol. This lookup is slow, but occurs rarely in correct code
1018 // and otherwise would terminate in a warning.
1019
Chris Lattner4d391482007-12-12 07:09:47 +00001020 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001021 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001022 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001023 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001024 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001025 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001026 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001027 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001028 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001029 // Ugly, but necessary. Method declared in protcol might have
1030 // have been synthesized due to a property declared in the class which
1031 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001032 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001034 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001035 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001036 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1037 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001038 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001039 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001040 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1041 << PDecl->getDeclName();
1042 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001043 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001044 }
1045 }
Chris Lattner4d391482007-12-12 07:09:47 +00001046 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001047 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001048 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001049 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001051 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1052 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001053 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001054 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001055 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001056 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001057 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001058 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1059 PDecl->getDeclName();
1060 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001061 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001062 }
Chris Lattner780f3292008-07-21 21:32:27 +00001063 // Check on this protocols's referenced protocols, recursively.
1064 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1065 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001066 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001067}
1068
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001069/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1070/// or protocol against those declared in their implementations.
1071///
1072void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1073 const llvm::DenseSet<Selector> &ClsMap,
1074 llvm::DenseSet<Selector> &InsMapSeen,
1075 llvm::DenseSet<Selector> &ClsMapSeen,
1076 ObjCImplDecl* IMPDecl,
1077 ObjCContainerDecl* CDecl,
1078 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001079 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001080 // Check and see if instance methods in class interface have been
1081 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001082 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1083 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001084 if (InsMapSeen.count((*I)->getSelector()))
1085 continue;
1086 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001087 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001088 !InsMap.count((*I)->getSelector())) {
1089 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001090 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1091 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001092 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001093 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001094 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001096 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001097 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001098 assert(MethodDecl &&
1099 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001100 // ImpMethodDecl may be null as in a @dynamic property.
1101 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001102 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1103 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001104 }
1105 }
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001107 // Check and see if class methods in class interface have been
1108 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001109 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001110 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001111 if (ClsMapSeen.count((*I)->getSelector()))
1112 continue;
1113 ClsMapSeen.insert((*I)->getSelector());
1114 if (!ClsMap.count((*I)->getSelector())) {
1115 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001116 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1117 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001118 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001119 ObjCMethodDecl *ImpMethodDecl =
1120 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001121 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001122 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001123 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1124 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001125 }
1126 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001127
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001128 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001129 // Also methods in class extensions need be looked at next.
1130 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1131 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1132 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1133 IMPDecl,
1134 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1135 IncompleteImpl, false);
1136
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001137 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001138 for (ObjCInterfaceDecl::all_protocol_iterator
1139 PI = I->all_referenced_protocol_begin(),
1140 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001141 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1142 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001143 (*PI), IncompleteImpl, false);
1144 if (I->getSuperClass())
1145 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001146 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001147 I->getSuperClass(), IncompleteImpl, false);
1148 }
1149}
1150
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001151void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001152 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001153 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001154 llvm::DenseSet<Selector> InsMap;
1155 // Check and see if instance methods in class interface have been
1156 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001157 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001158 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001159 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001161 // Check and see if properties declared in the interface have either 1)
1162 // an implementation or 2) there is a @synthesize/@dynamic implementation
1163 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001164 if (isa<ObjCInterfaceDecl>(CDecl) &&
1165 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001166 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001167
Chris Lattner4d391482007-12-12 07:09:47 +00001168 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001169 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001170 I = IMPDecl->classmeth_begin(),
1171 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001172 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001174 // Check for type conflict of methods declared in a class/protocol and
1175 // its implementation; if any.
1176 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001177 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1178 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001179 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Chris Lattner4d391482007-12-12 07:09:47 +00001181 // Check the protocol list for unimplemented methods in the @implementation
1182 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001183 // Check and see if class methods in class interface have been
1184 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattnercddc8882009-03-01 00:56:52 +00001186 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001187 for (ObjCInterfaceDecl::all_protocol_iterator
1188 PI = I->all_referenced_protocol_begin(),
1189 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001190 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001191 InsMap, ClsMap, I);
1192 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001193 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1194 Categories; Categories = Categories->getNextClassExtension())
1195 ImplMethodsVsClassMethods(S, IMPDecl,
1196 const_cast<ObjCCategoryDecl*>(Categories),
1197 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001198 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001199 // For extended class, unimplemented methods in its protocols will
1200 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001201 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001202 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1203 E = C->protocol_end(); PI != E; ++PI)
1204 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001205 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001206 // Report unimplemented properties in the category as well.
1207 // When reporting on missing setter/getters, do not report when
1208 // setter/getter is implemented in category's primary class
1209 // implementation.
1210 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1211 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1212 for (ObjCImplementationDecl::instmeth_iterator
1213 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1214 InsMap.insert((*I)->getSelector());
1215 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001216 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001217 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001218 } else
1219 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001220}
1221
Mike Stump1eb44332009-09-09 15:08:12 +00001222/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001223Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001224Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001225 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001226 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001227 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001228 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Chris Lattner4d391482007-12-12 07:09:47 +00001230 for (unsigned i = 0; i != NumElts; ++i) {
1231 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001232 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001233 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001234 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001235 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001236 // Maybe we will complain about the shadowed template parameter.
1237 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1238 // Just pretend that we didn't see the previous declaration.
1239 PrevDecl = 0;
1240 }
1241
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001242 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001243 // GCC apparently allows the following idiom:
1244 //
1245 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1246 // @class XCElementToggler;
1247 //
Mike Stump1eb44332009-09-09 15:08:12 +00001248 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001249 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001250 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001251 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001252 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001253 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001254 // a forward class declaration matching a typedef name of a class refers
1255 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001256 if (const ObjCObjectType *OI =
1257 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1258 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001259 }
Chris Lattner4d391482007-12-12 07:09:47 +00001260 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001261 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1262 if (!IDecl) { // Not already seen? Make a forward decl.
1263 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1264 IdentList[i], IdentLocs[i], true);
1265
1266 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1267 // the current DeclContext. This prevents clients that walk DeclContext
1268 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1269 // declared later (if at all). We also take care to explicitly make
1270 // sure this declaration is visible for name lookup.
1271 PushOnScopeChains(IDecl, TUScope, false);
1272 CurContext->makeDeclVisibleInContext(IDecl, true);
1273 }
Chris Lattner4d391482007-12-12 07:09:47 +00001274
1275 Interfaces.push_back(IDecl);
1276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenek321c22f2009-11-18 00:28:11 +00001278 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001279 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001280 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001281 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001282 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001283 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001284 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001285}
1286
1287
1288/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1289/// returns true, or false, accordingly.
1290/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001291bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001292 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001293 bool matchBasedOnSizeAndAlignment,
1294 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001295 QualType T1 = Context.getCanonicalType(Method->getResultType());
1296 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001298 if (T1 != T2) {
1299 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001300 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001301 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001302 // Incomplete types don't have a size and alignment.
1303 if (T1->isIncompleteType() || T2->isIncompleteType())
1304 return false;
1305 // Check is based on size and alignment.
1306 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1307 return false;
1308 }
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Chris Lattner89951a82009-02-20 18:43:26 +00001310 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1311 E = Method->param_end();
1312 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattner89951a82009-02-20 18:43:26 +00001314 for (; ParamI != E; ++ParamI, ++PrevI) {
1315 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1316 T1 = Context.getCanonicalType((*ParamI)->getType());
1317 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001318 if (T1 != T2) {
1319 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001320 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001321 return false;
1322 // Incomplete types don't have a size and alignment.
1323 if (T1->isIncompleteType() || T2->isIncompleteType())
1324 return false;
1325 // Check is based on size and alignment.
1326 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1327 return false;
1328 }
Chris Lattner4d391482007-12-12 07:09:47 +00001329 }
1330 return true;
1331}
1332
Sebastian Redldb9d2142010-08-02 23:18:59 +00001333/// \brief Read the contents of the method pool for a given selector from
1334/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001335///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001336/// This routine should only be called once, when the method pool has no entry
1337/// for this selector.
1338Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001339 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001340 assert(MethodPool.find(Sel) == MethodPool.end() &&
1341 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001342
1343 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001344 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Sebastian Redldb9d2142010-08-02 23:18:59 +00001346 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001347}
1348
Sebastian Redldb9d2142010-08-02 23:18:59 +00001349void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1350 bool instance) {
1351 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1352 if (Pos == MethodPool.end()) {
1353 if (ExternalSource)
1354 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001355 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001356 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1357 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001358 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001359 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001360 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001361 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001362 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001363 Entry.Method = Method;
1364 Entry.Next = 0;
1365 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001366 }
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Chris Lattnerb25df352009-03-04 05:16:45 +00001368 // We've seen a method with this name, see if we have already seen this type
1369 // signature.
1370 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001371 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001372 ObjCMethodDecl *PrevObjCMethod = List->Method;
1373 PrevObjCMethod->setDefined(impl);
1374 // If a method is deprecated, push it in the global pool.
1375 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001376 if (Method->isDeprecated()) {
1377 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001378 List->Method = Method;
1379 }
1380 // If new method is unavailable, push it into global pool
1381 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001382 if (Method->isUnavailable()) {
1383 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001384 List->Method = Method;
1385 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001386 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Chris Lattnerb25df352009-03-04 05:16:45 +00001389 // We have a new signature for an existing method - add it.
1390 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001391 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1392 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001393}
1394
Sebastian Redldb9d2142010-08-02 23:18:59 +00001395ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001396 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001397 bool warn, bool instance) {
1398 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1399 if (Pos == MethodPool.end()) {
1400 if (ExternalSource)
1401 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001402 else
1403 return 0;
1404 }
1405
Sebastian Redldb9d2142010-08-02 23:18:59 +00001406 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001408 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001409 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1410 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001411 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001412 if (warn && MethList.Method && MethList.Next) {
1413 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001414 if (strictSelectorMatch)
1415 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1416 // This checks if the methods differ in type mismatch.
1417 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1418 issueWarning = true;
1419 }
1420
1421 if (!issueWarning)
1422 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1423 // This checks if the methods differ by size & alignment.
1424 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1425 issueWarning = true;
1426 }
1427
Sebastian Redldb9d2142010-08-02 23:18:59 +00001428 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001429 if (strictSelectorMatch)
1430 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1431 else
1432 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001433 Diag(MethList.Method->getLocStart(), diag::note_using)
1434 << MethList.Method->getSourceRange();
1435 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1436 Diag(Next->Method->getLocStart(), diag::note_also_found)
1437 << Next->Method->getSourceRange();
1438 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001439 }
1440 return MethList.Method;
1441}
1442
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001443ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001444 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1445 if (Pos == MethodPool.end())
1446 return 0;
1447
1448 GlobalMethods &Methods = Pos->second;
1449
1450 if (Methods.first.Method && Methods.first.Method->isDefined())
1451 return Methods.first.Method;
1452 if (Methods.second.Method && Methods.second.Method->isDefined())
1453 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001454 return 0;
1455}
1456
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001457/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1458/// identical selector names in current and its super classes and issues
1459/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001460void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1461 ObjCMethodDecl *Method,
1462 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001463 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1464 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001466 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001467 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001468 SD->lookupMethod(Method->getSelector(), IsInstance);
1469 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001470 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001471 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001472 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001473 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1474 E = Method->param_end();
1475 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1476 for (; ParamI != E; ++ParamI, ++PrevI) {
1477 // Number of parameters are the same and is guaranteed by selector match.
1478 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1479 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1480 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001481 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001482 // respective argument type in the super class method, issue warning;
1483 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001484 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001485 << T1 << T2;
1486 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1487 return;
1488 }
1489 }
1490 ID = SD;
1491 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001492}
1493
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001494/// DiagnoseDuplicateIvars -
1495/// Check for duplicate ivars in the entire class at the start of
1496/// @implementation. This becomes necesssary because class extension can
1497/// add ivars to a class in random order which will not be known until
1498/// class's @implementation is seen.
1499void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1500 ObjCInterfaceDecl *SID) {
1501 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1502 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1503 ObjCIvarDecl* Ivar = (*IVI);
1504 if (Ivar->isInvalidDecl())
1505 continue;
1506 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1507 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1508 if (prevIvar) {
1509 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1510 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1511 Ivar->setInvalidDecl();
1512 }
1513 }
1514 }
1515}
1516
Steve Naroffa56f6162007-12-18 01:30:32 +00001517// Note: For class/category implemenations, allMethods/allProperties is
1518// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001519void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001520 Decl *ClassDecl,
1521 Decl **allMethods, unsigned allNum,
1522 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001523 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001524 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1525 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001526 // should be true.
1527 if (!ClassDecl)
1528 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001529
Mike Stump1eb44332009-09-09 15:08:12 +00001530 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001531 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1532 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001533 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001534
Ted Kremenek782f2f52010-01-07 01:20:12 +00001535 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1536 // FIXME: This is wrong. We shouldn't be pretending that there is
1537 // an '@end' in the declaration.
1538 SourceLocation L = ClassDecl->getLocation();
1539 AtEnd.setBegin(L);
1540 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001541 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001542 }
1543
Steve Naroff0701bbb2009-01-08 17:28:14 +00001544 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1545 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1546 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1547
Chris Lattner4d391482007-12-12 07:09:47 +00001548 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001549 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001550 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001551
1552 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001553 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001554 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001555 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001556 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001557 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001558 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001559 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001560 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001561 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001562 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001563 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001564 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001565 InsMap[Method->getSelector()] = Method;
1566 /// The following allows us to typecheck messages to "id".
1567 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001568 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001569 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001570 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001571 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001572 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001573 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001574 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001575 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001576 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001577 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001578 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001579 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001580 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001581 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001582 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001583 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001584 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001585 /// The following allows us to typecheck messages to "Class".
1586 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001587 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001588 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001589 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001590 }
1591 }
1592 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001593 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001594 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001595 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001596 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001597 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001598 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001599 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001600 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001601 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001602
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001603 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001604 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001605 if (C->IsClassExtension()) {
1606 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1607 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001608 }
Chris Lattner4d391482007-12-12 07:09:47 +00001609 }
Steve Naroff09c47192009-01-09 15:36:25 +00001610 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001611 if (CDecl->getIdentifier())
1612 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1613 // user-defined setter/getter. It also synthesizes setter/getter methods
1614 // and adds them to the DeclContext and global method pools.
1615 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1616 E = CDecl->prop_end();
1617 I != E; ++I)
1618 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001619 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001620 }
1621 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001622 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001623 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001624 // Any property declared in a class extension might have user
1625 // declared setter or getter in current class extension or one
1626 // of the other class extensions. Mark them as synthesized as
1627 // property will be synthesized when property with same name is
1628 // seen in the @implementation.
1629 for (const ObjCCategoryDecl *ClsExtDecl =
1630 IDecl->getFirstClassExtension();
1631 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1632 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1633 E = ClsExtDecl->prop_end(); I != E; ++I) {
1634 ObjCPropertyDecl *Property = (*I);
1635 // Skip over properties declared @dynamic
1636 if (const ObjCPropertyImplDecl *PIDecl
1637 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1638 if (PIDecl->getPropertyImplementation()
1639 == ObjCPropertyImplDecl::Dynamic)
1640 continue;
1641
1642 for (const ObjCCategoryDecl *CExtDecl =
1643 IDecl->getFirstClassExtension();
1644 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1645 if (ObjCMethodDecl *GetterMethod =
1646 CExtDecl->getInstanceMethod(Property->getGetterName()))
1647 GetterMethod->setSynthesized(true);
1648 if (!Property->isReadOnly())
1649 if (ObjCMethodDecl *SetterMethod =
1650 CExtDecl->getInstanceMethod(Property->getSetterName()))
1651 SetterMethod->setSynthesized(true);
1652 }
1653 }
1654 }
1655
Ted Kremenekc32647d2010-12-23 21:35:43 +00001656 if (LangOpts.ObjCDefaultSynthProperties &&
1657 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001658 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001659 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001660 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001661
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001662 if (LangOpts.ObjCNonFragileABI2)
1663 while (IDecl->getSuperClass()) {
1664 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1665 IDecl = IDecl->getSuperClass();
1666 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001667 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001668 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001669 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001670 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001671 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Chris Lattner4d391482007-12-12 07:09:47 +00001673 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001674 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001675 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001677 Categories; Categories = Categories->getNextClassCategory()) {
1678 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001679 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001680 break;
1681 }
1682 }
1683 }
1684 }
Chris Lattner682bf922009-03-29 16:50:03 +00001685 if (isInterfaceDeclKind) {
1686 // Reject invalid vardecls.
1687 for (unsigned i = 0; i != tuvNum; i++) {
1688 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1689 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1690 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001691 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001692 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001693 }
Chris Lattner682bf922009-03-29 16:50:03 +00001694 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001695 }
Chris Lattner4d391482007-12-12 07:09:47 +00001696}
1697
1698
1699/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1700/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001701static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001702CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00001703 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00001704}
1705
Ted Kremenek422bae72010-04-18 04:59:38 +00001706static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001707bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001708 // The 'ibaction' attribute is allowed on method definitions because of
1709 // how the IBAction macro is used on both method declarations and definitions.
1710 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001711 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1712 if ((*i)->getKind() != attr::IBAction)
1713 return true;
1714 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001715}
1716
John McCalld226f652010-08-21 09:40:31 +00001717Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001718 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001719 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001720 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001721 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001722 Selector Sel,
1723 // optional arguments. The number of types/arguments is obtained
1724 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001725 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001726 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001727 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001728 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001729 // Make sure we can establish a context for the method.
1730 if (!ClassDecl) {
1731 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00001732 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001733 }
Chris Lattner4d391482007-12-12 07:09:47 +00001734 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001736 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001737 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001738 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Steve Naroffccef3712009-02-20 22:59:16 +00001740 // Methods cannot return interface types. All ObjC objects are
1741 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001742 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001743 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1744 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001745 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001746 }
1747 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001748 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001749
1750 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001751 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001752 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001753 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001754 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001755 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001756 MethodDeclKind == tok::objc_optional ?
1757 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001758 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Chris Lattner0ed844b2008-04-04 06:12:32 +00001760 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Chris Lattner7db638d2009-04-11 19:42:43 +00001762 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001763 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001764 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Chris Lattnere294d3f2009-04-11 18:57:04 +00001766 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001767 ArgType = Context.getObjCIdType();
1768 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001769 } else {
John McCall58e46772009-10-23 21:48:59 +00001770 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001771 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001772 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001773 }
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001775 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1776 LookupOrdinaryName, ForRedeclaration);
1777 LookupName(R, S);
1778 if (R.isSingleResult()) {
1779 NamedDecl *PrevDecl = R.getFoundDecl();
1780 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001781 Diag(ArgInfo[i].NameLoc,
1782 (MethodDefinition ? diag::warn_method_param_redefinition
1783 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001784 << ArgInfo[i].Name;
1785 Diag(PrevDecl->getLocation(),
1786 diag::note_previous_declaration);
1787 }
1788 }
1789
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001790 SourceLocation StartLoc = DI
1791 ? DI->getTypeLoc().getBeginLoc()
1792 : ArgInfo[i].NameLoc;
1793
John McCall81ef3e62011-04-23 02:46:06 +00001794 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
1795 ArgInfo[i].NameLoc, ArgInfo[i].Name,
1796 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00001797
John McCall70798862011-05-02 00:30:12 +00001798 Param->setObjCMethodScopeInfo(i);
1799
Chris Lattner0ed844b2008-04-04 06:12:32 +00001800 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001801 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001803 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001804 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001806 S->AddDecl(Param);
1807 IdResolver.AddDecl(Param);
1808
Chris Lattner0ed844b2008-04-04 06:12:32 +00001809 Params.push_back(Param);
1810 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001811
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001812 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001813 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001814 QualType ArgType = Param->getType();
1815 if (ArgType.isNull())
1816 ArgType = Context.getObjCIdType();
1817 else
1818 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1819 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001820 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001821 Diag(Param->getLocation(),
1822 diag::err_object_cannot_be_passed_returned_by_value)
1823 << 1 << ArgType;
1824 Param->setInvalidDecl();
1825 }
1826 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001827
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001828 Params.push_back(Param);
1829 }
1830
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001831 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1832 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001833 ObjCMethod->setObjCDeclQualifier(
1834 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1835 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001836
1837 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001838 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001839
John McCall54abf7d2009-11-04 02:18:39 +00001840 const ObjCMethodDecl *InterfaceMD = 0;
1841
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001842 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00001843 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001844 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001845 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001846 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1847 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001848 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001849 PrevMethod = ImpDecl->getClassMethod(Sel);
1850 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001851 }
John McCall54abf7d2009-11-04 02:18:39 +00001852 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1853 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001854 if (ObjCMethod->hasAttrs() &&
1855 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001856 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001857 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001858 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001859 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001860 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1861 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001862 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001863 PrevMethod = CatImpDecl->getClassMethod(Sel);
1864 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001865 }
Sean Huntcf807c42010-08-18 23:23:40 +00001866 if (ObjCMethod->hasAttrs() &&
1867 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001868 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001869 } else {
1870 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001871 }
1872 if (PrevMethod) {
1873 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001874 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001875 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001876 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001877 }
John McCall54abf7d2009-11-04 02:18:39 +00001878
John McCalleca5d222011-03-02 04:00:57 +00001879 // Merge information down from the interface declaration if we have one.
Sean Huntcf807c42010-08-18 23:23:40 +00001880 if (InterfaceMD)
John McCalleca5d222011-03-02 04:00:57 +00001881 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
John McCall54abf7d2009-11-04 02:18:39 +00001882
John McCalld226f652010-08-21 09:40:31 +00001883 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001884}
1885
Chris Lattnercc98eac2008-12-17 07:13:27 +00001886bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001887 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001888 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Anders Carlsson15281452008-11-04 16:57:32 +00001890 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1891 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Anders Carlsson15281452008-11-04 16:57:32 +00001893 return true;
1894}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001895
Chris Lattnercc98eac2008-12-17 07:13:27 +00001896/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1897/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001898void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001899 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001900 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001901 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001902 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001903 if (!Class) {
1904 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1905 return;
1906 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001907 if (LangOpts.ObjCNonFragileABI) {
1908 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1909 return;
1910 }
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattnercc98eac2008-12-17 07:13:27 +00001912 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001913 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1914 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001915 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001916 for (unsigned i = 0; i < Ivars.size(); i++) {
1917 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001918 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001919 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
1920 /*FIXME: StartL=*/ID->getLocation(),
1921 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00001922 ID->getIdentifier(), ID->getType(),
1923 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001924 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Chris Lattnercc98eac2008-12-17 07:13:27 +00001927 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001928 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001929 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001930 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001931 if (getLangOptions().CPlusPlus)
1932 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001933 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001934 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001935 }
1936}
1937
Douglas Gregor160b5632010-04-26 17:32:49 +00001938/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001939VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
1940 SourceLocation StartLoc,
1941 SourceLocation IdLoc,
1942 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00001943 bool Invalid) {
1944 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1945 // duration shall not be qualified by an address-space qualifier."
1946 // Since all parameters have automatic store duration, they can not have
1947 // an address space.
1948 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001949 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00001950 Invalid = true;
1951 }
1952
1953 // An @catch parameter must be an unqualified object pointer type;
1954 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1955 if (Invalid) {
1956 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001957 } else if (T->isDependentType()) {
1958 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001959 } else if (!T->isObjCObjectPointerType()) {
1960 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001961 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00001962 } else if (T->isObjCQualifiedIdType()) {
1963 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001964 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00001965 }
1966
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001967 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
1968 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00001969 New->setExceptionVariable(true);
1970
Douglas Gregor160b5632010-04-26 17:32:49 +00001971 if (Invalid)
1972 New->setInvalidDecl();
1973 return New;
1974}
1975
John McCalld226f652010-08-21 09:40:31 +00001976Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001977 const DeclSpec &DS = D.getDeclSpec();
1978
1979 // We allow the "register" storage class on exception variables because
1980 // GCC did, but we drop it completely. Any other storage class is an error.
1981 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1982 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1983 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1984 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1985 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1986 << DS.getStorageClassSpec();
1987 }
1988 if (D.getDeclSpec().isThreadSpecified())
1989 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1990 D.getMutableDeclSpec().ClearStorageClassSpecs();
1991
1992 DiagnoseFunctionSpecifiers(D);
1993
1994 // Check that there are no default arguments inside the type of this
1995 // exception object (C++ only).
1996 if (getLangOptions().CPlusPlus)
1997 CheckExtraCXXDefaultArguments(D);
1998
Douglas Gregor160b5632010-04-26 17:32:49 +00001999 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002000 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2001 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002002
2003 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2004 // Objective-C++: Types shall not be defined in exception types.
2005 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2006 << Context.getTypeDeclType(OwnedDecl);
2007 }
2008
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002009 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2010 D.getSourceRange().getBegin(),
2011 D.getIdentifierLoc(),
2012 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002013 D.isInvalidType());
2014
2015 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2016 if (D.getCXXScopeSpec().isSet()) {
2017 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2018 << D.getCXXScopeSpec().getRange();
2019 New->setInvalidDecl();
2020 }
2021
2022 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002023 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002024 if (D.getIdentifier())
2025 IdResolver.AddDecl(New);
2026
2027 ProcessDeclAttributes(S, New, D);
2028
2029 if (New->hasAttr<BlocksAttr>())
2030 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002031 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002032}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002033
2034/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002035/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002036void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002037 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002038 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2039 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002040 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002041 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002042 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002043 }
2044}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002045
2046void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002047 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002048 unsigned numInitializers) {
2049 if (numInitializers > 0) {
2050 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002051 CXXCtorInitializer **ivarInitializers =
2052 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002053 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002054 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002055 IvarInitializers = ivarInitializers;
2056 }
2057}
2058
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002059void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002060 // Warning will be issued only when selector table is
2061 // generated (which means there is at lease one implementation
2062 // in the TU). This is to match gcc's behavior.
2063 if (ReferencedSelectors.empty() ||
2064 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002065 return;
2066 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2067 ReferencedSelectors.begin(),
2068 E = ReferencedSelectors.end(); S != E; ++S) {
2069 Selector Sel = (*S).first;
2070 if (!LookupImplementedMethodInGlobalPool(Sel))
2071 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2072 }
2073 return;
2074}