blob: 865f22aec5381e7a6036829a3a5a347010c834c1 [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) {
31
32 unsigned DIAG = diag::warn_depercated_def;
33 if (S.Diags.getDiagnosticLevel(DIAG, ImplLoc)== Diagnostic::Ignored)
34 return;
35 if (ND && ND->getAttr<DeprecatedAttr>()) {
36 S.Diag(ImplLoc, DIAG) << select;
37 if (select == 0)
38 S.Diag(ND->getLocation(), diag::note_method_declared_at);
39 else
40 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
41 }
42}
43
Steve Naroffebf64432009-02-28 16:59:13 +000044/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000045/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +000046void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000047 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +000048 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +000049
Steve Naroff394f3f42008-07-25 17:57:26 +000050 // If we don't have a valid method decl, simply return.
51 if (!MDecl)
52 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000053
54 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000056 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000057 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000058 AddFactoryMethodToGlobalPool(MDecl, true);
59
Chris Lattner4d391482007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000062 PushFunctionScope();
63
Chris Lattner4d391482007-12-12 07:09:47 +000064 // Create Decl objects for each parameter, entrring them in the scope for
65 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000066
67 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000068 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000069
Daniel Dunbar451318c2008-08-26 06:07:48 +000070 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
71 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000072
Chris Lattner8123a952008-04-10 02:22:51 +000073 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000074 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +000075 E = MDecl->param_end(); PI != E; ++PI) {
76 ParmVarDecl *Param = (*PI);
77 if (!Param->isInvalidDecl() &&
78 RequireCompleteType(Param->getLocation(), Param->getType(),
79 diag::err_typecheck_decl_incomplete_type))
80 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +000081 if ((*PI)->getIdentifier())
82 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +000083 }
Fariborz Jahanianb1224f62011-02-15 00:59:30 +000084 // Warn on implementating deprecated methods under
85 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000086 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
87 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +000088 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +000089 DiagnoseObjCImplementedDeprecations(*this,
90 dyn_cast<NamedDecl>(IMD),
91 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +000092}
93
John McCalld226f652010-08-21 09:40:31 +000094Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000095ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
96 IdentifierInfo *ClassName, SourceLocation ClassLoc,
97 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +000098 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000099 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000100 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000101 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000104 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000105 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000106
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000108 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000109 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000112 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
113 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000114 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000115 if (!IDecl->isForwardDecl()) {
116 IDecl->setInvalidDecl();
117 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
118 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000119
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000120 // Return the previous class interface.
121 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000122 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000123 } else {
124 IDecl->setLocation(AtInterfaceLoc);
125 IDecl->setForwardDecl(false);
126 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000127 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000128 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000129 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000130
131 // Since this ObjCInterfaceDecl was created by a forward declaration,
132 // we now add it to the DeclContext since it wasn't added before
133 // (see ActOnForwardClassDeclaration).
134 IDecl->setLexicalDeclContext(CurContext);
135 CurContext->addDecl(IDecl);
136
137 if (AttrList)
138 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000139 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000140 } else {
141 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
142 ClassName, ClassLoc);
143 if (AttrList)
144 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
145
146 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000147 }
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattner4d391482007-12-12 07:09:47 +0000149 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000150 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000151 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
152 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000153
154 if (!PrevDecl) {
155 // Try to correct for a typo in the superclass name.
156 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000157 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000158 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
159 Diag(SuperLoc, diag::err_undef_superclass_suggest)
160 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000161 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
162 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000163 }
164 }
165
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000166 if (PrevDecl == IDecl) {
167 Diag(SuperLoc, diag::err_recursive_superclass)
168 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
169 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000170 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000172 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000173
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 // Diagnose classes that inherit from deprecated classes.
175 if (SuperClassDecl)
176 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000178 if (PrevDecl && SuperClassDecl == 0) {
179 // The previous declaration was not a class decl. Check if we have a
180 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000181 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000182 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000183 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000184 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
185 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000186 }
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000189 // This handles the following case:
190 //
191 // typedef int SuperClass;
192 // @interface MyClass : SuperClass {} @end
193 //
194 if (!SuperClassDecl) {
195 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
196 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000197 }
198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000200 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
201 if (!SuperClassDecl)
202 Diag(SuperLoc, diag::err_undef_superclass)
203 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
204 else if (SuperClassDecl->isForwardDecl())
205 Diag(SuperLoc, diag::err_undef_superclass)
206 << SuperClassDecl->getDeclName() << ClassName
207 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000208 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000209 IDecl->setSuperClass(SuperClassDecl);
210 IDecl->setSuperClassLoc(SuperLoc);
211 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000212 }
Chris Lattner4d391482007-12-12 07:09:47 +0000213 } else { // we have a root class.
214 IDecl->setLocEnd(ClassLoc);
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Sebastian Redl0b17c612010-08-13 00:28:03 +0000217 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000218 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000219 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000220 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000221 IDecl->setLocEnd(EndProtoLoc);
222 }
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Anders Carlsson15281452008-11-04 16:57:32 +0000224 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000225 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000226}
227
228/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000229/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
231 IdentifierInfo *AliasName,
232 SourceLocation AliasLocation,
233 IdentifierInfo *ClassName,
234 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000235 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000236 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000237 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000238 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000239 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000240 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000241 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000242 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000243 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000244 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000245 }
246 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000247 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000248 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000249 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
250 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000251 if (T->isObjCObjectType()) {
252 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000253 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000254 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000255 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000256 }
257 }
258 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000259 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
260 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000261 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000262 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000263 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000264 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000267 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000268 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000269 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson15281452008-11-04 16:57:32 +0000271 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000272 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000273
John McCalld226f652010-08-21 09:40:31 +0000274 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000275}
276
Steve Naroff61d68522009-03-05 15:22:01 +0000277void Sema::CheckForwardProtocolDeclarationForCircularDependency(
278 IdentifierInfo *PName,
279 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000280 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000281 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
282 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Douglas Gregorc83c6872010-04-15 22:33:43 +0000284 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
285 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000286 if (PDecl->getIdentifier() == PName) {
287 Diag(Ploc, diag::err_protocol_has_circular_dependency);
288 Diag(PrevLoc, diag::note_previous_definition);
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000291 PDecl->getLocation(), PDecl->getReferencedProtocols());
292 }
293 }
294}
295
John McCalld226f652010-08-21 09:40:31 +0000296Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000297Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
298 IdentifierInfo *ProtocolName,
299 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000300 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000301 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000302 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000303 SourceLocation EndProtoLoc,
304 AttributeList *AttrList) {
305 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000306 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000307 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000308 if (PDecl) {
309 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000310 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000311 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000312 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000313 // Just return the protocol we already had.
314 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000315 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000316 }
Steve Naroff61d68522009-03-05 15:22:01 +0000317 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000318 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000319 CheckForwardProtocolDeclarationForCircularDependency(
320 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Steve Narofff11b5082008-08-13 16:39:22 +0000322 // Make sure the cached decl gets a valid start location.
323 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000324 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000325 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000326 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000327 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000328 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000329 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000330 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000331 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000332 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000333 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000334 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000335 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000336 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000337 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000338 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
339 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000340 PDecl->setLocEnd(EndProtoLoc);
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
343 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000344 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000345}
346
347/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000348/// issues an error if they are not declared. It returns list of
349/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000350void
Chris Lattnere13b9592008-07-26 04:03:38 +0000351Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000352 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000353 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000354 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000355 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000356 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
357 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000358 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000359 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
360 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000361 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000362 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
363 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
364 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000365 Diag(PDecl->getLocation(), diag::note_previous_decl)
366 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000367 }
368 }
369
370 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000371 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000372 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000373 continue;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000376 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000377
378 // If this is a forward declaration and we are supposed to warn in this
379 // case, do it.
380 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000381 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000382 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000383 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000384 }
385}
386
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000387/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000388/// a class method in its extension.
389///
Mike Stump1eb44332009-09-09 15:08:12 +0000390void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000391 ObjCInterfaceDecl *ID) {
392 if (!ID)
393 return; // Possibly due to previous error
394
395 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000396 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
397 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000398 ObjCMethodDecl *MD = *i;
399 MethodMap[MD->getSelector()] = MD;
400 }
401
402 if (MethodMap.empty())
403 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000404 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
405 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000406 ObjCMethodDecl *Method = *i;
407 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
408 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
409 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
410 << Method->getDeclName();
411 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
412 }
413 }
414}
415
Chris Lattner58fe03b2009-04-12 08:43:13 +0000416/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000417Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000418Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000419 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000420 unsigned NumElts,
421 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000422 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000423 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner4d391482007-12-12 07:09:47 +0000425 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000426 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000427 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000428 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000429 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000430 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000431 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000432 PushOnScopeChains(PDecl, TUScope, false);
433 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000434 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000435 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000436 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000437 if (!isNew)
438 PDecl->setChangedSinceDeserialization(true);
439 }
Chris Lattner4d391482007-12-12 07:09:47 +0000440 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000441 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
444 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000445 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000446 Protocols.data(), Protocols.size(),
447 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000448 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000449 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000450 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000451}
452
John McCalld226f652010-08-21 09:40:31 +0000453Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000454ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
455 IdentifierInfo *ClassName, SourceLocation ClassLoc,
456 IdentifierInfo *CategoryName,
457 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000458 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000459 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000460 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000461 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000462 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000463 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000464
465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl()) {
467 // Create an invalid ObjCCategoryDecl to serve as context for
468 // the enclosing method declarations. We mark the decl invalid
469 // to make it clear that this isn't a valid AST.
470 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
471 ClassLoc, CategoryLoc, CategoryName);
472 CDecl->setInvalidDecl();
473 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000474 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000475 }
476
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000477 if (!CategoryName && IDecl->getImplementation()) {
478 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
479 Diag(IDecl->getImplementation()->getLocation(),
480 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000481 }
482
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000483 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
484 ClassLoc, CategoryLoc, CategoryName);
485 // FIXME: PushOnScopeChains?
486 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000487
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000488 CDecl->setClassInterface(IDecl);
489 // Insert class extension to the list of class's categories.
490 if (!CategoryName)
491 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner16b34b42009-02-16 21:30:01 +0000493 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000494 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000495
Fariborz Jahanian25760612010-02-15 21:55:26 +0000496 if (CategoryName) {
497 /// Check for duplicate interface declaration for this category
498 ObjCCategoryDecl *CDeclChain;
499 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
500 CDeclChain = CDeclChain->getNextClassCategory()) {
501 if (CDeclChain->getIdentifier() == CategoryName) {
502 // Class extensions can be declared multiple times.
503 Diag(CategoryLoc, diag::warn_dup_category_def)
504 << ClassName << CategoryName;
505 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
506 break;
507 }
Chris Lattner70f19542009-02-16 21:26:43 +0000508 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000509 if (!CDeclChain)
510 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000511 }
Chris Lattner70f19542009-02-16 21:26:43 +0000512
Chris Lattner4d391482007-12-12 07:09:47 +0000513 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000514 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000515 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000516 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000517 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000518 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000519 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Anders Carlsson15281452008-11-04 16:57:32 +0000522 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000523 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000524}
525
526/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000528/// object.
John McCalld226f652010-08-21 09:40:31 +0000529Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000530 SourceLocation AtCatImplLoc,
531 IdentifierInfo *ClassName, SourceLocation ClassLoc,
532 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000533 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000534 ObjCCategoryDecl *CatIDecl = 0;
535 if (IDecl) {
536 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
537 if (!CatIDecl) {
538 // Category @implementation with no corresponding @interface.
539 // Create and install one.
540 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000541 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000542 CatName);
543 CatIDecl->setClassInterface(IDecl);
544 CatIDecl->insertNextClassCategory();
545 }
546 }
547
Mike Stump1eb44332009-09-09 15:08:12 +0000548 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000549 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
550 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000551 /// Check that class of this category is already completely declared.
552 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000553 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000554
Douglas Gregord0434102009-01-09 00:49:46 +0000555 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000556 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000557
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000558 /// Check that CatName, category name, is not used in another implementation.
559 if (CatIDecl) {
560 if (CatIDecl->getImplementation()) {
561 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
562 << CatName;
563 Diag(CatIDecl->getImplementation()->getLocation(),
564 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000565 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000566 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000567 // Warn on implementating category of deprecated class under
568 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000569 DiagnoseObjCImplementedDeprecations(*this,
570 dyn_cast<NamedDecl>(IDecl),
571 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000572 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Anders Carlsson15281452008-11-04 16:57:32 +0000575 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000576 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000577}
578
John McCalld226f652010-08-21 09:40:31 +0000579Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000580 SourceLocation AtClassImplLoc,
581 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000582 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000583 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000584 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000585 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000586 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000587 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
588 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000591 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000592 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
593 // If this is a forward declaration of an interface, warn.
594 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000595 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000596 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000597 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000598 } else {
599 // We did not find anything with the name ClassName; try to correct for
600 // typos in the class name.
601 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000602 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000603 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000604 // Suggest the (potentially) correct interface name. However, put the
605 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000606 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000607 // provide a code-modification hint or use the typo name for recovery,
608 // because this is just a warning. The program may actually be correct.
609 Diag(ClassLoc, diag::warn_undef_interface_suggest)
610 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000611 Diag(IDecl->getLocation(), diag::note_previous_decl)
612 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000613 << FixItHint::CreateReplacement(ClassLoc,
614 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000615 IDecl = 0;
616 } else {
617 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
618 }
Chris Lattner4d391482007-12-12 07:09:47 +0000619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner4d391482007-12-12 07:09:47 +0000621 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000623 if (SuperClassname) {
624 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000625 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
626 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
629 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000630 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000632 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 Diag(SuperClassLoc, diag::err_undef_superclass)
635 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000636 else if (IDecl && IDecl->getSuperClass() != SDecl) {
637 // This implementation and its interface do not have the same
638 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000640 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000641 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000642 }
643 }
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattner4d391482007-12-12 07:09:47 +0000646 if (!IDecl) {
647 // Legacy case of @implementation with no corresponding @interface.
648 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000649
Mike Stump390b4cc2009-05-16 07:39:55 +0000650 // FIXME: Do we support attributes on the @implementation? If so we should
651 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000652 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000653 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000654 IDecl->setSuperClass(SDecl);
655 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000656
657 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000658 } else {
659 // Mark the interface as being completed, even if it was just as
660 // @class ....;
661 // declaration; the user cannot reopen it.
662 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
665 ObjCImplementationDecl* IMPDecl =
666 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000667 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Anders Carlsson15281452008-11-04 16:57:32 +0000669 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000670 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner4d391482007-12-12 07:09:47 +0000672 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000673 if (IDecl->getImplementation()) {
674 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000676 Diag(IDecl->getImplementation()->getLocation(),
677 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000678 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000679 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000680 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000681 // Warn on implementating deprecated class under
682 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000683 DiagnoseObjCImplementedDeprecations(*this,
684 dyn_cast<NamedDecl>(IDecl),
685 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000686 }
John McCalld226f652010-08-21 09:40:31 +0000687 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000688}
689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
691 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000692 SourceLocation RBrace) {
693 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000694 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000695 if (!IDecl)
696 return;
697 /// Check case of non-existing @interface decl.
698 /// (legacy objective-c @implementation decl without an @interface decl).
699 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000700 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000701 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000702 // Add ivar's to class's DeclContext.
703 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000704 ivars[i]->setLexicalDeclContext(ImpDecl);
705 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000706 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000707 }
708
Chris Lattner4d391482007-12-12 07:09:47 +0000709 return;
710 }
711 // If implementation has empty ivar list, just return.
712 if (numIvars == 0)
713 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner4d391482007-12-12 07:09:47 +0000715 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000716 if (LangOpts.ObjCNonFragileABI2) {
717 if (ImpDecl->getSuperClass())
718 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
719 for (unsigned i = 0; i < numIvars; i++) {
720 ObjCIvarDecl* ImplIvar = ivars[i];
721 if (const ObjCIvarDecl *ClsIvar =
722 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
723 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
724 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
725 continue;
726 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000727 // Instance ivar to Implementation's DeclContext.
728 ImplIvar->setLexicalDeclContext(ImpDecl);
729 IDecl->makeDeclVisibleInContext(ImplIvar, false);
730 ImpDecl->addDecl(ImplIvar);
731 }
732 return;
733 }
Chris Lattner4d391482007-12-12 07:09:47 +0000734 // Check interface's Ivar list against those in the implementation.
735 // names and types must match.
736 //
Chris Lattner4d391482007-12-12 07:09:47 +0000737 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000738 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000739 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
740 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 ObjCIvarDecl* ImplIvar = ivars[j++];
742 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000743 assert (ImplIvar && "missing implementation ivar");
744 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Steve Naroffca331292009-03-03 14:49:36 +0000746 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000747 if (Context.getCanonicalType(ImplIvar->getType()) !=
748 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000750 << ImplIvar->getIdentifier()
751 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000752 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000753 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
754 Expr *ImplBitWidth = ImplIvar->getBitWidth();
755 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000756 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
757 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000758 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
759 << ImplIvar->getIdentifier();
760 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762 }
Steve Naroffca331292009-03-03 14:49:36 +0000763 // Make sure the names are identical.
764 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000765 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000766 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000767 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000768 }
769 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattner609e4c72007-12-12 18:11:49 +0000772 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000773 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000774 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000775 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000776}
777
Steve Naroff3c2eb662008-02-10 21:38:56 +0000778void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000779 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000780 if (!IncompleteImpl) {
781 Diag(ImpLoc, diag::warn_incomplete_impl);
782 IncompleteImpl = true;
783 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000784 if (DiagID == diag::warn_unimplemented_protocol_method)
785 Diag(ImpLoc, DiagID) << method->getDeclName();
786 else
787 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000788}
789
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000790/// Determines if type B can be substituted for type A. Returns true if we can
791/// guarantee that anything that the user will do to an object of type A can
792/// also be done to an object of type B. This is trivially true if the two
793/// types are the same, or if B is a subclass of A. It becomes more complex
794/// in cases where protocols are involved.
795///
796/// Object types in Objective-C describe the minimum requirements for an
797/// object, rather than providing a complete description of a type. For
798/// example, if A is a subclass of B, then B* may refer to an instance of A.
799/// The principle of substitutability means that we may use an instance of A
800/// anywhere that we may use an instance of B - it will implement all of the
801/// ivars of B and all of the methods of B.
802///
803/// This substitutability is important when type checking methods, because
804/// the implementation may have stricter type definitions than the interface.
805/// The interface specifies minimum requirements, but the implementation may
806/// have more accurate ones. For example, a method may privately accept
807/// instances of B, but only publish that it accepts instances of A. Any
808/// object passed to it will be type checked against B, and so will implicitly
809/// by a valid A*. Similarly, a method may return a subclass of the class that
810/// it is declared as returning.
811///
812/// This is most important when considering subclassing. A method in a
813/// subclass must accept any object as an argument that its superclass's
814/// implementation accepts. It may, however, accept a more general type
815/// without breaking substitutability (i.e. you can still use the subclass
816/// anywhere that you can use the superclass, but not vice versa). The
817/// converse requirement applies to return types: the return type for a
818/// subclass method must be a valid object of the kind that the superclass
819/// advertises, but it may be specified more accurately. This avoids the need
820/// for explicit down-casting by callers.
821///
822/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000823static bool isObjCTypeSubstitutable(ASTContext &Context,
824 const ObjCObjectPointerType *A,
825 const ObjCObjectPointerType *B,
826 bool rejectId) {
827 // Reject a protocol-unqualified id.
828 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000829
830 // If B is a qualified id, then A must also be a qualified id and it must
831 // implement all of the protocols in B. It may not be a qualified class.
832 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
833 // stricter definition so it is not substitutable for id<A>.
834 if (B->isObjCQualifiedIdType()) {
835 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000836 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
837 QualType(B,0),
838 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000839 }
840
841 /*
842 // id is a special type that bypasses type checking completely. We want a
843 // warning when it is used in one place but not another.
844 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
845
846
847 // If B is a qualified id, then A must also be a qualified id (which it isn't
848 // if we've got this far)
849 if (B->isObjCQualifiedIdType()) return false;
850 */
851
852 // Now we know that A and B are (potentially-qualified) class types. The
853 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000854 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000855}
856
John McCall10302c02010-10-28 02:34:38 +0000857static SourceRange getTypeRange(TypeSourceInfo *TSI) {
858 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
859}
860
861static void CheckMethodOverrideReturn(Sema &S,
862 ObjCMethodDecl *MethodImpl,
863 ObjCMethodDecl *MethodIface) {
864 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
865 MethodIface->getResultType()))
866 return;
867
868 unsigned DiagID = diag::warn_conflicting_ret_types;
869
870 // Mismatches between ObjC pointers go into a different warning
871 // category, and sometimes they're even completely whitelisted.
872 if (const ObjCObjectPointerType *ImplPtrTy =
873 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
874 if (const ObjCObjectPointerType *IfacePtrTy =
875 MethodIface->getResultType()->getAs<ObjCObjectPointerType>()) {
876 // Allow non-matching return types as long as they don't violate
877 // the principle of substitutability. Specifically, we permit
878 // return types that are subclasses of the declared return type,
879 // or that are more-qualified versions of the declared type.
880 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
881 return;
882
883 DiagID = diag::warn_non_covariant_ret_types;
884 }
885 }
886
887 S.Diag(MethodImpl->getLocation(), DiagID)
888 << MethodImpl->getDeclName()
889 << MethodIface->getResultType()
890 << MethodImpl->getResultType()
891 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
892 S.Diag(MethodIface->getLocation(), diag::note_previous_definition)
893 << getTypeRange(MethodIface->getResultTypeSourceInfo());
894}
895
896static void CheckMethodOverrideParam(Sema &S,
897 ObjCMethodDecl *MethodImpl,
898 ObjCMethodDecl *MethodIface,
899 ParmVarDecl *ImplVar,
900 ParmVarDecl *IfaceVar) {
901 QualType ImplTy = ImplVar->getType();
902 QualType IfaceTy = IfaceVar->getType();
903 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
904 return;
905
906 unsigned DiagID = diag::warn_conflicting_param_types;
907
908 // Mismatches between ObjC pointers go into a different warning
909 // category, and sometimes they're even completely whitelisted.
910 if (const ObjCObjectPointerType *ImplPtrTy =
911 ImplTy->getAs<ObjCObjectPointerType>()) {
912 if (const ObjCObjectPointerType *IfacePtrTy =
913 IfaceTy->getAs<ObjCObjectPointerType>()) {
914 // Allow non-matching argument types as long as they don't
915 // violate the principle of substitutability. Specifically, the
916 // implementation must accept any objects that the superclass
917 // accepts, however it may also accept others.
918 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
919 return;
920
921 DiagID = diag::warn_non_contravariant_param_types;
922 }
923 }
924
925 S.Diag(ImplVar->getLocation(), DiagID)
926 << getTypeRange(ImplVar->getTypeSourceInfo())
927 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
928 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
929 << getTypeRange(IfaceVar->getTypeSourceInfo());
930}
931
932
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000933void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
934 ObjCMethodDecl *IntfMethodDecl) {
John McCall10302c02010-10-28 02:34:38 +0000935 CheckMethodOverrideReturn(*this, ImpMethodDecl, IntfMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner3aff9192009-04-11 19:58:42 +0000937 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
938 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +0000939 IM != EM; ++IM, ++IF)
940 CheckMethodOverrideParam(*this, ImpMethodDecl, IntfMethodDecl, *IM, *IF);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000942 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
943 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
944 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
945 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000946}
947
Mike Stump390b4cc2009-05-16 07:39:55 +0000948/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
949/// improve the efficiency of selector lookups and type checking by associating
950/// with each protocol / interface / category the flattened instance tables. If
951/// we used an immutable set to keep the table then it wouldn't add significant
952/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000953
Steve Naroffefe7f362008-02-08 22:06:17 +0000954/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000955/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000956void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
957 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000958 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000959 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000960 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000961 ObjCContainerDecl *CDecl) {
962 ObjCInterfaceDecl *IDecl;
963 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
964 IDecl = C->getClassInterface();
965 else
966 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
967 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
968
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000969 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000970 ObjCInterfaceDecl *NSIDecl = 0;
971 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000972 // check to see if class implements forwardInvocation method and objects
973 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000974 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000975 // Under such conditions, which means that every method possible is
976 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000977 // found" warnings.
978 // FIXME: Use a general GetUnarySelector method for this.
979 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
980 Selector fISelector = Context.Selectors.getSelector(1, &II);
981 if (InsMap.count(fISelector))
982 // Is IDecl derived from 'NSProxy'? If so, no instance methods
983 // need be implemented in the implementation.
984 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000987 // If a method lookup fails locally we still need to look and see if
988 // the method was implemented by a base class or an inherited
989 // protocol. This lookup is slow, but occurs rarely in correct code
990 // and otherwise would terminate in a warning.
991
Chris Lattner4d391482007-12-12 07:09:47 +0000992 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000993 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000994 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000995 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000996 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000997 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000998 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000999 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001000 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001001 // Ugly, but necessary. Method declared in protcol might have
1002 // have been synthesized due to a property declared in the class which
1003 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001004 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001005 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001006 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001007 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001008 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1009 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001010 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001011 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001012 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1013 << PDecl->getDeclName();
1014 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001015 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001016 }
1017 }
Chris Lattner4d391482007-12-12 07:09:47 +00001018 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001019 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001020 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001021 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001023 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1024 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001025 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001026 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001027 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001028 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001029 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001030 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1031 PDecl->getDeclName();
1032 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001033 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001034 }
Chris Lattner780f3292008-07-21 21:32:27 +00001035 // Check on this protocols's referenced protocols, recursively.
1036 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1037 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001038 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001039}
1040
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001041/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1042/// or protocol against those declared in their implementations.
1043///
1044void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1045 const llvm::DenseSet<Selector> &ClsMap,
1046 llvm::DenseSet<Selector> &InsMapSeen,
1047 llvm::DenseSet<Selector> &ClsMapSeen,
1048 ObjCImplDecl* IMPDecl,
1049 ObjCContainerDecl* CDecl,
1050 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001051 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001052 // Check and see if instance methods in class interface have been
1053 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1055 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001056 if (InsMapSeen.count((*I)->getSelector()))
1057 continue;
1058 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001059 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001060 !InsMap.count((*I)->getSelector())) {
1061 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001062 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1063 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001064 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001065 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001066 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001067 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001068 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001069 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001070 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001071 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1072 // ImpMethodDecl may be null as in a @dynamic property.
1073 if (ImpMethodDecl)
1074 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1075 }
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001078 // Check and see if class methods in class interface have been
1079 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001080 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001081 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001082 if (ClsMapSeen.count((*I)->getSelector()))
1083 continue;
1084 ClsMapSeen.insert((*I)->getSelector());
1085 if (!ClsMap.count((*I)->getSelector())) {
1086 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001087 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1088 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001089 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001090 ObjCMethodDecl *ImpMethodDecl =
1091 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001092 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001093 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001094 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1095 }
1096 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001097
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001098 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001099 // Also methods in class extensions need be looked at next.
1100 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1101 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1102 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1103 IMPDecl,
1104 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1105 IncompleteImpl, false);
1106
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001107 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001108 for (ObjCInterfaceDecl::all_protocol_iterator
1109 PI = I->all_referenced_protocol_begin(),
1110 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001111 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1112 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001113 (*PI), IncompleteImpl, false);
1114 if (I->getSuperClass())
1115 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001116 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001117 I->getSuperClass(), IncompleteImpl, false);
1118 }
1119}
1120
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001121void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001122 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001123 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001124 llvm::DenseSet<Selector> InsMap;
1125 // Check and see if instance methods in class interface have been
1126 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001127 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001128 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001129 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001131 // Check and see if properties declared in the interface have either 1)
1132 // an implementation or 2) there is a @synthesize/@dynamic implementation
1133 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001134 if (isa<ObjCInterfaceDecl>(CDecl) &&
1135 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001136 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001137
Chris Lattner4d391482007-12-12 07:09:47 +00001138 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001139 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001140 I = IMPDecl->classmeth_begin(),
1141 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001142 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001144 // Check for type conflict of methods declared in a class/protocol and
1145 // its implementation; if any.
1146 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001147 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1148 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001149 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattner4d391482007-12-12 07:09:47 +00001151 // Check the protocol list for unimplemented methods in the @implementation
1152 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001153 // Check and see if class methods in class interface have been
1154 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Chris Lattnercddc8882009-03-01 00:56:52 +00001156 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001157 for (ObjCInterfaceDecl::all_protocol_iterator
1158 PI = I->all_referenced_protocol_begin(),
1159 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001160 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001161 InsMap, ClsMap, I);
1162 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001163 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1164 Categories; Categories = Categories->getNextClassExtension())
1165 ImplMethodsVsClassMethods(S, IMPDecl,
1166 const_cast<ObjCCategoryDecl*>(Categories),
1167 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001168 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001169 // For extended class, unimplemented methods in its protocols will
1170 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001171 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001172 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1173 E = C->protocol_end(); PI != E; ++PI)
1174 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001175 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001176 // Report unimplemented properties in the category as well.
1177 // When reporting on missing setter/getters, do not report when
1178 // setter/getter is implemented in category's primary class
1179 // implementation.
1180 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1181 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1182 for (ObjCImplementationDecl::instmeth_iterator
1183 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1184 InsMap.insert((*I)->getSelector());
1185 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001186 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001187 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001188 } else
1189 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001190}
1191
Mike Stump1eb44332009-09-09 15:08:12 +00001192/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001193Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001194Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001195 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001196 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001197 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001198 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner4d391482007-12-12 07:09:47 +00001200 for (unsigned i = 0; i != NumElts; ++i) {
1201 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001202 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001203 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001204 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001205 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001206 // Maybe we will complain about the shadowed template parameter.
1207 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1208 // Just pretend that we didn't see the previous declaration.
1209 PrevDecl = 0;
1210 }
1211
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001213 // GCC apparently allows the following idiom:
1214 //
1215 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1216 // @class XCElementToggler;
1217 //
Mike Stump1eb44332009-09-09 15:08:12 +00001218 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001219 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001220 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001221 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001222 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001223 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001224 // a forward class declaration matching a typedef name of a class refers
1225 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001226 if (const ObjCObjectType *OI =
1227 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1228 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001229 }
Chris Lattner4d391482007-12-12 07:09:47 +00001230 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001231 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1232 if (!IDecl) { // Not already seen? Make a forward decl.
1233 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1234 IdentList[i], IdentLocs[i], true);
1235
1236 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1237 // the current DeclContext. This prevents clients that walk DeclContext
1238 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1239 // declared later (if at all). We also take care to explicitly make
1240 // sure this declaration is visible for name lookup.
1241 PushOnScopeChains(IDecl, TUScope, false);
1242 CurContext->makeDeclVisibleInContext(IDecl, true);
1243 }
Chris Lattner4d391482007-12-12 07:09:47 +00001244
1245 Interfaces.push_back(IDecl);
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek321c22f2009-11-18 00:28:11 +00001248 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001249 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001250 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001251 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001252 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001253 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001254 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001255}
1256
1257
1258/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1259/// returns true, or false, accordingly.
1260/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001261bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001262 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001263 bool matchBasedOnSizeAndAlignment,
1264 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001265 QualType T1 = Context.getCanonicalType(Method->getResultType());
1266 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001268 if (T1 != T2) {
1269 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001270 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001271 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001272 // Incomplete types don't have a size and alignment.
1273 if (T1->isIncompleteType() || T2->isIncompleteType())
1274 return false;
1275 // Check is based on size and alignment.
1276 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1277 return false;
1278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Chris Lattner89951a82009-02-20 18:43:26 +00001280 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1281 E = Method->param_end();
1282 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Chris Lattner89951a82009-02-20 18:43:26 +00001284 for (; ParamI != E; ++ParamI, ++PrevI) {
1285 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1286 T1 = Context.getCanonicalType((*ParamI)->getType());
1287 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001288 if (T1 != T2) {
1289 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001290 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001291 return false;
1292 // Incomplete types don't have a size and alignment.
1293 if (T1->isIncompleteType() || T2->isIncompleteType())
1294 return false;
1295 // Check is based on size and alignment.
1296 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1297 return false;
1298 }
Chris Lattner4d391482007-12-12 07:09:47 +00001299 }
1300 return true;
1301}
1302
Sebastian Redldb9d2142010-08-02 23:18:59 +00001303/// \brief Read the contents of the method pool for a given selector from
1304/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001305///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001306/// This routine should only be called once, when the method pool has no entry
1307/// for this selector.
1308Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001309 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001310 assert(MethodPool.find(Sel) == MethodPool.end() &&
1311 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001312
1313 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001314 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Sebastian Redldb9d2142010-08-02 23:18:59 +00001316 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001317}
1318
Sebastian Redldb9d2142010-08-02 23:18:59 +00001319void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1320 bool instance) {
1321 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1322 if (Pos == MethodPool.end()) {
1323 if (ExternalSource)
1324 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001325 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001326 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1327 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001328 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001329 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001330 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001331 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001332 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001333 Entry.Method = Method;
1334 Entry.Next = 0;
1335 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattnerb25df352009-03-04 05:16:45 +00001338 // We've seen a method with this name, see if we have already seen this type
1339 // signature.
1340 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001341 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001342 ObjCMethodDecl *PrevObjCMethod = List->Method;
1343 PrevObjCMethod->setDefined(impl);
1344 // If a method is deprecated, push it in the global pool.
1345 // This is used for better diagnostics.
1346 if (Method->getAttr<DeprecatedAttr>()) {
1347 if (!PrevObjCMethod->getAttr<DeprecatedAttr>())
1348 List->Method = Method;
1349 }
1350 // If new method is unavailable, push it into global pool
1351 // unless previous one is deprecated.
1352 if (Method->getAttr<UnavailableAttr>()) {
1353 if (!PrevObjCMethod->getAttr<UnavailableAttr>() &&
1354 !PrevObjCMethod->getAttr<DeprecatedAttr>())
1355 List->Method = Method;
1356 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001357 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001358 }
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Chris Lattnerb25df352009-03-04 05:16:45 +00001360 // We have a new signature for an existing method - add it.
1361 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001362 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1363 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001364}
1365
Sebastian Redldb9d2142010-08-02 23:18:59 +00001366ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001367 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001368 bool warn, bool instance) {
1369 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1370 if (Pos == MethodPool.end()) {
1371 if (ExternalSource)
1372 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001373 else
1374 return 0;
1375 }
1376
Sebastian Redldb9d2142010-08-02 23:18:59 +00001377 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001379 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001380 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1381 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001382 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001383 if (warn && MethList.Method && MethList.Next) {
1384 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001385 if (strictSelectorMatch)
1386 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1387 // This checks if the methods differ in type mismatch.
1388 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1389 issueWarning = true;
1390 }
1391
1392 if (!issueWarning)
1393 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1394 // This checks if the methods differ by size & alignment.
1395 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1396 issueWarning = true;
1397 }
1398
Sebastian Redldb9d2142010-08-02 23:18:59 +00001399 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001400 if (strictSelectorMatch)
1401 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1402 else
1403 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001404 Diag(MethList.Method->getLocStart(), diag::note_using)
1405 << MethList.Method->getSourceRange();
1406 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1407 Diag(Next->Method->getLocStart(), diag::note_also_found)
1408 << Next->Method->getSourceRange();
1409 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001410 }
1411 return MethList.Method;
1412}
1413
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001414ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001415 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1416 if (Pos == MethodPool.end())
1417 return 0;
1418
1419 GlobalMethods &Methods = Pos->second;
1420
1421 if (Methods.first.Method && Methods.first.Method->isDefined())
1422 return Methods.first.Method;
1423 if (Methods.second.Method && Methods.second.Method->isDefined())
1424 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001425 return 0;
1426}
1427
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001428/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1429/// identical selector names in current and its super classes and issues
1430/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001431void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1432 ObjCMethodDecl *Method,
1433 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001434 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1435 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001437 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001438 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001439 SD->lookupMethod(Method->getSelector(), IsInstance);
1440 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001441 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001442 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001443 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001444 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1445 E = Method->param_end();
1446 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1447 for (; ParamI != E; ++ParamI, ++PrevI) {
1448 // Number of parameters are the same and is guaranteed by selector match.
1449 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1450 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1451 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1452 // If type of arguement of method in this class does not match its
1453 // respective argument type in the super class method, issue warning;
1454 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001455 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001456 << T1 << T2;
1457 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1458 return;
1459 }
1460 }
1461 ID = SD;
1462 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001463}
1464
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001465/// DiagnoseDuplicateIvars -
1466/// Check for duplicate ivars in the entire class at the start of
1467/// @implementation. This becomes necesssary because class extension can
1468/// add ivars to a class in random order which will not be known until
1469/// class's @implementation is seen.
1470void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1471 ObjCInterfaceDecl *SID) {
1472 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1473 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1474 ObjCIvarDecl* Ivar = (*IVI);
1475 if (Ivar->isInvalidDecl())
1476 continue;
1477 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1478 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1479 if (prevIvar) {
1480 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1481 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1482 Ivar->setInvalidDecl();
1483 }
1484 }
1485 }
1486}
1487
Steve Naroffa56f6162007-12-18 01:30:32 +00001488// Note: For class/category implemenations, allMethods/allProperties is
1489// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001490void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001491 Decl *ClassDecl,
1492 Decl **allMethods, unsigned allNum,
1493 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001494 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001495 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1496 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001497 // should be true.
1498 if (!ClassDecl)
1499 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001500
Mike Stump1eb44332009-09-09 15:08:12 +00001501 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001502 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1503 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001504 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001505
Ted Kremenek782f2f52010-01-07 01:20:12 +00001506 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1507 // FIXME: This is wrong. We shouldn't be pretending that there is
1508 // an '@end' in the declaration.
1509 SourceLocation L = ClassDecl->getLocation();
1510 AtEnd.setBegin(L);
1511 AtEnd.setEnd(L);
1512 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001513 }
1514
Steve Naroff0701bbb2009-01-08 17:28:14 +00001515 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1516 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1517 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1518
Chris Lattner4d391482007-12-12 07:09:47 +00001519 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001520 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001521 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001522
1523 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001524 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001525 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001526 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001527 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001528 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001529 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001530 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001531 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001532 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001533 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001534 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001535 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001536 InsMap[Method->getSelector()] = Method;
1537 /// The following allows us to typecheck messages to "id".
1538 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001539 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001540 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001541 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001542 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001543 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001544 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001546 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001547 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001549 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001550 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001551 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001552 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001553 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001554 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001555 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001556 /// The following allows us to typecheck messages to "Class".
1557 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001558 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001559 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001560 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001561 }
1562 }
1563 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001564 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001565 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001566 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001567 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001568 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001569 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001570 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001571 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001572 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001573
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001574 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001575 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001576 if (C->IsClassExtension()) {
1577 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1578 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001579 }
Chris Lattner4d391482007-12-12 07:09:47 +00001580 }
Steve Naroff09c47192009-01-09 15:36:25 +00001581 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001582 if (CDecl->getIdentifier())
1583 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1584 // user-defined setter/getter. It also synthesizes setter/getter methods
1585 // and adds them to the DeclContext and global method pools.
1586 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1587 E = CDecl->prop_end();
1588 I != E; ++I)
1589 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001590 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001591 }
1592 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001593 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001594 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001595 // Any property declared in a class extension might have user
1596 // declared setter or getter in current class extension or one
1597 // of the other class extensions. Mark them as synthesized as
1598 // property will be synthesized when property with same name is
1599 // seen in the @implementation.
1600 for (const ObjCCategoryDecl *ClsExtDecl =
1601 IDecl->getFirstClassExtension();
1602 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1603 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1604 E = ClsExtDecl->prop_end(); I != E; ++I) {
1605 ObjCPropertyDecl *Property = (*I);
1606 // Skip over properties declared @dynamic
1607 if (const ObjCPropertyImplDecl *PIDecl
1608 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1609 if (PIDecl->getPropertyImplementation()
1610 == ObjCPropertyImplDecl::Dynamic)
1611 continue;
1612
1613 for (const ObjCCategoryDecl *CExtDecl =
1614 IDecl->getFirstClassExtension();
1615 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1616 if (ObjCMethodDecl *GetterMethod =
1617 CExtDecl->getInstanceMethod(Property->getGetterName()))
1618 GetterMethod->setSynthesized(true);
1619 if (!Property->isReadOnly())
1620 if (ObjCMethodDecl *SetterMethod =
1621 CExtDecl->getInstanceMethod(Property->getSetterName()))
1622 SetterMethod->setSynthesized(true);
1623 }
1624 }
1625 }
1626
Ted Kremenekc32647d2010-12-23 21:35:43 +00001627 if (LangOpts.ObjCDefaultSynthProperties &&
1628 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001629 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001630 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001631 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001632
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001633 if (LangOpts.ObjCNonFragileABI2)
1634 while (IDecl->getSuperClass()) {
1635 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1636 IDecl = IDecl->getSuperClass();
1637 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001638 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001639 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001640 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001641 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001642 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chris Lattner4d391482007-12-12 07:09:47 +00001644 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001645 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001646 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001647 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001648 Categories; Categories = Categories->getNextClassCategory()) {
1649 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001650 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001651 break;
1652 }
1653 }
1654 }
1655 }
Chris Lattner682bf922009-03-29 16:50:03 +00001656 if (isInterfaceDeclKind) {
1657 // Reject invalid vardecls.
1658 for (unsigned i = 0; i != tuvNum; i++) {
1659 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1660 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1661 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001662 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001663 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001664 }
Chris Lattner682bf922009-03-29 16:50:03 +00001665 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001666 }
Chris Lattner4d391482007-12-12 07:09:47 +00001667}
1668
1669
1670/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1671/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001672static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001673CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1674 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1675 if (PQTVal & ObjCDeclSpec::DQ_In)
1676 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1677 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1678 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1679 if (PQTVal & ObjCDeclSpec::DQ_Out)
1680 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1681 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1682 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1683 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1684 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1685 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1686 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001687
1688 return ret;
1689}
1690
Ted Kremenek422bae72010-04-18 04:59:38 +00001691static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001692bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001693 // The 'ibaction' attribute is allowed on method definitions because of
1694 // how the IBAction macro is used on both method declarations and definitions.
1695 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001696 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1697 if ((*i)->getKind() != attr::IBAction)
1698 return true;
1699 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001700}
1701
John McCalld226f652010-08-21 09:40:31 +00001702Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001703 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001704 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001705 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001706 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001707 Selector Sel,
1708 // optional arguments. The number of types/arguments is obtained
1709 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001710 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001711 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001712 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1713 bool isVariadic) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001714 // Make sure we can establish a context for the method.
1715 if (!ClassDecl) {
1716 Diag(MethodLoc, diag::error_missing_method_context);
John McCall781472f2010-08-25 08:40:02 +00001717 getCurFunction()->LabelMap.clear();
John McCalld226f652010-08-21 09:40:31 +00001718 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001719 }
Chris Lattner4d391482007-12-12 07:09:47 +00001720 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001722 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001723 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001724 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Steve Naroffccef3712009-02-20 22:59:16 +00001726 // Methods cannot return interface types. All ObjC objects are
1727 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001728 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001729 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1730 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001731 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001732 }
1733 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001735
1736 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001737 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001738 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001739 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001740 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001741 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001742 MethodDeclKind == tok::objc_optional ?
1743 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001744 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Chris Lattner0ed844b2008-04-04 06:12:32 +00001746 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Chris Lattner7db638d2009-04-11 19:42:43 +00001748 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001749 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001750 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Chris Lattnere294d3f2009-04-11 18:57:04 +00001752 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001753 ArgType = Context.getObjCIdType();
1754 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001755 } else {
John McCall58e46772009-10-23 21:48:59 +00001756 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001757 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001758 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001759 }
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001761 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1762 LookupOrdinaryName, ForRedeclaration);
1763 LookupName(R, S);
1764 if (R.isSingleResult()) {
1765 NamedDecl *PrevDecl = R.getFoundDecl();
1766 if (S->isDeclScope(PrevDecl)) {
1767 // FIXME. This should be an error; but will break projects.
1768 Diag(ArgInfo[i].NameLoc, diag::warn_method_param_redefinition)
1769 << ArgInfo[i].Name;
1770 Diag(PrevDecl->getLocation(),
1771 diag::note_previous_declaration);
1772 }
1773 }
1774
John McCall58e46772009-10-23 21:48:59 +00001775 ParmVarDecl* Param
1776 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1777 ArgInfo[i].Name, ArgType, DI,
John McCalld931b082010-08-26 03:08:43 +00001778 SC_None, SC_None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001779
John McCallc12c5bb2010-05-15 11:32:37 +00001780 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001781 Diag(ArgInfo[i].NameLoc,
1782 diag::err_object_cannot_be_passed_returned_by_value)
1783 << 1 << ArgType;
1784 Param->setInvalidDecl();
1785 }
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Chris Lattner0ed844b2008-04-04 06:12:32 +00001787 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001788 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001790 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001791 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001793 S->AddDecl(Param);
1794 IdResolver.AddDecl(Param);
1795
Chris Lattner0ed844b2008-04-04 06:12:32 +00001796 Params.push_back(Param);
1797 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001798
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001799 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001800 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001801 QualType ArgType = Param->getType();
1802 if (ArgType.isNull())
1803 ArgType = Context.getObjCIdType();
1804 else
1805 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1806 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001807 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001808 Diag(Param->getLocation(),
1809 diag::err_object_cannot_be_passed_returned_by_value)
1810 << 1 << ArgType;
1811 Param->setInvalidDecl();
1812 }
1813 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001814
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001815 Params.push_back(Param);
1816 }
1817
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001818 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1819 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001820 ObjCMethod->setObjCDeclQualifier(
1821 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1822 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001823
1824 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001825 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001826
John McCall54abf7d2009-11-04 02:18:39 +00001827 const ObjCMethodDecl *InterfaceMD = 0;
1828
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001829 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00001830 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001831 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001832 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001833 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1834 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001835 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001836 PrevMethod = ImpDecl->getClassMethod(Sel);
1837 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001838 }
John McCall54abf7d2009-11-04 02:18:39 +00001839 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1840 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001841 if (ObjCMethod->hasAttrs() &&
1842 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001843 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001844 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001845 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001846 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001847 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1848 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001849 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001850 PrevMethod = CatImpDecl->getClassMethod(Sel);
1851 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001852 }
Sean Huntcf807c42010-08-18 23:23:40 +00001853 if (ObjCMethod->hasAttrs() &&
1854 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001855 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001856 } else {
1857 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001858 }
1859 if (PrevMethod) {
1860 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001861 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001862 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001863 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001864 }
John McCall54abf7d2009-11-04 02:18:39 +00001865
1866 // If the interface declared this method, and it was deprecated there,
1867 // mark it deprecated here.
Sean Huntcf807c42010-08-18 23:23:40 +00001868 if (InterfaceMD)
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001869 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>()) {
1870 StringLiteral *SE = StringLiteral::CreateEmpty(Context, 1);
1871 ObjCMethod->addAttr(::new (Context)
1872 DeprecatedAttr(DA->getLocation(),
1873 Context,
1874 SE->getString()));
1875 }
John McCall54abf7d2009-11-04 02:18:39 +00001876
John McCalld226f652010-08-21 09:40:31 +00001877 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001878}
1879
Chris Lattnercc98eac2008-12-17 07:13:27 +00001880bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001881 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001882 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Anders Carlsson15281452008-11-04 16:57:32 +00001884 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1885 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Anders Carlsson15281452008-11-04 16:57:32 +00001887 return true;
1888}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001889
Chris Lattnercc98eac2008-12-17 07:13:27 +00001890/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1891/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001892void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001893 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001894 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001895 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001896 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001897 if (!Class) {
1898 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1899 return;
1900 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001901 if (LangOpts.ObjCNonFragileABI) {
1902 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1903 return;
1904 }
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Chris Lattnercc98eac2008-12-17 07:13:27 +00001906 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001907 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1908 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001909 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001910 for (unsigned i = 0; i < Ivars.size(); i++) {
1911 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001912 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001913 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1914 ID->getIdentifier(), ID->getType(),
1915 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001916 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001917 }
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Chris Lattnercc98eac2008-12-17 07:13:27 +00001919 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001920 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001921 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001922 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001923 if (getLangOptions().CPlusPlus)
1924 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001925 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001926 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001927 }
1928}
1929
Douglas Gregor160b5632010-04-26 17:32:49 +00001930/// \brief Build a type-check a new Objective-C exception variable declaration.
1931VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1932 QualType T,
1933 IdentifierInfo *Name,
1934 SourceLocation NameLoc,
1935 bool Invalid) {
1936 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1937 // duration shall not be qualified by an address-space qualifier."
1938 // Since all parameters have automatic store duration, they can not have
1939 // an address space.
1940 if (T.getAddressSpace() != 0) {
1941 Diag(NameLoc, diag::err_arg_with_address_space);
1942 Invalid = true;
1943 }
1944
1945 // An @catch parameter must be an unqualified object pointer type;
1946 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1947 if (Invalid) {
1948 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001949 } else if (T->isDependentType()) {
1950 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001951 } else if (!T->isObjCObjectPointerType()) {
1952 Invalid = true;
1953 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1954 } else if (T->isObjCQualifiedIdType()) {
1955 Invalid = true;
1956 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1957 }
1958
1959 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
John McCalld931b082010-08-26 03:08:43 +00001960 SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00001961 New->setExceptionVariable(true);
1962
Douglas Gregor160b5632010-04-26 17:32:49 +00001963 if (Invalid)
1964 New->setInvalidDecl();
1965 return New;
1966}
1967
John McCalld226f652010-08-21 09:40:31 +00001968Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001969 const DeclSpec &DS = D.getDeclSpec();
1970
1971 // We allow the "register" storage class on exception variables because
1972 // GCC did, but we drop it completely. Any other storage class is an error.
1973 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1974 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1975 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1976 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1977 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1978 << DS.getStorageClassSpec();
1979 }
1980 if (D.getDeclSpec().isThreadSpecified())
1981 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1982 D.getMutableDeclSpec().ClearStorageClassSpecs();
1983
1984 DiagnoseFunctionSpecifiers(D);
1985
1986 // Check that there are no default arguments inside the type of this
1987 // exception object (C++ only).
1988 if (getLangOptions().CPlusPlus)
1989 CheckExtraCXXDefaultArguments(D);
1990
Douglas Gregor160b5632010-04-26 17:32:49 +00001991 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001992 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1993 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001994
1995 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1996 // Objective-C++: Types shall not be defined in exception types.
1997 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1998 << Context.getTypeDeclType(OwnedDecl);
1999 }
2000
2001 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
2002 D.getIdentifierLoc(),
2003 D.isInvalidType());
2004
2005 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2006 if (D.getCXXScopeSpec().isSet()) {
2007 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2008 << D.getCXXScopeSpec().getRange();
2009 New->setInvalidDecl();
2010 }
2011
2012 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002013 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002014 if (D.getIdentifier())
2015 IdResolver.AddDecl(New);
2016
2017 ProcessDeclAttributes(S, New, D);
2018
2019 if (New->hasAttr<BlocksAttr>())
2020 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002021 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002022}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002023
2024/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002025/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002026void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002027 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002028 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2029 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002030 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002031 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002032 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002033 }
2034}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002035
2036void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002037 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002038 unsigned numInitializers) {
2039 if (numInitializers > 0) {
2040 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002041 CXXCtorInitializer **ivarInitializers =
2042 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002043 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002044 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002045 IvarInitializers = ivarInitializers;
2046 }
2047}
2048
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002049void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002050 // Warning will be issued only when selector table is
2051 // generated (which means there is at lease one implementation
2052 // in the TU). This is to match gcc's behavior.
2053 if (ReferencedSelectors.empty() ||
2054 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002055 return;
2056 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2057 ReferencedSelectors.begin(),
2058 E = ReferencedSelectors.end(); S != E; ++S) {
2059 Selector Sel = (*S).first;
2060 if (!LookupImplementedMethodInGlobalPool(Sel))
2061 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2062 }
2063 return;
2064}