blob: 4e41aa93e5a5b9e3c73d138e231391f6da8ee305 [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
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000275bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000276 IdentifierInfo *PName,
277 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000278 const ObjCList<ObjCProtocolDecl> &PList) {
279
280 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000281 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
282 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000283 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
284 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000285 if (PDecl->getIdentifier() == PName) {
286 Diag(Ploc, diag::err_protocol_has_circular_dependency);
287 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000288 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000289 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000290 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
291 PDecl->getLocation(), PDecl->getReferencedProtocols()))
292 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000293 }
294 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000295 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000296}
297
John McCalld226f652010-08-21 09:40:31 +0000298Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000299Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
300 IdentifierInfo *ProtocolName,
301 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000302 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000303 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000304 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000305 SourceLocation EndProtoLoc,
306 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000307 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000308 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000309 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000310 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 if (PDecl) {
312 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000313 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000314 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000315 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000316 // Just return the protocol we already had.
317 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000318 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000319 }
Steve Naroff61d68522009-03-05 15:22:01 +0000320 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000321 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000322 err = CheckForwardProtocolDeclarationForCircularDependency(
323 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Steve Narofff11b5082008-08-13 16:39:22 +0000325 // Make sure the cached decl gets a valid start location.
326 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000327 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000328 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000329 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000330 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000331 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000332 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000333 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000334 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000335 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000336 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000337 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000338 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000339 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000340 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000341 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
342 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000343 PDecl->setLocEnd(EndProtoLoc);
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
346 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000347 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000348}
349
350/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000351/// issues an error if they are not declared. It returns list of
352/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000353void
Chris Lattnere13b9592008-07-26 04:03:38 +0000354Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000355 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000356 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000357 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000358 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000359 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
360 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000361 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000362 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
363 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000364 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000365 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
366 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
367 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000368 Diag(PDecl->getLocation(), diag::note_previous_decl)
369 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000370 }
371 }
372
373 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000374 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000375 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000376 continue;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000379 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000380
381 // If this is a forward declaration and we are supposed to warn in this
382 // case, do it.
383 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000384 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000385 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000386 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000387 }
388}
389
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000390/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000391/// a class method in its extension.
392///
Mike Stump1eb44332009-09-09 15:08:12 +0000393void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000394 ObjCInterfaceDecl *ID) {
395 if (!ID)
396 return; // Possibly due to previous error
397
398 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000399 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
400 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000401 ObjCMethodDecl *MD = *i;
402 MethodMap[MD->getSelector()] = MD;
403 }
404
405 if (MethodMap.empty())
406 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000407 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
408 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000409 ObjCMethodDecl *Method = *i;
410 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
411 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
412 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
413 << Method->getDeclName();
414 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
415 }
416 }
417}
418
Chris Lattner58fe03b2009-04-12 08:43:13 +0000419/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000420Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000421Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000422 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000423 unsigned NumElts,
424 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000425 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000426 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner4d391482007-12-12 07:09:47 +0000428 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000429 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000430 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000431 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000432 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000433 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000434 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000435 PushOnScopeChains(PDecl, TUScope, false);
436 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000437 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000438 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000439 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000440 if (!isNew)
441 PDecl->setChangedSinceDeserialization(true);
442 }
Chris Lattner4d391482007-12-12 07:09:47 +0000443 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000444 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
447 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000448 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000449 Protocols.data(), Protocols.size(),
450 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000451 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000452 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000453 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000454}
455
John McCalld226f652010-08-21 09:40:31 +0000456Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000457ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
458 IdentifierInfo *ClassName, SourceLocation ClassLoc,
459 IdentifierInfo *CategoryName,
460 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000461 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000462 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000463 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000464 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000465 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000466 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000467
468 /// Check that class of this category is already completely declared.
469 if (!IDecl || IDecl->isForwardDecl()) {
470 // Create an invalid ObjCCategoryDecl to serve as context for
471 // the enclosing method declarations. We mark the decl invalid
472 // to make it clear that this isn't a valid AST.
473 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
474 ClassLoc, CategoryLoc, CategoryName);
475 CDecl->setInvalidDecl();
476 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000477 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000478 }
479
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000480 if (!CategoryName && IDecl->getImplementation()) {
481 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
482 Diag(IDecl->getImplementation()->getLocation(),
483 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000484 }
485
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000486 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
487 ClassLoc, CategoryLoc, CategoryName);
488 // FIXME: PushOnScopeChains?
489 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000490
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000491 CDecl->setClassInterface(IDecl);
492 // Insert class extension to the list of class's categories.
493 if (!CategoryName)
494 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner16b34b42009-02-16 21:30:01 +0000496 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000497 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000498
Fariborz Jahanian25760612010-02-15 21:55:26 +0000499 if (CategoryName) {
500 /// Check for duplicate interface declaration for this category
501 ObjCCategoryDecl *CDeclChain;
502 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
503 CDeclChain = CDeclChain->getNextClassCategory()) {
504 if (CDeclChain->getIdentifier() == CategoryName) {
505 // Class extensions can be declared multiple times.
506 Diag(CategoryLoc, diag::warn_dup_category_def)
507 << ClassName << CategoryName;
508 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
509 break;
510 }
Chris Lattner70f19542009-02-16 21:26:43 +0000511 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000512 if (!CDeclChain)
513 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000514 }
Chris Lattner70f19542009-02-16 21:26:43 +0000515
Chris Lattner4d391482007-12-12 07:09:47 +0000516 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000517 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000518 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000519 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000520 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000521 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000522 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson15281452008-11-04 16:57:32 +0000525 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000526 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000527}
528
529/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000531/// object.
John McCalld226f652010-08-21 09:40:31 +0000532Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000533 SourceLocation AtCatImplLoc,
534 IdentifierInfo *ClassName, SourceLocation ClassLoc,
535 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000536 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000537 ObjCCategoryDecl *CatIDecl = 0;
538 if (IDecl) {
539 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
540 if (!CatIDecl) {
541 // Category @implementation with no corresponding @interface.
542 // Create and install one.
543 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000544 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000545 CatName);
546 CatIDecl->setClassInterface(IDecl);
547 CatIDecl->insertNextClassCategory();
548 }
549 }
550
Mike Stump1eb44332009-09-09 15:08:12 +0000551 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000552 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
553 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000554 /// Check that class of this category is already completely declared.
555 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000557
Douglas Gregord0434102009-01-09 00:49:46 +0000558 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000559 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000560
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000561 /// Check that CatName, category name, is not used in another implementation.
562 if (CatIDecl) {
563 if (CatIDecl->getImplementation()) {
564 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
565 << CatName;
566 Diag(CatIDecl->getImplementation()->getLocation(),
567 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000568 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000569 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000570 // Warn on implementating category of deprecated class under
571 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000572 DiagnoseObjCImplementedDeprecations(*this,
573 dyn_cast<NamedDecl>(IDecl),
574 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000575 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Anders Carlsson15281452008-11-04 16:57:32 +0000578 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000579 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000580}
581
John McCalld226f652010-08-21 09:40:31 +0000582Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000583 SourceLocation AtClassImplLoc,
584 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000585 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000586 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000587 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000588 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000589 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000590 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
591 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000593 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000594 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000595 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
596 // If this is a forward declaration of an interface, warn.
597 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000598 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000599 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000600 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000601 } else {
602 // We did not find anything with the name ClassName; try to correct for
603 // typos in the class name.
604 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000605 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000606 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000607 // Suggest the (potentially) correct interface name. However, put the
608 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000609 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000610 // provide a code-modification hint or use the typo name for recovery,
611 // because this is just a warning. The program may actually be correct.
612 Diag(ClassLoc, diag::warn_undef_interface_suggest)
613 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000614 Diag(IDecl->getLocation(), diag::note_previous_decl)
615 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000616 << FixItHint::CreateReplacement(ClassLoc,
617 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000618 IDecl = 0;
619 } else {
620 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
621 }
Chris Lattner4d391482007-12-12 07:09:47 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner4d391482007-12-12 07:09:47 +0000624 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000626 if (SuperClassname) {
627 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000628 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
629 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
632 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000633 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000635 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000636 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000637 Diag(SuperClassLoc, diag::err_undef_superclass)
638 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000639 else if (IDecl && IDecl->getSuperClass() != SDecl) {
640 // This implementation and its interface do not have the same
641 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000642 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000643 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000644 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000645 }
646 }
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Chris Lattner4d391482007-12-12 07:09:47 +0000649 if (!IDecl) {
650 // Legacy case of @implementation with no corresponding @interface.
651 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000652
Mike Stump390b4cc2009-05-16 07:39:55 +0000653 // FIXME: Do we support attributes on the @implementation? If so we should
654 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000655 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000656 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000657 IDecl->setSuperClass(SDecl);
658 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000659
660 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000661 } else {
662 // Mark the interface as being completed, even if it was just as
663 // @class ....;
664 // declaration; the user cannot reopen it.
665 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
668 ObjCImplementationDecl* IMPDecl =
669 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000670 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Anders Carlsson15281452008-11-04 16:57:32 +0000672 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000673 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Chris Lattner4d391482007-12-12 07:09:47 +0000675 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000676 if (IDecl->getImplementation()) {
677 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000678 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000679 Diag(IDecl->getImplementation()->getLocation(),
680 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000681 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000682 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000683 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000684 // Warn on implementating deprecated class under
685 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000686 DiagnoseObjCImplementedDeprecations(*this,
687 dyn_cast<NamedDecl>(IDecl),
688 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000689 }
John McCalld226f652010-08-21 09:40:31 +0000690 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000691}
692
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
694 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000695 SourceLocation RBrace) {
696 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000697 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000698 if (!IDecl)
699 return;
700 /// Check case of non-existing @interface decl.
701 /// (legacy objective-c @implementation decl without an @interface decl).
702 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000703 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000704 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000705 // Add ivar's to class's DeclContext.
706 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000707 ivars[i]->setLexicalDeclContext(ImpDecl);
708 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000709 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000710 }
711
Chris Lattner4d391482007-12-12 07:09:47 +0000712 return;
713 }
714 // If implementation has empty ivar list, just return.
715 if (numIvars == 0)
716 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattner4d391482007-12-12 07:09:47 +0000718 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000719 if (LangOpts.ObjCNonFragileABI2) {
720 if (ImpDecl->getSuperClass())
721 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
722 for (unsigned i = 0; i < numIvars; i++) {
723 ObjCIvarDecl* ImplIvar = ivars[i];
724 if (const ObjCIvarDecl *ClsIvar =
725 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
726 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
727 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
728 continue;
729 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000730 // Instance ivar to Implementation's DeclContext.
731 ImplIvar->setLexicalDeclContext(ImpDecl);
732 IDecl->makeDeclVisibleInContext(ImplIvar, false);
733 ImpDecl->addDecl(ImplIvar);
734 }
735 return;
736 }
Chris Lattner4d391482007-12-12 07:09:47 +0000737 // Check interface's Ivar list against those in the implementation.
738 // names and types must match.
739 //
Chris Lattner4d391482007-12-12 07:09:47 +0000740 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000741 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000742 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
743 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 ObjCIvarDecl* ImplIvar = ivars[j++];
745 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000746 assert (ImplIvar && "missing implementation ivar");
747 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Steve Naroffca331292009-03-03 14:49:36 +0000749 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000750 if (Context.getCanonicalType(ImplIvar->getType()) !=
751 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000752 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000753 << ImplIvar->getIdentifier()
754 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000755 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000756 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
757 Expr *ImplBitWidth = ImplIvar->getBitWidth();
758 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000759 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
760 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000761 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
762 << ImplIvar->getIdentifier();
763 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765 }
Steve Naroffca331292009-03-03 14:49:36 +0000766 // Make sure the names are identical.
767 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000768 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000769 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000770 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000771 }
772 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattner609e4c72007-12-12 18:11:49 +0000775 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000776 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000777 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000778 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000779}
780
Steve Naroff3c2eb662008-02-10 21:38:56 +0000781void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000782 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000783 if (!IncompleteImpl) {
784 Diag(ImpLoc, diag::warn_incomplete_impl);
785 IncompleteImpl = true;
786 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000787 if (DiagID == diag::warn_unimplemented_protocol_method)
788 Diag(ImpLoc, DiagID) << method->getDeclName();
789 else
790 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000791}
792
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000793/// Determines if type B can be substituted for type A. Returns true if we can
794/// guarantee that anything that the user will do to an object of type A can
795/// also be done to an object of type B. This is trivially true if the two
796/// types are the same, or if B is a subclass of A. It becomes more complex
797/// in cases where protocols are involved.
798///
799/// Object types in Objective-C describe the minimum requirements for an
800/// object, rather than providing a complete description of a type. For
801/// example, if A is a subclass of B, then B* may refer to an instance of A.
802/// The principle of substitutability means that we may use an instance of A
803/// anywhere that we may use an instance of B - it will implement all of the
804/// ivars of B and all of the methods of B.
805///
806/// This substitutability is important when type checking methods, because
807/// the implementation may have stricter type definitions than the interface.
808/// The interface specifies minimum requirements, but the implementation may
809/// have more accurate ones. For example, a method may privately accept
810/// instances of B, but only publish that it accepts instances of A. Any
811/// object passed to it will be type checked against B, and so will implicitly
812/// by a valid A*. Similarly, a method may return a subclass of the class that
813/// it is declared as returning.
814///
815/// This is most important when considering subclassing. A method in a
816/// subclass must accept any object as an argument that its superclass's
817/// implementation accepts. It may, however, accept a more general type
818/// without breaking substitutability (i.e. you can still use the subclass
819/// anywhere that you can use the superclass, but not vice versa). The
820/// converse requirement applies to return types: the return type for a
821/// subclass method must be a valid object of the kind that the superclass
822/// advertises, but it may be specified more accurately. This avoids the need
823/// for explicit down-casting by callers.
824///
825/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000826static bool isObjCTypeSubstitutable(ASTContext &Context,
827 const ObjCObjectPointerType *A,
828 const ObjCObjectPointerType *B,
829 bool rejectId) {
830 // Reject a protocol-unqualified id.
831 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000832
833 // If B is a qualified id, then A must also be a qualified id and it must
834 // implement all of the protocols in B. It may not be a qualified class.
835 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
836 // stricter definition so it is not substitutable for id<A>.
837 if (B->isObjCQualifiedIdType()) {
838 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000839 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
840 QualType(B,0),
841 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000842 }
843
844 /*
845 // id is a special type that bypasses type checking completely. We want a
846 // warning when it is used in one place but not another.
847 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
848
849
850 // If B is a qualified id, then A must also be a qualified id (which it isn't
851 // if we've got this far)
852 if (B->isObjCQualifiedIdType()) return false;
853 */
854
855 // Now we know that A and B are (potentially-qualified) class types. The
856 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000857 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000858}
859
John McCall10302c02010-10-28 02:34:38 +0000860static SourceRange getTypeRange(TypeSourceInfo *TSI) {
861 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
862}
863
864static void CheckMethodOverrideReturn(Sema &S,
865 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000866 ObjCMethodDecl *MethodDecl,
867 bool IsProtocolMethodDecl) {
868 if (IsProtocolMethodDecl &&
869 (MethodDecl->getObjCDeclQualifier() !=
870 MethodImpl->getObjCDeclQualifier())) {
871 S.Diag(MethodImpl->getLocation(),
872 diag::warn_conflicting_ret_type_modifiers)
873 << MethodImpl->getDeclName()
874 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
875 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
876 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
877 }
878
John McCall10302c02010-10-28 02:34:38 +0000879 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000880 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +0000881 return;
882
883 unsigned DiagID = diag::warn_conflicting_ret_types;
884
885 // Mismatches between ObjC pointers go into a different warning
886 // category, and sometimes they're even completely whitelisted.
887 if (const ObjCObjectPointerType *ImplPtrTy =
888 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
889 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000890 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +0000891 // Allow non-matching return types as long as they don't violate
892 // the principle of substitutability. Specifically, we permit
893 // return types that are subclasses of the declared return type,
894 // or that are more-qualified versions of the declared type.
895 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
896 return;
897
898 DiagID = diag::warn_non_covariant_ret_types;
899 }
900 }
901
902 S.Diag(MethodImpl->getLocation(), DiagID)
903 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000904 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +0000905 << MethodImpl->getResultType()
906 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000907 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
908 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +0000909}
910
911static void CheckMethodOverrideParam(Sema &S,
912 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000913 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +0000914 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000915 ParmVarDecl *IfaceVar,
916 bool IsProtocolMethodDecl) {
917 if (IsProtocolMethodDecl &&
918 (ImplVar->getObjCDeclQualifier() !=
919 IfaceVar->getObjCDeclQualifier())) {
920 S.Diag(ImplVar->getLocation(),
921 diag::warn_conflicting_param_modifiers)
922 << getTypeRange(ImplVar->getTypeSourceInfo())
923 << MethodImpl->getDeclName();
924 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
925 << getTypeRange(IfaceVar->getTypeSourceInfo());
926 }
927
John McCall10302c02010-10-28 02:34:38 +0000928 QualType ImplTy = ImplVar->getType();
929 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000930
John McCall10302c02010-10-28 02:34:38 +0000931 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
932 return;
933
934 unsigned DiagID = diag::warn_conflicting_param_types;
935
936 // Mismatches between ObjC pointers go into a different warning
937 // category, and sometimes they're even completely whitelisted.
938 if (const ObjCObjectPointerType *ImplPtrTy =
939 ImplTy->getAs<ObjCObjectPointerType>()) {
940 if (const ObjCObjectPointerType *IfacePtrTy =
941 IfaceTy->getAs<ObjCObjectPointerType>()) {
942 // Allow non-matching argument types as long as they don't
943 // violate the principle of substitutability. Specifically, the
944 // implementation must accept any objects that the superclass
945 // accepts, however it may also accept others.
946 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
947 return;
948
949 DiagID = diag::warn_non_contravariant_param_types;
950 }
951 }
952
953 S.Diag(ImplVar->getLocation(), DiagID)
954 << getTypeRange(ImplVar->getTypeSourceInfo())
955 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
956 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
957 << getTypeRange(IfaceVar->getTypeSourceInfo());
958}
959
960
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000961void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000962 ObjCMethodDecl *MethodDecl,
963 bool IsProtocolMethodDecl) {
964 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
965 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Chris Lattner3aff9192009-04-11 19:58:42 +0000967 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000968 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +0000969 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000970 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
971 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000973 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000974 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +0000975 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000976 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000977}
978
Mike Stump390b4cc2009-05-16 07:39:55 +0000979/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
980/// improve the efficiency of selector lookups and type checking by associating
981/// with each protocol / interface / category the flattened instance tables. If
982/// we used an immutable set to keep the table then it wouldn't add significant
983/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000984
Steve Naroffefe7f362008-02-08 22:06:17 +0000985/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000986/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000987void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
988 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000989 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000990 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000991 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000992 ObjCContainerDecl *CDecl) {
993 ObjCInterfaceDecl *IDecl;
994 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
995 IDecl = C->getClassInterface();
996 else
997 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
998 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
999
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001000 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001001 ObjCInterfaceDecl *NSIDecl = 0;
1002 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001003 // check to see if class implements forwardInvocation method and objects
1004 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001005 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001006 // Under such conditions, which means that every method possible is
1007 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001008 // found" warnings.
1009 // FIXME: Use a general GetUnarySelector method for this.
1010 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1011 Selector fISelector = Context.Selectors.getSelector(1, &II);
1012 if (InsMap.count(fISelector))
1013 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1014 // need be implemented in the implementation.
1015 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001018 // If a method lookup fails locally we still need to look and see if
1019 // the method was implemented by a base class or an inherited
1020 // protocol. This lookup is slow, but occurs rarely in correct code
1021 // and otherwise would terminate in a warning.
1022
Chris Lattner4d391482007-12-12 07:09:47 +00001023 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001024 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001025 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001026 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001027 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001028 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001029 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001030 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001031 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001032 // Ugly, but necessary. Method declared in protcol might have
1033 // have been synthesized due to a property declared in the class which
1034 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001035 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001036 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001037 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001038 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001039 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1040 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001041 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001042 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001043 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1044 << PDecl->getDeclName();
1045 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001046 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001047 }
1048 }
Chris Lattner4d391482007-12-12 07:09:47 +00001049 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001050 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001051 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001052 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001053 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001054 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1055 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001056 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001057 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001058 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001059 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001060 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001061 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1062 PDecl->getDeclName();
1063 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001064 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001065 }
Chris Lattner780f3292008-07-21 21:32:27 +00001066 // Check on this protocols's referenced protocols, recursively.
1067 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1068 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001069 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001070}
1071
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001072/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1073/// or protocol against those declared in their implementations.
1074///
1075void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1076 const llvm::DenseSet<Selector> &ClsMap,
1077 llvm::DenseSet<Selector> &InsMapSeen,
1078 llvm::DenseSet<Selector> &ClsMapSeen,
1079 ObjCImplDecl* IMPDecl,
1080 ObjCContainerDecl* CDecl,
1081 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001082 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001083 // Check and see if instance methods in class interface have been
1084 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001085 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1086 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001087 if (InsMapSeen.count((*I)->getSelector()))
1088 continue;
1089 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001090 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001091 !InsMap.count((*I)->getSelector())) {
1092 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001093 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1094 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001095 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001096 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001097 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001098 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001099 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001100 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001101 assert(MethodDecl &&
1102 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001103 // ImpMethodDecl may be null as in a @dynamic property.
1104 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001105 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1106 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001107 }
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001110 // Check and see if class methods in class interface have been
1111 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001112 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001113 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001114 if (ClsMapSeen.count((*I)->getSelector()))
1115 continue;
1116 ClsMapSeen.insert((*I)->getSelector());
1117 if (!ClsMap.count((*I)->getSelector())) {
1118 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001119 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1120 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001121 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001122 ObjCMethodDecl *ImpMethodDecl =
1123 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001124 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001125 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001126 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1127 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001128 }
1129 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001130
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001131 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001132 // Also methods in class extensions need be looked at next.
1133 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1134 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1135 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1136 IMPDecl,
1137 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1138 IncompleteImpl, false);
1139
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001140 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001141 for (ObjCInterfaceDecl::all_protocol_iterator
1142 PI = I->all_referenced_protocol_begin(),
1143 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001144 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1145 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001146 (*PI), IncompleteImpl, false);
1147 if (I->getSuperClass())
1148 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001149 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001150 I->getSuperClass(), IncompleteImpl, false);
1151 }
1152}
1153
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001154void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001155 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001156 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001157 llvm::DenseSet<Selector> InsMap;
1158 // Check and see if instance methods in class interface have been
1159 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001160 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001161 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001162 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001164 // Check and see if properties declared in the interface have either 1)
1165 // an implementation or 2) there is a @synthesize/@dynamic implementation
1166 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001167 if (isa<ObjCInterfaceDecl>(CDecl) &&
1168 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001169 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001170
Chris Lattner4d391482007-12-12 07:09:47 +00001171 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001172 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001173 I = IMPDecl->classmeth_begin(),
1174 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001175 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001177 // Check for type conflict of methods declared in a class/protocol and
1178 // its implementation; if any.
1179 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001180 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1181 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001182 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattner4d391482007-12-12 07:09:47 +00001184 // Check the protocol list for unimplemented methods in the @implementation
1185 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001186 // Check and see if class methods in class interface have been
1187 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Chris Lattnercddc8882009-03-01 00:56:52 +00001189 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001190 for (ObjCInterfaceDecl::all_protocol_iterator
1191 PI = I->all_referenced_protocol_begin(),
1192 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001193 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001194 InsMap, ClsMap, I);
1195 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001196 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1197 Categories; Categories = Categories->getNextClassExtension())
1198 ImplMethodsVsClassMethods(S, IMPDecl,
1199 const_cast<ObjCCategoryDecl*>(Categories),
1200 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001201 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001202 // For extended class, unimplemented methods in its protocols will
1203 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001204 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001205 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1206 E = C->protocol_end(); PI != E; ++PI)
1207 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001208 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001209 // Report unimplemented properties in the category as well.
1210 // When reporting on missing setter/getters, do not report when
1211 // setter/getter is implemented in category's primary class
1212 // implementation.
1213 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1214 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1215 for (ObjCImplementationDecl::instmeth_iterator
1216 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1217 InsMap.insert((*I)->getSelector());
1218 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001219 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001220 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001221 } else
1222 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001223}
1224
Mike Stump1eb44332009-09-09 15:08:12 +00001225/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001226Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001227Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001228 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001229 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001230 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001231 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattner4d391482007-12-12 07:09:47 +00001233 for (unsigned i = 0; i != NumElts; ++i) {
1234 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001235 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001236 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001237 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001238 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001239 // Maybe we will complain about the shadowed template parameter.
1240 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1241 // Just pretend that we didn't see the previous declaration.
1242 PrevDecl = 0;
1243 }
1244
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001245 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001246 // GCC apparently allows the following idiom:
1247 //
1248 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1249 // @class XCElementToggler;
1250 //
Mike Stump1eb44332009-09-09 15:08:12 +00001251 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001252 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001253 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001254 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001255 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001256 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 // a forward class declaration matching a typedef name of a class refers
1258 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001259 if (const ObjCObjectType *OI =
1260 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1261 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001262 }
Chris Lattner4d391482007-12-12 07:09:47 +00001263 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001264 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1265 if (!IDecl) { // Not already seen? Make a forward decl.
1266 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1267 IdentList[i], IdentLocs[i], true);
1268
1269 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1270 // the current DeclContext. This prevents clients that walk DeclContext
1271 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1272 // declared later (if at all). We also take care to explicitly make
1273 // sure this declaration is visible for name lookup.
1274 PushOnScopeChains(IDecl, TUScope, false);
1275 CurContext->makeDeclVisibleInContext(IDecl, true);
1276 }
Chris Lattner4d391482007-12-12 07:09:47 +00001277
1278 Interfaces.push_back(IDecl);
1279 }
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek321c22f2009-11-18 00:28:11 +00001281 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001282 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001283 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001284 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001285 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001286 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001287 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001288}
1289
1290
1291/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1292/// returns true, or false, accordingly.
1293/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001294bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001295 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001296 bool matchBasedOnSizeAndAlignment,
1297 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001298 QualType T1 = Context.getCanonicalType(Method->getResultType());
1299 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001301 if (T1 != T2) {
1302 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001303 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001304 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001305 // Incomplete types don't have a size and alignment.
1306 if (T1->isIncompleteType() || T2->isIncompleteType())
1307 return false;
1308 // Check is based on size and alignment.
1309 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1310 return false;
1311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Chris Lattner89951a82009-02-20 18:43:26 +00001313 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1314 E = Method->param_end();
1315 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Chris Lattner89951a82009-02-20 18:43:26 +00001317 for (; ParamI != E; ++ParamI, ++PrevI) {
1318 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1319 T1 = Context.getCanonicalType((*ParamI)->getType());
1320 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001321 if (T1 != T2) {
1322 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001323 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001324 return false;
1325 // Incomplete types don't have a size and alignment.
1326 if (T1->isIncompleteType() || T2->isIncompleteType())
1327 return false;
1328 // Check is based on size and alignment.
1329 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1330 return false;
1331 }
Chris Lattner4d391482007-12-12 07:09:47 +00001332 }
1333 return true;
1334}
1335
Sebastian Redldb9d2142010-08-02 23:18:59 +00001336/// \brief Read the contents of the method pool for a given selector from
1337/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001338///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001339/// This routine should only be called once, when the method pool has no entry
1340/// for this selector.
1341Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001342 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001343 assert(MethodPool.find(Sel) == MethodPool.end() &&
1344 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001345
1346 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001347 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Sebastian Redldb9d2142010-08-02 23:18:59 +00001349 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001350}
1351
Sebastian Redldb9d2142010-08-02 23:18:59 +00001352void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1353 bool instance) {
1354 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1355 if (Pos == MethodPool.end()) {
1356 if (ExternalSource)
1357 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001358 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001359 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1360 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001361 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001362 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001363 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001364 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001365 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001366 Entry.Method = Method;
1367 Entry.Next = 0;
1368 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Chris Lattnerb25df352009-03-04 05:16:45 +00001371 // We've seen a method with this name, see if we have already seen this type
1372 // signature.
1373 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001374 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001375 ObjCMethodDecl *PrevObjCMethod = List->Method;
1376 PrevObjCMethod->setDefined(impl);
1377 // If a method is deprecated, push it in the global pool.
1378 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001379 if (Method->isDeprecated()) {
1380 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001381 List->Method = Method;
1382 }
1383 // If new method is unavailable, push it into global pool
1384 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001385 if (Method->isUnavailable()) {
1386 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001387 List->Method = Method;
1388 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001389 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001390 }
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Chris Lattnerb25df352009-03-04 05:16:45 +00001392 // We have a new signature for an existing method - add it.
1393 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001394 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1395 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001396}
1397
Sebastian Redldb9d2142010-08-02 23:18:59 +00001398ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001399 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001400 bool warn, bool instance) {
1401 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1402 if (Pos == MethodPool.end()) {
1403 if (ExternalSource)
1404 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001405 else
1406 return 0;
1407 }
1408
Sebastian Redldb9d2142010-08-02 23:18:59 +00001409 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001411 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001412 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1413 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001414 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001415 if (warn && MethList.Method && MethList.Next) {
1416 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001417 if (strictSelectorMatch)
1418 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1419 // This checks if the methods differ in type mismatch.
1420 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1421 issueWarning = true;
1422 }
1423
1424 if (!issueWarning)
1425 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1426 // This checks if the methods differ by size & alignment.
1427 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1428 issueWarning = true;
1429 }
1430
Sebastian Redldb9d2142010-08-02 23:18:59 +00001431 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001432 if (strictSelectorMatch)
1433 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1434 else
1435 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001436 Diag(MethList.Method->getLocStart(), diag::note_using)
1437 << MethList.Method->getSourceRange();
1438 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1439 Diag(Next->Method->getLocStart(), diag::note_also_found)
1440 << Next->Method->getSourceRange();
1441 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001442 }
1443 return MethList.Method;
1444}
1445
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001446ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001447 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1448 if (Pos == MethodPool.end())
1449 return 0;
1450
1451 GlobalMethods &Methods = Pos->second;
1452
1453 if (Methods.first.Method && Methods.first.Method->isDefined())
1454 return Methods.first.Method;
1455 if (Methods.second.Method && Methods.second.Method->isDefined())
1456 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001457 return 0;
1458}
1459
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001460/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1461/// identical selector names in current and its super classes and issues
1462/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001463void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1464 ObjCMethodDecl *Method,
1465 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001466 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1467 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001469 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001470 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001471 SD->lookupMethod(Method->getSelector(), IsInstance);
1472 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001473 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001474 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001475 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001476 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1477 E = Method->param_end();
1478 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1479 for (; ParamI != E; ++ParamI, ++PrevI) {
1480 // Number of parameters are the same and is guaranteed by selector match.
1481 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1482 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1483 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001484 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001485 // respective argument type in the super class method, issue warning;
1486 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001487 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001488 << T1 << T2;
1489 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1490 return;
1491 }
1492 }
1493 ID = SD;
1494 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001495}
1496
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001497/// DiagnoseDuplicateIvars -
1498/// Check for duplicate ivars in the entire class at the start of
1499/// @implementation. This becomes necesssary because class extension can
1500/// add ivars to a class in random order which will not be known until
1501/// class's @implementation is seen.
1502void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1503 ObjCInterfaceDecl *SID) {
1504 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1505 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1506 ObjCIvarDecl* Ivar = (*IVI);
1507 if (Ivar->isInvalidDecl())
1508 continue;
1509 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1510 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1511 if (prevIvar) {
1512 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1513 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1514 Ivar->setInvalidDecl();
1515 }
1516 }
1517 }
1518}
1519
Steve Naroffa56f6162007-12-18 01:30:32 +00001520// Note: For class/category implemenations, allMethods/allProperties is
1521// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001522void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001523 Decl *ClassDecl,
1524 Decl **allMethods, unsigned allNum,
1525 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001526 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001527 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1528 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001529 // should be true.
1530 if (!ClassDecl)
1531 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001532
Mike Stump1eb44332009-09-09 15:08:12 +00001533 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001534 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1535 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001537
Ted Kremenek782f2f52010-01-07 01:20:12 +00001538 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1539 // FIXME: This is wrong. We shouldn't be pretending that there is
1540 // an '@end' in the declaration.
1541 SourceLocation L = ClassDecl->getLocation();
1542 AtEnd.setBegin(L);
1543 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001544 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001545 }
1546
Steve Naroff0701bbb2009-01-08 17:28:14 +00001547 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1548 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1549 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1550
Chris Lattner4d391482007-12-12 07:09:47 +00001551 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001552 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001553 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001554
1555 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001556 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001557 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001558 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001559 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001560 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001561 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001562 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001563 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001564 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001565 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001566 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001567 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001568 InsMap[Method->getSelector()] = Method;
1569 /// The following allows us to typecheck messages to "id".
1570 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001571 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001572 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001573 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001574 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001575 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001576 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001577 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001578 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001579 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001580 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001581 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001582 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001583 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001584 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001585 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001586 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001587 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001588 /// The following allows us to typecheck messages to "Class".
1589 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001590 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001591 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001592 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001593 }
1594 }
1595 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001596 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001597 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001598 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001599 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001600 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001601 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001602 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001603 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001604 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001605
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001606 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001607 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001608 if (C->IsClassExtension()) {
1609 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1610 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001611 }
Chris Lattner4d391482007-12-12 07:09:47 +00001612 }
Steve Naroff09c47192009-01-09 15:36:25 +00001613 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001614 if (CDecl->getIdentifier())
1615 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1616 // user-defined setter/getter. It also synthesizes setter/getter methods
1617 // and adds them to the DeclContext and global method pools.
1618 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1619 E = CDecl->prop_end();
1620 I != E; ++I)
1621 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001622 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001623 }
1624 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001625 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001626 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001627 // Any property declared in a class extension might have user
1628 // declared setter or getter in current class extension or one
1629 // of the other class extensions. Mark them as synthesized as
1630 // property will be synthesized when property with same name is
1631 // seen in the @implementation.
1632 for (const ObjCCategoryDecl *ClsExtDecl =
1633 IDecl->getFirstClassExtension();
1634 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1635 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1636 E = ClsExtDecl->prop_end(); I != E; ++I) {
1637 ObjCPropertyDecl *Property = (*I);
1638 // Skip over properties declared @dynamic
1639 if (const ObjCPropertyImplDecl *PIDecl
1640 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1641 if (PIDecl->getPropertyImplementation()
1642 == ObjCPropertyImplDecl::Dynamic)
1643 continue;
1644
1645 for (const ObjCCategoryDecl *CExtDecl =
1646 IDecl->getFirstClassExtension();
1647 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1648 if (ObjCMethodDecl *GetterMethod =
1649 CExtDecl->getInstanceMethod(Property->getGetterName()))
1650 GetterMethod->setSynthesized(true);
1651 if (!Property->isReadOnly())
1652 if (ObjCMethodDecl *SetterMethod =
1653 CExtDecl->getInstanceMethod(Property->getSetterName()))
1654 SetterMethod->setSynthesized(true);
1655 }
1656 }
1657 }
1658
Ted Kremenekc32647d2010-12-23 21:35:43 +00001659 if (LangOpts.ObjCDefaultSynthProperties &&
1660 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001661 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001662 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001663 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001664
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001665 if (LangOpts.ObjCNonFragileABI2)
1666 while (IDecl->getSuperClass()) {
1667 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1668 IDecl = IDecl->getSuperClass();
1669 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001670 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001671 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001672 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001673 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001674 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Chris Lattner4d391482007-12-12 07:09:47 +00001676 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001677 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001678 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001679 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001680 Categories; Categories = Categories->getNextClassCategory()) {
1681 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001682 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001683 break;
1684 }
1685 }
1686 }
1687 }
Chris Lattner682bf922009-03-29 16:50:03 +00001688 if (isInterfaceDeclKind) {
1689 // Reject invalid vardecls.
1690 for (unsigned i = 0; i != tuvNum; i++) {
1691 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1692 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1693 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001694 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001695 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001696 }
Chris Lattner682bf922009-03-29 16:50:03 +00001697 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001698 }
Chris Lattner4d391482007-12-12 07:09:47 +00001699}
1700
1701
1702/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1703/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001704static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001705CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00001706 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00001707}
1708
Ted Kremenek422bae72010-04-18 04:59:38 +00001709static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001710bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001711 // The 'ibaction' attribute is allowed on method definitions because of
1712 // how the IBAction macro is used on both method declarations and definitions.
1713 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001714 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1715 if ((*i)->getKind() != attr::IBAction)
1716 return true;
1717 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001718}
1719
John McCalld226f652010-08-21 09:40:31 +00001720Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001721 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001722 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001723 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001724 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001725 Selector Sel,
1726 // optional arguments. The number of types/arguments is obtained
1727 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001728 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001729 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001730 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001731 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001732 // Make sure we can establish a context for the method.
1733 if (!ClassDecl) {
1734 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00001735 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001736 }
Chris Lattner4d391482007-12-12 07:09:47 +00001737 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001739 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001740 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001741 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Steve Naroffccef3712009-02-20 22:59:16 +00001743 // Methods cannot return interface types. All ObjC objects are
1744 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001745 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001746 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1747 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001748 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001749 }
1750 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001751 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001752
1753 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001754 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001755 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001756 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001757 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001758 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001759 MethodDeclKind == tok::objc_optional ?
1760 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001761 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Chris Lattner0ed844b2008-04-04 06:12:32 +00001763 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Chris Lattner7db638d2009-04-11 19:42:43 +00001765 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001766 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001767 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Chris Lattnere294d3f2009-04-11 18:57:04 +00001769 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001770 ArgType = Context.getObjCIdType();
1771 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001772 } else {
John McCall58e46772009-10-23 21:48:59 +00001773 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001774 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001775 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001778 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1779 LookupOrdinaryName, ForRedeclaration);
1780 LookupName(R, S);
1781 if (R.isSingleResult()) {
1782 NamedDecl *PrevDecl = R.getFoundDecl();
1783 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001784 Diag(ArgInfo[i].NameLoc,
1785 (MethodDefinition ? diag::warn_method_param_redefinition
1786 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001787 << ArgInfo[i].Name;
1788 Diag(PrevDecl->getLocation(),
1789 diag::note_previous_declaration);
1790 }
1791 }
1792
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001793 SourceLocation StartLoc = DI
1794 ? DI->getTypeLoc().getBeginLoc()
1795 : ArgInfo[i].NameLoc;
1796
John McCall81ef3e62011-04-23 02:46:06 +00001797 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
1798 ArgInfo[i].NameLoc, ArgInfo[i].Name,
1799 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00001800
John McCall70798862011-05-02 00:30:12 +00001801 Param->setObjCMethodScopeInfo(i);
1802
Chris Lattner0ed844b2008-04-04 06:12:32 +00001803 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001804 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001806 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001807 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001809 S->AddDecl(Param);
1810 IdResolver.AddDecl(Param);
1811
Chris Lattner0ed844b2008-04-04 06:12:32 +00001812 Params.push_back(Param);
1813 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001814
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001815 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001816 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001817 QualType ArgType = Param->getType();
1818 if (ArgType.isNull())
1819 ArgType = Context.getObjCIdType();
1820 else
1821 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1822 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001823 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001824 Diag(Param->getLocation(),
1825 diag::err_object_cannot_be_passed_returned_by_value)
1826 << 1 << ArgType;
1827 Param->setInvalidDecl();
1828 }
1829 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001830
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001831 Params.push_back(Param);
1832 }
1833
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001834 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1835 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001836 ObjCMethod->setObjCDeclQualifier(
1837 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1838 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001839
1840 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001841 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001842
John McCall54abf7d2009-11-04 02:18:39 +00001843 const ObjCMethodDecl *InterfaceMD = 0;
1844
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001845 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00001846 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001847 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001848 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001849 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1850 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001851 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001852 PrevMethod = ImpDecl->getClassMethod(Sel);
1853 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001854 }
John McCall54abf7d2009-11-04 02:18:39 +00001855 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1856 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001857 if (ObjCMethod->hasAttrs() &&
1858 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001859 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001860 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001861 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001862 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001863 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1864 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001865 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001866 PrevMethod = CatImpDecl->getClassMethod(Sel);
1867 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001868 }
Sean Huntcf807c42010-08-18 23:23:40 +00001869 if (ObjCMethod->hasAttrs() &&
1870 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001871 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001872 } else {
1873 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001874 }
1875 if (PrevMethod) {
1876 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001877 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001878 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001879 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001880 }
John McCall54abf7d2009-11-04 02:18:39 +00001881
John McCalleca5d222011-03-02 04:00:57 +00001882 // Merge information down from the interface declaration if we have one.
Sean Huntcf807c42010-08-18 23:23:40 +00001883 if (InterfaceMD)
John McCalleca5d222011-03-02 04:00:57 +00001884 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
John McCall54abf7d2009-11-04 02:18:39 +00001885
John McCalld226f652010-08-21 09:40:31 +00001886 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001887}
1888
Chris Lattnercc98eac2008-12-17 07:13:27 +00001889bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001890 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Anders Carlsson15281452008-11-04 16:57:32 +00001893 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1894 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Anders Carlsson15281452008-11-04 16:57:32 +00001896 return true;
1897}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001898
Chris Lattnercc98eac2008-12-17 07:13:27 +00001899/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1900/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001901void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001902 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001903 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001904 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001905 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001906 if (!Class) {
1907 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1908 return;
1909 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001910 if (LangOpts.ObjCNonFragileABI) {
1911 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1912 return;
1913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Chris Lattnercc98eac2008-12-17 07:13:27 +00001915 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001916 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1917 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001918 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001919 for (unsigned i = 0; i < Ivars.size(); i++) {
1920 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001921 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001922 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
1923 /*FIXME: StartL=*/ID->getLocation(),
1924 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00001925 ID->getIdentifier(), ID->getType(),
1926 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001927 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Chris Lattnercc98eac2008-12-17 07:13:27 +00001930 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001931 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001932 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001933 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001934 if (getLangOptions().CPlusPlus)
1935 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001936 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001937 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001938 }
1939}
1940
Douglas Gregor160b5632010-04-26 17:32:49 +00001941/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001942VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
1943 SourceLocation StartLoc,
1944 SourceLocation IdLoc,
1945 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00001946 bool Invalid) {
1947 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1948 // duration shall not be qualified by an address-space qualifier."
1949 // Since all parameters have automatic store duration, they can not have
1950 // an address space.
1951 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001952 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00001953 Invalid = true;
1954 }
1955
1956 // An @catch parameter must be an unqualified object pointer type;
1957 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1958 if (Invalid) {
1959 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001960 } else if (T->isDependentType()) {
1961 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001962 } else if (!T->isObjCObjectPointerType()) {
1963 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001964 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00001965 } else if (T->isObjCQualifiedIdType()) {
1966 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001967 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00001968 }
1969
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001970 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
1971 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00001972 New->setExceptionVariable(true);
1973
Douglas Gregor160b5632010-04-26 17:32:49 +00001974 if (Invalid)
1975 New->setInvalidDecl();
1976 return New;
1977}
1978
John McCalld226f652010-08-21 09:40:31 +00001979Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001980 const DeclSpec &DS = D.getDeclSpec();
1981
1982 // We allow the "register" storage class on exception variables because
1983 // GCC did, but we drop it completely. Any other storage class is an error.
1984 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1985 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1986 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1987 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1988 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1989 << DS.getStorageClassSpec();
1990 }
1991 if (D.getDeclSpec().isThreadSpecified())
1992 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1993 D.getMutableDeclSpec().ClearStorageClassSpecs();
1994
1995 DiagnoseFunctionSpecifiers(D);
1996
1997 // Check that there are no default arguments inside the type of this
1998 // exception object (C++ only).
1999 if (getLangOptions().CPlusPlus)
2000 CheckExtraCXXDefaultArguments(D);
2001
Douglas Gregor160b5632010-04-26 17:32:49 +00002002 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002003 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2004 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002005
2006 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2007 // Objective-C++: Types shall not be defined in exception types.
2008 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2009 << Context.getTypeDeclType(OwnedDecl);
2010 }
2011
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002012 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2013 D.getSourceRange().getBegin(),
2014 D.getIdentifierLoc(),
2015 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002016 D.isInvalidType());
2017
2018 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2019 if (D.getCXXScopeSpec().isSet()) {
2020 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2021 << D.getCXXScopeSpec().getRange();
2022 New->setInvalidDecl();
2023 }
2024
2025 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002026 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002027 if (D.getIdentifier())
2028 IdResolver.AddDecl(New);
2029
2030 ProcessDeclAttributes(S, New, D);
2031
2032 if (New->hasAttr<BlocksAttr>())
2033 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002034 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002035}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002036
2037/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002038/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002039void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002040 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002041 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2042 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002043 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002044 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002045 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002046 }
2047}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002048
2049void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002050 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002051 unsigned numInitializers) {
2052 if (numInitializers > 0) {
2053 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002054 CXXCtorInitializer **ivarInitializers =
2055 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002056 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002057 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002058 IvarInitializers = ivarInitializers;
2059 }
2060}
2061
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002062void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002063 // Warning will be issued only when selector table is
2064 // generated (which means there is at lease one implementation
2065 // in the TU). This is to match gcc's behavior.
2066 if (ReferencedSelectors.empty() ||
2067 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002068 return;
2069 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2070 ReferencedSelectors.begin(),
2071 E = ReferencedSelectors.end(); S != E; ++S) {
2072 Selector Sel = (*S).first;
2073 if (!LookupImplementedMethodInGlobalPool(Sel))
2074 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2075 }
2076 return;
2077}