blob: ba6e7713f229244e76135c94911622cfd014a936 [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
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000014#include "clang/Sema/Sema.h"
15#include "clang/Sema/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"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/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.
John McCall48871652010-08-21 09:40:31 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCall48871652010-08-21 09:40:31 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
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())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000035 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroff1d2538c2007-12-18 01:30:32 +000036 else
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000037 AddFactoryMethodToGlobalPool(MDecl, true);
38
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
John McCall48871652010-08-21 09:40:31 +000059Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +000063 Decl * const *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.
Douglas Gregorb2ccf012010-04-15 22:33:43 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +000070 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +000071
Ted Kremenek1b0ea822008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Douglas Gregor1c283312010-08-11 12:19:30 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
78 if (IDecl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000079 // Class already seen. Is it a forward declaration?
Douglas Gregor1c283312010-08-11 12:19:30 +000080 if (!IDecl->isForwardDecl()) {
81 IDecl->setInvalidDecl();
82 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
83 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnere6447ef2008-11-23 22:46:27 +000084
Douglas Gregor1c283312010-08-11 12:19:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
John McCall48871652010-08-21 09:40:31 +000087 return IDecl;
Douglas Gregor1c283312010-08-11 12:19:30 +000088 } else {
89 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
91 IDecl->setClassLoc(ClassLoc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +000092 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redld44cd6a2010-08-18 23:57:06 +000093 // dependent AST file.
Sebastian Redle7c1fe62010-08-13 00:28:03 +000094 IDecl->setChangedSinceDeserialization(true);
Douglas Gregor1c283312010-08-11 12:19:30 +000095
96 // Since this ObjCInterfaceDecl was created by a forward declaration,
97 // we now add it to the DeclContext since it wasn't added before
98 // (see ActOnForwardClassDeclaration).
99 IDecl->setLexicalDeclContext(CurContext);
100 CurContext->addDecl(IDecl);
101
102 if (AttrList)
103 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000104 }
Douglas Gregor1c283312010-08-11 12:19:30 +0000105 } else {
106 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
107 ClassName, ClassLoc);
108 if (AttrList)
109 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
110
111 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000112 }
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattnerda463fe2007-12-12 07:09:47 +0000114 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000115 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000116 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
117 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);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000122 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000123 (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.
Douglas Gregor1c283312010-08-11 12:19:30 +0000146 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000147 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000148 if (T->isObjCObjectType()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000149 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
150 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000151 }
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
Sebastian Redle7c1fe62010-08-13 00:28:03 +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);
John McCall48871652010-08-21 09:40:31 +0000190 return 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.
John McCall48871652010-08-21 09:40:31 +0000195Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
196 IdentifierInfo *AliasName,
197 SourceLocation AliasLocation,
198 IdentifierInfo *ClassName,
199 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000200 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000201 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000202 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000203 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000204 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000205 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000206 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000207 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000208 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000209 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000210 }
211 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000212 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000213 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000214 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
215 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000216 if (T->isObjCObjectType()) {
217 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000218 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000219 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000220 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000221 }
222 }
223 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000224 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
225 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000226 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000227 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000228 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000229 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattner219b3e92008-03-16 21:17:37 +0000232 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000233 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000234 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000235
Anders Carlssona6b508a2008-11-04 16:57:32 +0000236 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000237 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000238
John McCall48871652010-08-21 09:40:31 +0000239 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000240}
241
Steve Naroff41d09ad2009-03-05 15:22:01 +0000242void Sema::CheckForwardProtocolDeclarationForCircularDependency(
243 IdentifierInfo *PName,
244 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000245 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000246 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
247 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000248
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000249 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
250 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000251 if (PDecl->getIdentifier() == PName) {
252 Diag(Ploc, diag::err_protocol_has_circular_dependency);
253 Diag(PrevLoc, diag::note_previous_definition);
254 }
Mike Stump11289f42009-09-09 15:08:12 +0000255 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000256 PDecl->getLocation(), PDecl->getReferencedProtocols());
257 }
258 }
259}
260
John McCall48871652010-08-21 09:40:31 +0000261Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000262Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
263 IdentifierInfo *ProtocolName,
264 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000265 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000266 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000267 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000268 SourceLocation EndProtoLoc,
269 AttributeList *AttrList) {
270 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000271 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000272 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000273 if (PDecl) {
274 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000275 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000276 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000277 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000278 // Just return the protocol we already had.
279 // FIXME: don't leak the objects passed in!
John McCall48871652010-08-21 09:40:31 +0000280 return PDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000281 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000282 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000283 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000284 CheckForwardProtocolDeclarationForCircularDependency(
285 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump11289f42009-09-09 15:08:12 +0000286
Steve Naroffeb03dac2008-08-13 16:39:22 +0000287 // Make sure the cached decl gets a valid start location.
288 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000289 PDecl->setForwardDecl(false);
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000290 CurContext->addDecl(PDecl);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000291 // Repeat in dependent AST files.
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000292 PDecl->setChangedSinceDeserialization(true);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000293 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000294 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000295 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000296 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000297 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000298 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000299 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000300 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000301 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000302 /// Check then save referenced protocols.
Douglas Gregor002b6712010-01-16 15:02:53 +0000303 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
304 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000305 PDecl->setLocEnd(EndProtoLoc);
306 }
Mike Stump11289f42009-09-09 15:08:12 +0000307
308 CheckObjCDeclScope(PDecl);
John McCall48871652010-08-21 09:40:31 +0000309 return PDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000310}
311
312/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000313/// issues an error if they are not declared. It returns list of
314/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000315void
Chris Lattner3bbae002008-07-26 04:03:38 +0000316Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000317 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000318 unsigned NumProtocols,
John McCall48871652010-08-21 09:40:31 +0000319 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000320 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000321 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
322 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000323 if (!PDecl) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000324 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
325 LookupObjCProtocolName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000326 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000327 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
328 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
329 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor6da83622010-01-07 00:17:44 +0000330 Diag(PDecl->getLocation(), diag::note_previous_decl)
331 << PDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000332 }
333 }
334
335 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000336 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000337 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000338 continue;
339 }
Mike Stump11289f42009-09-09 15:08:12 +0000340
Douglas Gregor171c45a2009-02-18 21:56:37 +0000341 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000342
343 // If this is a forward declaration and we are supposed to warn in this
344 // case, do it.
345 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner3b054132008-11-19 05:08:23 +0000346 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000347 << ProtocolId[i].first;
John McCall48871652010-08-21 09:40:31 +0000348 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000349 }
350}
351
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000352/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000353/// a class method in its extension.
354///
Mike Stump11289f42009-09-09 15:08:12 +0000355void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000356 ObjCInterfaceDecl *ID) {
357 if (!ID)
358 return; // Possibly due to previous error
359
360 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000361 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
362 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000363 ObjCMethodDecl *MD = *i;
364 MethodMap[MD->getSelector()] = MD;
365 }
366
367 if (MethodMap.empty())
368 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000369 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
370 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000371 ObjCMethodDecl *Method = *i;
372 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
373 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
374 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
375 << Method->getDeclName();
376 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
377 }
378 }
379}
380
Chris Lattnerdac168d2009-04-12 08:43:13 +0000381/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCall48871652010-08-21 09:40:31 +0000382Decl *
Chris Lattnerda463fe2007-12-12 07:09:47 +0000383Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000384 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000385 unsigned NumElts,
386 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000387 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000388 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnerda463fe2007-12-12 07:09:47 +0000390 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000391 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000392 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000393 bool isNew = false;
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000394 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000395 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000396 IdentList[i].second, Ident);
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000397 PushOnScopeChains(PDecl, TUScope, false);
398 isNew = true;
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000399 }
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000400 if (attrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +0000401 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000402 if (!isNew)
403 PDecl->setChangedSinceDeserialization(true);
404 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000405 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000406 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000407 }
Mike Stump11289f42009-09-09 15:08:12 +0000408
409 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000410 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000411 Protocols.data(), Protocols.size(),
412 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000413 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000414 CheckObjCDeclScope(PDecl);
John McCall48871652010-08-21 09:40:31 +0000415 return PDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000416}
417
John McCall48871652010-08-21 09:40:31 +0000418Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000419ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
420 IdentifierInfo *ClassName, SourceLocation ClassLoc,
421 IdentifierInfo *CategoryName,
422 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000423 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000424 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000425 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000426 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000427 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000428 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000429
430 /// Check that class of this category is already completely declared.
431 if (!IDecl || IDecl->isForwardDecl()) {
432 // Create an invalid ObjCCategoryDecl to serve as context for
433 // the enclosing method declarations. We mark the decl invalid
434 // to make it clear that this isn't a valid AST.
435 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
436 ClassLoc, CategoryLoc, CategoryName);
437 CDecl->setInvalidDecl();
438 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall48871652010-08-21 09:40:31 +0000439 return CDecl;
Ted Kremenek514ff702010-02-23 19:39:46 +0000440 }
441
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000442 if (!CategoryName && IDecl->getImplementation()) {
443 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
444 Diag(IDecl->getImplementation()->getLocation(),
445 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000446 }
447
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000448 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
449 ClassLoc, CategoryLoc, CategoryName);
450 // FIXME: PushOnScopeChains?
451 CurContext->addDecl(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000452
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000453 CDecl->setClassInterface(IDecl);
454 // Insert class extension to the list of class's categories.
455 if (!CategoryName)
456 CDecl->insertNextClassCategory();
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerced903b2009-02-16 21:30:01 +0000458 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000459 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000460
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000461 if (CategoryName) {
462 /// Check for duplicate interface declaration for this category
463 ObjCCategoryDecl *CDeclChain;
464 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
465 CDeclChain = CDeclChain->getNextClassCategory()) {
466 if (CDeclChain->getIdentifier() == CategoryName) {
467 // Class extensions can be declared multiple times.
468 Diag(CategoryLoc, diag::warn_dup_category_def)
469 << ClassName << CategoryName;
470 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
471 break;
472 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000473 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000474 if (!CDeclChain)
475 CDecl->insertNextClassCategory();
Chris Lattner9018ca82009-02-16 21:26:43 +0000476 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000477
Chris Lattnerda463fe2007-12-12 07:09:47 +0000478 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000479 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000480 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000481 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000482 if (CDecl->IsClassExtension())
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000483 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000484 NumProtoRefs, ProtoLocs,
485 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Anders Carlssona6b508a2008-11-04 16:57:32 +0000488 CheckObjCDeclScope(CDecl);
John McCall48871652010-08-21 09:40:31 +0000489 return CDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000490}
491
492/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000493/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000494/// object.
John McCall48871652010-08-21 09:40:31 +0000495Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000496 SourceLocation AtCatImplLoc,
497 IdentifierInfo *ClassName, SourceLocation ClassLoc,
498 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000499 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000500 ObjCCategoryDecl *CatIDecl = 0;
501 if (IDecl) {
502 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
503 if (!CatIDecl) {
504 // Category @implementation with no corresponding @interface.
505 // Create and install one.
506 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000507 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000508 CatName);
509 CatIDecl->setClassInterface(IDecl);
510 CatIDecl->insertNextClassCategory();
511 }
512 }
513
Mike Stump11289f42009-09-09 15:08:12 +0000514 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000515 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
516 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000517 /// Check that class of this category is already completely declared.
518 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000519 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000520
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000521 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000522 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000523
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000524 /// Check that CatName, category name, is not used in another implementation.
525 if (CatIDecl) {
526 if (CatIDecl->getImplementation()) {
527 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
528 << CatName;
529 Diag(CatIDecl->getImplementation()->getLocation(),
530 diag::note_previous_definition);
531 } else
532 CatIDecl->setImplementation(CDecl);
533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Anders Carlssona6b508a2008-11-04 16:57:32 +0000535 CheckObjCDeclScope(CDecl);
John McCall48871652010-08-21 09:40:31 +0000536 return CDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000537}
538
John McCall48871652010-08-21 09:40:31 +0000539Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000540 SourceLocation AtClassImplLoc,
541 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000542 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000543 SourceLocation SuperClassLoc) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000544 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000545 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000546 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000547 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
548 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000549 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000550 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000551 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +0000552 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
553 // If this is a forward declaration of an interface, warn.
554 if (IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000555 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregor1c283312010-08-11 12:19:30 +0000556 IDecl = 0;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000557 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000558 } else {
559 // We did not find anything with the name ClassName; try to correct for
560 // typos in the class name.
561 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000562 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor40f7a002010-01-04 17:27:12 +0000563 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000564 // Suggest the (potentially) correct interface name. However, put the
565 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregor1c283312010-08-11 12:19:30 +0000566 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000567 // provide a code-modification hint or use the typo name for recovery,
568 // because this is just a warning. The program may actually be correct.
569 Diag(ClassLoc, diag::warn_undef_interface_suggest)
570 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000571 Diag(IDecl->getLocation(), diag::note_previous_decl)
572 << R.getLookupName()
Douglas Gregora771f462010-03-31 17:46:05 +0000573 << FixItHint::CreateReplacement(ClassLoc,
574 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000575 IDecl = 0;
576 } else {
577 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
578 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000579 }
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattnerda463fe2007-12-12 07:09:47 +0000581 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000582 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000583 if (SuperClassname) {
584 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000585 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
586 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000587 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000588 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
589 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000590 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000591 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000592 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000593 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000594 Diag(SuperClassLoc, diag::err_undef_superclass)
595 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000596 else if (IDecl && IDecl->getSuperClass() != SDecl) {
597 // This implementation and its interface do not have the same
598 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000599 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000600 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000601 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000602 }
603 }
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattnerda463fe2007-12-12 07:09:47 +0000606 if (!IDecl) {
607 // Legacy case of @implementation with no corresponding @interface.
608 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000609
Mike Stump87c57ac2009-05-16 07:39:55 +0000610 // FIXME: Do we support attributes on the @implementation? If so we should
611 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000612 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor1c283312010-08-11 12:19:30 +0000613 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000614 IDecl->setSuperClass(SDecl);
615 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000616
617 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +0000618 } else {
619 // Mark the interface as being completed, even if it was just as
620 // @class ....;
621 // declaration; the user cannot reopen it.
622 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
625 ObjCImplementationDecl* IMPDecl =
626 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000627 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Anders Carlssona6b508a2008-11-04 16:57:32 +0000629 if (CheckObjCDeclScope(IMPDecl))
John McCall48871652010-08-21 09:40:31 +0000630 return IMPDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattnerda463fe2007-12-12 07:09:47 +0000632 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +0000633 if (IDecl->getImplementation()) {
634 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000635 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000636 Diag(IDecl->getImplementation()->getLocation(),
637 diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +0000638 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000639 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000640 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000641 }
John McCall48871652010-08-21 09:40:31 +0000642 return IMPDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000643}
644
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000645void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
646 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000647 SourceLocation RBrace) {
648 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000649 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000650 if (!IDecl)
651 return;
652 /// Check case of non-existing @interface decl.
653 /// (legacy objective-c @implementation decl without an @interface decl).
654 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000655 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000656 IDecl->setLocEnd(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000657 // Add ivar's to class's DeclContext.
658 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +0000659 ivars[i]->setLexicalDeclContext(ImpDecl);
660 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000661 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000662 }
663
Chris Lattnerda463fe2007-12-12 07:09:47 +0000664 return;
665 }
666 // If implementation has empty ivar list, just return.
667 if (numIvars == 0)
668 return;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattnerda463fe2007-12-12 07:09:47 +0000670 assert(ivars && "missing @implementation ivars");
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000671 if (LangOpts.ObjCNonFragileABI2) {
672 if (ImpDecl->getSuperClass())
673 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
674 for (unsigned i = 0; i < numIvars; i++) {
675 ObjCIvarDecl* ImplIvar = ivars[i];
676 if (const ObjCIvarDecl *ClsIvar =
677 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
678 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
679 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
680 continue;
681 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000682 // Instance ivar to Implementation's DeclContext.
683 ImplIvar->setLexicalDeclContext(ImpDecl);
684 IDecl->makeDeclVisibleInContext(ImplIvar, false);
685 ImpDecl->addDecl(ImplIvar);
686 }
687 return;
688 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000689 // Check interface's Ivar list against those in the implementation.
690 // names and types must match.
691 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000692 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000693 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000694 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
695 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000696 ObjCIvarDecl* ImplIvar = ivars[j++];
697 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000698 assert (ImplIvar && "missing implementation ivar");
699 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000700
Steve Naroff157599f2009-03-03 14:49:36 +0000701 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000702 if (Context.getCanonicalType(ImplIvar->getType()) !=
703 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000704 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000705 << ImplIvar->getIdentifier()
706 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000707 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000708 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
709 Expr *ImplBitWidth = ImplIvar->getBitWidth();
710 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000711 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
712 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000713 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
714 << ImplIvar->getIdentifier();
715 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717 }
Steve Naroff157599f2009-03-03 14:49:36 +0000718 // Make sure the names are identical.
719 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000720 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000721 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000722 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000723 }
724 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner0f29d982007-12-12 18:11:49 +0000727 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000728 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000729 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000730 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000731}
732
Steve Naroff15833ed2008-02-10 21:38:56 +0000733void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000734 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff15833ed2008-02-10 21:38:56 +0000735 if (!IncompleteImpl) {
736 Diag(ImpLoc, diag::warn_incomplete_impl);
737 IncompleteImpl = true;
738 }
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000739 Diag(method->getLocation(), DiagID)
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000740 << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000741}
742
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000743void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
744 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000745 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000746 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000747 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
748 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000749 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000750 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
751 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000752 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Chris Lattner67f35b02009-04-11 19:58:42 +0000755 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
756 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
757 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000758 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
759 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
760 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
761 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000762 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000763
764 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000765 << ImpMethodDecl->getDeclName() << (*IF)->getType()
766 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000767 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000768 }
Fariborz Jahanian5981b042010-05-21 23:28:58 +0000769 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
770 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
771 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
772 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000773}
774
Mike Stump87c57ac2009-05-16 07:39:55 +0000775/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
776/// improve the efficiency of selector lookups and type checking by associating
777/// with each protocol / interface / category the flattened instance tables. If
778/// we used an immutable set to keep the table then it wouldn't add significant
779/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000780
Steve Naroffa36992242008-02-08 22:06:17 +0000781/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000782/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000783void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
784 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000785 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000786 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000787 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +0000788 ObjCContainerDecl *CDecl) {
789 ObjCInterfaceDecl *IDecl;
790 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
791 IDecl = C->getClassInterface();
792 else
793 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
794 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
795
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000796 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000797 ObjCInterfaceDecl *NSIDecl = 0;
798 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000799 // check to see if class implements forwardInvocation method and objects
800 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000801 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000802 // Under such conditions, which means that every method possible is
803 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000804 // found" warnings.
805 // FIXME: Use a general GetUnarySelector method for this.
806 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
807 Selector fISelector = Context.Selectors.getSelector(1, &II);
808 if (InsMap.count(fISelector))
809 // Is IDecl derived from 'NSProxy'? If so, no instance methods
810 // need be implemented in the implementation.
811 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000814 // If a method lookup fails locally we still need to look and see if
815 // the method was implemented by a base class or an inherited
816 // protocol. This lookup is slow, but occurs rarely in correct code
817 // and otherwise would terminate in a warning.
818
Chris Lattnerda463fe2007-12-12 07:09:47 +0000819 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000820 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000821 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000822 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000823 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000824 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000825 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000826 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000827 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000828 // Ugly, but necessary. Method declared in protcol might have
829 // have been synthesized due to a property declared in the class which
830 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000831 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000832 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000833 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000834 unsigned DIAG = diag::warn_unimplemented_protocol_method;
835 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
836 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
837 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
838 << PDecl->getDeclName();
839 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000840 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000841 }
842 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000843 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000844 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000845 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000846 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000847 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000848 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
849 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000850 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000851 unsigned DIAG = diag::warn_unimplemented_protocol_method;
852 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
853 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
854 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
855 PDecl->getDeclName();
856 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000857 }
Steve Naroff3ce37a62007-12-14 23:37:57 +0000858 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000859 // Check on this protocols's referenced protocols, recursively.
860 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
861 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000862 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000863}
864
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000865/// MatchAllMethodDeclarations - Check methods declaraed in interface or
866/// or protocol against those declared in their implementations.
867///
868void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
869 const llvm::DenseSet<Selector> &ClsMap,
870 llvm::DenseSet<Selector> &InsMapSeen,
871 llvm::DenseSet<Selector> &ClsMapSeen,
872 ObjCImplDecl* IMPDecl,
873 ObjCContainerDecl* CDecl,
874 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000875 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000876 // Check and see if instance methods in class interface have been
877 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000878 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
879 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000880 if (InsMapSeen.count((*I)->getSelector()))
881 continue;
882 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000883 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000884 !InsMap.count((*I)->getSelector())) {
885 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000886 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
887 diag::note_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000888 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000889 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000890 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000891 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000892 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000893 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000894 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000895 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
896 // ImpMethodDecl may be null as in a @dynamic property.
897 if (ImpMethodDecl)
898 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
899 }
900 }
Mike Stump11289f42009-09-09 15:08:12 +0000901
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000902 // Check and see if class methods in class interface have been
903 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000904 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000905 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000906 if (ClsMapSeen.count((*I)->getSelector()))
907 continue;
908 ClsMapSeen.insert((*I)->getSelector());
909 if (!ClsMap.count((*I)->getSelector())) {
910 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000911 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
912 diag::note_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +0000913 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000914 ObjCMethodDecl *ImpMethodDecl =
915 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000916 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000917 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000918 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
919 }
920 }
921 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
922 // Check for any implementation of a methods declared in protocol.
923 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
924 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000925 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
926 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000927 (*PI), IncompleteImpl, false);
928 if (I->getSuperClass())
929 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +0000930 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000931 I->getSuperClass(), IncompleteImpl, false);
932 }
933}
934
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000935void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000936 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000937 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000938 llvm::DenseSet<Selector> InsMap;
939 // Check and see if instance methods in class interface have been
940 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000941 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000942 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000943 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000944
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +0000945 // Check and see if properties declared in the interface have either 1)
946 // an implementation or 2) there is a @synthesize/@dynamic implementation
947 // of the property in the @implementation.
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +0000948 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000949 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian98609b32010-01-20 01:51:55 +0000950
Chris Lattnerda463fe2007-12-12 07:09:47 +0000951 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +0000952 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000953 I = IMPDecl->classmeth_begin(),
954 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000955 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000956
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000957 // Check for type conflict of methods declared in a class/protocol and
958 // its implementation; if any.
959 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +0000960 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
961 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000962 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattnerda463fe2007-12-12 07:09:47 +0000964 // Check the protocol list for unimplemented methods in the @implementation
965 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000966 // Check and see if class methods in class interface have been
967 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000968
Chris Lattner9ef10f42009-03-01 00:56:52 +0000969 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000970 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +0000971 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000972 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000973 InsMap, ClsMap, I);
974 // Check class extensions (unnamed categories)
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000975 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
976 Categories; Categories = Categories->getNextClassExtension())
977 ImplMethodsVsClassMethods(S, IMPDecl,
978 const_cast<ObjCCategoryDecl*>(Categories),
979 IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +0000980 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000981 // For extended class, unimplemented methods in its protocols will
982 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000983 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000984 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
985 E = C->protocol_end(); PI != E; ++PI)
986 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +0000987 InsMap, ClsMap, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000988 // Report unimplemented properties in the category as well.
989 // When reporting on missing setter/getters, do not report when
990 // setter/getter is implemented in category's primary class
991 // implementation.
992 if (ObjCInterfaceDecl *ID = C->getClassInterface())
993 if (ObjCImplDecl *IMP = ID->getImplementation()) {
994 for (ObjCImplementationDecl::instmeth_iterator
995 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
996 InsMap.insert((*I)->getSelector());
997 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000998 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000999 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001000 } else
1001 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001002}
1003
Mike Stump11289f42009-09-09 15:08:12 +00001004/// ActOnForwardClassDeclaration -
John McCall48871652010-08-21 09:40:31 +00001005Decl *
Chris Lattnerda463fe2007-12-12 07:09:47 +00001006Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001007 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001008 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001009 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001010 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattnerda463fe2007-12-12 07:09:47 +00001012 for (unsigned i = 0; i != NumElts; ++i) {
1013 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001014 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001015 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001016 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001017 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001018 // Maybe we will complain about the shadowed template parameter.
1019 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1020 // Just pretend that we didn't see the previous declaration.
1021 PrevDecl = 0;
1022 }
1023
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001024 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001025 // GCC apparently allows the following idiom:
1026 //
1027 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1028 // @class XCElementToggler;
1029 //
Mike Stump11289f42009-09-09 15:08:12 +00001030 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001031 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00001032 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001033 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001034 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00001035 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00001036 // a forward class declaration matching a typedef name of a class refers
1037 // to the underlying class.
John McCall8b07ec22010-05-15 11:32:37 +00001038 if (const ObjCObjectType *OI =
1039 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1040 PrevDecl = OI->getInterface();
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001041 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001042 }
Douglas Gregor1c283312010-08-11 12:19:30 +00001043 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1044 if (!IDecl) { // Not already seen? Make a forward decl.
1045 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1046 IdentList[i], IdentLocs[i], true);
1047
1048 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1049 // the current DeclContext. This prevents clients that walk DeclContext
1050 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1051 // declared later (if at all). We also take care to explicitly make
1052 // sure this declaration is visible for name lookup.
1053 PushOnScopeChains(IDecl, TUScope, false);
1054 CurContext->makeDeclVisibleInContext(IDecl, true);
1055 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001056
1057 Interfaces.push_back(IDecl);
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Ted Kremenek9b124e12009-11-18 00:28:11 +00001060 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001061 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001062 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001063 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001064 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001065 CheckObjCDeclScope(CDecl);
John McCall48871652010-08-21 09:40:31 +00001066 return CDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001067}
1068
1069
1070/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1071/// returns true, or false, accordingly.
1072/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001073bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001074 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001075 bool matchBasedOnSizeAndAlignment,
1076 bool matchBasedOnStrictEqulity) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001077 QualType T1 = Context.getCanonicalType(Method->getResultType());
1078 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001079
Steve Naroffa0ed1652008-10-21 10:37:50 +00001080 if (T1 != T2) {
1081 // The result types are different.
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001082 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001083 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001084 // Incomplete types don't have a size and alignment.
1085 if (T1->isIncompleteType() || T2->isIncompleteType())
1086 return false;
1087 // Check is based on size and alignment.
1088 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1089 return false;
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091
Chris Lattnera4997152009-02-20 18:43:26 +00001092 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1093 E = Method->param_end();
1094 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattnera4997152009-02-20 18:43:26 +00001096 for (; ParamI != E; ++ParamI, ++PrevI) {
1097 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1098 T1 = Context.getCanonicalType((*ParamI)->getType());
1099 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001100 if (T1 != T2) {
1101 // The result types are different.
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001102 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Naroffa0ed1652008-10-21 10:37:50 +00001103 return false;
1104 // Incomplete types don't have a size and alignment.
1105 if (T1->isIncompleteType() || T2->isIncompleteType())
1106 return false;
1107 // Check is based on size and alignment.
1108 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1109 return false;
1110 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001111 }
1112 return true;
1113}
1114
Sebastian Redl75d8a322010-08-02 23:18:59 +00001115/// \brief Read the contents of the method pool for a given selector from
1116/// external storage.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001117///
Sebastian Redl75d8a322010-08-02 23:18:59 +00001118/// This routine should only be called once, when the method pool has no entry
1119/// for this selector.
1120Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001121 assert(ExternalSource && "We need an external AST source");
Sebastian Redl75d8a322010-08-02 23:18:59 +00001122 assert(MethodPool.find(Sel) == MethodPool.end() &&
1123 "Selector data already loaded into the method pool");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001124
1125 // Read the method list from the external source.
Sebastian Redl75d8a322010-08-02 23:18:59 +00001126 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001127
Sebastian Redl75d8a322010-08-02 23:18:59 +00001128 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001129}
1130
Sebastian Redl75d8a322010-08-02 23:18:59 +00001131void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1132 bool instance) {
1133 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1134 if (Pos == MethodPool.end()) {
1135 if (ExternalSource)
1136 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001137 else
Sebastian Redl75d8a322010-08-02 23:18:59 +00001138 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1139 GlobalMethods())).first;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001140 }
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001141 Method->setDefined(impl);
Sebastian Redl75d8a322010-08-02 23:18:59 +00001142 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001143 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001144 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001145 Entry.Method = Method;
1146 Entry.Next = 0;
1147 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149
Chris Lattner7b26b292009-03-04 05:16:45 +00001150 // We've seen a method with this name, see if we have already seen this type
1151 // signature.
1152 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001153 if (MatchTwoMethodDeclarations(Method, List->Method)) {
1154 List->Method->setDefined(impl);
Chris Lattner7b26b292009-03-04 05:16:45 +00001155 return;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Chris Lattner7b26b292009-03-04 05:16:45 +00001158 // We have a new signature for an existing method - add it.
1159 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001160 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1161 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001162}
1163
Sebastian Redl75d8a322010-08-02 23:18:59 +00001164ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001165 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00001166 bool warn, bool instance) {
1167 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1168 if (Pos == MethodPool.end()) {
1169 if (ExternalSource)
1170 Pos = ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001171 else
1172 return 0;
1173 }
1174
Sebastian Redl75d8a322010-08-02 23:18:59 +00001175 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump11289f42009-09-09 15:08:12 +00001176
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001177 bool strictSelectorMatch = receiverIdOrClass && warn &&
1178 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) !=
1179 Diagnostic::Ignored);
Sebastian Redl75d8a322010-08-02 23:18:59 +00001180 if (warn && MethList.Method && MethList.Next) {
1181 bool issueWarning = false;
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001182 if (strictSelectorMatch)
1183 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1184 // This checks if the methods differ in type mismatch.
1185 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1186 issueWarning = true;
1187 }
1188
1189 if (!issueWarning)
1190 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1191 // This checks if the methods differ by size & alignment.
1192 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1193 issueWarning = true;
1194 }
1195
Sebastian Redl75d8a322010-08-02 23:18:59 +00001196 if (issueWarning) {
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001197 if (strictSelectorMatch)
1198 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1199 else
1200 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redl75d8a322010-08-02 23:18:59 +00001201 Diag(MethList.Method->getLocStart(), diag::note_using)
1202 << MethList.Method->getSourceRange();
1203 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1204 Diag(Next->Method->getLocStart(), diag::note_also_found)
1205 << Next->Method->getSourceRange();
1206 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001207 }
1208 return MethList.Method;
1209}
1210
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001211ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00001212 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1213 if (Pos == MethodPool.end())
1214 return 0;
1215
1216 GlobalMethods &Methods = Pos->second;
1217
1218 if (Methods.first.Method && Methods.first.Method->isDefined())
1219 return Methods.first.Method;
1220 if (Methods.second.Method && Methods.second.Method->isDefined())
1221 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001222 return 0;
1223}
1224
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001225/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1226/// identical selector names in current and its super classes and issues
1227/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001228void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1229 ObjCMethodDecl *Method,
1230 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001231 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1232 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001233
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001234 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001235 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001236 SD->lookupMethod(Method->getSelector(), IsInstance);
1237 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001238 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001239 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001240 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001241 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1242 E = Method->param_end();
1243 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1244 for (; ParamI != E; ++ParamI, ++PrevI) {
1245 // Number of parameters are the same and is guaranteed by selector match.
1246 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1247 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1248 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1249 // If type of arguement of method in this class does not match its
1250 // respective argument type in the super class method, issue warning;
1251 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001252 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001253 << T1 << T2;
1254 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1255 return;
1256 }
1257 }
1258 ID = SD;
1259 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001260}
1261
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001262/// DiagnoseDuplicateIvars -
1263/// Check for duplicate ivars in the entire class at the start of
1264/// @implementation. This becomes necesssary because class extension can
1265/// add ivars to a class in random order which will not be known until
1266/// class's @implementation is seen.
1267void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1268 ObjCInterfaceDecl *SID) {
1269 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1270 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1271 ObjCIvarDecl* Ivar = (*IVI);
1272 if (Ivar->isInvalidDecl())
1273 continue;
1274 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1275 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1276 if (prevIvar) {
1277 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1278 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1279 Ivar->setInvalidDecl();
1280 }
1281 }
1282 }
1283}
1284
Steve Naroff1d2538c2007-12-18 01:30:32 +00001285// Note: For class/category implemenations, allMethods/allProperties is
1286// always null.
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001287void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCall48871652010-08-21 09:40:31 +00001288 Decl *ClassDecl,
1289 Decl **allMethods, unsigned allNum,
1290 Decl **allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001291 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroff1d2538c2007-12-18 01:30:32 +00001292 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1293 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001294 // should be true.
1295 if (!ClassDecl)
1296 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001297
Mike Stump11289f42009-09-09 15:08:12 +00001298 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001299 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1300 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001301 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001302
Ted Kremenekc7c64312010-01-07 01:20:12 +00001303 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1304 // FIXME: This is wrong. We shouldn't be pretending that there is
1305 // an '@end' in the declaration.
1306 SourceLocation L = ClassDecl->getLocation();
1307 AtEnd.setBegin(L);
1308 AtEnd.setEnd(L);
1309 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001310 }
1311
Steve Naroff35c62ae2009-01-08 17:28:14 +00001312 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001313
1314 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1315 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1316 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1317
Chris Lattnerda463fe2007-12-12 07:09:47 +00001318 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001319 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00001320 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001321
1322 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001323 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001324 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001325 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001326 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001327 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001328 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001329 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001330 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001331 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001332 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001333 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001334 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001335 InsMap[Method->getSelector()] = Method;
1336 /// The following allows us to typecheck messages to "id".
1337 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001338 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001339 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001340 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001341 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001342 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001343 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001344 const ObjCMethodDecl *&PrevMethod = ClsMap[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 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001355 /// The following allows us to typecheck messages to "Class".
1356 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001357 // verify that the class 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, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001360 }
1361 }
1362 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001363 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001364 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001365 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001366 ComparePropertiesInBaseAndSuper(I);
John McCall48871652010-08-21 09:40:31 +00001367 CompareProperties(I, I);
Steve Naroffb3a87982009-01-09 15:36:25 +00001368 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001369 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001370 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001371 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001372
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001373 // Compare protocol properties with those in category
John McCall48871652010-08-21 09:40:31 +00001374 CompareProperties(C, C);
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001375 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001376 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001377 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001378 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001379 if (CDecl->getIdentifier())
1380 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1381 // user-defined setter/getter. It also synthesizes setter/getter methods
1382 // and adds them to the DeclContext and global method pools.
1383 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1384 E = CDecl->prop_end();
1385 I != E; ++I)
1386 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001387 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001388 }
1389 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001390 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001391 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001392 if (LangOpts.ObjCNonFragileABI2)
1393 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001394 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001395 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001396
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001397 if (LangOpts.ObjCNonFragileABI2)
1398 while (IDecl->getSuperClass()) {
1399 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1400 IDecl = IDecl->getSuperClass();
1401 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001402 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001403 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00001404 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001405 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001406 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattnerda463fe2007-12-12 07:09:47 +00001408 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001409 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001410 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001411 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001412 Categories; Categories = Categories->getNextClassCategory()) {
1413 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001414 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001415 break;
1416 }
1417 }
1418 }
1419 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001420 if (isInterfaceDeclKind) {
1421 // Reject invalid vardecls.
1422 for (unsigned i = 0; i != tuvNum; i++) {
1423 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1424 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1425 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001426 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001427 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001428 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001429 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001430 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001431}
1432
1433
1434/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1435/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001436static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001437CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1438 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1439 if (PQTVal & ObjCDeclSpec::DQ_In)
1440 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1441 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1442 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1443 if (PQTVal & ObjCDeclSpec::DQ_Out)
1444 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1445 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1446 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1447 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1448 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1449 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1450 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001451
1452 return ret;
1453}
1454
Ted Kremenek36712b22010-04-18 04:59:38 +00001455static inline
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001456bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek36712b22010-04-18 04:59:38 +00001457 // The 'ibaction' attribute is allowed on method definitions because of
1458 // how the IBAction macro is used on both method declarations and definitions.
1459 // If the method definitions contains any other attributes, return true.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001460 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1461 if ((*i)->getKind() != attr::IBAction)
1462 return true;
1463 return false;
Ted Kremenek36712b22010-04-18 04:59:38 +00001464}
1465
John McCall48871652010-08-21 09:40:31 +00001466Decl *Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001467 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCall48871652010-08-21 09:40:31 +00001468 tok::TokenKind MethodType, Decl *ClassDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001469 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001470 Selector Sel,
1471 // optional arguments. The number of types/arguments is obtained
1472 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001473 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001474 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00001475 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1476 bool isVariadic) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001477 // Make sure we can establish a context for the method.
1478 if (!ClassDecl) {
1479 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9a28e842010-03-01 23:15:13 +00001480 getLabelMap().clear();
John McCall48871652010-08-21 09:40:31 +00001481 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00001482 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001483 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001484
Douglas Gregor12852d92010-03-08 14:59:44 +00001485 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00001486 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00001487 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001488
Steve Naroff32606412009-02-20 22:59:16 +00001489 // Methods cannot return interface types. All ObjC objects are
1490 // passed by reference.
John McCall8b07ec22010-05-15 11:32:37 +00001491 if (resultDeclType->isObjCObjectType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001492 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1493 << 0 << resultDeclType;
John McCall48871652010-08-21 09:40:31 +00001494 return 0;
Steve Naroff32606412009-02-20 22:59:16 +00001495 }
1496 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001497 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001498
1499 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001500 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00001501 ResultTInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001502 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001503 MethodType == tok::minus, isVariadic,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001504 false, false,
Mike Stump11289f42009-09-09 15:08:12 +00001505 MethodDeclKind == tok::objc_optional ?
1506 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001507 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001508
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001509 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001510
Chris Lattner23b0faf2009-04-11 19:42:43 +00001511 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001512 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001513 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001514
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001515 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001516 ArgType = Context.getObjCIdType();
1517 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001518 } else {
John McCall856bbea2009-10-23 21:48:59 +00001519 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001520 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001521 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001522 }
Mike Stump11289f42009-09-09 15:08:12 +00001523
John McCall856bbea2009-10-23 21:48:59 +00001524 ParmVarDecl* Param
1525 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1526 ArgInfo[i].Name, ArgType, DI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001527 VarDecl::None, VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001528
John McCall8b07ec22010-05-15 11:32:37 +00001529 if (ArgType->isObjCObjectType()) {
Chris Lattner9713a1c2009-04-11 19:34:56 +00001530 Diag(ArgInfo[i].NameLoc,
1531 diag::err_object_cannot_be_passed_returned_by_value)
1532 << 1 << ArgType;
1533 Param->setInvalidDecl();
1534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001536 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001537 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001538
Chris Lattner9713a1c2009-04-11 19:34:56 +00001539 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001540 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001542 Params.push_back(Param);
1543 }
1544
Fariborz Jahanian60462092010-04-08 00:30:06 +00001545 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00001546 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001547 QualType ArgType = Param->getType();
1548 if (ArgType.isNull())
1549 ArgType = Context.getObjCIdType();
1550 else
1551 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1552 ArgType = adjustParameterType(ArgType);
John McCall8b07ec22010-05-15 11:32:37 +00001553 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian60462092010-04-08 00:30:06 +00001554 Diag(Param->getLocation(),
1555 diag::err_object_cannot_be_passed_returned_by_value)
1556 << 1 << ArgType;
1557 Param->setInvalidDecl();
1558 }
1559 Param->setDeclContext(ObjCMethod);
Douglas Gregor114e55d2010-08-07 12:29:18 +00001560 if (Param->getDeclName())
1561 IdResolver.RemoveDecl(Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001562 Params.push_back(Param);
1563 }
1564
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00001565 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1566 Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001567 ObjCMethod->setObjCDeclQualifier(
1568 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1569 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001570
1571 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001572 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001573
John McCall28a6aea2009-11-04 02:18:39 +00001574 const ObjCMethodDecl *InterfaceMD = 0;
1575
Mike Stump11289f42009-09-09 15:08:12 +00001576 // For implementations (which can be very "coarse grain"), we add the
1577 // method now. This allows the AST to implement lookup methods that work
1578 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001579 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001580 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001581 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001582 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001583 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1584 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001585 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001586 PrevMethod = ImpDecl->getClassMethod(Sel);
1587 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001588 }
John McCall28a6aea2009-11-04 02:18:39 +00001589 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1590 MethodType == tok::minus);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001591 if (ObjCMethod->hasAttrs() &&
1592 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001593 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001594 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001595 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001596 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001597 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1598 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001599 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001600 PrevMethod = CatImpDecl->getClassMethod(Sel);
1601 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001602 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001603 if (ObjCMethod->hasAttrs() &&
1604 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001605 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001606 }
1607 if (PrevMethod) {
1608 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001609 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001610 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001611 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001612 }
John McCall28a6aea2009-11-04 02:18:39 +00001613
1614 // If the interface declared this method, and it was deprecated there,
1615 // mark it deprecated here.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001616 if (InterfaceMD)
1617 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>())
1618 ObjCMethod->addAttr(::new (Context) DeprecatedAttr(DA->getLocation(),
1619 Context));
John McCall28a6aea2009-11-04 02:18:39 +00001620
John McCall48871652010-08-21 09:40:31 +00001621 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001622}
1623
Chris Lattner438e5012008-12-17 07:13:27 +00001624bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001625 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00001626 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001627
Anders Carlssona6b508a2008-11-04 16:57:32 +00001628 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1629 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001630
Anders Carlssona6b508a2008-11-04 16:57:32 +00001631 return true;
1632}
Chris Lattner438e5012008-12-17 07:13:27 +00001633
Chris Lattner438e5012008-12-17 07:13:27 +00001634/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1635/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00001636void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00001637 IdentifierInfo *ClassName,
John McCall48871652010-08-21 09:40:31 +00001638 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00001639 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001640 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00001641 if (!Class) {
1642 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1643 return;
1644 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00001645 if (LangOpts.ObjCNonFragileABI) {
1646 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1647 return;
1648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Chris Lattner438e5012008-12-17 07:13:27 +00001650 // Collect the instance variables
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001651 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1652 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001653 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001654 for (unsigned i = 0; i < Ivars.size(); i++) {
1655 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00001656 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001657 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1658 ID->getIdentifier(), ID->getType(),
1659 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00001660 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001661 }
Mike Stump11289f42009-09-09 15:08:12 +00001662
Chris Lattner438e5012008-12-17 07:13:27 +00001663 // Introduce all of these fields into the appropriate scope.
John McCall48871652010-08-21 09:40:31 +00001664 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00001665 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00001666 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattner438e5012008-12-17 07:13:27 +00001667 if (getLangOptions().CPlusPlus)
1668 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00001669 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001670 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00001671 }
1672}
1673
Douglas Gregorf3564192010-04-26 17:32:49 +00001674/// \brief Build a type-check a new Objective-C exception variable declaration.
1675VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1676 QualType T,
1677 IdentifierInfo *Name,
1678 SourceLocation NameLoc,
1679 bool Invalid) {
1680 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1681 // duration shall not be qualified by an address-space qualifier."
1682 // Since all parameters have automatic store duration, they can not have
1683 // an address space.
1684 if (T.getAddressSpace() != 0) {
1685 Diag(NameLoc, diag::err_arg_with_address_space);
1686 Invalid = true;
1687 }
1688
1689 // An @catch parameter must be an unqualified object pointer type;
1690 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1691 if (Invalid) {
1692 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00001693 } else if (T->isDependentType()) {
1694 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00001695 } else if (!T->isObjCObjectPointerType()) {
1696 Invalid = true;
1697 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1698 } else if (T->isObjCQualifiedIdType()) {
1699 Invalid = true;
1700 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1701 }
1702
1703 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor3f324d562010-05-03 18:51:14 +00001704 VarDecl::None, VarDecl::None);
1705 New->setExceptionVariable(true);
1706
Douglas Gregorf3564192010-04-26 17:32:49 +00001707 if (Invalid)
1708 New->setInvalidDecl();
1709 return New;
1710}
1711
John McCall48871652010-08-21 09:40:31 +00001712Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00001713 const DeclSpec &DS = D.getDeclSpec();
1714
1715 // We allow the "register" storage class on exception variables because
1716 // GCC did, but we drop it completely. Any other storage class is an error.
1717 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1718 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1719 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1720 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1721 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1722 << DS.getStorageClassSpec();
1723 }
1724 if (D.getDeclSpec().isThreadSpecified())
1725 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1726 D.getMutableDeclSpec().ClearStorageClassSpecs();
1727
1728 DiagnoseFunctionSpecifiers(D);
1729
1730 // Check that there are no default arguments inside the type of this
1731 // exception object (C++ only).
1732 if (getLangOptions().CPlusPlus)
1733 CheckExtraCXXDefaultArguments(D);
1734
Douglas Gregorf3564192010-04-26 17:32:49 +00001735 TagDecl *OwnedDecl = 0;
John McCall8cb7bdf2010-06-04 23:28:52 +00001736 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1737 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00001738
1739 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1740 // Objective-C++: Types shall not be defined in exception types.
1741 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1742 << Context.getTypeDeclType(OwnedDecl);
1743 }
1744
1745 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1746 D.getIdentifierLoc(),
1747 D.isInvalidType());
1748
1749 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1750 if (D.getCXXScopeSpec().isSet()) {
1751 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1752 << D.getCXXScopeSpec().getRange();
1753 New->setInvalidDecl();
1754 }
1755
1756 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00001757 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00001758 if (D.getIdentifier())
1759 IdResolver.AddDecl(New);
1760
1761 ProcessDeclAttributes(S, New, D);
1762
1763 if (New->hasAttr<BlocksAttr>())
1764 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00001765 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00001766}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001767
1768/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001769/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001770void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001771 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001772 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
1773 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001774 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001775 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001776 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001777 }
1778}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001779
1780void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1781 CXXBaseOrMemberInitializer ** initializers,
1782 unsigned numInitializers) {
1783 if (numInitializers > 0) {
1784 NumIvarInitializers = numInitializers;
1785 CXXBaseOrMemberInitializer **ivarInitializers =
1786 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1787 memcpy(ivarInitializers, initializers,
1788 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1789 IvarInitializers = ivarInitializers;
1790 }
1791}
1792
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001793void Sema::DiagnoseUseOfUnimplementedSelectors() {
1794 if (ReferencedSelectors.empty())
1795 return;
1796 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
1797 ReferencedSelectors.begin(),
1798 E = ReferencedSelectors.end(); S != E; ++S) {
1799 Selector Sel = (*S).first;
1800 if (!LookupImplementedMethodInGlobalPool(Sel))
1801 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
1802 }
1803 return;
1804}