blob: b2f671760ebefc74e658e3d4cade2e926a055eb0 [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerda463fe2007-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 Gregor35b0bac2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorc78d3462009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000021using namespace clang;
22
Steve Naroff70f41d62009-02-28 16:59:13 +000023/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000024/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000028
Steve Naroff542cd5d2008-07-25 17:57:26 +000029 // If we don't have a valid method decl, simply return.
30 if (!MDecl)
31 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000032
33 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000038
Chris Lattnerda463fe2007-12-12 07:09:47 +000039 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +000041 PushFunctionScope();
42
Chris Lattnerda463fe2007-12-12 07:09:47 +000043 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000048
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000051
Chris Lattner58258242008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-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 Lattnerda463fe2007-12-12 07:09:47 +000057}
58
Chris Lattner83f095c2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +000064 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000065 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000066 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattnerda463fe2007-12-12 07:09:47 +000068 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +000070 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-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 Kremenek1b0ea822008-01-07 19:49:32 +000077 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000078 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000079 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Ted Kremenek1b0ea822008-01-07 19:49:32 +000082 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +000083 if (IDecl) {
84 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +000085 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +000086 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +000087 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +000088 Diag(IDecl->getLocation(), diag::note_previous_definition);
89
Steve Naroff119f60e2008-11-18 19:15:30 +000090 // Return the previous class interface.
91 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +000092 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +000093 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +000094 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +000096 IDecl->setClassLoc(ClassLoc);
Ted Kremenek707ece62009-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 Jahaniand3612392009-11-17 19:08:08 +0000103 if (AttrList)
104 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000105 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000106 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000107 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000108 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000109 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000110 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000111
Steve Naroff3c301dc2009-04-23 15:15:40 +0000112 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000113 }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattnerda463fe2007-12-12 07:09:47 +0000115 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000116 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000117 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregor35b0bac2010-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 Gregor6da83622010-01-07 00:17:44 +0000126 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
127 << PrevDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000128 }
129 }
130
Fariborz Jahanian5582f232009-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 Stump12b8ce12009-08-04 21:02:39 +0000135 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000136 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000137 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000138
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000139 // Diagnose classes that inherit from deprecated classes.
140 if (SuperClassDecl)
141 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Fariborz Jahanian5582f232009-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 McCall9dd450b2009-09-21 23:43:11 +0000149 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000150 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
151 }
152 }
Mike Stump11289f42009-09-09 15:08:12 +0000153
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000162 }
163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000173 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000174 IDecl->setSuperClass(SuperClassDecl);
175 IDecl->setSuperClassLoc(SuperLoc);
176 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000177 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000178 } else { // we have a root class.
179 IDecl->setLocEnd(ClassLoc);
180 }
Mike Stump11289f42009-09-09 15:08:12 +0000181
Steve Naroff119f60e2008-11-18 19:15:30 +0000182 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000183 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000184 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000185 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000186 IDecl->setLocEnd(EndProtoLoc);
187 }
Mike Stump11289f42009-09-09 15:08:12 +0000188
Anders Carlssona6b508a2008-11-04 16:57:32 +0000189 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000190 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000191}
192
193/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000194/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000195Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000196 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000197 SourceLocation AliasLocation,
198 IdentifierInfo *ClassName,
199 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000200 // Look for previous declaration of alias name
John McCall9f3059a2009-10-09 21:13:30 +0000201 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000202 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000203 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000204 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000205 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000206 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000207 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000208 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000209 }
210 // Check for class declaration
John McCall9f3059a2009-10-09 21:13:30 +0000211 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-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 McCall9dd450b2009-09-21 23:43:11 +0000215 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000216 ClassName = IDecl->getIdentifier();
John McCall9f3059a2009-10-09 21:13:30 +0000217 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000218 }
219 }
220 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000221 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
222 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000223 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000224 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000225 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000226 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner219b3e92008-03-16 21:17:37 +0000229 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000230 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000231 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000232
Anders Carlssona6b508a2008-11-04 16:57:32 +0000233 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000234 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000235
Chris Lattner83f095c2009-03-28 19:18:32 +0000236 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000237}
238
Steve Naroff41d09ad2009-03-05 15:22:01 +0000239void Sema::CheckForwardProtocolDeclarationForCircularDependency(
240 IdentifierInfo *PName,
241 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000242 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000243 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
244 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000245
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000246 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff41d09ad2009-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 Stump11289f42009-09-09 15:08:12 +0000251 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000252 PDecl->getLocation(), PDecl->getReferencedProtocols());
253 }
254 }
255}
256
Chris Lattner83f095c2009-03-28 19:18:32 +0000257Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000258Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
259 IdentifierInfo *ProtocolName,
260 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000261 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000262 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000263 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000264 SourceLocation EndProtoLoc,
265 AttributeList *AttrList) {
266 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000267 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000268 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000269 if (PDecl) {
270 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000271 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000272 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000273 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000274 // Just return the protocol we already had.
275 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000276 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000277 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000278 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000279 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000280 CheckForwardProtocolDeclarationForCircularDependency(
281 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
282 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000283
Steve Naroffeb03dac2008-08-13 16:39:22 +0000284 // Make sure the cached decl gets a valid start location.
285 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000286 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000287 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000288 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000289 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000290 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000291 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000292 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000293 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000294 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000295 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000296 /// Check then save referenced protocols.
Douglas Gregor002b6712010-01-16 15:02:53 +0000297 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
298 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000299 PDecl->setLocEnd(EndProtoLoc);
300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
302 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000303 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000304}
305
306/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-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 Lattnerda463fe2007-12-12 07:09:47 +0000309void
Chris Lattner3bbae002008-07-26 04:03:38 +0000310Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000311 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000312 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000313 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000315 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000316 if (!PDecl) {
Douglas Gregor35b0bac2010-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 Gregor6da83622010-01-07 00:17:44 +0000323 Diag(PDecl->getLocation(), diag::note_previous_decl)
324 << PDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000325 }
326 }
327
328 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000329 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000330 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000331 continue;
332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Douglas Gregor171c45a2009-02-18 21:56:37 +0000334 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-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 Lattner3b054132008-11-19 05:08:23 +0000339 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000340 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000341 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000342 }
343}
344
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000345/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000346/// a class method in its extension.
347///
Mike Stump11289f42009-09-09 15:08:12 +0000348void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000354 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
355 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000356 ObjCMethodDecl *MD = *i;
357 MethodMap[MD->getSelector()] = MD;
358 }
359
360 if (MethodMap.empty())
361 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000362 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
363 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-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 Lattnerdac168d2009-04-12 08:43:13 +0000374/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000375Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000376Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000377 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000378 unsigned NumElts,
379 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000380 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000381 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattnerda463fe2007-12-12 07:09:47 +0000383 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000384 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000385 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000386 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000387 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000388 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000389 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000390 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000391 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000392 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000393 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000394 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
397 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000398 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000399 Protocols.data(), Protocols.size(),
400 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000401 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000402 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000403 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000404}
405
Chris Lattner83f095c2009-03-28 19:18:32 +0000406Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000407ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
408 IdentifierInfo *ClassName, SourceLocation ClassLoc,
409 IdentifierInfo *CategoryName,
410 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000411 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000412 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000413 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000414 SourceLocation EndProtoLoc) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000415 ObjCCategoryDecl *CDecl = 0;
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000416 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Ted Kremenek514ff702010-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 Jahanian30a42922010-02-15 21:55:26 +0000431 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek514ff702010-02-23 19:39:46 +0000432 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000433 CDecl = IDecl->getClassExtension();
Ted Kremenek514ff702010-02-23 19:39:46 +0000434 }
435
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000436 if (!CDecl) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000437 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
438 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000439 // FIXME: PushOnScopeChains?
440 CurContext->addDecl(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000441
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000442 CDecl->setClassInterface(IDecl);
443 // Insert first use of class extension to the list of class's categories.
444 if (!CategoryName)
445 CDecl->insertNextClassCategory();
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Chris Lattnerced903b2009-02-16 21:30:01 +0000448 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000449 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000450
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000451 if (CategoryName) {
452 /// Check for duplicate interface declaration for this category
453 ObjCCategoryDecl *CDeclChain;
454 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
455 CDeclChain = CDeclChain->getNextClassCategory()) {
456 if (CDeclChain->getIdentifier() == CategoryName) {
457 // Class extensions can be declared multiple times.
458 Diag(CategoryLoc, diag::warn_dup_category_def)
459 << ClassName << CategoryName;
460 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
461 break;
462 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000463 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000464 if (!CDeclChain)
465 CDecl->insertNextClassCategory();
Chris Lattner9018ca82009-02-16 21:26:43 +0000466 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000467
Chris Lattnerda463fe2007-12-12 07:09:47 +0000468 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000469 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000470 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000471 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000472 if (CDecl->IsClassExtension())
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000473 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000474 NumProtoRefs, ProtoLocs,
475 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Anders Carlssona6b508a2008-11-04 16:57:32 +0000478 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000479 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000480}
481
482/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000483/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000484/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000485Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000486 SourceLocation AtCatImplLoc,
487 IdentifierInfo *ClassName, SourceLocation ClassLoc,
488 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000490 ObjCCategoryDecl *CatIDecl = 0;
491 if (IDecl) {
492 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
493 if (!CatIDecl) {
494 // Category @implementation with no corresponding @interface.
495 // Create and install one.
496 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000497 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000498 CatName);
499 CatIDecl->setClassInterface(IDecl);
500 CatIDecl->insertNextClassCategory();
501 }
502 }
503
Mike Stump11289f42009-09-09 15:08:12 +0000504 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000505 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
506 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000507 /// Check that class of this category is already completely declared.
508 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000509 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000510
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000511 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000512 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000513
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000514 /// Check that CatName, category name, is not used in another implementation.
515 if (CatIDecl) {
516 if (CatIDecl->getImplementation()) {
517 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
518 << CatName;
519 Diag(CatIDecl->getImplementation()->getLocation(),
520 diag::note_previous_definition);
521 } else
522 CatIDecl->setImplementation(CDecl);
523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Anders Carlssona6b508a2008-11-04 16:57:32 +0000525 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000526 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000527}
528
Chris Lattner83f095c2009-03-28 19:18:32 +0000529Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000530 SourceLocation AtClassImplLoc,
531 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000532 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000533 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000534 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000535 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000536 NamedDecl *PrevDecl
537 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000538 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000539 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000540 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor40f7a002010-01-04 17:27:12 +0000541 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
542 // If this is a forward declaration of an interface, warn.
543 if (IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000544 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000545 IDecl = 0;
546 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000547 } else {
548 // We did not find anything with the name ClassName; try to correct for
549 // typos in the class name.
550 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
551 if (CorrectTypo(R, TUScope, 0) &&
552 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000553 // Suggest the (potentially) correct interface name. However, put the
554 // fix-it hint itself in a separate note, since changing the name in
555 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000556 // provide a code-modification hint or use the typo name for recovery,
557 // because this is just a warning. The program may actually be correct.
558 Diag(ClassLoc, diag::warn_undef_interface_suggest)
559 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000560 Diag(IDecl->getLocation(), diag::note_previous_decl)
561 << R.getLookupName()
562 << CodeModificationHint::CreateReplacement(ClassLoc,
563 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000564 IDecl = 0;
565 } else {
566 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
567 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattnerda463fe2007-12-12 07:09:47 +0000570 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000571 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000572 if (SuperClassname) {
573 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000574 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000575 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000576 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
577 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000578 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000579 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000580 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000581 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000582 Diag(SuperClassLoc, diag::err_undef_superclass)
583 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000584 else if (IDecl && IDecl->getSuperClass() != SDecl) {
585 // This implementation and its interface do not have the same
586 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000587 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000588 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000589 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000590 }
591 }
592 }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattnerda463fe2007-12-12 07:09:47 +0000594 if (!IDecl) {
595 // Legacy case of @implementation with no corresponding @interface.
596 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000597
Mike Stump87c57ac2009-05-16 07:39:55 +0000598 // FIXME: Do we support attributes on the @implementation? If so we should
599 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000600 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000601 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000602 IDecl->setSuperClass(SDecl);
603 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000604
605 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000606 } else {
607 // Mark the interface as being completed, even if it was just as
608 // @class ....;
609 // declaration; the user cannot reopen it.
610 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
613 ObjCImplementationDecl* IMPDecl =
614 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000615 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000616
Anders Carlssona6b508a2008-11-04 16:57:32 +0000617 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000618 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattnerda463fe2007-12-12 07:09:47 +0000620 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000621 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000622 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000623 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000624 Diag(IDecl->getImplementation()->getLocation(),
625 diag::note_previous_definition);
626 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000627 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000628 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000629 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000630 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000631}
632
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000633void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
634 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000635 SourceLocation RBrace) {
636 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000637 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000638 if (!IDecl)
639 return;
640 /// Check case of non-existing @interface decl.
641 /// (legacy objective-c @implementation decl without an @interface decl).
642 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000643 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000644 IDecl->setLocEnd(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000645 // Add ivar's to class's DeclContext.
646 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +0000647 ivars[i]->setLexicalDeclContext(ImpDecl);
648 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000649 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000650 }
651
Chris Lattnerda463fe2007-12-12 07:09:47 +0000652 return;
653 }
654 // If implementation has empty ivar list, just return.
655 if (numIvars == 0)
656 return;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattnerda463fe2007-12-12 07:09:47 +0000658 assert(ivars && "missing @implementation ivars");
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000659 if (LangOpts.ObjCNonFragileABI2) {
660 if (ImpDecl->getSuperClass())
661 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
662 for (unsigned i = 0; i < numIvars; i++) {
663 ObjCIvarDecl* ImplIvar = ivars[i];
664 if (const ObjCIvarDecl *ClsIvar =
665 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
666 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
667 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
668 continue;
669 }
670 if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private)
671 Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration);
672 // Instance ivar to Implementation's DeclContext.
673 ImplIvar->setLexicalDeclContext(ImpDecl);
674 IDecl->makeDeclVisibleInContext(ImplIvar, false);
675 ImpDecl->addDecl(ImplIvar);
676 }
677 return;
678 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000679 // Check interface's Ivar list against those in the implementation.
680 // names and types must match.
681 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000682 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000683 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000684 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
685 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000686 ObjCIvarDecl* ImplIvar = ivars[j++];
687 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000688 assert (ImplIvar && "missing implementation ivar");
689 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000690
Steve Naroff157599f2009-03-03 14:49:36 +0000691 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000692 if (Context.getCanonicalType(ImplIvar->getType()) !=
693 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000694 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000695 << ImplIvar->getIdentifier()
696 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000697 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000698 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
699 Expr *ImplBitWidth = ImplIvar->getBitWidth();
700 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000701 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
702 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000703 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
704 << ImplIvar->getIdentifier();
705 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707 }
Steve Naroff157599f2009-03-03 14:49:36 +0000708 // Make sure the names are identical.
709 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000710 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000711 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000712 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000713 }
714 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattner0f29d982007-12-12 18:11:49 +0000717 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000718 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000719 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000720 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000721}
722
Steve Naroff15833ed2008-02-10 21:38:56 +0000723void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
724 bool &IncompleteImpl) {
725 if (!IncompleteImpl) {
726 Diag(ImpLoc, diag::warn_incomplete_impl);
727 IncompleteImpl = true;
728 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000729 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000730}
731
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000732void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
733 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000734 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000735 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000736 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
737 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000738 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000739 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
740 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000741 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner67f35b02009-04-11 19:58:42 +0000744 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
745 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
746 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000747 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
748 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
749 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
750 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000751 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000752
753 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000754 << ImpMethodDecl->getDeclName() << (*IF)->getType()
755 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000756 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000757 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000758}
759
Mike Stump87c57ac2009-05-16 07:39:55 +0000760/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
761/// improve the efficiency of selector lookups and type checking by associating
762/// with each protocol / interface / category the flattened instance tables. If
763/// we used an immutable set to keep the table then it wouldn't add significant
764/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000765
Steve Naroffa36992242008-02-08 22:06:17 +0000766/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000767/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000768void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
769 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000770 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000771 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000772 const llvm::DenseSet<Selector> &ClsMap,
773 ObjCInterfaceDecl *IDecl) {
774 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000775 ObjCInterfaceDecl *NSIDecl = 0;
776 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000777 // check to see if class implements forwardInvocation method and objects
778 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000779 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000780 // Under such conditions, which means that every method possible is
781 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000782 // found" warnings.
783 // FIXME: Use a general GetUnarySelector method for this.
784 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
785 Selector fISelector = Context.Selectors.getSelector(1, &II);
786 if (InsMap.count(fISelector))
787 // Is IDecl derived from 'NSProxy'? If so, no instance methods
788 // need be implemented in the implementation.
789 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000792 // If a method lookup fails locally we still need to look and see if
793 // the method was implemented by a base class or an inherited
794 // protocol. This lookup is slow, but occurs rarely in correct code
795 // and otherwise would terminate in a warning.
796
Chris Lattnerda463fe2007-12-12 07:09:47 +0000797 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000798 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000799 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000800 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000801 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000802 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000803 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000804 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000805 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000806 // Ugly, but necessary. Method declared in protcol might have
807 // have been synthesized due to a property declared in the class which
808 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000809 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000810 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000811 if (!MethodInClass || !MethodInClass->isSynthesized())
812 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
813 }
814 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000815 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000816 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000817 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000818 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000819 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000820 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
821 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000822 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +0000823 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +0000824 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000825 // Check on this protocols's referenced protocols, recursively.
826 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
827 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000828 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000829}
830
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000831/// MatchAllMethodDeclarations - Check methods declaraed in interface or
832/// or protocol against those declared in their implementations.
833///
834void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
835 const llvm::DenseSet<Selector> &ClsMap,
836 llvm::DenseSet<Selector> &InsMapSeen,
837 llvm::DenseSet<Selector> &ClsMapSeen,
838 ObjCImplDecl* IMPDecl,
839 ObjCContainerDecl* CDecl,
840 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000841 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000842 // Check and see if instance methods in class interface have been
843 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000844 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
845 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000846 if (InsMapSeen.count((*I)->getSelector()))
847 continue;
848 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000849 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000850 !InsMap.count((*I)->getSelector())) {
851 if (ImmediateClass)
852 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
853 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000854 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000855 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000856 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000857 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000858 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000859 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000860 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
861 // ImpMethodDecl may be null as in a @dynamic property.
862 if (ImpMethodDecl)
863 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
864 }
865 }
Mike Stump11289f42009-09-09 15:08:12 +0000866
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000867 // Check and see if class methods in class interface have been
868 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000869 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000870 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000871 if (ClsMapSeen.count((*I)->getSelector()))
872 continue;
873 ClsMapSeen.insert((*I)->getSelector());
874 if (!ClsMap.count((*I)->getSelector())) {
875 if (ImmediateClass)
876 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +0000877 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000878 ObjCMethodDecl *ImpMethodDecl =
879 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000880 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000881 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000882 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
883 }
884 }
885 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
886 // Check for any implementation of a methods declared in protocol.
887 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
888 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000889 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
890 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000891 (*PI), IncompleteImpl, false);
892 if (I->getSuperClass())
893 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +0000894 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000895 I->getSuperClass(), IncompleteImpl, false);
896 }
897}
898
Mike Stump11289f42009-09-09 15:08:12 +0000899void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
900 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000901 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000902 llvm::DenseSet<Selector> InsMap;
903 // Check and see if instance methods in class interface have been
904 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000905 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000906 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000907 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000908
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +0000909 // Check and see if properties declared in the interface have either 1)
910 // an implementation or 2) there is a @synthesize/@dynamic implementation
911 // of the property in the @implementation.
912 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian98609b32010-01-20 01:51:55 +0000913 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
914
Chris Lattnerda463fe2007-12-12 07:09:47 +0000915 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +0000916 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000917 I = IMPDecl->classmeth_begin(),
918 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000919 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000920
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000921 // Check for type conflict of methods declared in a class/protocol and
922 // its implementation; if any.
923 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +0000924 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
925 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000926 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattnerda463fe2007-12-12 07:09:47 +0000928 // Check the protocol list for unimplemented methods in the @implementation
929 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000930 // Check and see if class methods in class interface have been
931 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner9ef10f42009-03-01 00:56:52 +0000933 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000934 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +0000935 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000936 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000937 InsMap, ClsMap, I);
938 // Check class extensions (unnamed categories)
939 for (ObjCCategoryDecl *Categories = I->getCategoryList();
940 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000941 if (Categories->IsClassExtension()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +0000942 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
943 break;
944 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000945 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000946 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000947 // For extended class, unimplemented methods in its protocols will
948 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000949 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000950 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
951 E = C->protocol_end(); PI != E; ++PI)
952 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
953 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000954 // Report unimplemented properties in the category as well.
955 // When reporting on missing setter/getters, do not report when
956 // setter/getter is implemented in category's primary class
957 // implementation.
958 if (ObjCInterfaceDecl *ID = C->getClassInterface())
959 if (ObjCImplDecl *IMP = ID->getImplementation()) {
960 for (ObjCImplementationDecl::instmeth_iterator
961 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
962 InsMap.insert((*I)->getSelector());
963 }
964 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
965 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000966 } else
967 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +0000968}
969
Mike Stump11289f42009-09-09 15:08:12 +0000970/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +0000971Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000972Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +0000973 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +0000974 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +0000975 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000976 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattnerda463fe2007-12-12 07:09:47 +0000978 for (unsigned i = 0; i != NumElts; ++i) {
979 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000980 NamedDecl *PrevDecl
981 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +0000982 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000983 // Maybe we will complain about the shadowed template parameter.
984 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
985 // Just pretend that we didn't see the previous declaration.
986 PrevDecl = 0;
987 }
988
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000989 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +0000990 // GCC apparently allows the following idiom:
991 //
992 // typedef NSObject < XCElementTogglerP > XCElementToggler;
993 // @class XCElementToggler;
994 //
Mike Stump11289f42009-09-09 15:08:12 +0000995 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +0000996 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
997 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000998 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +0000999 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001000 } else if (TDD) {
1001 // a forward class declaration matching a typedef name of a class refers
1002 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001003 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001004 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1005 PrevDecl = OI->getDecl();
1006 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001009 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001010 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001011 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001012
1013 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1014 // the current DeclContext. This prevents clients that walk DeclContext
1015 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1016 // declared later (if at all). We also take care to explicitly make
1017 // sure this declaration is visible for name lookup.
1018 PushOnScopeChains(IDecl, TUScope, false);
1019 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001020 }
1021
1022 Interfaces.push_back(IDecl);
1023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Ted Kremenek9b124e12009-11-18 00:28:11 +00001025 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001026 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001027 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001028 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001029 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001030 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001031 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001032}
1033
1034
1035/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1036/// returns true, or false, accordingly.
1037/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001038bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001039 const ObjCMethodDecl *PrevMethod,
1040 bool matchBasedOnSizeAndAlignment) {
1041 QualType T1 = Context.getCanonicalType(Method->getResultType());
1042 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001043
Steve Naroffa0ed1652008-10-21 10:37:50 +00001044 if (T1 != T2) {
1045 // The result types are different.
1046 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001047 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001048 // Incomplete types don't have a size and alignment.
1049 if (T1->isIncompleteType() || T2->isIncompleteType())
1050 return false;
1051 // Check is based on size and alignment.
1052 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1053 return false;
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattnera4997152009-02-20 18:43:26 +00001056 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1057 E = Method->param_end();
1058 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattnera4997152009-02-20 18:43:26 +00001060 for (; ParamI != E; ++ParamI, ++PrevI) {
1061 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1062 T1 = Context.getCanonicalType((*ParamI)->getType());
1063 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001064 if (T1 != T2) {
1065 // The result types are different.
1066 if (!matchBasedOnSizeAndAlignment)
1067 return false;
1068 // Incomplete types don't have a size and alignment.
1069 if (T1->isIncompleteType() || T2->isIncompleteType())
1070 return false;
1071 // Check is based on size and alignment.
1072 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1073 return false;
1074 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001075 }
1076 return true;
1077}
1078
Douglas Gregorc78d3462009-04-24 21:10:55 +00001079/// \brief Read the contents of the instance and factory method pools
1080/// for a given selector from external storage.
1081///
1082/// This routine should only be called once, when neither the instance
1083/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001084Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001085 bool isInstance) {
1086 assert(ExternalSource && "We need an external AST source");
1087 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1088 "Selector data already loaded into the instance method pool");
1089 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1090 "Selector data already loaded into the factory method pool");
1091
1092 // Read the method list from the external source.
1093 std::pair<ObjCMethodList, ObjCMethodList> Methods
1094 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregorc78d3462009-04-24 21:10:55 +00001096 if (isInstance) {
1097 if (Methods.second.Method)
1098 FactoryMethodPool[Sel] = Methods.second;
1099 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001100 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001101
1102 if (Methods.first.Method)
1103 InstanceMethodPool[Sel] = Methods.first;
1104
1105 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1106}
1107
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001108void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001109 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1110 = InstanceMethodPool.find(Method->getSelector());
1111 if (Pos == InstanceMethodPool.end()) {
1112 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1113 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1114 else
1115 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1116 ObjCMethodList())).first;
1117 }
1118
1119 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001120 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001121 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001122 Entry.Method = Method;
1123 Entry.Next = 0;
1124 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner7b26b292009-03-04 05:16:45 +00001127 // We've seen a method with this name, see if we have already seen this type
1128 // signature.
1129 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1130 if (MatchTwoMethodDeclarations(Method, List->Method))
1131 return;
Mike Stump11289f42009-09-09 15:08:12 +00001132
Chris Lattner7b26b292009-03-04 05:16:45 +00001133 // We have a new signature for an existing method - add it.
1134 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001135 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1136 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001137}
1138
Steve Naroff9ebc0502008-10-21 10:50:19 +00001139// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001140ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001141 SourceRange R,
1142 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001143 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1144 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001145 if (Pos == InstanceMethodPool.end()) {
1146 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001147 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1148 else
1149 return 0;
1150 }
1151
1152 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001153 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Steve Naroff4a82d812008-09-30 14:38:43 +00001155 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001156 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1157 // This checks if the methods differ by size & alignment.
1158 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001159 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001160 }
1161 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001162 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001163 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001164 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001165 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001166 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001167 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001168 }
1169 return MethList.Method;
1170}
1171
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001172void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001173 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1174 = FactoryMethodPool.find(Method->getSelector());
1175 if (Pos == FactoryMethodPool.end()) {
1176 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1177 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1178 else
1179 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1180 ObjCMethodList())).first;
1181 }
1182
1183 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001184 if (!FirstMethod.Method) {
1185 // Haven't seen a method with this selector name yet - add it.
1186 FirstMethod.Method = Method;
1187 FirstMethod.Next = 0;
1188 } else {
1189 // We've seen a method with this name, now check the type signature(s).
1190 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001191
1192 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001193 Next = Next->Next)
1194 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattnerda463fe2007-12-12 07:09:47 +00001196 if (!match) {
1197 // We have a new signature for an existing method - add it.
1198 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001199 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1200 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001201 FirstMethod.Next = OMI;
1202 }
1203 }
1204}
1205
Mike Stump11289f42009-09-09 15:08:12 +00001206ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001207 SourceRange R) {
1208 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1209 = FactoryMethodPool.find(Sel);
1210 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001211 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001212 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1213 else
1214 return 0;
1215 }
1216
1217 ObjCMethodList &MethList = Pos->second;
1218 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001219
Douglas Gregorc78d3462009-04-24 21:10:55 +00001220 if (MethList.Method && MethList.Next) {
1221 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1222 // This checks if the methods differ by size & alignment.
1223 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1224 issueWarning = true;
1225 }
1226 if (issueWarning && (MethList.Method && MethList.Next)) {
1227 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001228 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001229 << MethList.Method->getSourceRange();
1230 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001231 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001232 << Next->Method->getSourceRange();
1233 }
1234 return MethList.Method;
1235}
1236
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001237/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1238/// identical selector names in current and its super classes and issues
1239/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001240void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1241 ObjCMethodDecl *Method,
1242 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001243 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1244 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001245
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001246 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001247 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001248 SD->lookupMethod(Method->getSelector(), IsInstance);
1249 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001250 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001251 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001252 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001253 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1254 E = Method->param_end();
1255 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1256 for (; ParamI != E; ++ParamI, ++PrevI) {
1257 // Number of parameters are the same and is guaranteed by selector match.
1258 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1259 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1260 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1261 // If type of arguement of method in this class does not match its
1262 // respective argument type in the super class method, issue warning;
1263 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001264 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001265 << T1 << T2;
1266 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1267 return;
1268 }
1269 }
1270 ID = SD;
1271 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001272}
1273
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001274/// DiagnoseDuplicateIvars -
1275/// Check for duplicate ivars in the entire class at the start of
1276/// @implementation. This becomes necesssary because class extension can
1277/// add ivars to a class in random order which will not be known until
1278/// class's @implementation is seen.
1279void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1280 ObjCInterfaceDecl *SID) {
1281 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1282 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1283 ObjCIvarDecl* Ivar = (*IVI);
1284 if (Ivar->isInvalidDecl())
1285 continue;
1286 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1287 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1288 if (prevIvar) {
1289 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1290 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1291 Ivar->setInvalidDecl();
1292 }
1293 }
1294 }
1295}
1296
Steve Naroff1d2538c2007-12-18 01:30:32 +00001297// Note: For class/category implemenations, allMethods/allProperties is
1298// always null.
Ted Kremenekc7c64312010-01-07 01:20:12 +00001299void Sema::ActOnAtEnd(SourceRange AtEnd,
1300 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001301 DeclPtrTy *allMethods, unsigned allNum,
1302 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001303 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001304 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001305
Steve Naroff1d2538c2007-12-18 01:30:32 +00001306 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1307 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001308 // should be true.
1309 if (!ClassDecl)
1310 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001311
Mike Stump11289f42009-09-09 15:08:12 +00001312 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001313 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1314 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001315 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001316
Ted Kremenekc7c64312010-01-07 01:20:12 +00001317 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1318 // FIXME: This is wrong. We shouldn't be pretending that there is
1319 // an '@end' in the declaration.
1320 SourceLocation L = ClassDecl->getLocation();
1321 AtEnd.setBegin(L);
1322 AtEnd.setEnd(L);
1323 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001324 }
1325
Steve Naroff35c62ae2009-01-08 17:28:14 +00001326 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001327
1328 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1329 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1330 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1331
Chris Lattnerda463fe2007-12-12 07:09:47 +00001332 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001333 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001334 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001335
1336 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001337 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001338 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001339 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001340 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001341 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001342 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001343 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001344 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001345 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001346 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001347 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001348 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001349 InsMap[Method->getSelector()] = Method;
1350 /// The following allows us to typecheck messages to "id".
1351 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001352 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001353 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001354 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001355 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001356 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001357 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001358 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001359 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001360 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001361 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001362 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001363 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001364 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001365 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001366 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001367 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001368 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001369 /// The following allows us to typecheck messages to "Class".
1370 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001371 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001372 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001373 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001374 }
1375 }
1376 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001377 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001378 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001379 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001380 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001381 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001382 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001383 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001384 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001385 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001386
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001387 // Compare protocol properties with those in category
1388 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001389 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001390 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001391 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001392 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001393 if (CDecl->getIdentifier())
1394 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1395 // user-defined setter/getter. It also synthesizes setter/getter methods
1396 // and adds them to the DeclContext and global method pools.
1397 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1398 E = CDecl->prop_end();
1399 I != E; ++I)
1400 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001401 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001402 }
1403 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001404 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001405 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001406 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001407 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001408 if (LangOpts.ObjCNonFragileABI2)
1409 while (IDecl->getSuperClass()) {
1410 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1411 IDecl = IDecl->getSuperClass();
1412 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001415 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001416 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001417
Chris Lattnerda463fe2007-12-12 07:09:47 +00001418 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001419 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001420 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001421 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001422 Categories; Categories = Categories->getNextClassCategory()) {
1423 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001424 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001425 break;
1426 }
1427 }
1428 }
1429 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001430 if (isInterfaceDeclKind) {
1431 // Reject invalid vardecls.
1432 for (unsigned i = 0; i != tuvNum; i++) {
1433 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1434 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1435 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001436 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001437 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001438 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001439 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001440 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001441}
1442
1443
1444/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1445/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001446static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001447CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1448 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1449 if (PQTVal & ObjCDeclSpec::DQ_In)
1450 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1451 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1452 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1453 if (PQTVal & ObjCDeclSpec::DQ_Out)
1454 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1455 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1456 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1457 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1458 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1459 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1460 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001461
1462 return ret;
1463}
1464
Chris Lattner83f095c2009-03-28 19:18:32 +00001465Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001466 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001467 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001468 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001469 Selector Sel,
1470 // optional arguments. The number of types/arguments is obtained
1471 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001472 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001473 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001474 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1475 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001476 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001477
1478 // Make sure we can establish a context for the method.
1479 if (!ClassDecl) {
1480 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9a28e842010-03-01 23:15:13 +00001481 getLabelMap().clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001482 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001483 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001484 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001485
Douglas Gregor12852d92010-03-08 14:59:44 +00001486 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00001487 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00001488 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001489
Steve Naroff32606412009-02-20 22:59:16 +00001490 // Methods cannot return interface types. All ObjC objects are
1491 // passed by reference.
1492 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001493 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1494 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001495 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001496 }
1497 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001498 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001499
1500 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001501 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00001502 ResultTInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001503 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001504 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001505 false,
Mike Stump11289f42009-09-09 15:08:12 +00001506 MethodDeclKind == tok::objc_optional ?
1507 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001508 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001509
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001510 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001511
Chris Lattner23b0faf2009-04-11 19:42:43 +00001512 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001513 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001514 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001515
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001516 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001517 ArgType = Context.getObjCIdType();
1518 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001519 } else {
John McCall856bbea2009-10-23 21:48:59 +00001520 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001521 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001522 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001523 }
Mike Stump11289f42009-09-09 15:08:12 +00001524
John McCall856bbea2009-10-23 21:48:59 +00001525 ParmVarDecl* Param
1526 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1527 ArgInfo[i].Name, ArgType, DI,
1528 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001529
Chris Lattner9713a1c2009-04-11 19:34:56 +00001530 if (ArgType->isObjCInterfaceType()) {
1531 Diag(ArgInfo[i].NameLoc,
1532 diag::err_object_cannot_be_passed_returned_by_value)
1533 << 1 << ArgType;
1534 Param->setInvalidDecl();
1535 }
Mike Stump11289f42009-09-09 15:08:12 +00001536
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001537 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001538 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001539
Chris Lattner9713a1c2009-04-11 19:34:56 +00001540 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001541 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001542
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001543 Params.push_back(Param);
1544 }
1545
Jay Foad7d0479f2009-05-21 09:52:38 +00001546 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001547 ObjCMethod->setObjCDeclQualifier(
1548 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1549 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001550
1551 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001552 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001553
John McCall28a6aea2009-11-04 02:18:39 +00001554 const ObjCMethodDecl *InterfaceMD = 0;
1555
Mike Stump11289f42009-09-09 15:08:12 +00001556 // For implementations (which can be very "coarse grain"), we add the
1557 // method now. This allows the AST to implement lookup methods that work
1558 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001559 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001560 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001561 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001562 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001563 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1564 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001565 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001566 PrevMethod = ImpDecl->getClassMethod(Sel);
1567 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001568 }
John McCall28a6aea2009-11-04 02:18:39 +00001569 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1570 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001571 if (AttrList)
1572 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001573 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001574 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001575 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001576 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1577 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001578 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001579 PrevMethod = CatImpDecl->getClassMethod(Sel);
1580 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001581 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001582 if (AttrList)
1583 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001584 }
1585 if (PrevMethod) {
1586 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001587 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001588 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001589 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001590 }
John McCall28a6aea2009-11-04 02:18:39 +00001591
1592 // If the interface declared this method, and it was deprecated there,
1593 // mark it deprecated here.
1594 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1595 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1596
Chris Lattner83f095c2009-03-28 19:18:32 +00001597 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001598}
1599
Chris Lattner438e5012008-12-17 07:13:27 +00001600bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001601 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00001602 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001603
Anders Carlssona6b508a2008-11-04 16:57:32 +00001604 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1605 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001606
Anders Carlssona6b508a2008-11-04 16:57:32 +00001607 return true;
1608}
Chris Lattner438e5012008-12-17 07:13:27 +00001609
Chris Lattner438e5012008-12-17 07:13:27 +00001610/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1611/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00001612void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00001613 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00001614 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00001615 // Check that ClassName is a valid class
1616 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1617 if (!Class) {
1618 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1619 return;
1620 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00001621 if (LangOpts.ObjCNonFragileABI) {
1622 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1623 return;
1624 }
Mike Stump11289f42009-09-09 15:08:12 +00001625
Chris Lattner438e5012008-12-17 07:13:27 +00001626 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001627 llvm::SmallVector<FieldDecl*, 32> RecFields;
1628 Context.CollectObjCIvars(Class, RecFields);
1629 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1630 for (unsigned i = 0; i < RecFields.size(); i++) {
1631 FieldDecl* ID = RecFields[i];
1632 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1633 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1634 ID->getIdentifier(), ID->getType(),
1635 ID->getBitWidth());
1636 Decls.push_back(Sema::DeclPtrTy::make(FD));
1637 }
Mike Stump11289f42009-09-09 15:08:12 +00001638
Chris Lattner438e5012008-12-17 07:13:27 +00001639 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00001640 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00001641 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001642 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00001643 if (getLangOptions().CPlusPlus)
1644 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001645 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001646 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00001647 }
1648}
1649