blob: 3aeb50b032d0dc8b9e7d62c8c57a155fcc50f6d6 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
24
Chris Lattner4d391482007-12-12 07:09:47 +000025using namespace clang;
26
Steve Naroffebf64432009-02-28 16:59:13 +000027/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000028/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +000029void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000030 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +000031 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +000032
Steve Naroff394f3f42008-07-25 17:57:26 +000033 // If we don't have a valid method decl, simply return.
34 if (!MDecl)
35 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000036
37 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000038 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000039 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000040 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000041 AddFactoryMethodToGlobalPool(MDecl, true);
42
Chris Lattner4d391482007-12-12 07:09:47 +000043 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000044 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000045 PushFunctionScope();
46
Chris Lattner4d391482007-12-12 07:09:47 +000047 // Create Decl objects for each parameter, entrring them in the scope for
48 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000049
50 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000051 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000052
Daniel Dunbar451318c2008-08-26 06:07:48 +000053 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
54 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000055
Chris Lattner8123a952008-04-10 02:22:51 +000056 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000057 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +000058 E = MDecl->param_end(); PI != E; ++PI) {
59 ParmVarDecl *Param = (*PI);
60 if (!Param->isInvalidDecl() &&
61 RequireCompleteType(Param->getLocation(), Param->getType(),
62 diag::err_typecheck_decl_incomplete_type))
63 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +000064 if ((*PI)->getIdentifier())
65 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +000066 }
Fariborz Jahanianb1224f62011-02-15 00:59:30 +000067 // Warn on implementating deprecated methods under
68 // -Wdeprecated-implementations flag.
69 // FIXME. Refactor using common routine.
70 unsigned DIAG = diag::warn_depercated_def;
71 if (Diags.getDiagnosticLevel(DIAG, MDecl->getLocation())
72 != Diagnostic::Ignored)
73 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
74 if (ObjCMethodDecl *IMD =
75 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
76 if (NamedDecl *ND = dyn_cast<NamedDecl>(IMD))
77 if (ND->getAttr<DeprecatedAttr>()) {
78 Diag(MDecl->getLocation(), DIAG) << 0;
79 Diag(IMD->getLocation(), diag::note_method_declared_at);
80 }
81 }
Chris Lattner4d391482007-12-12 07:09:47 +000082}
83
John McCalld226f652010-08-21 09:40:31 +000084Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000085ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
86 IdentifierInfo *ClassName, SourceLocation ClassLoc,
87 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +000088 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000089 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000090 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000091 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner4d391482007-12-12 07:09:47 +000093 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +000094 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +000095 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +000096
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000098 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000099 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000102 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
103 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000105 if (!IDecl->isForwardDecl()) {
106 IDecl->setInvalidDecl();
107 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
108 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000109
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000110 // Return the previous class interface.
111 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000112 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000113 } else {
114 IDecl->setLocation(AtInterfaceLoc);
115 IDecl->setForwardDecl(false);
116 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000117 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000118 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000119 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000120
121 // Since this ObjCInterfaceDecl was created by a forward declaration,
122 // we now add it to the DeclContext since it wasn't added before
123 // (see ActOnForwardClassDeclaration).
124 IDecl->setLexicalDeclContext(CurContext);
125 CurContext->addDecl(IDecl);
126
127 if (AttrList)
128 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000129 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000130 } else {
131 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
132 ClassName, ClassLoc);
133 if (AttrList)
134 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
135
136 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner4d391482007-12-12 07:09:47 +0000139 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000140 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000141 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
142 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000143
144 if (!PrevDecl) {
145 // Try to correct for a typo in the superclass name.
146 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000147 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000148 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
149 Diag(SuperLoc, diag::err_undef_superclass_suggest)
150 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000151 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
152 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000153 }
154 }
155
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000156 if (PrevDecl == IDecl) {
157 Diag(SuperLoc, diag::err_recursive_superclass)
158 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
159 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000160 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000161 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000162 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000163
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000164 // Diagnose classes that inherit from deprecated classes.
165 if (SuperClassDecl)
166 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000168 if (PrevDecl && SuperClassDecl == 0) {
169 // The previous declaration was not a class decl. Check if we have a
170 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000171 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000172 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000173 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000174 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
175 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000176 }
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000179 // This handles the following case:
180 //
181 // typedef int SuperClass;
182 // @interface MyClass : SuperClass {} @end
183 //
184 if (!SuperClassDecl) {
185 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
186 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000187 }
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000190 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
191 if (!SuperClassDecl)
192 Diag(SuperLoc, diag::err_undef_superclass)
193 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
194 else if (SuperClassDecl->isForwardDecl())
195 Diag(SuperLoc, diag::err_undef_superclass)
196 << SuperClassDecl->getDeclName() << ClassName
197 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000198 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000199 IDecl->setSuperClass(SuperClassDecl);
200 IDecl->setSuperClassLoc(SuperLoc);
201 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000202 }
Chris Lattner4d391482007-12-12 07:09:47 +0000203 } else { // we have a root class.
204 IDecl->setLocEnd(ClassLoc);
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Sebastian Redl0b17c612010-08-13 00:28:03 +0000207 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000208 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000209 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000210 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000211 IDecl->setLocEnd(EndProtoLoc);
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anders Carlsson15281452008-11-04 16:57:32 +0000214 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000215 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000216}
217
218/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000219/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000220Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
221 IdentifierInfo *AliasName,
222 SourceLocation AliasLocation,
223 IdentifierInfo *ClassName,
224 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000225 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000226 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000227 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000228 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000229 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000230 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000231 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000232 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000233 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000234 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000235 }
236 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000237 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000238 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000239 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
240 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000241 if (T->isObjCObjectType()) {
242 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000243 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000244 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000245 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000246 }
247 }
248 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000249 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
250 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000251 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000252 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000253 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000254 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000257 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000258 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000259 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson15281452008-11-04 16:57:32 +0000261 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000262 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000263
John McCalld226f652010-08-21 09:40:31 +0000264 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000265}
266
Steve Naroff61d68522009-03-05 15:22:01 +0000267void Sema::CheckForwardProtocolDeclarationForCircularDependency(
268 IdentifierInfo *PName,
269 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000270 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000271 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
272 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Douglas Gregorc83c6872010-04-15 22:33:43 +0000274 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
275 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000276 if (PDecl->getIdentifier() == PName) {
277 Diag(Ploc, diag::err_protocol_has_circular_dependency);
278 Diag(PrevLoc, diag::note_previous_definition);
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000281 PDecl->getLocation(), PDecl->getReferencedProtocols());
282 }
283 }
284}
285
John McCalld226f652010-08-21 09:40:31 +0000286Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000287Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
288 IdentifierInfo *ProtocolName,
289 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000290 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000291 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000292 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000293 SourceLocation EndProtoLoc,
294 AttributeList *AttrList) {
295 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000296 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000297 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000298 if (PDecl) {
299 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000300 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000301 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000302 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000303 // Just return the protocol we already had.
304 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000305 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000306 }
Steve Naroff61d68522009-03-05 15:22:01 +0000307 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000308 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000309 CheckForwardProtocolDeclarationForCircularDependency(
310 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Steve Narofff11b5082008-08-13 16:39:22 +0000312 // Make sure the cached decl gets a valid start location.
313 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000314 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000315 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000316 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000317 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000318 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000319 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000320 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000321 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000322 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000323 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000324 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000325 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000326 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000327 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000328 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
329 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000330 PDecl->setLocEnd(EndProtoLoc);
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
333 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000334 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000335}
336
337/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000338/// issues an error if they are not declared. It returns list of
339/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000340void
Chris Lattnere13b9592008-07-26 04:03:38 +0000341Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000342 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000343 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000344 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000345 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000346 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
347 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000348 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000349 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
350 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000351 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000352 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
353 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
354 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000355 Diag(PDecl->getLocation(), diag::note_previous_decl)
356 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000357 }
358 }
359
360 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000361 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000362 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000363 continue;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000366 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000367
368 // If this is a forward declaration and we are supposed to warn in this
369 // case, do it.
370 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000371 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000372 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000373 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000374 }
375}
376
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000377/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000378/// a class method in its extension.
379///
Mike Stump1eb44332009-09-09 15:08:12 +0000380void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000381 ObjCInterfaceDecl *ID) {
382 if (!ID)
383 return; // Possibly due to previous error
384
385 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000386 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
387 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000388 ObjCMethodDecl *MD = *i;
389 MethodMap[MD->getSelector()] = MD;
390 }
391
392 if (MethodMap.empty())
393 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000394 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
395 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000396 ObjCMethodDecl *Method = *i;
397 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
398 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
399 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
400 << Method->getDeclName();
401 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
402 }
403 }
404}
405
Chris Lattner58fe03b2009-04-12 08:43:13 +0000406/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000407Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000408Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000409 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000410 unsigned NumElts,
411 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000413 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattner4d391482007-12-12 07:09:47 +0000415 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000416 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000417 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000418 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000419 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000420 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000421 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000422 PushOnScopeChains(PDecl, TUScope, false);
423 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000424 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000425 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000426 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000427 if (!isNew)
428 PDecl->setChangedSinceDeserialization(true);
429 }
Chris Lattner4d391482007-12-12 07:09:47 +0000430 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000431 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
434 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000435 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000436 Protocols.data(), Protocols.size(),
437 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000438 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000439 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000440 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000441}
442
John McCalld226f652010-08-21 09:40:31 +0000443Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000444ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
445 IdentifierInfo *ClassName, SourceLocation ClassLoc,
446 IdentifierInfo *CategoryName,
447 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000448 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000449 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000450 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000451 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000452 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000453 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000454
455 /// Check that class of this category is already completely declared.
456 if (!IDecl || IDecl->isForwardDecl()) {
457 // Create an invalid ObjCCategoryDecl to serve as context for
458 // the enclosing method declarations. We mark the decl invalid
459 // to make it clear that this isn't a valid AST.
460 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
461 ClassLoc, CategoryLoc, CategoryName);
462 CDecl->setInvalidDecl();
463 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000464 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000465 }
466
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000467 if (!CategoryName && IDecl->getImplementation()) {
468 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
469 Diag(IDecl->getImplementation()->getLocation(),
470 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000471 }
472
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000473 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
474 ClassLoc, CategoryLoc, CategoryName);
475 // FIXME: PushOnScopeChains?
476 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000477
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000478 CDecl->setClassInterface(IDecl);
479 // Insert class extension to the list of class's categories.
480 if (!CategoryName)
481 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattner16b34b42009-02-16 21:30:01 +0000483 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000484 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000485
Fariborz Jahanian25760612010-02-15 21:55:26 +0000486 if (CategoryName) {
487 /// Check for duplicate interface declaration for this category
488 ObjCCategoryDecl *CDeclChain;
489 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
490 CDeclChain = CDeclChain->getNextClassCategory()) {
491 if (CDeclChain->getIdentifier() == CategoryName) {
492 // Class extensions can be declared multiple times.
493 Diag(CategoryLoc, diag::warn_dup_category_def)
494 << ClassName << CategoryName;
495 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
496 break;
497 }
Chris Lattner70f19542009-02-16 21:26:43 +0000498 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000499 if (!CDeclChain)
500 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000501 }
Chris Lattner70f19542009-02-16 21:26:43 +0000502
Chris Lattner4d391482007-12-12 07:09:47 +0000503 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000504 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000505 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000506 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000507 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000508 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000509 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Anders Carlsson15281452008-11-04 16:57:32 +0000512 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000513 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000514}
515
516/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000518/// object.
John McCalld226f652010-08-21 09:40:31 +0000519Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000520 SourceLocation AtCatImplLoc,
521 IdentifierInfo *ClassName, SourceLocation ClassLoc,
522 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000523 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000524 ObjCCategoryDecl *CatIDecl = 0;
525 if (IDecl) {
526 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
527 if (!CatIDecl) {
528 // Category @implementation with no corresponding @interface.
529 // Create and install one.
530 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000531 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000532 CatName);
533 CatIDecl->setClassInterface(IDecl);
534 CatIDecl->insertNextClassCategory();
535 }
536 }
537
Mike Stump1eb44332009-09-09 15:08:12 +0000538 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000539 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
540 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000541 /// Check that class of this category is already completely declared.
542 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000543 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000544
Douglas Gregord0434102009-01-09 00:49:46 +0000545 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000547
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000548 /// Check that CatName, category name, is not used in another implementation.
549 if (CatIDecl) {
550 if (CatIDecl->getImplementation()) {
551 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
552 << CatName;
553 Diag(CatIDecl->getImplementation()->getLocation(),
554 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000555 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000556 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000557 // Warn on implementating category of deprecated class under
558 // -Wdeprecated-implementations flag.
559 // FIXME. Refactor using common routine.
560 unsigned DIAG = diag::warn_depercated_def;
561 if (Diags.getDiagnosticLevel(DIAG, CDecl->getLocation())
562 != Diagnostic::Ignored)
563 if (NamedDecl *ND = dyn_cast<NamedDecl>(IDecl))
564 if (ND->getAttr<DeprecatedAttr>()) {
565 Diag(CDecl->getLocation(), DIAG) << 2;
566 Diag(IDecl->getLocation(), diag::note_previous_decl) << "class";
567 }
568
569 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Anders Carlsson15281452008-11-04 16:57:32 +0000572 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000573 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000574}
575
John McCalld226f652010-08-21 09:40:31 +0000576Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000577 SourceLocation AtClassImplLoc,
578 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000579 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000580 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000581 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000582 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000583 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000584 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
585 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000588 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000589 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
590 // If this is a forward declaration of an interface, warn.
591 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000593 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000594 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000595 } else {
596 // We did not find anything with the name ClassName; try to correct for
597 // typos in the class name.
598 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000599 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000600 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000601 // Suggest the (potentially) correct interface name. However, put the
602 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000603 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000604 // provide a code-modification hint or use the typo name for recovery,
605 // because this is just a warning. The program may actually be correct.
606 Diag(ClassLoc, diag::warn_undef_interface_suggest)
607 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000608 Diag(IDecl->getLocation(), diag::note_previous_decl)
609 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000610 << FixItHint::CreateReplacement(ClassLoc,
611 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000612 IDecl = 0;
613 } else {
614 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
615 }
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner4d391482007-12-12 07:09:47 +0000618 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000620 if (SuperClassname) {
621 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000622 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
623 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000625 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
626 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000627 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000629 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000630 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 Diag(SuperClassLoc, diag::err_undef_superclass)
632 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000633 else if (IDecl && IDecl->getSuperClass() != SDecl) {
634 // This implementation and its interface do not have the same
635 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000637 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000638 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000639 }
640 }
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Chris Lattner4d391482007-12-12 07:09:47 +0000643 if (!IDecl) {
644 // Legacy case of @implementation with no corresponding @interface.
645 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000646
Mike Stump390b4cc2009-05-16 07:39:55 +0000647 // FIXME: Do we support attributes on the @implementation? If so we should
648 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000649 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000650 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000651 IDecl->setSuperClass(SDecl);
652 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000653
654 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000655 } else {
656 // Mark the interface as being completed, even if it was just as
657 // @class ....;
658 // declaration; the user cannot reopen it.
659 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
662 ObjCImplementationDecl* IMPDecl =
663 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000664 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson15281452008-11-04 16:57:32 +0000666 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000667 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattner4d391482007-12-12 07:09:47 +0000669 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000670 if (IDecl->getImplementation()) {
671 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000673 Diag(IDecl->getImplementation()->getLocation(),
674 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000675 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000676 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000677 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000678 // Warn on implementating deprecated class under
679 // -Wdeprecated-implementations flag.
680 // FIXME. Refactor using common routine.
681 unsigned DIAG = diag::warn_depercated_def;
682 if (Diags.getDiagnosticLevel(DIAG, IMPDecl->getLocation())
683 != Diagnostic::Ignored)
684 if (NamedDecl *ND = dyn_cast<NamedDecl>(IDecl))
685 if (ND->getAttr<DeprecatedAttr>()) {
686 Diag(IMPDecl->getLocation(), DIAG) << 1;
687 Diag(IDecl->getLocation(), diag::note_previous_decl) << "class";
688 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000689 }
John McCalld226f652010-08-21 09:40:31 +0000690 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000691}
692
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
694 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000695 SourceLocation RBrace) {
696 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000697 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000698 if (!IDecl)
699 return;
700 /// Check case of non-existing @interface decl.
701 /// (legacy objective-c @implementation decl without an @interface decl).
702 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000703 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000704 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000705 // Add ivar's to class's DeclContext.
706 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000707 ivars[i]->setLexicalDeclContext(ImpDecl);
708 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000709 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000710 }
711
Chris Lattner4d391482007-12-12 07:09:47 +0000712 return;
713 }
714 // If implementation has empty ivar list, just return.
715 if (numIvars == 0)
716 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattner4d391482007-12-12 07:09:47 +0000718 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000719 if (LangOpts.ObjCNonFragileABI2) {
720 if (ImpDecl->getSuperClass())
721 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
722 for (unsigned i = 0; i < numIvars; i++) {
723 ObjCIvarDecl* ImplIvar = ivars[i];
724 if (const ObjCIvarDecl *ClsIvar =
725 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
726 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
727 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
728 continue;
729 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000730 // Instance ivar to Implementation's DeclContext.
731 ImplIvar->setLexicalDeclContext(ImpDecl);
732 IDecl->makeDeclVisibleInContext(ImplIvar, false);
733 ImpDecl->addDecl(ImplIvar);
734 }
735 return;
736 }
Chris Lattner4d391482007-12-12 07:09:47 +0000737 // Check interface's Ivar list against those in the implementation.
738 // names and types must match.
739 //
Chris Lattner4d391482007-12-12 07:09:47 +0000740 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000741 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000742 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
743 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 ObjCIvarDecl* ImplIvar = ivars[j++];
745 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000746 assert (ImplIvar && "missing implementation ivar");
747 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Steve Naroffca331292009-03-03 14:49:36 +0000749 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000750 if (Context.getCanonicalType(ImplIvar->getType()) !=
751 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000752 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000753 << ImplIvar->getIdentifier()
754 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000755 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000756 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
757 Expr *ImplBitWidth = ImplIvar->getBitWidth();
758 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000759 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
760 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000761 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
762 << ImplIvar->getIdentifier();
763 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765 }
Steve Naroffca331292009-03-03 14:49:36 +0000766 // Make sure the names are identical.
767 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000768 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000769 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000770 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000771 }
772 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattner609e4c72007-12-12 18:11:49 +0000775 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000776 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000777 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000778 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000779}
780
Steve Naroff3c2eb662008-02-10 21:38:56 +0000781void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000782 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000783 if (!IncompleteImpl) {
784 Diag(ImpLoc, diag::warn_incomplete_impl);
785 IncompleteImpl = true;
786 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000787 if (DiagID == diag::warn_unimplemented_protocol_method)
788 Diag(ImpLoc, DiagID) << method->getDeclName();
789 else
790 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000791}
792
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000793/// Determines if type B can be substituted for type A. Returns true if we can
794/// guarantee that anything that the user will do to an object of type A can
795/// also be done to an object of type B. This is trivially true if the two
796/// types are the same, or if B is a subclass of A. It becomes more complex
797/// in cases where protocols are involved.
798///
799/// Object types in Objective-C describe the minimum requirements for an
800/// object, rather than providing a complete description of a type. For
801/// example, if A is a subclass of B, then B* may refer to an instance of A.
802/// The principle of substitutability means that we may use an instance of A
803/// anywhere that we may use an instance of B - it will implement all of the
804/// ivars of B and all of the methods of B.
805///
806/// This substitutability is important when type checking methods, because
807/// the implementation may have stricter type definitions than the interface.
808/// The interface specifies minimum requirements, but the implementation may
809/// have more accurate ones. For example, a method may privately accept
810/// instances of B, but only publish that it accepts instances of A. Any
811/// object passed to it will be type checked against B, and so will implicitly
812/// by a valid A*. Similarly, a method may return a subclass of the class that
813/// it is declared as returning.
814///
815/// This is most important when considering subclassing. A method in a
816/// subclass must accept any object as an argument that its superclass's
817/// implementation accepts. It may, however, accept a more general type
818/// without breaking substitutability (i.e. you can still use the subclass
819/// anywhere that you can use the superclass, but not vice versa). The
820/// converse requirement applies to return types: the return type for a
821/// subclass method must be a valid object of the kind that the superclass
822/// advertises, but it may be specified more accurately. This avoids the need
823/// for explicit down-casting by callers.
824///
825/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000826static bool isObjCTypeSubstitutable(ASTContext &Context,
827 const ObjCObjectPointerType *A,
828 const ObjCObjectPointerType *B,
829 bool rejectId) {
830 // Reject a protocol-unqualified id.
831 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000832
833 // If B is a qualified id, then A must also be a qualified id and it must
834 // implement all of the protocols in B. It may not be a qualified class.
835 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
836 // stricter definition so it is not substitutable for id<A>.
837 if (B->isObjCQualifiedIdType()) {
838 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000839 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
840 QualType(B,0),
841 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000842 }
843
844 /*
845 // id is a special type that bypasses type checking completely. We want a
846 // warning when it is used in one place but not another.
847 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
848
849
850 // If B is a qualified id, then A must also be a qualified id (which it isn't
851 // if we've got this far)
852 if (B->isObjCQualifiedIdType()) return false;
853 */
854
855 // Now we know that A and B are (potentially-qualified) class types. The
856 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000857 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000858}
859
John McCall10302c02010-10-28 02:34:38 +0000860static SourceRange getTypeRange(TypeSourceInfo *TSI) {
861 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
862}
863
864static void CheckMethodOverrideReturn(Sema &S,
865 ObjCMethodDecl *MethodImpl,
866 ObjCMethodDecl *MethodIface) {
867 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
868 MethodIface->getResultType()))
869 return;
870
871 unsigned DiagID = diag::warn_conflicting_ret_types;
872
873 // Mismatches between ObjC pointers go into a different warning
874 // category, and sometimes they're even completely whitelisted.
875 if (const ObjCObjectPointerType *ImplPtrTy =
876 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
877 if (const ObjCObjectPointerType *IfacePtrTy =
878 MethodIface->getResultType()->getAs<ObjCObjectPointerType>()) {
879 // Allow non-matching return types as long as they don't violate
880 // the principle of substitutability. Specifically, we permit
881 // return types that are subclasses of the declared return type,
882 // or that are more-qualified versions of the declared type.
883 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
884 return;
885
886 DiagID = diag::warn_non_covariant_ret_types;
887 }
888 }
889
890 S.Diag(MethodImpl->getLocation(), DiagID)
891 << MethodImpl->getDeclName()
892 << MethodIface->getResultType()
893 << MethodImpl->getResultType()
894 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
895 S.Diag(MethodIface->getLocation(), diag::note_previous_definition)
896 << getTypeRange(MethodIface->getResultTypeSourceInfo());
897}
898
899static void CheckMethodOverrideParam(Sema &S,
900 ObjCMethodDecl *MethodImpl,
901 ObjCMethodDecl *MethodIface,
902 ParmVarDecl *ImplVar,
903 ParmVarDecl *IfaceVar) {
904 QualType ImplTy = ImplVar->getType();
905 QualType IfaceTy = IfaceVar->getType();
906 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
907 return;
908
909 unsigned DiagID = diag::warn_conflicting_param_types;
910
911 // Mismatches between ObjC pointers go into a different warning
912 // category, and sometimes they're even completely whitelisted.
913 if (const ObjCObjectPointerType *ImplPtrTy =
914 ImplTy->getAs<ObjCObjectPointerType>()) {
915 if (const ObjCObjectPointerType *IfacePtrTy =
916 IfaceTy->getAs<ObjCObjectPointerType>()) {
917 // Allow non-matching argument types as long as they don't
918 // violate the principle of substitutability. Specifically, the
919 // implementation must accept any objects that the superclass
920 // accepts, however it may also accept others.
921 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
922 return;
923
924 DiagID = diag::warn_non_contravariant_param_types;
925 }
926 }
927
928 S.Diag(ImplVar->getLocation(), DiagID)
929 << getTypeRange(ImplVar->getTypeSourceInfo())
930 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
931 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
932 << getTypeRange(IfaceVar->getTypeSourceInfo());
933}
934
935
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000936void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
937 ObjCMethodDecl *IntfMethodDecl) {
John McCall10302c02010-10-28 02:34:38 +0000938 CheckMethodOverrideReturn(*this, ImpMethodDecl, IntfMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner3aff9192009-04-11 19:58:42 +0000940 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
941 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +0000942 IM != EM; ++IM, ++IF)
943 CheckMethodOverrideParam(*this, ImpMethodDecl, IntfMethodDecl, *IM, *IF);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000945 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
946 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
947 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
948 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000949}
950
Mike Stump390b4cc2009-05-16 07:39:55 +0000951/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
952/// improve the efficiency of selector lookups and type checking by associating
953/// with each protocol / interface / category the flattened instance tables. If
954/// we used an immutable set to keep the table then it wouldn't add significant
955/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000956
Steve Naroffefe7f362008-02-08 22:06:17 +0000957/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000958/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000959void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
960 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000961 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000962 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000963 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000964 ObjCContainerDecl *CDecl) {
965 ObjCInterfaceDecl *IDecl;
966 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
967 IDecl = C->getClassInterface();
968 else
969 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
970 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
971
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000972 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000973 ObjCInterfaceDecl *NSIDecl = 0;
974 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000975 // check to see if class implements forwardInvocation method and objects
976 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000977 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000978 // Under such conditions, which means that every method possible is
979 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000980 // found" warnings.
981 // FIXME: Use a general GetUnarySelector method for this.
982 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
983 Selector fISelector = Context.Selectors.getSelector(1, &II);
984 if (InsMap.count(fISelector))
985 // Is IDecl derived from 'NSProxy'? If so, no instance methods
986 // need be implemented in the implementation.
987 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000990 // If a method lookup fails locally we still need to look and see if
991 // the method was implemented by a base class or an inherited
992 // protocol. This lookup is slow, but occurs rarely in correct code
993 // and otherwise would terminate in a warning.
994
Chris Lattner4d391482007-12-12 07:09:47 +0000995 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000996 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000997 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000998 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000999 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001000 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001001 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001002 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001004 // Ugly, but necessary. Method declared in protcol might have
1005 // have been synthesized due to a property declared in the class which
1006 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001007 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001008 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001009 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001010 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001011 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1012 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001013 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001014 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001015 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1016 << PDecl->getDeclName();
1017 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001018 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001019 }
1020 }
Chris Lattner4d391482007-12-12 07:09:47 +00001021 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001022 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001023 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001024 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001025 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001026 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1027 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001028 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001029 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001030 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001031 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001032 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001033 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1034 PDecl->getDeclName();
1035 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001036 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001037 }
Chris Lattner780f3292008-07-21 21:32:27 +00001038 // Check on this protocols's referenced protocols, recursively.
1039 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1040 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001041 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001042}
1043
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001044/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1045/// or protocol against those declared in their implementations.
1046///
1047void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1048 const llvm::DenseSet<Selector> &ClsMap,
1049 llvm::DenseSet<Selector> &InsMapSeen,
1050 llvm::DenseSet<Selector> &ClsMapSeen,
1051 ObjCImplDecl* IMPDecl,
1052 ObjCContainerDecl* CDecl,
1053 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001054 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001055 // Check and see if instance methods in class interface have been
1056 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001057 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1058 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001059 if (InsMapSeen.count((*I)->getSelector()))
1060 continue;
1061 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001062 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001063 !InsMap.count((*I)->getSelector())) {
1064 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001065 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1066 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001067 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001068 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001069 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001070 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001071 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001072 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001073 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001074 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1075 // ImpMethodDecl may be null as in a @dynamic property.
1076 if (ImpMethodDecl)
1077 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1078 }
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001081 // Check and see if class methods in class interface have been
1082 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001083 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001084 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001085 if (ClsMapSeen.count((*I)->getSelector()))
1086 continue;
1087 ClsMapSeen.insert((*I)->getSelector());
1088 if (!ClsMap.count((*I)->getSelector())) {
1089 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001090 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1091 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001092 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001093 ObjCMethodDecl *ImpMethodDecl =
1094 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001095 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001096 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001097 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1098 }
1099 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001100
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001101 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001102 // Also methods in class extensions need be looked at next.
1103 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1104 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1105 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1106 IMPDecl,
1107 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1108 IncompleteImpl, false);
1109
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001110 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001111 for (ObjCInterfaceDecl::all_protocol_iterator
1112 PI = I->all_referenced_protocol_begin(),
1113 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001114 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1115 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001116 (*PI), IncompleteImpl, false);
1117 if (I->getSuperClass())
1118 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001119 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001120 I->getSuperClass(), IncompleteImpl, false);
1121 }
1122}
1123
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001124void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001125 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001126 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001127 llvm::DenseSet<Selector> InsMap;
1128 // Check and see if instance methods in class interface have been
1129 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001130 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001131 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001132 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001134 // Check and see if properties declared in the interface have either 1)
1135 // an implementation or 2) there is a @synthesize/@dynamic implementation
1136 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001137 if (isa<ObjCInterfaceDecl>(CDecl) &&
1138 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001139 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001140
Chris Lattner4d391482007-12-12 07:09:47 +00001141 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001142 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001143 I = IMPDecl->classmeth_begin(),
1144 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001145 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001147 // Check for type conflict of methods declared in a class/protocol and
1148 // its implementation; if any.
1149 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001150 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1151 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001152 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Chris Lattner4d391482007-12-12 07:09:47 +00001154 // Check the protocol list for unimplemented methods in the @implementation
1155 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001156 // Check and see if class methods in class interface have been
1157 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattnercddc8882009-03-01 00:56:52 +00001159 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001160 for (ObjCInterfaceDecl::all_protocol_iterator
1161 PI = I->all_referenced_protocol_begin(),
1162 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001163 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001164 InsMap, ClsMap, I);
1165 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001166 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1167 Categories; Categories = Categories->getNextClassExtension())
1168 ImplMethodsVsClassMethods(S, IMPDecl,
1169 const_cast<ObjCCategoryDecl*>(Categories),
1170 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001171 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001172 // For extended class, unimplemented methods in its protocols will
1173 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001174 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001175 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1176 E = C->protocol_end(); PI != E; ++PI)
1177 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001178 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001179 // Report unimplemented properties in the category as well.
1180 // When reporting on missing setter/getters, do not report when
1181 // setter/getter is implemented in category's primary class
1182 // implementation.
1183 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1184 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1185 for (ObjCImplementationDecl::instmeth_iterator
1186 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1187 InsMap.insert((*I)->getSelector());
1188 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001189 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001190 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001191 } else
1192 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001193}
1194
Mike Stump1eb44332009-09-09 15:08:12 +00001195/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001196Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001197Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001198 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001199 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001200 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001201 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattner4d391482007-12-12 07:09:47 +00001203 for (unsigned i = 0; i != NumElts; ++i) {
1204 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001205 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001206 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001207 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001208 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001209 // Maybe we will complain about the shadowed template parameter.
1210 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1211 // Just pretend that we didn't see the previous declaration.
1212 PrevDecl = 0;
1213 }
1214
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001216 // GCC apparently allows the following idiom:
1217 //
1218 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1219 // @class XCElementToggler;
1220 //
Mike Stump1eb44332009-09-09 15:08:12 +00001221 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001222 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001223 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001224 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001225 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001226 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001227 // a forward class declaration matching a typedef name of a class refers
1228 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001229 if (const ObjCObjectType *OI =
1230 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1231 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001232 }
Chris Lattner4d391482007-12-12 07:09:47 +00001233 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001234 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1235 if (!IDecl) { // Not already seen? Make a forward decl.
1236 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1237 IdentList[i], IdentLocs[i], true);
1238
1239 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1240 // the current DeclContext. This prevents clients that walk DeclContext
1241 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1242 // declared later (if at all). We also take care to explicitly make
1243 // sure this declaration is visible for name lookup.
1244 PushOnScopeChains(IDecl, TUScope, false);
1245 CurContext->makeDeclVisibleInContext(IDecl, true);
1246 }
Chris Lattner4d391482007-12-12 07:09:47 +00001247
1248 Interfaces.push_back(IDecl);
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenek321c22f2009-11-18 00:28:11 +00001251 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001252 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001253 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001254 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001255 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001256 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001257 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001258}
1259
1260
1261/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1262/// returns true, or false, accordingly.
1263/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001264bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001265 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001266 bool matchBasedOnSizeAndAlignment,
1267 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001268 QualType T1 = Context.getCanonicalType(Method->getResultType());
1269 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001271 if (T1 != T2) {
1272 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001273 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001274 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001275 // Incomplete types don't have a size and alignment.
1276 if (T1->isIncompleteType() || T2->isIncompleteType())
1277 return false;
1278 // Check is based on size and alignment.
1279 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1280 return false;
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Chris Lattner89951a82009-02-20 18:43:26 +00001283 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1284 E = Method->param_end();
1285 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattner89951a82009-02-20 18:43:26 +00001287 for (; ParamI != E; ++ParamI, ++PrevI) {
1288 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1289 T1 = Context.getCanonicalType((*ParamI)->getType());
1290 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001291 if (T1 != T2) {
1292 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001293 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001294 return false;
1295 // Incomplete types don't have a size and alignment.
1296 if (T1->isIncompleteType() || T2->isIncompleteType())
1297 return false;
1298 // Check is based on size and alignment.
1299 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1300 return false;
1301 }
Chris Lattner4d391482007-12-12 07:09:47 +00001302 }
1303 return true;
1304}
1305
Sebastian Redldb9d2142010-08-02 23:18:59 +00001306/// \brief Read the contents of the method pool for a given selector from
1307/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001308///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001309/// This routine should only be called once, when the method pool has no entry
1310/// for this selector.
1311Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001312 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001313 assert(MethodPool.find(Sel) == MethodPool.end() &&
1314 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001315
1316 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001317 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Sebastian Redldb9d2142010-08-02 23:18:59 +00001319 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001320}
1321
Sebastian Redldb9d2142010-08-02 23:18:59 +00001322void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1323 bool instance) {
1324 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1325 if (Pos == MethodPool.end()) {
1326 if (ExternalSource)
1327 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001328 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001329 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1330 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001331 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001332 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001333 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001334 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001335 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001336 Entry.Method = Method;
1337 Entry.Next = 0;
1338 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattnerb25df352009-03-04 05:16:45 +00001341 // We've seen a method with this name, see if we have already seen this type
1342 // signature.
1343 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001344 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001345 ObjCMethodDecl *PrevObjCMethod = List->Method;
1346 PrevObjCMethod->setDefined(impl);
1347 // If a method is deprecated, push it in the global pool.
1348 // This is used for better diagnostics.
1349 if (Method->getAttr<DeprecatedAttr>()) {
1350 if (!PrevObjCMethod->getAttr<DeprecatedAttr>())
1351 List->Method = Method;
1352 }
1353 // If new method is unavailable, push it into global pool
1354 // unless previous one is deprecated.
1355 if (Method->getAttr<UnavailableAttr>()) {
1356 if (!PrevObjCMethod->getAttr<UnavailableAttr>() &&
1357 !PrevObjCMethod->getAttr<DeprecatedAttr>())
1358 List->Method = Method;
1359 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001360 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattnerb25df352009-03-04 05:16:45 +00001363 // We have a new signature for an existing method - add it.
1364 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001365 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1366 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001367}
1368
Sebastian Redldb9d2142010-08-02 23:18:59 +00001369ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001370 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001371 bool warn, bool instance) {
1372 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1373 if (Pos == MethodPool.end()) {
1374 if (ExternalSource)
1375 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001376 else
1377 return 0;
1378 }
1379
Sebastian Redldb9d2142010-08-02 23:18:59 +00001380 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001382 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001383 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1384 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001385 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001386 if (warn && MethList.Method && MethList.Next) {
1387 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001388 if (strictSelectorMatch)
1389 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1390 // This checks if the methods differ in type mismatch.
1391 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1392 issueWarning = true;
1393 }
1394
1395 if (!issueWarning)
1396 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1397 // This checks if the methods differ by size & alignment.
1398 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1399 issueWarning = true;
1400 }
1401
Sebastian Redldb9d2142010-08-02 23:18:59 +00001402 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001403 if (strictSelectorMatch)
1404 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1405 else
1406 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001407 Diag(MethList.Method->getLocStart(), diag::note_using)
1408 << MethList.Method->getSourceRange();
1409 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1410 Diag(Next->Method->getLocStart(), diag::note_also_found)
1411 << Next->Method->getSourceRange();
1412 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001413 }
1414 return MethList.Method;
1415}
1416
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001417ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001418 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1419 if (Pos == MethodPool.end())
1420 return 0;
1421
1422 GlobalMethods &Methods = Pos->second;
1423
1424 if (Methods.first.Method && Methods.first.Method->isDefined())
1425 return Methods.first.Method;
1426 if (Methods.second.Method && Methods.second.Method->isDefined())
1427 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001428 return 0;
1429}
1430
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001431/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1432/// identical selector names in current and its super classes and issues
1433/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001434void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1435 ObjCMethodDecl *Method,
1436 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001437 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1438 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001440 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001441 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001442 SD->lookupMethod(Method->getSelector(), IsInstance);
1443 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001444 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001445 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001446 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001447 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1448 E = Method->param_end();
1449 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1450 for (; ParamI != E; ++ParamI, ++PrevI) {
1451 // Number of parameters are the same and is guaranteed by selector match.
1452 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1453 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1454 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1455 // If type of arguement of method in this class does not match its
1456 // respective argument type in the super class method, issue warning;
1457 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001458 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001459 << T1 << T2;
1460 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1461 return;
1462 }
1463 }
1464 ID = SD;
1465 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001466}
1467
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001468/// DiagnoseDuplicateIvars -
1469/// Check for duplicate ivars in the entire class at the start of
1470/// @implementation. This becomes necesssary because class extension can
1471/// add ivars to a class in random order which will not be known until
1472/// class's @implementation is seen.
1473void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1474 ObjCInterfaceDecl *SID) {
1475 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1476 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1477 ObjCIvarDecl* Ivar = (*IVI);
1478 if (Ivar->isInvalidDecl())
1479 continue;
1480 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1481 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1482 if (prevIvar) {
1483 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1484 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1485 Ivar->setInvalidDecl();
1486 }
1487 }
1488 }
1489}
1490
Steve Naroffa56f6162007-12-18 01:30:32 +00001491// Note: For class/category implemenations, allMethods/allProperties is
1492// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001493void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001494 Decl *ClassDecl,
1495 Decl **allMethods, unsigned allNum,
1496 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001497 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001498 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1499 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001500 // should be true.
1501 if (!ClassDecl)
1502 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001503
Mike Stump1eb44332009-09-09 15:08:12 +00001504 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001505 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1506 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001507 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001508
Ted Kremenek782f2f52010-01-07 01:20:12 +00001509 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1510 // FIXME: This is wrong. We shouldn't be pretending that there is
1511 // an '@end' in the declaration.
1512 SourceLocation L = ClassDecl->getLocation();
1513 AtEnd.setBegin(L);
1514 AtEnd.setEnd(L);
1515 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001516 }
1517
Steve Naroff0701bbb2009-01-08 17:28:14 +00001518 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1519 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1520 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1521
Chris Lattner4d391482007-12-12 07:09:47 +00001522 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001523 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001524 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001525
1526 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001527 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001528 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001529 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001530 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001531 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001532 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001533 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001534 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001535 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001536 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001537 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001538 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001539 InsMap[Method->getSelector()] = Method;
1540 /// The following allows us to typecheck messages to "id".
1541 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001542 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001543 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001544 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001545 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001546 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001547 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001549 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001550 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001551 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001552 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001553 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001554 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001555 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001556 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001557 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001558 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001559 /// The following allows us to typecheck messages to "Class".
1560 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001561 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001562 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001563 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001564 }
1565 }
1566 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001567 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001568 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001569 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001570 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001571 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001572 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001573 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001574 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001575 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001576
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001577 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001578 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001579 if (C->IsClassExtension()) {
1580 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1581 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001582 }
Chris Lattner4d391482007-12-12 07:09:47 +00001583 }
Steve Naroff09c47192009-01-09 15:36:25 +00001584 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001585 if (CDecl->getIdentifier())
1586 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1587 // user-defined setter/getter. It also synthesizes setter/getter methods
1588 // and adds them to the DeclContext and global method pools.
1589 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1590 E = CDecl->prop_end();
1591 I != E; ++I)
1592 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001593 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001594 }
1595 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001596 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001597 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001598 // Any property declared in a class extension might have user
1599 // declared setter or getter in current class extension or one
1600 // of the other class extensions. Mark them as synthesized as
1601 // property will be synthesized when property with same name is
1602 // seen in the @implementation.
1603 for (const ObjCCategoryDecl *ClsExtDecl =
1604 IDecl->getFirstClassExtension();
1605 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1606 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1607 E = ClsExtDecl->prop_end(); I != E; ++I) {
1608 ObjCPropertyDecl *Property = (*I);
1609 // Skip over properties declared @dynamic
1610 if (const ObjCPropertyImplDecl *PIDecl
1611 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1612 if (PIDecl->getPropertyImplementation()
1613 == ObjCPropertyImplDecl::Dynamic)
1614 continue;
1615
1616 for (const ObjCCategoryDecl *CExtDecl =
1617 IDecl->getFirstClassExtension();
1618 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1619 if (ObjCMethodDecl *GetterMethod =
1620 CExtDecl->getInstanceMethod(Property->getGetterName()))
1621 GetterMethod->setSynthesized(true);
1622 if (!Property->isReadOnly())
1623 if (ObjCMethodDecl *SetterMethod =
1624 CExtDecl->getInstanceMethod(Property->getSetterName()))
1625 SetterMethod->setSynthesized(true);
1626 }
1627 }
1628 }
1629
Ted Kremenekc32647d2010-12-23 21:35:43 +00001630 if (LangOpts.ObjCDefaultSynthProperties &&
1631 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001632 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001633 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001634 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001635
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001636 if (LangOpts.ObjCNonFragileABI2)
1637 while (IDecl->getSuperClass()) {
1638 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1639 IDecl = IDecl->getSuperClass();
1640 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001641 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001642 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001643 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001644 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001645 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Chris Lattner4d391482007-12-12 07:09:47 +00001647 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001648 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001649 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001650 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001651 Categories; Categories = Categories->getNextClassCategory()) {
1652 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001653 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001654 break;
1655 }
1656 }
1657 }
1658 }
Chris Lattner682bf922009-03-29 16:50:03 +00001659 if (isInterfaceDeclKind) {
1660 // Reject invalid vardecls.
1661 for (unsigned i = 0; i != tuvNum; i++) {
1662 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1663 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1664 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001665 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001666 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001667 }
Chris Lattner682bf922009-03-29 16:50:03 +00001668 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001669 }
Chris Lattner4d391482007-12-12 07:09:47 +00001670}
1671
1672
1673/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1674/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001675static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1677 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1678 if (PQTVal & ObjCDeclSpec::DQ_In)
1679 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1680 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1681 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1682 if (PQTVal & ObjCDeclSpec::DQ_Out)
1683 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1684 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1685 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1686 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1687 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1688 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1689 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001690
1691 return ret;
1692}
1693
Ted Kremenek422bae72010-04-18 04:59:38 +00001694static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001695bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001696 // The 'ibaction' attribute is allowed on method definitions because of
1697 // how the IBAction macro is used on both method declarations and definitions.
1698 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001699 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1700 if ((*i)->getKind() != attr::IBAction)
1701 return true;
1702 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001703}
1704
John McCalld226f652010-08-21 09:40:31 +00001705Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001706 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001707 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001708 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001709 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001710 Selector Sel,
1711 // optional arguments. The number of types/arguments is obtained
1712 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001713 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001714 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001715 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1716 bool isVariadic) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001717 // Make sure we can establish a context for the method.
1718 if (!ClassDecl) {
1719 Diag(MethodLoc, diag::error_missing_method_context);
John McCall781472f2010-08-25 08:40:02 +00001720 getCurFunction()->LabelMap.clear();
John McCalld226f652010-08-21 09:40:31 +00001721 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001722 }
Chris Lattner4d391482007-12-12 07:09:47 +00001723 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001725 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001726 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001727 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Steve Naroffccef3712009-02-20 22:59:16 +00001729 // Methods cannot return interface types. All ObjC objects are
1730 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001731 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001732 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1733 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001734 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001735 }
1736 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001738
1739 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001740 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001741 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001742 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001743 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001744 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001745 MethodDeclKind == tok::objc_optional ?
1746 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001747 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Chris Lattner0ed844b2008-04-04 06:12:32 +00001749 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Chris Lattner7db638d2009-04-11 19:42:43 +00001751 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001752 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001753 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Chris Lattnere294d3f2009-04-11 18:57:04 +00001755 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001756 ArgType = Context.getObjCIdType();
1757 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001758 } else {
John McCall58e46772009-10-23 21:48:59 +00001759 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001760 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001761 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001762 }
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001764 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1765 LookupOrdinaryName, ForRedeclaration);
1766 LookupName(R, S);
1767 if (R.isSingleResult()) {
1768 NamedDecl *PrevDecl = R.getFoundDecl();
1769 if (S->isDeclScope(PrevDecl)) {
1770 // FIXME. This should be an error; but will break projects.
1771 Diag(ArgInfo[i].NameLoc, diag::warn_method_param_redefinition)
1772 << ArgInfo[i].Name;
1773 Diag(PrevDecl->getLocation(),
1774 diag::note_previous_declaration);
1775 }
1776 }
1777
John McCall58e46772009-10-23 21:48:59 +00001778 ParmVarDecl* Param
1779 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1780 ArgInfo[i].Name, ArgType, DI,
John McCalld931b082010-08-26 03:08:43 +00001781 SC_None, SC_None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001782
John McCallc12c5bb2010-05-15 11:32:37 +00001783 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001784 Diag(ArgInfo[i].NameLoc,
1785 diag::err_object_cannot_be_passed_returned_by_value)
1786 << 1 << ArgType;
1787 Param->setInvalidDecl();
1788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattner0ed844b2008-04-04 06:12:32 +00001790 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001791 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001793 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001794 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001796 S->AddDecl(Param);
1797 IdResolver.AddDecl(Param);
1798
Chris Lattner0ed844b2008-04-04 06:12:32 +00001799 Params.push_back(Param);
1800 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001801
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001802 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001803 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001804 QualType ArgType = Param->getType();
1805 if (ArgType.isNull())
1806 ArgType = Context.getObjCIdType();
1807 else
1808 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1809 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001810 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001811 Diag(Param->getLocation(),
1812 diag::err_object_cannot_be_passed_returned_by_value)
1813 << 1 << ArgType;
1814 Param->setInvalidDecl();
1815 }
1816 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001817
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001818 Params.push_back(Param);
1819 }
1820
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001821 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1822 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001823 ObjCMethod->setObjCDeclQualifier(
1824 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1825 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001826
1827 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001828 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001829
John McCall54abf7d2009-11-04 02:18:39 +00001830 const ObjCMethodDecl *InterfaceMD = 0;
1831
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001832 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00001833 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001834 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001835 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001836 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1837 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001838 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001839 PrevMethod = ImpDecl->getClassMethod(Sel);
1840 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001841 }
John McCall54abf7d2009-11-04 02:18:39 +00001842 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1843 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001844 if (ObjCMethod->hasAttrs() &&
1845 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001846 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001847 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001848 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001849 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001850 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1851 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001852 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001853 PrevMethod = CatImpDecl->getClassMethod(Sel);
1854 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001855 }
Sean Huntcf807c42010-08-18 23:23:40 +00001856 if (ObjCMethod->hasAttrs() &&
1857 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001858 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001859 } else {
1860 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001861 }
1862 if (PrevMethod) {
1863 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001864 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001865 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001866 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001867 }
John McCall54abf7d2009-11-04 02:18:39 +00001868
1869 // If the interface declared this method, and it was deprecated there,
1870 // mark it deprecated here.
Sean Huntcf807c42010-08-18 23:23:40 +00001871 if (InterfaceMD)
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001872 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>()) {
1873 StringLiteral *SE = StringLiteral::CreateEmpty(Context, 1);
1874 ObjCMethod->addAttr(::new (Context)
1875 DeprecatedAttr(DA->getLocation(),
1876 Context,
1877 SE->getString()));
1878 }
John McCall54abf7d2009-11-04 02:18:39 +00001879
John McCalld226f652010-08-21 09:40:31 +00001880 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001881}
1882
Chris Lattnercc98eac2008-12-17 07:13:27 +00001883bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001884 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001885 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Anders Carlsson15281452008-11-04 16:57:32 +00001887 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1888 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Anders Carlsson15281452008-11-04 16:57:32 +00001890 return true;
1891}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001892
Chris Lattnercc98eac2008-12-17 07:13:27 +00001893/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1894/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001895void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001896 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001897 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001898 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001899 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001900 if (!Class) {
1901 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1902 return;
1903 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001904 if (LangOpts.ObjCNonFragileABI) {
1905 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1906 return;
1907 }
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Chris Lattnercc98eac2008-12-17 07:13:27 +00001909 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001910 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1911 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001912 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001913 for (unsigned i = 0; i < Ivars.size(); i++) {
1914 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001915 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001916 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1917 ID->getIdentifier(), ID->getType(),
1918 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001919 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001920 }
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Chris Lattnercc98eac2008-12-17 07:13:27 +00001922 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001923 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001924 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001925 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001926 if (getLangOptions().CPlusPlus)
1927 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001928 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001929 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001930 }
1931}
1932
Douglas Gregor160b5632010-04-26 17:32:49 +00001933/// \brief Build a type-check a new Objective-C exception variable declaration.
1934VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1935 QualType T,
1936 IdentifierInfo *Name,
1937 SourceLocation NameLoc,
1938 bool Invalid) {
1939 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1940 // duration shall not be qualified by an address-space qualifier."
1941 // Since all parameters have automatic store duration, they can not have
1942 // an address space.
1943 if (T.getAddressSpace() != 0) {
1944 Diag(NameLoc, diag::err_arg_with_address_space);
1945 Invalid = true;
1946 }
1947
1948 // An @catch parameter must be an unqualified object pointer type;
1949 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1950 if (Invalid) {
1951 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001952 } else if (T->isDependentType()) {
1953 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001954 } else if (!T->isObjCObjectPointerType()) {
1955 Invalid = true;
1956 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1957 } else if (T->isObjCQualifiedIdType()) {
1958 Invalid = true;
1959 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1960 }
1961
1962 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
John McCalld931b082010-08-26 03:08:43 +00001963 SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00001964 New->setExceptionVariable(true);
1965
Douglas Gregor160b5632010-04-26 17:32:49 +00001966 if (Invalid)
1967 New->setInvalidDecl();
1968 return New;
1969}
1970
John McCalld226f652010-08-21 09:40:31 +00001971Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001972 const DeclSpec &DS = D.getDeclSpec();
1973
1974 // We allow the "register" storage class on exception variables because
1975 // GCC did, but we drop it completely. Any other storage class is an error.
1976 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1977 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1978 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1979 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1980 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1981 << DS.getStorageClassSpec();
1982 }
1983 if (D.getDeclSpec().isThreadSpecified())
1984 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1985 D.getMutableDeclSpec().ClearStorageClassSpecs();
1986
1987 DiagnoseFunctionSpecifiers(D);
1988
1989 // Check that there are no default arguments inside the type of this
1990 // exception object (C++ only).
1991 if (getLangOptions().CPlusPlus)
1992 CheckExtraCXXDefaultArguments(D);
1993
Douglas Gregor160b5632010-04-26 17:32:49 +00001994 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001995 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1996 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001997
1998 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1999 // Objective-C++: Types shall not be defined in exception types.
2000 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2001 << Context.getTypeDeclType(OwnedDecl);
2002 }
2003
2004 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
2005 D.getIdentifierLoc(),
2006 D.isInvalidType());
2007
2008 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2009 if (D.getCXXScopeSpec().isSet()) {
2010 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2011 << D.getCXXScopeSpec().getRange();
2012 New->setInvalidDecl();
2013 }
2014
2015 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002016 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002017 if (D.getIdentifier())
2018 IdResolver.AddDecl(New);
2019
2020 ProcessDeclAttributes(S, New, D);
2021
2022 if (New->hasAttr<BlocksAttr>())
2023 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002024 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002025}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002026
2027/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002028/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002029void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002030 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002031 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2032 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002033 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002034 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002035 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002036 }
2037}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002038
2039void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002040 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002041 unsigned numInitializers) {
2042 if (numInitializers > 0) {
2043 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002044 CXXCtorInitializer **ivarInitializers =
2045 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002046 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002047 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002048 IvarInitializers = ivarInitializers;
2049 }
2050}
2051
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002052void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002053 // Warning will be issued only when selector table is
2054 // generated (which means there is at lease one implementation
2055 // in the TU). This is to match gcc's behavior.
2056 if (ReferencedSelectors.empty() ||
2057 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002058 return;
2059 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2060 ReferencedSelectors.begin(),
2061 E = ReferencedSelectors.end(); S != E; ++S) {
2062 Selector Sel = (*S).first;
2063 if (!LookupImplementedMethodInGlobalPool(Sel))
2064 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2065 }
2066 return;
2067}