blob: 79a664f2e1f8f6807f75e7e429bf2db1200eb830 [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 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000670 // Instance ivar to Implementation's DeclContext.
671 ImplIvar->setLexicalDeclContext(ImpDecl);
672 IDecl->makeDeclVisibleInContext(ImplIvar, false);
673 ImpDecl->addDecl(ImplIvar);
674 }
675 return;
676 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000677 // Check interface's Ivar list against those in the implementation.
678 // names and types must match.
679 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000680 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000681 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000682 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
683 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000684 ObjCIvarDecl* ImplIvar = ivars[j++];
685 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000686 assert (ImplIvar && "missing implementation ivar");
687 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000688
Steve Naroff157599f2009-03-03 14:49:36 +0000689 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000690 if (Context.getCanonicalType(ImplIvar->getType()) !=
691 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000692 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000693 << ImplIvar->getIdentifier()
694 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000695 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000696 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
697 Expr *ImplBitWidth = ImplIvar->getBitWidth();
698 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000699 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
700 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000701 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
702 << ImplIvar->getIdentifier();
703 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
704 }
Mike Stump11289f42009-09-09 15:08:12 +0000705 }
Steve Naroff157599f2009-03-03 14:49:36 +0000706 // Make sure the names are identical.
707 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000708 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000709 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000710 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000711 }
712 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattner0f29d982007-12-12 18:11:49 +0000715 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000716 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000717 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000718 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000719}
720
Steve Naroff15833ed2008-02-10 21:38:56 +0000721void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
722 bool &IncompleteImpl) {
723 if (!IncompleteImpl) {
724 Diag(ImpLoc, diag::warn_incomplete_impl);
725 IncompleteImpl = true;
726 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000727 Diag(method->getLocation(), diag::note_undef_method_impl)
728 << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000729}
730
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000731void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
732 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000733 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000734 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000735 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
736 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000737 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000738 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
739 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000740 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
741 }
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattner67f35b02009-04-11 19:58:42 +0000743 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
744 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
745 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000746 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
747 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
748 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
749 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000750 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000751
752 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000753 << ImpMethodDecl->getDeclName() << (*IF)->getType()
754 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000755 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000756 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000757}
758
Mike Stump87c57ac2009-05-16 07:39:55 +0000759/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
760/// improve the efficiency of selector lookups and type checking by associating
761/// with each protocol / interface / category the flattened instance tables. If
762/// we used an immutable set to keep the table then it wouldn't add significant
763/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000764
Steve Naroffa36992242008-02-08 22:06:17 +0000765/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000766/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000767void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
768 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000769 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000770 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000771 const llvm::DenseSet<Selector> &ClsMap,
772 ObjCInterfaceDecl *IDecl) {
773 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000774 ObjCInterfaceDecl *NSIDecl = 0;
775 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000776 // check to see if class implements forwardInvocation method and objects
777 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000778 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000779 // Under such conditions, which means that every method possible is
780 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000781 // found" warnings.
782 // FIXME: Use a general GetUnarySelector method for this.
783 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
784 Selector fISelector = Context.Selectors.getSelector(1, &II);
785 if (InsMap.count(fISelector))
786 // Is IDecl derived from 'NSProxy'? If so, no instance methods
787 // need be implemented in the implementation.
788 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
789 }
Mike Stump11289f42009-09-09 15:08:12 +0000790
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000791 // If a method lookup fails locally we still need to look and see if
792 // the method was implemented by a base class or an inherited
793 // protocol. This lookup is slow, but occurs rarely in correct code
794 // and otherwise would terminate in a warning.
795
Chris Lattnerda463fe2007-12-12 07:09:47 +0000796 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000797 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000798 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000799 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000800 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000801 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000802 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000803 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000804 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000805 // Ugly, but necessary. Method declared in protcol might have
806 // have been synthesized due to a property declared in the class which
807 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000808 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000809 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000810 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000811 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000812 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
813 PDecl->getDeclName();
814 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000815 }
816 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000817 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000818 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000819 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000820 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000821 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000822 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
823 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000824 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Steve Naroff15833ed2008-02-10 21:38:56 +0000825 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000826 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
827 PDecl->getDeclName();
828 }
Steve Naroff3ce37a62007-12-14 23:37:57 +0000829 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000830 // Check on this protocols's referenced protocols, recursively.
831 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
832 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000833 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000834}
835
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000836/// MatchAllMethodDeclarations - Check methods declaraed in interface or
837/// or protocol against those declared in their implementations.
838///
839void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
840 const llvm::DenseSet<Selector> &ClsMap,
841 llvm::DenseSet<Selector> &InsMapSeen,
842 llvm::DenseSet<Selector> &ClsMapSeen,
843 ObjCImplDecl* IMPDecl,
844 ObjCContainerDecl* CDecl,
845 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000846 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000847 // Check and see if instance methods in class interface have been
848 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000849 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
850 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000851 if (InsMapSeen.count((*I)->getSelector()))
852 continue;
853 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000854 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000855 !InsMap.count((*I)->getSelector())) {
856 if (ImmediateClass)
857 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
858 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000859 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000860 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000861 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000862 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000863 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000864 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000865 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
866 // ImpMethodDecl may be null as in a @dynamic property.
867 if (ImpMethodDecl)
868 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
869 }
870 }
Mike Stump11289f42009-09-09 15:08:12 +0000871
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000872 // Check and see if class methods in class interface have been
873 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000874 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000875 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000876 if (ClsMapSeen.count((*I)->getSelector()))
877 continue;
878 ClsMapSeen.insert((*I)->getSelector());
879 if (!ClsMap.count((*I)->getSelector())) {
880 if (ImmediateClass)
881 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +0000882 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000883 ObjCMethodDecl *ImpMethodDecl =
884 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000885 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000886 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000887 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
888 }
889 }
890 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
891 // Check for any implementation of a methods declared in protocol.
892 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
893 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000894 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
895 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000896 (*PI), IncompleteImpl, false);
897 if (I->getSuperClass())
898 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +0000899 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000900 I->getSuperClass(), IncompleteImpl, false);
901 }
902}
903
Mike Stump11289f42009-09-09 15:08:12 +0000904void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
905 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000906 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000907 llvm::DenseSet<Selector> InsMap;
908 // Check and see if instance methods in class interface have been
909 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000910 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000911 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000912 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000913
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +0000914 // Check and see if properties declared in the interface have either 1)
915 // an implementation or 2) there is a @synthesize/@dynamic implementation
916 // of the property in the @implementation.
917 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian98609b32010-01-20 01:51:55 +0000918 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
919
Chris Lattnerda463fe2007-12-12 07:09:47 +0000920 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +0000921 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000922 I = IMPDecl->classmeth_begin(),
923 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000924 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000925
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000926 // Check for type conflict of methods declared in a class/protocol and
927 // its implementation; if any.
928 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +0000929 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
930 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000931 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattnerda463fe2007-12-12 07:09:47 +0000933 // Check the protocol list for unimplemented methods in the @implementation
934 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000935 // Check and see if class methods in class interface have been
936 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattner9ef10f42009-03-01 00:56:52 +0000938 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000939 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +0000940 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000941 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000942 InsMap, ClsMap, I);
943 // Check class extensions (unnamed categories)
944 for (ObjCCategoryDecl *Categories = I->getCategoryList();
945 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000946 if (Categories->IsClassExtension()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +0000947 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
948 break;
949 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000950 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000951 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000952 // For extended class, unimplemented methods in its protocols will
953 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000954 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000955 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
956 E = C->protocol_end(); PI != E; ++PI)
957 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
958 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000959 // Report unimplemented properties in the category as well.
960 // When reporting on missing setter/getters, do not report when
961 // setter/getter is implemented in category's primary class
962 // implementation.
963 if (ObjCInterfaceDecl *ID = C->getClassInterface())
964 if (ObjCImplDecl *IMP = ID->getImplementation()) {
965 for (ObjCImplementationDecl::instmeth_iterator
966 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
967 InsMap.insert((*I)->getSelector());
968 }
969 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
970 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000971 } else
972 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +0000973}
974
Mike Stump11289f42009-09-09 15:08:12 +0000975/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +0000976Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000977Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +0000978 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +0000979 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +0000980 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000981 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +0000982
Chris Lattnerda463fe2007-12-12 07:09:47 +0000983 for (unsigned i = 0; i != NumElts; ++i) {
984 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000985 NamedDecl *PrevDecl
986 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +0000987 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000988 // Maybe we will complain about the shadowed template parameter.
989 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
990 // Just pretend that we didn't see the previous declaration.
991 PrevDecl = 0;
992 }
993
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000994 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +0000995 // GCC apparently allows the following idiom:
996 //
997 // typedef NSObject < XCElementTogglerP > XCElementToggler;
998 // @class XCElementToggler;
999 //
Mike Stump11289f42009-09-09 15:08:12 +00001000 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001001 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1002 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001003 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001004 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001005 } else if (TDD) {
1006 // a forward class declaration matching a typedef name of a class refers
1007 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001008 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001009 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1010 PrevDecl = OI->getDecl();
1011 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001012 }
Mike Stump11289f42009-09-09 15:08:12 +00001013 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001014 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001015 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001016 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001017
1018 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1019 // the current DeclContext. This prevents clients that walk DeclContext
1020 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1021 // declared later (if at all). We also take care to explicitly make
1022 // sure this declaration is visible for name lookup.
1023 PushOnScopeChains(IDecl, TUScope, false);
1024 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001025 }
1026
1027 Interfaces.push_back(IDecl);
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Ted Kremenek9b124e12009-11-18 00:28:11 +00001030 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001031 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001032 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001033 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001034 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001035 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001036 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001037}
1038
1039
1040/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1041/// returns true, or false, accordingly.
1042/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001043bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001044 const ObjCMethodDecl *PrevMethod,
1045 bool matchBasedOnSizeAndAlignment) {
1046 QualType T1 = Context.getCanonicalType(Method->getResultType());
1047 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001048
Steve Naroffa0ed1652008-10-21 10:37:50 +00001049 if (T1 != T2) {
1050 // The result types are different.
1051 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001052 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001053 // Incomplete types don't have a size and alignment.
1054 if (T1->isIncompleteType() || T2->isIncompleteType())
1055 return false;
1056 // Check is based on size and alignment.
1057 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1058 return false;
1059 }
Mike Stump11289f42009-09-09 15:08:12 +00001060
Chris Lattnera4997152009-02-20 18:43:26 +00001061 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1062 E = Method->param_end();
1063 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattnera4997152009-02-20 18:43:26 +00001065 for (; ParamI != E; ++ParamI, ++PrevI) {
1066 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1067 T1 = Context.getCanonicalType((*ParamI)->getType());
1068 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001069 if (T1 != T2) {
1070 // The result types are different.
1071 if (!matchBasedOnSizeAndAlignment)
1072 return false;
1073 // Incomplete types don't have a size and alignment.
1074 if (T1->isIncompleteType() || T2->isIncompleteType())
1075 return false;
1076 // Check is based on size and alignment.
1077 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1078 return false;
1079 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001080 }
1081 return true;
1082}
1083
Douglas Gregorc78d3462009-04-24 21:10:55 +00001084/// \brief Read the contents of the instance and factory method pools
1085/// for a given selector from external storage.
1086///
1087/// This routine should only be called once, when neither the instance
1088/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001089Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001090 bool isInstance) {
1091 assert(ExternalSource && "We need an external AST source");
1092 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1093 "Selector data already loaded into the instance method pool");
1094 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1095 "Selector data already loaded into the factory method pool");
1096
1097 // Read the method list from the external source.
1098 std::pair<ObjCMethodList, ObjCMethodList> Methods
1099 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Douglas Gregorc78d3462009-04-24 21:10:55 +00001101 if (isInstance) {
1102 if (Methods.second.Method)
1103 FactoryMethodPool[Sel] = Methods.second;
1104 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001105 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001106
1107 if (Methods.first.Method)
1108 InstanceMethodPool[Sel] = Methods.first;
1109
1110 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1111}
1112
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001113void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001114 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1115 = InstanceMethodPool.find(Method->getSelector());
1116 if (Pos == InstanceMethodPool.end()) {
1117 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1118 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1119 else
1120 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1121 ObjCMethodList())).first;
1122 }
1123
1124 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001125 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001126 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001127 Entry.Method = Method;
1128 Entry.Next = 0;
1129 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001130 }
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner7b26b292009-03-04 05:16:45 +00001132 // We've seen a method with this name, see if we have already seen this type
1133 // signature.
1134 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1135 if (MatchTwoMethodDeclarations(Method, List->Method))
1136 return;
Mike Stump11289f42009-09-09 15:08:12 +00001137
Chris Lattner7b26b292009-03-04 05:16:45 +00001138 // We have a new signature for an existing method - add it.
1139 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001140 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1141 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001142}
1143
Steve Naroff9ebc0502008-10-21 10:50:19 +00001144// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001145ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001146 SourceRange R,
1147 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001148 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1149 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001150 if (Pos == InstanceMethodPool.end()) {
1151 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001152 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1153 else
1154 return 0;
1155 }
1156
1157 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001158 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001159
Steve Naroff4a82d812008-09-30 14:38:43 +00001160 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001161 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1162 // This checks if the methods differ by size & alignment.
1163 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001164 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001165 }
1166 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001167 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001168 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001169 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001170 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001171 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001172 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001173 }
1174 return MethList.Method;
1175}
1176
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001177void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001178 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1179 = FactoryMethodPool.find(Method->getSelector());
1180 if (Pos == FactoryMethodPool.end()) {
1181 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1182 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1183 else
1184 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1185 ObjCMethodList())).first;
1186 }
1187
1188 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001189 if (!FirstMethod.Method) {
1190 // Haven't seen a method with this selector name yet - add it.
1191 FirstMethod.Method = Method;
1192 FirstMethod.Next = 0;
1193 } else {
1194 // We've seen a method with this name, now check the type signature(s).
1195 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001196
1197 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001198 Next = Next->Next)
1199 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001200
Chris Lattnerda463fe2007-12-12 07:09:47 +00001201 if (!match) {
1202 // We have a new signature for an existing method - add it.
1203 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001204 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1205 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001206 FirstMethod.Next = OMI;
1207 }
1208 }
1209}
1210
Mike Stump11289f42009-09-09 15:08:12 +00001211ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001212 SourceRange R) {
1213 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1214 = FactoryMethodPool.find(Sel);
1215 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001216 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001217 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1218 else
1219 return 0;
1220 }
1221
1222 ObjCMethodList &MethList = Pos->second;
1223 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001224
Douglas Gregorc78d3462009-04-24 21:10:55 +00001225 if (MethList.Method && MethList.Next) {
1226 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1227 // This checks if the methods differ by size & alignment.
1228 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1229 issueWarning = true;
1230 }
1231 if (issueWarning && (MethList.Method && MethList.Next)) {
1232 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001233 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001234 << MethList.Method->getSourceRange();
1235 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001236 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001237 << Next->Method->getSourceRange();
1238 }
1239 return MethList.Method;
1240}
1241
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001242/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1243/// identical selector names in current and its super classes and issues
1244/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001245void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1246 ObjCMethodDecl *Method,
1247 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001248 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1249 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001250
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001251 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001252 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001253 SD->lookupMethod(Method->getSelector(), IsInstance);
1254 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001255 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001256 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001257 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001258 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1259 E = Method->param_end();
1260 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1261 for (; ParamI != E; ++ParamI, ++PrevI) {
1262 // Number of parameters are the same and is guaranteed by selector match.
1263 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1264 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1265 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1266 // If type of arguement of method in this class does not match its
1267 // respective argument type in the super class method, issue warning;
1268 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001269 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001270 << T1 << T2;
1271 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1272 return;
1273 }
1274 }
1275 ID = SD;
1276 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001277}
1278
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001279/// DiagnoseDuplicateIvars -
1280/// Check for duplicate ivars in the entire class at the start of
1281/// @implementation. This becomes necesssary because class extension can
1282/// add ivars to a class in random order which will not be known until
1283/// class's @implementation is seen.
1284void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1285 ObjCInterfaceDecl *SID) {
1286 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1287 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1288 ObjCIvarDecl* Ivar = (*IVI);
1289 if (Ivar->isInvalidDecl())
1290 continue;
1291 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1292 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1293 if (prevIvar) {
1294 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1295 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1296 Ivar->setInvalidDecl();
1297 }
1298 }
1299 }
1300}
1301
Steve Naroff1d2538c2007-12-18 01:30:32 +00001302// Note: For class/category implemenations, allMethods/allProperties is
1303// always null.
Ted Kremenekc7c64312010-01-07 01:20:12 +00001304void Sema::ActOnAtEnd(SourceRange AtEnd,
1305 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001306 DeclPtrTy *allMethods, unsigned allNum,
1307 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001308 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001309 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001310
Steve Naroff1d2538c2007-12-18 01:30:32 +00001311 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1312 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001313 // should be true.
1314 if (!ClassDecl)
1315 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001316
Mike Stump11289f42009-09-09 15:08:12 +00001317 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001318 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1319 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001320 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001321
Ted Kremenekc7c64312010-01-07 01:20:12 +00001322 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1323 // FIXME: This is wrong. We shouldn't be pretending that there is
1324 // an '@end' in the declaration.
1325 SourceLocation L = ClassDecl->getLocation();
1326 AtEnd.setBegin(L);
1327 AtEnd.setEnd(L);
1328 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001329 }
1330
Steve Naroff35c62ae2009-01-08 17:28:14 +00001331 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001332
1333 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1334 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1335 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1336
Chris Lattnerda463fe2007-12-12 07:09:47 +00001337 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001338 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001339 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001340
1341 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001342 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001343 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001344 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001345 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001346 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001347 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001348 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001349 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001350 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001351 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001352 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001353 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001354 InsMap[Method->getSelector()] = Method;
1355 /// The following allows us to typecheck messages to "id".
1356 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001357 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001358 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001359 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001360 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001361 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001362 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001363 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001364 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001365 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001366 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001367 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001368 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001369 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001370 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001371 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001372 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001373 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001374 /// The following allows us to typecheck messages to "Class".
1375 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001376 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001377 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001378 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001379 }
1380 }
1381 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001382 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001383 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001384 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001385 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001386 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001387 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001388 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001389 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001390 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001391
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001392 // Compare protocol properties with those in category
1393 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001394 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001395 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001396 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001397 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001398 if (CDecl->getIdentifier())
1399 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1400 // user-defined setter/getter. It also synthesizes setter/getter methods
1401 // and adds them to the DeclContext and global method pools.
1402 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1403 E = CDecl->prop_end();
1404 I != E; ++I)
1405 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001406 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001407 }
1408 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001409 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001410 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001411 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001412 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001413 if (LangOpts.ObjCNonFragileABI2)
1414 while (IDecl->getSuperClass()) {
1415 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1416 IDecl = IDecl->getSuperClass();
1417 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001418 }
Mike Stump11289f42009-09-09 15:08:12 +00001419 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001420 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001421 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattnerda463fe2007-12-12 07:09:47 +00001423 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001424 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001425 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001426 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001427 Categories; Categories = Categories->getNextClassCategory()) {
1428 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001429 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001430 break;
1431 }
1432 }
1433 }
1434 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001435 if (isInterfaceDeclKind) {
1436 // Reject invalid vardecls.
1437 for (unsigned i = 0; i != tuvNum; i++) {
1438 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1439 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1440 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001441 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001442 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001443 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001444 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001445 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001446}
1447
1448
1449/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1450/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001451static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001452CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1453 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1454 if (PQTVal & ObjCDeclSpec::DQ_In)
1455 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1456 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1457 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1458 if (PQTVal & ObjCDeclSpec::DQ_Out)
1459 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1460 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1461 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1462 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1463 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1464 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1465 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001466
1467 return ret;
1468}
1469
Chris Lattner83f095c2009-03-28 19:18:32 +00001470Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001471 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001472 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001473 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001474 Selector Sel,
1475 // optional arguments. The number of types/arguments is obtained
1476 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001477 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001478 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001479 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1480 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001481 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001482
1483 // Make sure we can establish a context for the method.
1484 if (!ClassDecl) {
1485 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9a28e842010-03-01 23:15:13 +00001486 getLabelMap().clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001487 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001488 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001489 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001490
Douglas Gregor12852d92010-03-08 14:59:44 +00001491 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00001492 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00001493 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001494
Steve Naroff32606412009-02-20 22:59:16 +00001495 // Methods cannot return interface types. All ObjC objects are
1496 // passed by reference.
1497 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001498 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1499 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001500 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001501 }
1502 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001503 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001504
1505 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001506 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00001507 ResultTInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001508 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001509 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001510 false,
Mike Stump11289f42009-09-09 15:08:12 +00001511 MethodDeclKind == tok::objc_optional ?
1512 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001513 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001514
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001515 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001516
Chris Lattner23b0faf2009-04-11 19:42:43 +00001517 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001518 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001519 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001521 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001522 ArgType = Context.getObjCIdType();
1523 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001524 } else {
John McCall856bbea2009-10-23 21:48:59 +00001525 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001526 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001527 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001528 }
Mike Stump11289f42009-09-09 15:08:12 +00001529
John McCall856bbea2009-10-23 21:48:59 +00001530 ParmVarDecl* Param
1531 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1532 ArgInfo[i].Name, ArgType, DI,
1533 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001534
Chris Lattner9713a1c2009-04-11 19:34:56 +00001535 if (ArgType->isObjCInterfaceType()) {
1536 Diag(ArgInfo[i].NameLoc,
1537 diag::err_object_cannot_be_passed_returned_by_value)
1538 << 1 << ArgType;
1539 Param->setInvalidDecl();
1540 }
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001542 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001543 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001544
Chris Lattner9713a1c2009-04-11 19:34:56 +00001545 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001546 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001547
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001548 Params.push_back(Param);
1549 }
1550
Jay Foad7d0479f2009-05-21 09:52:38 +00001551 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001552 ObjCMethod->setObjCDeclQualifier(
1553 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1554 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001555
1556 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001557 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001558
John McCall28a6aea2009-11-04 02:18:39 +00001559 const ObjCMethodDecl *InterfaceMD = 0;
1560
Mike Stump11289f42009-09-09 15:08:12 +00001561 // For implementations (which can be very "coarse grain"), we add the
1562 // method now. This allows the AST to implement lookup methods that work
1563 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001564 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001565 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001566 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001567 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001568 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1569 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001570 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001571 PrevMethod = ImpDecl->getClassMethod(Sel);
1572 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001573 }
John McCall28a6aea2009-11-04 02:18:39 +00001574 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1575 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001576 if (AttrList)
1577 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001578 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001579 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001580 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001581 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1582 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001583 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001584 PrevMethod = CatImpDecl->getClassMethod(Sel);
1585 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001586 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001587 if (AttrList)
1588 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001589 }
1590 if (PrevMethod) {
1591 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001592 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001593 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001594 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001595 }
John McCall28a6aea2009-11-04 02:18:39 +00001596
1597 // If the interface declared this method, and it was deprecated there,
1598 // mark it deprecated here.
1599 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1600 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1601
Chris Lattner83f095c2009-03-28 19:18:32 +00001602 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001603}
1604
Chris Lattner438e5012008-12-17 07:13:27 +00001605bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001606 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00001607 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Anders Carlssona6b508a2008-11-04 16:57:32 +00001609 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1610 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001611
Anders Carlssona6b508a2008-11-04 16:57:32 +00001612 return true;
1613}
Chris Lattner438e5012008-12-17 07:13:27 +00001614
Chris Lattner438e5012008-12-17 07:13:27 +00001615/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1616/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00001617void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00001618 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00001619 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00001620 // Check that ClassName is a valid class
1621 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1622 if (!Class) {
1623 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1624 return;
1625 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00001626 if (LangOpts.ObjCNonFragileABI) {
1627 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1628 return;
1629 }
Mike Stump11289f42009-09-09 15:08:12 +00001630
Chris Lattner438e5012008-12-17 07:13:27 +00001631 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001632 llvm::SmallVector<FieldDecl*, 32> RecFields;
1633 Context.CollectObjCIvars(Class, RecFields);
1634 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1635 for (unsigned i = 0; i < RecFields.size(); i++) {
1636 FieldDecl* ID = RecFields[i];
1637 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1638 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1639 ID->getIdentifier(), ID->getType(),
1640 ID->getBitWidth());
1641 Decls.push_back(Sema::DeclPtrTy::make(FD));
1642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Chris Lattner438e5012008-12-17 07:13:27 +00001644 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00001645 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00001646 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001647 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00001648 if (getLangOptions().CPlusPlus)
1649 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001650 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001651 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00001652 }
1653}
1654