blob: 9bc0846901aa7cf3f2705eb9a87bd3788372b1c5 [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
14#include "Sema.h"
Douglas Gregorf06cdae2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Steve Naroffebf64432009-02-28 16:59:13 +000023/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000024/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000028
Steve Naroff394f3f42008-07-25 17:57:26 +000029 // If we don't have a valid method decl, simply return.
30 if (!MDecl)
31 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000032
33 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattner4d391482007-12-12 07:09:47 +000039 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000041 PushFunctionScope();
42
Chris Lattner4d391482007-12-12 07:09:47 +000043 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000048
Daniel Dunbar451318c2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000051
Chris Lattner8123a952008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000057}
58
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000064 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000065 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000066 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner4d391482007-12-12 07:09:47 +000068 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000070 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000071 // Maybe we will complain about the shadowed template parameter.
72 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
73 // Just pretend that we didn't see the previous declaration.
74 PrevDecl = 0;
75 }
76
Ted Kremeneka526c5c2008-01-07 19:49:32 +000077 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000078 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000079 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremeneka526c5c2008-01-07 19:49:32 +000082 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000083 if (IDecl) {
84 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000085 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000086 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000087 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000088 Diag(IDecl->getLocation(), diag::note_previous_definition);
89
Steve Naroffcfe8bf32008-11-18 19:15:30 +000090 // Return the previous class interface.
91 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000092 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000093 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000094 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +000096 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +000097
98 // Since this ObjCInterfaceDecl was created by a forward declaration,
99 // we now add it to the DeclContext since it wasn't added before
100 // (see ActOnForwardClassDeclaration).
101 CurContext->addDecl(IDecl);
102
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000103 if (AttrList)
104 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000105 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000106 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000108 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000109 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000110 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Steve Naroffa7503a72009-04-23 15:15:40 +0000112 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner4d391482007-12-12 07:09:47 +0000115 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000116 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000117 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000118
119 if (!PrevDecl) {
120 // Try to correct for a typo in the superclass name.
121 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
122 if (CorrectTypo(R, TUScope, 0) &&
123 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
124 Diag(SuperLoc, diag::err_undef_superclass_suggest)
125 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000126 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
127 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000128 }
129 }
130
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000131 if (PrevDecl == IDecl) {
132 Diag(SuperLoc, diag::err_recursive_superclass)
133 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
134 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000135 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000136 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000137 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000138
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000139 // Diagnose classes that inherit from deprecated classes.
140 if (SuperClassDecl)
141 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000143 if (PrevDecl && SuperClassDecl == 0) {
144 // The previous declaration was not a class decl. Check if we have a
145 // typedef. If we do, get the underlying class type.
146 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
147 QualType T = TDecl->getUnderlyingType();
148 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000149 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000150 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
151 }
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000154 // This handles the following case:
155 //
156 // typedef int SuperClass;
157 // @interface MyClass : SuperClass {} @end
158 //
159 if (!SuperClassDecl) {
160 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
161 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000162 }
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000165 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
166 if (!SuperClassDecl)
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
169 else if (SuperClassDecl->isForwardDecl())
170 Diag(SuperLoc, diag::err_undef_superclass)
171 << SuperClassDecl->getDeclName() << ClassName
172 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000173 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 IDecl->setSuperClass(SuperClassDecl);
175 IDecl->setSuperClassLoc(SuperLoc);
176 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000177 }
Chris Lattner4d391482007-12-12 07:09:47 +0000178 } else { // we have a root class.
179 IDecl->setLocEnd(ClassLoc);
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000182 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000183 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000184 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000185 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000186 IDecl->setLocEnd(EndProtoLoc);
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlsson15281452008-11-04 16:57:32 +0000189 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000190 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000191}
192
193/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000194/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000195Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000196 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000197 SourceLocation AliasLocation,
198 IdentifierInfo *ClassName,
199 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000200 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000201 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000202 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000203 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000204 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000205 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000206 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000207 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000208 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000209 }
210 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000211 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000212 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
213 QualType T = TDecl->getUnderlyingType();
214 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000215 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000216 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000217 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000218 }
219 }
220 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000221 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
222 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000223 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000224 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000225 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000226 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000229 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000230 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000231 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Anders Carlsson15281452008-11-04 16:57:32 +0000233 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000234 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000235
Chris Lattnerb28317a2009-03-28 19:18:32 +0000236 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000237}
238
Steve Naroff61d68522009-03-05 15:22:01 +0000239void Sema::CheckForwardProtocolDeclarationForCircularDependency(
240 IdentifierInfo *PName,
241 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000242 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000243 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
244 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Douglas Gregor6e378de2009-04-23 23:18:26 +0000246 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000247 if (PDecl->getIdentifier() == PName) {
248 Diag(Ploc, diag::err_protocol_has_circular_dependency);
249 Diag(PrevLoc, diag::note_previous_definition);
250 }
Mike Stump1eb44332009-09-09 15:08:12 +0000251 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000252 PDecl->getLocation(), PDecl->getReferencedProtocols());
253 }
254 }
255}
256
Chris Lattnerb28317a2009-03-28 19:18:32 +0000257Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000258Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
259 IdentifierInfo *ProtocolName,
260 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000261 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000262 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000263 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000264 SourceLocation EndProtoLoc,
265 AttributeList *AttrList) {
266 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000267 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000268 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000269 if (PDecl) {
270 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000271 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000272 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000273 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000274 // Just return the protocol we already had.
275 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000276 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000277 }
Steve Naroff61d68522009-03-05 15:22:01 +0000278 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000279 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000280 CheckForwardProtocolDeclarationForCircularDependency(
281 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
282 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Steve Narofff11b5082008-08-13 16:39:22 +0000284 // Make sure the cached decl gets a valid start location.
285 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000286 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000287 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000288 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000289 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000290 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000291 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000292 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000293 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000294 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000295 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000296 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000297 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
298 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000299 PDecl->setLocEnd(EndProtoLoc);
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
302 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000303 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000304}
305
306/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000307/// issues an error if they are not declared. It returns list of
308/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000309void
Chris Lattnere13b9592008-07-26 04:03:38 +0000310Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000311 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000312 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000313 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000314 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000315 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000316 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000317 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
318 LookupObjCProtocolName);
319 if (CorrectTypo(R, TUScope, 0) &&
320 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
321 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
322 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000323 Diag(PDecl->getLocation(), diag::note_previous_decl)
324 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000325 }
326 }
327
328 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000329 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000330 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000331 continue;
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000334 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000335
336 // If this is a forward declaration and we are supposed to warn in this
337 // case, do it.
338 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000339 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000340 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000341 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000342 }
343}
344
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000345/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000346/// a class method in its extension.
347///
Mike Stump1eb44332009-09-09 15:08:12 +0000348void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000349 ObjCInterfaceDecl *ID) {
350 if (!ID)
351 return; // Possibly due to previous error
352
353 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000354 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
355 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000356 ObjCMethodDecl *MD = *i;
357 MethodMap[MD->getSelector()] = MD;
358 }
359
360 if (MethodMap.empty())
361 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000362 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
363 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000364 ObjCMethodDecl *Method = *i;
365 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
366 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
367 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
368 << Method->getDeclName();
369 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
370 }
371 }
372}
373
Chris Lattner58fe03b2009-04-12 08:43:13 +0000374/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000375Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000376Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000377 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000378 unsigned NumElts,
379 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000381 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner4d391482007-12-12 07:09:47 +0000383 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000385 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000386 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000387 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000388 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000389 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000390 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000391 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000392 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000393 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000394 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
397 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000398 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000399 Protocols.data(), Protocols.size(),
400 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000401 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000402 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000403 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000404}
405
Chris Lattnerb28317a2009-03-28 19:18:32 +0000406Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000407ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
408 IdentifierInfo *ClassName, SourceLocation ClassLoc,
409 IdentifierInfo *CategoryName,
410 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000411 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000412 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000413 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000414 SourceLocation EndProtoLoc) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000415 ObjCCategoryDecl *CDecl = 0;
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000416 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Ted Kremenek09b68972010-02-23 19:39:46 +0000417
418 /// Check that class of this category is already completely declared.
419 if (!IDecl || IDecl->isForwardDecl()) {
420 // Create an invalid ObjCCategoryDecl to serve as context for
421 // the enclosing method declarations. We mark the decl invalid
422 // to make it clear that this isn't a valid AST.
423 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
424 ClassLoc, CategoryLoc, CategoryName);
425 CDecl->setInvalidDecl();
426 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
427 return DeclPtrTy::make(CDecl);
428 }
429
430 if (!CategoryName) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000431 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek09b68972010-02-23 19:39:46 +0000432 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000433 CDecl = IDecl->getClassExtension();
Ted Kremenek09b68972010-02-23 19:39:46 +0000434 }
435
Fariborz Jahanian25760612010-02-15 21:55:26 +0000436 if (!CDecl) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000437 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
438 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000439 // FIXME: PushOnScopeChains?
440 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000441
Fariborz Jahanian25760612010-02-15 21:55:26 +0000442 CDecl->setClassInterface(IDecl);
443 // Insert first use of class extension to the list of class's categories.
444 if (!CategoryName)
445 CDecl->insertNextClassCategory();
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner16b34b42009-02-16 21:30:01 +0000448 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000449 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000450
Fariborz Jahanian25760612010-02-15 21:55:26 +0000451 if (CategoryName) {
452 /// Check for duplicate interface declaration for this category
453 ObjCCategoryDecl *CDeclChain;
454 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
455 CDeclChain = CDeclChain->getNextClassCategory()) {
456 if (CDeclChain->getIdentifier() == CategoryName) {
457 // Class extensions can be declared multiple times.
458 Diag(CategoryLoc, diag::warn_dup_category_def)
459 << ClassName << CategoryName;
460 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
461 break;
462 }
Chris Lattner70f19542009-02-16 21:26:43 +0000463 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000464 if (!CDeclChain)
465 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000466 }
Chris Lattner70f19542009-02-16 21:26:43 +0000467
Chris Lattner4d391482007-12-12 07:09:47 +0000468 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000469 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000470 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000471 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000472 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000473 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000474 NumProtoRefs, ProtoLocs,
475 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson15281452008-11-04 16:57:32 +0000478 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000479 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000480}
481
482/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000484/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000485Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000486 SourceLocation AtCatImplLoc,
487 IdentifierInfo *ClassName, SourceLocation ClassLoc,
488 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000490 ObjCCategoryDecl *CatIDecl = 0;
491 if (IDecl) {
492 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
493 if (!CatIDecl) {
494 // Category @implementation with no corresponding @interface.
495 // Create and install one.
496 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000497 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000498 CatName);
499 CatIDecl->setClassInterface(IDecl);
500 CatIDecl->insertNextClassCategory();
501 }
502 }
503
Mike Stump1eb44332009-09-09 15:08:12 +0000504 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000505 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
506 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000507 /// Check that class of this category is already completely declared.
508 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000510
Douglas Gregord0434102009-01-09 00:49:46 +0000511 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000512 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000513
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000514 /// Check that CatName, category name, is not used in another implementation.
515 if (CatIDecl) {
516 if (CatIDecl->getImplementation()) {
517 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
518 << CatName;
519 Diag(CatIDecl->getImplementation()->getLocation(),
520 diag::note_previous_definition);
521 } else
522 CatIDecl->setImplementation(CDecl);
523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson15281452008-11-04 16:57:32 +0000525 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000526 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000527}
528
Chris Lattnerb28317a2009-03-28 19:18:32 +0000529Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000530 SourceLocation AtClassImplLoc,
531 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000532 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000533 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000535 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000536 NamedDecl *PrevDecl
537 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000539 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000540 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000541 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
542 // If this is a forward declaration of an interface, warn.
543 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000544 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000545 IDecl = 0;
546 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000547 } else {
548 // We did not find anything with the name ClassName; try to correct for
549 // typos in the class name.
550 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
551 if (CorrectTypo(R, TUScope, 0) &&
552 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000553 // Suggest the (potentially) correct interface name. However, put the
554 // fix-it hint itself in a separate note, since changing the name in
555 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000556 // provide a code-modification hint or use the typo name for recovery,
557 // because this is just a warning. The program may actually be correct.
558 Diag(ClassLoc, diag::warn_undef_interface_suggest)
559 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000560 Diag(IDecl->getLocation(), diag::note_previous_decl)
561 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000562 << FixItHint::CreateReplacement(ClassLoc,
563 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000564 IDecl = 0;
565 } else {
566 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
567 }
Chris Lattner4d391482007-12-12 07:09:47 +0000568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner4d391482007-12-12 07:09:47 +0000570 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000572 if (SuperClassname) {
573 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000574 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
577 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000578 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000579 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000580 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000582 Diag(SuperClassLoc, diag::err_undef_superclass)
583 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000584 else if (IDecl && IDecl->getSuperClass() != SDecl) {
585 // This implementation and its interface do not have the same
586 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000588 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000589 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000590 }
591 }
592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Chris Lattner4d391482007-12-12 07:09:47 +0000594 if (!IDecl) {
595 // Legacy case of @implementation with no corresponding @interface.
596 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000597
Mike Stump390b4cc2009-05-16 07:39:55 +0000598 // FIXME: Do we support attributes on the @implementation? If so we should
599 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000600 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000601 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 IDecl->setSuperClass(SDecl);
603 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000604
605 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000606 } else {
607 // Mark the interface as being completed, even if it was just as
608 // @class ....;
609 // declaration; the user cannot reopen it.
610 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
613 ObjCImplementationDecl* IMPDecl =
614 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000615 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Anders Carlsson15281452008-11-04 16:57:32 +0000617 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000618 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Chris Lattner4d391482007-12-12 07:09:47 +0000620 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000621 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000622 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000623 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000624 Diag(IDecl->getImplementation()->getLocation(),
625 diag::note_previous_definition);
626 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000627 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000628 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000629 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000630 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000631}
632
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
634 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000635 SourceLocation RBrace) {
636 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000637 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000638 if (!IDecl)
639 return;
640 /// Check case of non-existing @interface decl.
641 /// (legacy objective-c @implementation decl without an @interface decl).
642 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000643 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000644 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000645 // Add ivar's to class's DeclContext.
646 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000647 ivars[i]->setLexicalDeclContext(ImpDecl);
648 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000649 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000650 }
651
Chris Lattner4d391482007-12-12 07:09:47 +0000652 return;
653 }
654 // If implementation has empty ivar list, just return.
655 if (numIvars == 0)
656 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Chris Lattner4d391482007-12-12 07:09:47 +0000658 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000659 if (LangOpts.ObjCNonFragileABI2) {
660 if (ImpDecl->getSuperClass())
661 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
662 for (unsigned i = 0; i < numIvars; i++) {
663 ObjCIvarDecl* ImplIvar = ivars[i];
664 if (const ObjCIvarDecl *ClsIvar =
665 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
666 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
667 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
668 continue;
669 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000670 // Instance ivar to Implementation's DeclContext.
671 ImplIvar->setLexicalDeclContext(ImpDecl);
672 IDecl->makeDeclVisibleInContext(ImplIvar, false);
673 ImpDecl->addDecl(ImplIvar);
674 }
675 return;
676 }
Chris Lattner4d391482007-12-12 07:09:47 +0000677 // Check interface's Ivar list against those in the implementation.
678 // names and types must match.
679 //
Chris Lattner4d391482007-12-12 07:09:47 +0000680 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000681 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000682 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
683 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 ObjCIvarDecl* ImplIvar = ivars[j++];
685 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000686 assert (ImplIvar && "missing implementation ivar");
687 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Steve Naroffca331292009-03-03 14:49:36 +0000689 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000690 if (Context.getCanonicalType(ImplIvar->getType()) !=
691 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000692 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000693 << ImplIvar->getIdentifier()
694 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000695 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000696 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
697 Expr *ImplBitWidth = ImplIvar->getBitWidth();
698 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000699 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
700 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000701 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
702 << ImplIvar->getIdentifier();
703 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705 }
Steve Naroffca331292009-03-03 14:49:36 +0000706 // Make sure the names are identical.
707 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000708 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000709 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000710 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000711 }
712 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner609e4c72007-12-12 18:11:49 +0000715 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000716 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000717 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000718 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000719}
720
Steve Naroff3c2eb662008-02-10 21:38:56 +0000721void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000722 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000723 if (!IncompleteImpl) {
724 Diag(ImpLoc, diag::warn_incomplete_impl);
725 IncompleteImpl = true;
726 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000727 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000728 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000729}
730
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000731void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
732 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000733 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000734 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000735 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
736 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000737 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000738 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
739 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000740 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattner3aff9192009-04-11 19:58:42 +0000743 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
744 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
745 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000746 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
747 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
748 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
749 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000750 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
752 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000753 << ImpMethodDecl->getDeclName() << (*IF)->getType()
754 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000755 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000756 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000757}
758
Mike Stump390b4cc2009-05-16 07:39:55 +0000759/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
760/// improve the efficiency of selector lookups and type checking by associating
761/// with each protocol / interface / category the flattened instance tables. If
762/// we used an immutable set to keep the table then it wouldn't add significant
763/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000764
Steve Naroffefe7f362008-02-08 22:06:17 +0000765/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000766/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000767void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
768 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000769 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000770 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000771 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000772 ObjCContainerDecl *CDecl) {
773 ObjCInterfaceDecl *IDecl;
774 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
775 IDecl = C->getClassInterface();
776 else
777 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
778 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
779
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000780 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000781 ObjCInterfaceDecl *NSIDecl = 0;
782 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000783 // check to see if class implements forwardInvocation method and objects
784 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000785 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000786 // Under such conditions, which means that every method possible is
787 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000788 // found" warnings.
789 // FIXME: Use a general GetUnarySelector method for this.
790 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
791 Selector fISelector = Context.Selectors.getSelector(1, &II);
792 if (InsMap.count(fISelector))
793 // Is IDecl derived from 'NSProxy'? If so, no instance methods
794 // need be implemented in the implementation.
795 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000798 // If a method lookup fails locally we still need to look and see if
799 // the method was implemented by a base class or an inherited
800 // protocol. This lookup is slow, but occurs rarely in correct code
801 // and otherwise would terminate in a warning.
802
Chris Lattner4d391482007-12-12 07:09:47 +0000803 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000804 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000805 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000806 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000807 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000808 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000809 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000810 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000811 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000812 // Ugly, but necessary. Method declared in protcol might have
813 // have been synthesized due to a property declared in the class which
814 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000815 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000817 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000818 unsigned DIAG = diag::warn_unimplemented_protocol_method;
819 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
820 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
821 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
822 << PDecl->getDeclName();
823 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000824 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000825 }
826 }
Chris Lattner4d391482007-12-12 07:09:47 +0000827 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000828 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000829 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000830 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000831 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000832 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
833 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000834 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000835 unsigned DIAG = diag::warn_unimplemented_protocol_method;
836 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
837 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
838 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
839 PDecl->getDeclName();
840 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000841 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000842 }
Chris Lattner780f3292008-07-21 21:32:27 +0000843 // Check on this protocols's referenced protocols, recursively.
844 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
845 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000846 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000847}
848
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000849/// MatchAllMethodDeclarations - Check methods declaraed in interface or
850/// or protocol against those declared in their implementations.
851///
852void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
853 const llvm::DenseSet<Selector> &ClsMap,
854 llvm::DenseSet<Selector> &InsMapSeen,
855 llvm::DenseSet<Selector> &ClsMapSeen,
856 ObjCImplDecl* IMPDecl,
857 ObjCContainerDecl* CDecl,
858 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000859 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000860 // Check and see if instance methods in class interface have been
861 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000862 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
863 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000864 if (InsMapSeen.count((*I)->getSelector()))
865 continue;
866 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000867 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000868 !InsMap.count((*I)->getSelector())) {
869 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000870 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
871 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000872 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000873 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000874 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000875 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000876 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000878 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000879 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
880 // ImpMethodDecl may be null as in a @dynamic property.
881 if (ImpMethodDecl)
882 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
883 }
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000886 // Check and see if class methods in class interface have been
887 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000888 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000889 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000890 if (ClsMapSeen.count((*I)->getSelector()))
891 continue;
892 ClsMapSeen.insert((*I)->getSelector());
893 if (!ClsMap.count((*I)->getSelector())) {
894 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000895 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
896 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000897 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000898 ObjCMethodDecl *ImpMethodDecl =
899 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000900 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000901 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000902 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
903 }
904 }
905 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
906 // Check for any implementation of a methods declared in protocol.
907 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
908 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000909 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
910 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000911 (*PI), IncompleteImpl, false);
912 if (I->getSuperClass())
913 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000914 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000915 I->getSuperClass(), IncompleteImpl, false);
916 }
917}
918
Mike Stump1eb44332009-09-09 15:08:12 +0000919void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
920 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000921 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000922 llvm::DenseSet<Selector> InsMap;
923 // Check and see if instance methods in class interface have been
924 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000925 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000926 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000927 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000929 // Check and see if properties declared in the interface have either 1)
930 // an implementation or 2) there is a @synthesize/@dynamic implementation
931 // of the property in the @implementation.
932 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000933 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
934
Chris Lattner4d391482007-12-12 07:09:47 +0000935 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000936 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000937 I = IMPDecl->classmeth_begin(),
938 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000939 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000941 // Check for type conflict of methods declared in a class/protocol and
942 // its implementation; if any.
943 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000944 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
945 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000946 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner4d391482007-12-12 07:09:47 +0000948 // Check the protocol list for unimplemented methods in the @implementation
949 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000950 // Check and see if class methods in class interface have been
951 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattnercddc8882009-03-01 00:56:52 +0000953 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000954 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000955 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000956 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000957 InsMap, ClsMap, I);
958 // Check class extensions (unnamed categories)
959 for (ObjCCategoryDecl *Categories = I->getCategoryList();
960 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000961 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +0000962 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
963 break;
964 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000965 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000966 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000967 // For extended class, unimplemented methods in its protocols will
968 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000969 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000970 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
971 E = C->protocol_end(); PI != E; ++PI)
972 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000973 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000974 // Report unimplemented properties in the category as well.
975 // When reporting on missing setter/getters, do not report when
976 // setter/getter is implemented in category's primary class
977 // implementation.
978 if (ObjCInterfaceDecl *ID = C->getClassInterface())
979 if (ObjCImplDecl *IMP = ID->getImplementation()) {
980 for (ObjCImplementationDecl::instmeth_iterator
981 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
982 InsMap.insert((*I)->getSelector());
983 }
984 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
985 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000986 } else
987 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000988}
989
Mike Stump1eb44332009-09-09 15:08:12 +0000990/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000991Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000992Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000993 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000994 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000995 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner4d391482007-12-12 07:09:47 +0000998 for (unsigned i = 0; i != NumElts; ++i) {
999 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001000 NamedDecl *PrevDecl
1001 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001002 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001003 // Maybe we will complain about the shadowed template parameter.
1004 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1005 // Just pretend that we didn't see the previous declaration.
1006 PrevDecl = 0;
1007 }
1008
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001010 // GCC apparently allows the following idiom:
1011 //
1012 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1013 // @class XCElementToggler;
1014 //
Mike Stump1eb44332009-09-09 15:08:12 +00001015 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001016 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1017 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001018 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001019 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001020 } else if (TDD) {
1021 // a forward class declaration matching a typedef name of a class refers
1022 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001023 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001024 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1025 PrevDecl = OI->getDecl();
1026 }
Chris Lattner4d391482007-12-12 07:09:47 +00001027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001029 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001030 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001031 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001032
1033 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1034 // the current DeclContext. This prevents clients that walk DeclContext
1035 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1036 // declared later (if at all). We also take care to explicitly make
1037 // sure this declaration is visible for name lookup.
1038 PushOnScopeChains(IDecl, TUScope, false);
1039 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001040 }
1041
1042 Interfaces.push_back(IDecl);
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenek321c22f2009-11-18 00:28:11 +00001045 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001046 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001047 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001048 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001049 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001050 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001051 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001052}
1053
1054
1055/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1056/// returns true, or false, accordingly.
1057/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001058bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001059 const ObjCMethodDecl *PrevMethod,
1060 bool matchBasedOnSizeAndAlignment) {
1061 QualType T1 = Context.getCanonicalType(Method->getResultType());
1062 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001064 if (T1 != T2) {
1065 // The result types are different.
1066 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001067 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001068 // Incomplete types don't have a size and alignment.
1069 if (T1->isIncompleteType() || T2->isIncompleteType())
1070 return false;
1071 // Check is based on size and alignment.
1072 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1073 return false;
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Chris Lattner89951a82009-02-20 18:43:26 +00001076 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1077 E = Method->param_end();
1078 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner89951a82009-02-20 18:43:26 +00001080 for (; ParamI != E; ++ParamI, ++PrevI) {
1081 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1082 T1 = Context.getCanonicalType((*ParamI)->getType());
1083 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001084 if (T1 != T2) {
1085 // The result types are different.
1086 if (!matchBasedOnSizeAndAlignment)
1087 return false;
1088 // Incomplete types don't have a size and alignment.
1089 if (T1->isIncompleteType() || T2->isIncompleteType())
1090 return false;
1091 // Check is based on size and alignment.
1092 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1093 return false;
1094 }
Chris Lattner4d391482007-12-12 07:09:47 +00001095 }
1096 return true;
1097}
1098
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001099/// \brief Read the contents of the instance and factory method pools
1100/// for a given selector from external storage.
1101///
1102/// This routine should only be called once, when neither the instance
1103/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001104Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001105 bool isInstance) {
1106 assert(ExternalSource && "We need an external AST source");
1107 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1108 "Selector data already loaded into the instance method pool");
1109 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1110 "Selector data already loaded into the factory method pool");
1111
1112 // Read the method list from the external source.
1113 std::pair<ObjCMethodList, ObjCMethodList> Methods
1114 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001116 if (isInstance) {
1117 if (Methods.second.Method)
1118 FactoryMethodPool[Sel] = Methods.second;
1119 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001120 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001121
1122 if (Methods.first.Method)
1123 InstanceMethodPool[Sel] = Methods.first;
1124
1125 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1126}
1127
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001128void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001129 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1130 = InstanceMethodPool.find(Method->getSelector());
1131 if (Pos == InstanceMethodPool.end()) {
1132 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1133 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1134 else
1135 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1136 ObjCMethodList())).first;
1137 }
1138
1139 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001140 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001141 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001142 Entry.Method = Method;
1143 Entry.Next = 0;
1144 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Chris Lattnerb25df352009-03-04 05:16:45 +00001147 // We've seen a method with this name, see if we have already seen this type
1148 // signature.
1149 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1150 if (MatchTwoMethodDeclarations(Method, List->Method))
1151 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattnerb25df352009-03-04 05:16:45 +00001153 // We have a new signature for an existing method - add it.
1154 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001155 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1156 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001157}
1158
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001159// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001160ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001161 SourceRange R,
1162 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001163 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1164 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001165 if (Pos == InstanceMethodPool.end()) {
1166 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001167 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1168 else
1169 return 0;
1170 }
1171
1172 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001173 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Steve Naroff037cda52008-09-30 14:38:43 +00001175 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001176 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1177 // This checks if the methods differ by size & alignment.
1178 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001179 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001180 }
1181 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001182 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001183 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001184 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001185 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001186 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001187 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001188 }
1189 return MethList.Method;
1190}
1191
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001192void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001193 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1194 = FactoryMethodPool.find(Method->getSelector());
1195 if (Pos == FactoryMethodPool.end()) {
1196 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1197 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1198 else
1199 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1200 ObjCMethodList())).first;
1201 }
1202
1203 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001204 if (!FirstMethod.Method) {
1205 // Haven't seen a method with this selector name yet - add it.
1206 FirstMethod.Method = Method;
1207 FirstMethod.Next = 0;
1208 } else {
1209 // We've seen a method with this name, now check the type signature(s).
1210 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
1212 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001213 Next = Next->Next)
1214 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Chris Lattner4d391482007-12-12 07:09:47 +00001216 if (!match) {
1217 // We have a new signature for an existing method - add it.
1218 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001219 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1220 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001221 FirstMethod.Next = OMI;
1222 }
1223 }
1224}
1225
Mike Stump1eb44332009-09-09 15:08:12 +00001226ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001227 SourceRange R) {
1228 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1229 = FactoryMethodPool.find(Sel);
1230 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001231 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001232 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1233 else
1234 return 0;
1235 }
1236
1237 ObjCMethodList &MethList = Pos->second;
1238 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001240 if (MethList.Method && MethList.Next) {
1241 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1242 // This checks if the methods differ by size & alignment.
1243 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1244 issueWarning = true;
1245 }
1246 if (issueWarning && (MethList.Method && MethList.Next)) {
1247 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001248 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001249 << MethList.Method->getSourceRange();
1250 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001251 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001252 << Next->Method->getSourceRange();
1253 }
1254 return MethList.Method;
1255}
1256
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001257/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1258/// identical selector names in current and its super classes and issues
1259/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001260void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1261 ObjCMethodDecl *Method,
1262 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001263 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1264 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001266 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001267 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001268 SD->lookupMethod(Method->getSelector(), IsInstance);
1269 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001270 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001271 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001272 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001273 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1274 E = Method->param_end();
1275 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1276 for (; ParamI != E; ++ParamI, ++PrevI) {
1277 // Number of parameters are the same and is guaranteed by selector match.
1278 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1279 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1280 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1281 // If type of arguement of method in this class does not match its
1282 // respective argument type in the super class method, issue warning;
1283 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001284 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001285 << T1 << T2;
1286 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1287 return;
1288 }
1289 }
1290 ID = SD;
1291 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001292}
1293
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001294/// DiagnoseDuplicateIvars -
1295/// Check for duplicate ivars in the entire class at the start of
1296/// @implementation. This becomes necesssary because class extension can
1297/// add ivars to a class in random order which will not be known until
1298/// class's @implementation is seen.
1299void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1300 ObjCInterfaceDecl *SID) {
1301 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1302 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1303 ObjCIvarDecl* Ivar = (*IVI);
1304 if (Ivar->isInvalidDecl())
1305 continue;
1306 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1307 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1308 if (prevIvar) {
1309 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1310 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1311 Ivar->setInvalidDecl();
1312 }
1313 }
1314 }
1315}
1316
Steve Naroffa56f6162007-12-18 01:30:32 +00001317// Note: For class/category implemenations, allMethods/allProperties is
1318// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001319void Sema::ActOnAtEnd(SourceRange AtEnd,
1320 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001321 DeclPtrTy *allMethods, unsigned allNum,
1322 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001323 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001324 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001325
Steve Naroffa56f6162007-12-18 01:30:32 +00001326 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1327 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001328 // should be true.
1329 if (!ClassDecl)
1330 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001331
Mike Stump1eb44332009-09-09 15:08:12 +00001332 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001333 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1334 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001335 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001336
Ted Kremenek782f2f52010-01-07 01:20:12 +00001337 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1338 // FIXME: This is wrong. We shouldn't be pretending that there is
1339 // an '@end' in the declaration.
1340 SourceLocation L = ClassDecl->getLocation();
1341 AtEnd.setBegin(L);
1342 AtEnd.setEnd(L);
1343 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001344 }
1345
Steve Naroff0701bbb2009-01-08 17:28:14 +00001346 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001347
1348 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1349 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1350 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1351
Chris Lattner4d391482007-12-12 07:09:47 +00001352 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001353 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001354 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001355
1356 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001357 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001358 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001360 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001361 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001362 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001363 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001364 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001365 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001366 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001367 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001368 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001369 InsMap[Method->getSelector()] = Method;
1370 /// The following allows us to typecheck messages to "id".
1371 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001372 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001373 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001374 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001375 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001376 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001377 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001378 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001379 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001380 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001381 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001382 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001383 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001384 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001385 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001386 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001387 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001388 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001389 /// The following allows us to typecheck messages to "Class".
1390 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001391 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001392 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001393 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001394 }
1395 }
1396 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001398 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001399 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001400 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001401 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001402 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001403 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001404 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001405 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001406
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001407 // Compare protocol properties with those in category
1408 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001409 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001410 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001411 }
Steve Naroff09c47192009-01-09 15:36:25 +00001412 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001413 if (CDecl->getIdentifier())
1414 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1415 // user-defined setter/getter. It also synthesizes setter/getter methods
1416 // and adds them to the DeclContext and global method pools.
1417 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1418 E = CDecl->prop_end();
1419 I != E; ++I)
1420 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001421 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001422 }
1423 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001424 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001425 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001426 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001427 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001428 if (LangOpts.ObjCNonFragileABI2)
1429 while (IDecl->getSuperClass()) {
1430 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1431 IDecl = IDecl->getSuperClass();
1432 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001435 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001436 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Chris Lattner4d391482007-12-12 07:09:47 +00001438 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001439 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001440 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001442 Categories; Categories = Categories->getNextClassCategory()) {
1443 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001444 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001445 break;
1446 }
1447 }
1448 }
1449 }
Chris Lattner682bf922009-03-29 16:50:03 +00001450 if (isInterfaceDeclKind) {
1451 // Reject invalid vardecls.
1452 for (unsigned i = 0; i != tuvNum; i++) {
1453 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1454 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1455 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001456 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001457 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001458 }
Chris Lattner682bf922009-03-29 16:50:03 +00001459 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001460 }
Chris Lattner4d391482007-12-12 07:09:47 +00001461}
1462
1463
1464/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1465/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001466static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001467CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1468 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1469 if (PQTVal & ObjCDeclSpec::DQ_In)
1470 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1471 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1472 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1473 if (PQTVal & ObjCDeclSpec::DQ_Out)
1474 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1475 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1476 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1477 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1478 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1479 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1480 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001481
1482 return ret;
1483}
1484
Chris Lattnerb28317a2009-03-28 19:18:32 +00001485Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001486 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001487 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001488 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001489 Selector Sel,
1490 // optional arguments. The number of types/arguments is obtained
1491 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001492 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001493 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001494 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1495 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001496 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001497
1498 // Make sure we can establish a context for the method.
1499 if (!ClassDecl) {
1500 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001501 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001502 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001503 }
Chris Lattner4d391482007-12-12 07:09:47 +00001504 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001506 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001507 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001508 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Steve Naroffccef3712009-02-20 22:59:16 +00001510 // Methods cannot return interface types. All ObjC objects are
1511 // passed by reference.
1512 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001513 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1514 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001515 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001516 }
1517 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001519
1520 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001521 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001522 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001523 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001524 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001525 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001526 MethodDeclKind == tok::objc_optional ?
1527 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001528 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattner0ed844b2008-04-04 06:12:32 +00001530 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Chris Lattner7db638d2009-04-11 19:42:43 +00001532 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001533 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001534 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Chris Lattnere294d3f2009-04-11 18:57:04 +00001536 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001537 ArgType = Context.getObjCIdType();
1538 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001539 } else {
John McCall58e46772009-10-23 21:48:59 +00001540 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001541 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001542 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
John McCall58e46772009-10-23 21:48:59 +00001545 ParmVarDecl* Param
1546 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1547 ArgInfo[i].Name, ArgType, DI,
1548 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001550 if (ArgType->isObjCInterfaceType()) {
1551 Diag(ArgInfo[i].NameLoc,
1552 diag::err_object_cannot_be_passed_returned_by_value)
1553 << 1 << ArgType;
1554 Param->setInvalidDecl();
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattner0ed844b2008-04-04 06:12:32 +00001557 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001558 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001560 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001561 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Chris Lattner0ed844b2008-04-04 06:12:32 +00001563 Params.push_back(Param);
1564 }
1565
Jay Foadbeaaccd2009-05-21 09:52:38 +00001566 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001567 ObjCMethod->setObjCDeclQualifier(
1568 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1569 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001570
1571 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001572 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
John McCall54abf7d2009-11-04 02:18:39 +00001574 const ObjCMethodDecl *InterfaceMD = 0;
1575
Mike Stump1eb44332009-09-09 15:08:12 +00001576 // For implementations (which can be very "coarse grain"), we add the
1577 // method now. This allows the AST to implement lookup methods that work
1578 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001579 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001580 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001581 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001582 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001583 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1584 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001585 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001586 PrevMethod = ImpDecl->getClassMethod(Sel);
1587 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001588 }
John McCall54abf7d2009-11-04 02:18:39 +00001589 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1590 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001591 if (AttrList)
1592 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001593 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001594 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001595 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001596 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1597 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001598 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001599 PrevMethod = CatImpDecl->getClassMethod(Sel);
1600 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001601 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001602 if (AttrList)
1603 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001604 }
1605 if (PrevMethod) {
1606 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001607 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001608 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001609 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001610 }
John McCall54abf7d2009-11-04 02:18:39 +00001611
1612 // If the interface declared this method, and it was deprecated there,
1613 // mark it deprecated here.
1614 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1615 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1616
Chris Lattnerb28317a2009-03-28 19:18:32 +00001617 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001618}
1619
Chris Lattnercc98eac2008-12-17 07:13:27 +00001620bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001621 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001622 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Anders Carlsson15281452008-11-04 16:57:32 +00001624 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1625 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Anders Carlsson15281452008-11-04 16:57:32 +00001627 return true;
1628}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001629
Chris Lattnercc98eac2008-12-17 07:13:27 +00001630/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1631/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00001632void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001633 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001634 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001635 // Check that ClassName is a valid class
1636 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1637 if (!Class) {
1638 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1639 return;
1640 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001641 if (LangOpts.ObjCNonFragileABI) {
1642 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1643 return;
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Chris Lattnercc98eac2008-12-17 07:13:27 +00001646 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00001647 llvm::SmallVector<FieldDecl*, 32> RecFields;
1648 Context.CollectObjCIvars(Class, RecFields);
1649 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1650 for (unsigned i = 0; i < RecFields.size(); i++) {
1651 FieldDecl* ID = RecFields[i];
1652 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1653 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1654 ID->getIdentifier(), ID->getType(),
1655 ID->getBitWidth());
1656 Decls.push_back(Sema::DeclPtrTy::make(FD));
1657 }
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Chris Lattnercc98eac2008-12-17 07:13:27 +00001659 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001660 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001661 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001662 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001663 if (getLangOptions().CPlusPlus)
1664 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001665 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001666 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001667 }
1668}
1669