blob: 0c47e63d99ebdbaa1dfa4e0405cb42511d4aff9c [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();
Fariborz Jahanian24e14082010-04-02 20:53:05 +0000434 if (IDecl->getImplementation()) {
435 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
436 Diag(IDecl->getImplementation()->getLocation(),
437 diag::note_implementation_declared);
438 }
Ted Kremenek09b68972010-02-23 19:39:46 +0000439 }
440
Fariborz Jahanian25760612010-02-15 21:55:26 +0000441 if (!CDecl) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000442 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
443 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000444 // FIXME: PushOnScopeChains?
445 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000446
Fariborz Jahanian25760612010-02-15 21:55:26 +0000447 CDecl->setClassInterface(IDecl);
448 // Insert first use of class extension to the list of class's categories.
449 if (!CategoryName)
450 CDecl->insertNextClassCategory();
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner16b34b42009-02-16 21:30:01 +0000453 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000454 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000455
Fariborz Jahanian25760612010-02-15 21:55:26 +0000456 if (CategoryName) {
457 /// Check for duplicate interface declaration for this category
458 ObjCCategoryDecl *CDeclChain;
459 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
460 CDeclChain = CDeclChain->getNextClassCategory()) {
461 if (CDeclChain->getIdentifier() == CategoryName) {
462 // Class extensions can be declared multiple times.
463 Diag(CategoryLoc, diag::warn_dup_category_def)
464 << ClassName << CategoryName;
465 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
466 break;
467 }
Chris Lattner70f19542009-02-16 21:26:43 +0000468 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000469 if (!CDeclChain)
470 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000471 }
Chris Lattner70f19542009-02-16 21:26:43 +0000472
Chris Lattner4d391482007-12-12 07:09:47 +0000473 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000474 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000475 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000476 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000477 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000478 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000479 NumProtoRefs, ProtoLocs,
480 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson15281452008-11-04 16:57:32 +0000483 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000484 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000485}
486
487/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000488/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000489/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000490Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000491 SourceLocation AtCatImplLoc,
492 IdentifierInfo *ClassName, SourceLocation ClassLoc,
493 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000494 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000495 ObjCCategoryDecl *CatIDecl = 0;
496 if (IDecl) {
497 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
498 if (!CatIDecl) {
499 // Category @implementation with no corresponding @interface.
500 // Create and install one.
501 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000502 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000503 CatName);
504 CatIDecl->setClassInterface(IDecl);
505 CatIDecl->insertNextClassCategory();
506 }
507 }
508
Mike Stump1eb44332009-09-09 15:08:12 +0000509 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000510 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
511 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000512 /// Check that class of this category is already completely declared.
513 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000514 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000515
Douglas Gregord0434102009-01-09 00:49:46 +0000516 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000517 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000518
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000519 /// Check that CatName, category name, is not used in another implementation.
520 if (CatIDecl) {
521 if (CatIDecl->getImplementation()) {
522 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
523 << CatName;
524 Diag(CatIDecl->getImplementation()->getLocation(),
525 diag::note_previous_definition);
526 } else
527 CatIDecl->setImplementation(CDecl);
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlsson15281452008-11-04 16:57:32 +0000530 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000531 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000532}
533
Chris Lattnerb28317a2009-03-28 19:18:32 +0000534Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000535 SourceLocation AtClassImplLoc,
536 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000537 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000538 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000540 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000541 NamedDecl *PrevDecl
542 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000543 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000544 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000545 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000546 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
547 // If this is a forward declaration of an interface, warn.
548 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000549 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000550 IDecl = 0;
551 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000552 } else {
553 // We did not find anything with the name ClassName; try to correct for
554 // typos in the class name.
555 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
556 if (CorrectTypo(R, TUScope, 0) &&
557 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000558 // Suggest the (potentially) correct interface name. However, put the
559 // fix-it hint itself in a separate note, since changing the name in
560 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000561 // provide a code-modification hint or use the typo name for recovery,
562 // because this is just a warning. The program may actually be correct.
563 Diag(ClassLoc, diag::warn_undef_interface_suggest)
564 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000565 Diag(IDecl->getLocation(), diag::note_previous_decl)
566 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000567 << FixItHint::CreateReplacement(ClassLoc,
568 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000569 IDecl = 0;
570 } else {
571 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
572 }
Chris Lattner4d391482007-12-12 07:09:47 +0000573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner4d391482007-12-12 07:09:47 +0000575 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000576 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000577 if (SuperClassname) {
578 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000579 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
582 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000583 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000584 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000585 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000586 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 Diag(SuperClassLoc, diag::err_undef_superclass)
588 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000589 else if (IDecl && IDecl->getSuperClass() != SDecl) {
590 // This implementation and its interface do not have the same
591 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000593 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000594 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000595 }
596 }
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattner4d391482007-12-12 07:09:47 +0000599 if (!IDecl) {
600 // Legacy case of @implementation with no corresponding @interface.
601 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000602
Mike Stump390b4cc2009-05-16 07:39:55 +0000603 // FIXME: Do we support attributes on the @implementation? If so we should
604 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000605 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000606 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 IDecl->setSuperClass(SDecl);
608 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000609
610 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000611 } else {
612 // Mark the interface as being completed, even if it was just as
613 // @class ....;
614 // declaration; the user cannot reopen it.
615 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
618 ObjCImplementationDecl* IMPDecl =
619 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000620 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Anders Carlsson15281452008-11-04 16:57:32 +0000622 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000623 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattner4d391482007-12-12 07:09:47 +0000625 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000626 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000627 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000629 Diag(IDecl->getImplementation()->getLocation(),
630 diag::note_previous_definition);
631 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000632 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000633 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000634 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000635 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000636}
637
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
639 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000640 SourceLocation RBrace) {
641 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000642 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000643 if (!IDecl)
644 return;
645 /// Check case of non-existing @interface decl.
646 /// (legacy objective-c @implementation decl without an @interface decl).
647 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000648 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000649 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000650 // Add ivar's to class's DeclContext.
651 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000652 ivars[i]->setLexicalDeclContext(ImpDecl);
653 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000654 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000655 }
656
Chris Lattner4d391482007-12-12 07:09:47 +0000657 return;
658 }
659 // If implementation has empty ivar list, just return.
660 if (numIvars == 0)
661 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Chris Lattner4d391482007-12-12 07:09:47 +0000663 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000664 if (LangOpts.ObjCNonFragileABI2) {
665 if (ImpDecl->getSuperClass())
666 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
667 for (unsigned i = 0; i < numIvars; i++) {
668 ObjCIvarDecl* ImplIvar = ivars[i];
669 if (const ObjCIvarDecl *ClsIvar =
670 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
671 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
672 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
673 continue;
674 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000675 // Instance ivar to Implementation's DeclContext.
676 ImplIvar->setLexicalDeclContext(ImpDecl);
677 IDecl->makeDeclVisibleInContext(ImplIvar, false);
678 ImpDecl->addDecl(ImplIvar);
679 }
680 return;
681 }
Chris Lattner4d391482007-12-12 07:09:47 +0000682 // Check interface's Ivar list against those in the implementation.
683 // names and types must match.
684 //
Chris Lattner4d391482007-12-12 07:09:47 +0000685 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000686 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000687 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
688 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 ObjCIvarDecl* ImplIvar = ivars[j++];
690 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000691 assert (ImplIvar && "missing implementation ivar");
692 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Steve Naroffca331292009-03-03 14:49:36 +0000694 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000695 if (Context.getCanonicalType(ImplIvar->getType()) !=
696 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000697 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000698 << ImplIvar->getIdentifier()
699 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000700 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000701 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
702 Expr *ImplBitWidth = ImplIvar->getBitWidth();
703 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000704 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
705 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000706 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
707 << ImplIvar->getIdentifier();
708 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710 }
Steve Naroffca331292009-03-03 14:49:36 +0000711 // Make sure the names are identical.
712 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000713 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000714 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000715 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000716 }
717 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattner609e4c72007-12-12 18:11:49 +0000720 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000721 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000722 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000723 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000724}
725
Steve Naroff3c2eb662008-02-10 21:38:56 +0000726void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000727 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000728 if (!IncompleteImpl) {
729 Diag(ImpLoc, diag::warn_incomplete_impl);
730 IncompleteImpl = true;
731 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000732 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000733 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000734}
735
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000736void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
737 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000738 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000739 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000740 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
741 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000742 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000743 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
744 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000745 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner3aff9192009-04-11 19:58:42 +0000748 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
749 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
750 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000751 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
752 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
753 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
754 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000755 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
757 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000758 << ImpMethodDecl->getDeclName() << (*IF)->getType()
759 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000760 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000761 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000762}
763
Mike Stump390b4cc2009-05-16 07:39:55 +0000764/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
765/// improve the efficiency of selector lookups and type checking by associating
766/// with each protocol / interface / category the flattened instance tables. If
767/// we used an immutable set to keep the table then it wouldn't add significant
768/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000769
Steve Naroffefe7f362008-02-08 22:06:17 +0000770/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000771/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000772void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
773 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000774 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000775 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000776 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000777 ObjCContainerDecl *CDecl) {
778 ObjCInterfaceDecl *IDecl;
779 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
780 IDecl = C->getClassInterface();
781 else
782 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
783 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
784
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000785 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000786 ObjCInterfaceDecl *NSIDecl = 0;
787 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000788 // check to see if class implements forwardInvocation method and objects
789 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000790 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000791 // Under such conditions, which means that every method possible is
792 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000793 // found" warnings.
794 // FIXME: Use a general GetUnarySelector method for this.
795 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
796 Selector fISelector = Context.Selectors.getSelector(1, &II);
797 if (InsMap.count(fISelector))
798 // Is IDecl derived from 'NSProxy'? If so, no instance methods
799 // need be implemented in the implementation.
800 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000803 // If a method lookup fails locally we still need to look and see if
804 // the method was implemented by a base class or an inherited
805 // protocol. This lookup is slow, but occurs rarely in correct code
806 // and otherwise would terminate in a warning.
807
Chris Lattner4d391482007-12-12 07:09:47 +0000808 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000809 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000810 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000811 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000812 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000813 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000814 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000815 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000817 // Ugly, but necessary. Method declared in protcol might have
818 // have been synthesized due to a property declared in the class which
819 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000820 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000821 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000822 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000823 unsigned DIAG = diag::warn_unimplemented_protocol_method;
824 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
825 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
826 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
827 << PDecl->getDeclName();
828 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000829 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000830 }
831 }
Chris Lattner4d391482007-12-12 07:09:47 +0000832 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000833 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000834 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000835 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000837 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
838 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000839 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000840 unsigned DIAG = diag::warn_unimplemented_protocol_method;
841 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
842 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
843 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
844 PDecl->getDeclName();
845 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000846 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000847 }
Chris Lattner780f3292008-07-21 21:32:27 +0000848 // Check on this protocols's referenced protocols, recursively.
849 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
850 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000851 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000852}
853
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000854/// MatchAllMethodDeclarations - Check methods declaraed in interface or
855/// or protocol against those declared in their implementations.
856///
857void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
858 const llvm::DenseSet<Selector> &ClsMap,
859 llvm::DenseSet<Selector> &InsMapSeen,
860 llvm::DenseSet<Selector> &ClsMapSeen,
861 ObjCImplDecl* IMPDecl,
862 ObjCContainerDecl* CDecl,
863 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000864 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000865 // Check and see if instance methods in class interface have been
866 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000867 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
868 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000869 if (InsMapSeen.count((*I)->getSelector()))
870 continue;
871 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000872 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000873 !InsMap.count((*I)->getSelector())) {
874 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000875 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
876 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000877 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000878 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000879 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000880 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000881 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000883 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000884 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
885 // ImpMethodDecl may be null as in a @dynamic property.
886 if (ImpMethodDecl)
887 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
888 }
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000891 // Check and see if class methods in class interface have been
892 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000893 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000894 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000895 if (ClsMapSeen.count((*I)->getSelector()))
896 continue;
897 ClsMapSeen.insert((*I)->getSelector());
898 if (!ClsMap.count((*I)->getSelector())) {
899 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000900 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
901 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000902 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000903 ObjCMethodDecl *ImpMethodDecl =
904 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000905 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000906 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000907 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
908 }
909 }
910 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
911 // Check for any implementation of a methods declared in protocol.
912 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
913 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000914 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
915 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000916 (*PI), IncompleteImpl, false);
917 if (I->getSuperClass())
918 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000919 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000920 I->getSuperClass(), IncompleteImpl, false);
921 }
922}
923
Mike Stump1eb44332009-09-09 15:08:12 +0000924void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
925 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000926 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000927 llvm::DenseSet<Selector> InsMap;
928 // Check and see if instance methods in class interface have been
929 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000930 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000932 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000934 // Check and see if properties declared in the interface have either 1)
935 // an implementation or 2) there is a @synthesize/@dynamic implementation
936 // of the property in the @implementation.
937 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000938 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
939
Chris Lattner4d391482007-12-12 07:09:47 +0000940 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000941 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 I = IMPDecl->classmeth_begin(),
943 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000944 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000946 // Check for type conflict of methods declared in a class/protocol and
947 // its implementation; if any.
948 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000949 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
950 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000951 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner4d391482007-12-12 07:09:47 +0000953 // Check the protocol list for unimplemented methods in the @implementation
954 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000955 // Check and see if class methods in class interface have been
956 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattnercddc8882009-03-01 00:56:52 +0000958 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000959 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000960 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000962 InsMap, ClsMap, I);
963 // Check class extensions (unnamed categories)
964 for (ObjCCategoryDecl *Categories = I->getCategoryList();
965 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000966 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +0000967 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
968 break;
969 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000970 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000971 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000972 // For extended class, unimplemented methods in its protocols will
973 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000974 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000975 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
976 E = C->protocol_end(); PI != E; ++PI)
977 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000978 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000979 // Report unimplemented properties in the category as well.
980 // When reporting on missing setter/getters, do not report when
981 // setter/getter is implemented in category's primary class
982 // implementation.
983 if (ObjCInterfaceDecl *ID = C->getClassInterface())
984 if (ObjCImplDecl *IMP = ID->getImplementation()) {
985 for (ObjCImplementationDecl::instmeth_iterator
986 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
987 InsMap.insert((*I)->getSelector());
988 }
989 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
990 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000991 } else
992 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000993}
994
Mike Stump1eb44332009-09-09 15:08:12 +0000995/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000996Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000997Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000998 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000999 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001000 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001001 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattner4d391482007-12-12 07:09:47 +00001003 for (unsigned i = 0; i != NumElts; ++i) {
1004 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001005 NamedDecl *PrevDecl
1006 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001007 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001008 // Maybe we will complain about the shadowed template parameter.
1009 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1010 // Just pretend that we didn't see the previous declaration.
1011 PrevDecl = 0;
1012 }
1013
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001014 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001015 // GCC apparently allows the following idiom:
1016 //
1017 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1018 // @class XCElementToggler;
1019 //
Mike Stump1eb44332009-09-09 15:08:12 +00001020 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001021 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1022 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001023 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001024 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001025 } else if (TDD) {
1026 // a forward class declaration matching a typedef name of a class refers
1027 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001028 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001029 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1030 PrevDecl = OI->getDecl();
1031 }
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001034 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001035 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001036 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001037
1038 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1039 // the current DeclContext. This prevents clients that walk DeclContext
1040 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1041 // declared later (if at all). We also take care to explicitly make
1042 // sure this declaration is visible for name lookup.
1043 PushOnScopeChains(IDecl, TUScope, false);
1044 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001045 }
1046
1047 Interfaces.push_back(IDecl);
1048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek321c22f2009-11-18 00:28:11 +00001050 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001051 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001052 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001053 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001055 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001056 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001057}
1058
1059
1060/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1061/// returns true, or false, accordingly.
1062/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001063bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001064 const ObjCMethodDecl *PrevMethod,
1065 bool matchBasedOnSizeAndAlignment) {
1066 QualType T1 = Context.getCanonicalType(Method->getResultType());
1067 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001069 if (T1 != T2) {
1070 // The result types are different.
1071 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001072 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001073 // Incomplete types don't have a size and alignment.
1074 if (T1->isIncompleteType() || T2->isIncompleteType())
1075 return false;
1076 // Check is based on size and alignment.
1077 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1078 return false;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner89951a82009-02-20 18:43:26 +00001081 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1082 E = Method->param_end();
1083 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner89951a82009-02-20 18:43:26 +00001085 for (; ParamI != E; ++ParamI, ++PrevI) {
1086 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1087 T1 = Context.getCanonicalType((*ParamI)->getType());
1088 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001089 if (T1 != T2) {
1090 // The result types are different.
1091 if (!matchBasedOnSizeAndAlignment)
1092 return false;
1093 // Incomplete types don't have a size and alignment.
1094 if (T1->isIncompleteType() || T2->isIncompleteType())
1095 return false;
1096 // Check is based on size and alignment.
1097 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1098 return false;
1099 }
Chris Lattner4d391482007-12-12 07:09:47 +00001100 }
1101 return true;
1102}
1103
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001104/// \brief Read the contents of the instance and factory method pools
1105/// for a given selector from external storage.
1106///
1107/// This routine should only be called once, when neither the instance
1108/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001109Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001110 bool isInstance) {
1111 assert(ExternalSource && "We need an external AST source");
1112 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1113 "Selector data already loaded into the instance method pool");
1114 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1115 "Selector data already loaded into the factory method pool");
1116
1117 // Read the method list from the external source.
1118 std::pair<ObjCMethodList, ObjCMethodList> Methods
1119 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001121 if (isInstance) {
1122 if (Methods.second.Method)
1123 FactoryMethodPool[Sel] = Methods.second;
1124 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001125 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001126
1127 if (Methods.first.Method)
1128 InstanceMethodPool[Sel] = Methods.first;
1129
1130 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1131}
1132
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001133void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001134 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1135 = InstanceMethodPool.find(Method->getSelector());
1136 if (Pos == InstanceMethodPool.end()) {
1137 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1138 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1139 else
1140 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1141 ObjCMethodList())).first;
1142 }
1143
1144 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001145 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001146 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001147 Entry.Method = Method;
1148 Entry.Next = 0;
1149 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Chris Lattnerb25df352009-03-04 05:16:45 +00001152 // We've seen a method with this name, see if we have already seen this type
1153 // signature.
1154 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1155 if (MatchTwoMethodDeclarations(Method, List->Method))
1156 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattnerb25df352009-03-04 05:16:45 +00001158 // We have a new signature for an existing method - add it.
1159 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001160 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1161 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001162}
1163
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001164// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001165ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001166 SourceRange R,
1167 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001168 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1169 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001170 if (Pos == InstanceMethodPool.end()) {
1171 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001172 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1173 else
1174 return 0;
1175 }
1176
1177 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001178 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Steve Naroff037cda52008-09-30 14:38:43 +00001180 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001181 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1182 // This checks if the methods differ by size & alignment.
1183 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001184 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001185 }
1186 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001187 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001188 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001189 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001190 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001191 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001192 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001193 }
1194 return MethList.Method;
1195}
1196
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001197void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001198 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1199 = FactoryMethodPool.find(Method->getSelector());
1200 if (Pos == FactoryMethodPool.end()) {
1201 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1202 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1203 else
1204 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1205 ObjCMethodList())).first;
1206 }
1207
1208 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001209 if (!FirstMethod.Method) {
1210 // Haven't seen a method with this selector name yet - add it.
1211 FirstMethod.Method = Method;
1212 FirstMethod.Next = 0;
1213 } else {
1214 // We've seen a method with this name, now check the type signature(s).
1215 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001216
1217 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001218 Next = Next->Next)
1219 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner4d391482007-12-12 07:09:47 +00001221 if (!match) {
1222 // We have a new signature for an existing method - add it.
1223 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001224 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1225 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001226 FirstMethod.Next = OMI;
1227 }
1228 }
1229}
1230
Mike Stump1eb44332009-09-09 15:08:12 +00001231ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001232 SourceRange R) {
1233 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1234 = FactoryMethodPool.find(Sel);
1235 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001236 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001237 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1238 else
1239 return 0;
1240 }
1241
1242 ObjCMethodList &MethList = Pos->second;
1243 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001245 if (MethList.Method && MethList.Next) {
1246 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1247 // This checks if the methods differ by size & alignment.
1248 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1249 issueWarning = true;
1250 }
1251 if (issueWarning && (MethList.Method && MethList.Next)) {
1252 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001253 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001254 << MethList.Method->getSourceRange();
1255 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001256 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001257 << Next->Method->getSourceRange();
1258 }
1259 return MethList.Method;
1260}
1261
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001262/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1263/// identical selector names in current and its super classes and issues
1264/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001265void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1266 ObjCMethodDecl *Method,
1267 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001268 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1269 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001271 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001272 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001273 SD->lookupMethod(Method->getSelector(), IsInstance);
1274 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001275 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001276 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001277 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001278 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1279 E = Method->param_end();
1280 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1281 for (; ParamI != E; ++ParamI, ++PrevI) {
1282 // Number of parameters are the same and is guaranteed by selector match.
1283 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1284 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1285 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1286 // If type of arguement of method in this class does not match its
1287 // respective argument type in the super class method, issue warning;
1288 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001289 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001290 << T1 << T2;
1291 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1292 return;
1293 }
1294 }
1295 ID = SD;
1296 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001297}
1298
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001299/// DiagnoseDuplicateIvars -
1300/// Check for duplicate ivars in the entire class at the start of
1301/// @implementation. This becomes necesssary because class extension can
1302/// add ivars to a class in random order which will not be known until
1303/// class's @implementation is seen.
1304void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1305 ObjCInterfaceDecl *SID) {
1306 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1307 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1308 ObjCIvarDecl* Ivar = (*IVI);
1309 if (Ivar->isInvalidDecl())
1310 continue;
1311 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1312 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1313 if (prevIvar) {
1314 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1315 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1316 Ivar->setInvalidDecl();
1317 }
1318 }
1319 }
1320}
1321
Steve Naroffa56f6162007-12-18 01:30:32 +00001322// Note: For class/category implemenations, allMethods/allProperties is
1323// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001324void Sema::ActOnAtEnd(SourceRange AtEnd,
1325 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001326 DeclPtrTy *allMethods, unsigned allNum,
1327 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001328 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001330
Steve Naroffa56f6162007-12-18 01:30:32 +00001331 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1332 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001333 // should be true.
1334 if (!ClassDecl)
1335 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001336
Mike Stump1eb44332009-09-09 15:08:12 +00001337 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001338 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1339 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001341
Ted Kremenek782f2f52010-01-07 01:20:12 +00001342 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1343 // FIXME: This is wrong. We shouldn't be pretending that there is
1344 // an '@end' in the declaration.
1345 SourceLocation L = ClassDecl->getLocation();
1346 AtEnd.setBegin(L);
1347 AtEnd.setEnd(L);
1348 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001349 }
1350
Steve Naroff0701bbb2009-01-08 17:28:14 +00001351 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001352
1353 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1354 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1355 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1356
Chris Lattner4d391482007-12-12 07:09:47 +00001357 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001359 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001360
1361 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001362 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001363 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001364 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001365 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001366 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001367 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001368 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001369 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001370 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001371 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001372 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001373 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001374 InsMap[Method->getSelector()] = Method;
1375 /// The following allows us to typecheck messages to "id".
1376 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001378 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001379 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001380 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001381 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001382 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001384 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001385 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001386 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001387 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001388 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001389 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001390 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001391 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001392 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001393 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001394 /// The following allows us to typecheck messages to "Class".
1395 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001396 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001397 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001398 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001399 }
1400 }
1401 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001403 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001404 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001405 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001406 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001407 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001408 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001409 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001410 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001411
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001412 // Compare protocol properties with those in category
1413 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001414 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001415 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001416 }
Steve Naroff09c47192009-01-09 15:36:25 +00001417 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001418 if (CDecl->getIdentifier())
1419 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1420 // user-defined setter/getter. It also synthesizes setter/getter methods
1421 // and adds them to the DeclContext and global method pools.
1422 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1423 E = CDecl->prop_end();
1424 I != E; ++I)
1425 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001426 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001427 }
1428 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001429 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001430 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001431 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001432 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001433 if (LangOpts.ObjCNonFragileABI2)
1434 while (IDecl->getSuperClass()) {
1435 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1436 IDecl = IDecl->getSuperClass();
1437 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001440 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001441 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Chris Lattner4d391482007-12-12 07:09:47 +00001443 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001444 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001445 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001446 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001447 Categories; Categories = Categories->getNextClassCategory()) {
1448 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001449 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001450 break;
1451 }
1452 }
1453 }
1454 }
Chris Lattner682bf922009-03-29 16:50:03 +00001455 if (isInterfaceDeclKind) {
1456 // Reject invalid vardecls.
1457 for (unsigned i = 0; i != tuvNum; i++) {
1458 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1459 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1460 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001461 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001462 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001463 }
Chris Lattner682bf922009-03-29 16:50:03 +00001464 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001465 }
Chris Lattner4d391482007-12-12 07:09:47 +00001466}
1467
1468
1469/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1470/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001471static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001472CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1473 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1474 if (PQTVal & ObjCDeclSpec::DQ_In)
1475 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1476 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1477 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1478 if (PQTVal & ObjCDeclSpec::DQ_Out)
1479 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1480 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1481 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1482 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1483 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1484 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1485 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001486
1487 return ret;
1488}
1489
Chris Lattnerb28317a2009-03-28 19:18:32 +00001490Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001491 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001492 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001493 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001494 Selector Sel,
1495 // optional arguments. The number of types/arguments is obtained
1496 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001497 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001498 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001499 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1500 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001501 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001502
1503 // Make sure we can establish a context for the method.
1504 if (!ClassDecl) {
1505 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001506 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001507 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001508 }
Chris Lattner4d391482007-12-12 07:09:47 +00001509 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001511 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001512 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001513 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Steve Naroffccef3712009-02-20 22:59:16 +00001515 // Methods cannot return interface types. All ObjC objects are
1516 // passed by reference.
1517 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001518 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1519 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001520 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001521 }
1522 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001523 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001524
1525 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001526 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001527 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001528 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001529 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001530 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001531 MethodDeclKind == tok::objc_optional ?
1532 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001533 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Chris Lattner0ed844b2008-04-04 06:12:32 +00001535 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Chris Lattner7db638d2009-04-11 19:42:43 +00001537 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001538 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001539 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Chris Lattnere294d3f2009-04-11 18:57:04 +00001541 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001542 ArgType = Context.getObjCIdType();
1543 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001544 } else {
John McCall58e46772009-10-23 21:48:59 +00001545 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001546 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001547 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
John McCall58e46772009-10-23 21:48:59 +00001550 ParmVarDecl* Param
1551 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1552 ArgInfo[i].Name, ArgType, DI,
1553 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001555 if (ArgType->isObjCInterfaceType()) {
1556 Diag(ArgInfo[i].NameLoc,
1557 diag::err_object_cannot_be_passed_returned_by_value)
1558 << 1 << ArgType;
1559 Param->setInvalidDecl();
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Chris Lattner0ed844b2008-04-04 06:12:32 +00001562 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001563 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001565 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001566 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Chris Lattner0ed844b2008-04-04 06:12:32 +00001568 Params.push_back(Param);
1569 }
1570
Jay Foadbeaaccd2009-05-21 09:52:38 +00001571 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572 ObjCMethod->setObjCDeclQualifier(
1573 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1574 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001575
1576 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001577 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
John McCall54abf7d2009-11-04 02:18:39 +00001579 const ObjCMethodDecl *InterfaceMD = 0;
1580
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // For implementations (which can be very "coarse grain"), we add the
1582 // method now. This allows the AST to implement lookup methods that work
1583 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001584 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001586 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001587 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001588 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1589 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001590 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001591 PrevMethod = ImpDecl->getClassMethod(Sel);
1592 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001593 }
John McCall54abf7d2009-11-04 02:18:39 +00001594 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1595 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001596 if (AttrList)
1597 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001598 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001599 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001600 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001601 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1602 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001603 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001604 PrevMethod = CatImpDecl->getClassMethod(Sel);
1605 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001606 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001607 if (AttrList)
1608 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001609 }
1610 if (PrevMethod) {
1611 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001612 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001613 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001614 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001615 }
John McCall54abf7d2009-11-04 02:18:39 +00001616
1617 // If the interface declared this method, and it was deprecated there,
1618 // mark it deprecated here.
1619 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1620 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1621
Chris Lattnerb28317a2009-03-28 19:18:32 +00001622 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001623}
1624
Chris Lattnercc98eac2008-12-17 07:13:27 +00001625bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001626 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001627 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Anders Carlsson15281452008-11-04 16:57:32 +00001629 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1630 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Anders Carlsson15281452008-11-04 16:57:32 +00001632 return true;
1633}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001634
Chris Lattnercc98eac2008-12-17 07:13:27 +00001635/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1636/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00001637void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001638 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001639 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001640 // Check that ClassName is a valid class
1641 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1642 if (!Class) {
1643 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1644 return;
1645 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001646 if (LangOpts.ObjCNonFragileABI) {
1647 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1648 return;
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Chris Lattnercc98eac2008-12-17 07:13:27 +00001651 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00001652 llvm::SmallVector<FieldDecl*, 32> RecFields;
1653 Context.CollectObjCIvars(Class, RecFields);
1654 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1655 for (unsigned i = 0; i < RecFields.size(); i++) {
1656 FieldDecl* ID = RecFields[i];
1657 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1658 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1659 ID->getIdentifier(), ID->getType(),
1660 ID->getBitWidth());
1661 Decls.push_back(Sema::DeclPtrTy::make(FD));
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Chris Lattnercc98eac2008-12-17 07:13:27 +00001664 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001665 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001666 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001667 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001668 if (getLangOptions().CPlusPlus)
1669 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001670 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001671 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001672 }
1673}
1674