blob: 5d9a924ed8c5e6d7e69ff1342be2a5c6360709d7 [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 }
Chris Lattner4d391482007-12-12 07:09:47 +000067}
68
John McCalld226f652010-08-21 09:40:31 +000069Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000070ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
71 IdentifierInfo *ClassName, SourceLocation ClassLoc,
72 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +000073 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000074 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000075 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000076 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner4d391482007-12-12 07:09:47 +000078 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +000079 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +000080 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +000081
Ted Kremeneka526c5c2008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000084 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000085 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregordeacbdc2010-08-11 12:19:30 +000087 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
88 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +000089 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +000090 if (!IDecl->isForwardDecl()) {
91 IDecl->setInvalidDecl();
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
93 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +000094
Douglas Gregordeacbdc2010-08-11 12:19:30 +000095 // Return the previous class interface.
96 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +000097 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +000098 } else {
99 IDecl->setLocation(AtInterfaceLoc);
100 IDecl->setForwardDecl(false);
101 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000102 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000103 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000104 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000105
106 // Since this ObjCInterfaceDecl was created by a forward declaration,
107 // we now add it to the DeclContext since it wasn't added before
108 // (see ActOnForwardClassDeclaration).
109 IDecl->setLexicalDeclContext(CurContext);
110 CurContext->addDecl(IDecl);
111
112 if (AttrList)
113 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000114 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000115 } else {
116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
117 ClassName, ClassLoc);
118 if (AttrList)
119 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
120
121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner4d391482007-12-12 07:09:47 +0000124 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000126 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
127 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000128
129 if (!PrevDecl) {
130 // Try to correct for a typo in the superclass name.
131 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000132 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000133 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
134 Diag(SuperLoc, diag::err_undef_superclass_suggest)
135 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000136 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
137 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000138 }
139 }
140
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000141 if (PrevDecl == IDecl) {
142 Diag(SuperLoc, diag::err_recursive_superclass)
143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
144 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000145 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000147 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000148
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000149 // Diagnose classes that inherit from deprecated classes.
150 if (SuperClassDecl)
151 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000153 if (PrevDecl && SuperClassDecl == 0) {
154 // The previous declaration was not a class decl. Check if we have a
155 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000156 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000157 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000158 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000159 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
160 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000161 }
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000164 // This handles the following case:
165 //
166 // typedef int SuperClass;
167 // @interface MyClass : SuperClass {} @end
168 //
169 if (!SuperClassDecl) {
170 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
171 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000172 }
173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000175 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
176 if (!SuperClassDecl)
177 Diag(SuperLoc, diag::err_undef_superclass)
178 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
179 else if (SuperClassDecl->isForwardDecl())
180 Diag(SuperLoc, diag::err_undef_superclass)
181 << SuperClassDecl->getDeclName() << ClassName
182 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000183 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000184 IDecl->setSuperClass(SuperClassDecl);
185 IDecl->setSuperClassLoc(SuperLoc);
186 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000187 }
Chris Lattner4d391482007-12-12 07:09:47 +0000188 } else { // we have a root class.
189 IDecl->setLocEnd(ClassLoc);
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Sebastian Redl0b17c612010-08-13 00:28:03 +0000192 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000193 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000194 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000195 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000196 IDecl->setLocEnd(EndProtoLoc);
197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Anders Carlsson15281452008-11-04 16:57:32 +0000199 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000200 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000201}
202
203/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000204/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000205Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
206 IdentifierInfo *AliasName,
207 SourceLocation AliasLocation,
208 IdentifierInfo *ClassName,
209 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000210 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000211 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000212 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000213 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000214 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000215 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000216 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000217 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000218 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000219 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000220 }
221 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000222 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000223 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000224 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
225 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000226 if (T->isObjCObjectType()) {
227 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000228 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000229 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000230 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000231 }
232 }
233 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000234 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
235 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000236 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000237 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000238 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000239 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000242 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000243 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000244 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Anders Carlsson15281452008-11-04 16:57:32 +0000246 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000247 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000248
John McCalld226f652010-08-21 09:40:31 +0000249 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000250}
251
Steve Naroff61d68522009-03-05 15:22:01 +0000252void Sema::CheckForwardProtocolDeclarationForCircularDependency(
253 IdentifierInfo *PName,
254 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000255 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000256 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
257 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Douglas Gregorc83c6872010-04-15 22:33:43 +0000259 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
260 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000261 if (PDecl->getIdentifier() == PName) {
262 Diag(Ploc, diag::err_protocol_has_circular_dependency);
263 Diag(PrevLoc, diag::note_previous_definition);
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000266 PDecl->getLocation(), PDecl->getReferencedProtocols());
267 }
268 }
269}
270
John McCalld226f652010-08-21 09:40:31 +0000271Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000272Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
273 IdentifierInfo *ProtocolName,
274 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000275 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000276 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000277 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000278 SourceLocation EndProtoLoc,
279 AttributeList *AttrList) {
280 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000281 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000282 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000283 if (PDecl) {
284 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000285 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000286 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000287 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000288 // Just return the protocol we already had.
289 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000290 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000291 }
Steve Naroff61d68522009-03-05 15:22:01 +0000292 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000293 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000294 CheckForwardProtocolDeclarationForCircularDependency(
295 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Steve Narofff11b5082008-08-13 16:39:22 +0000297 // Make sure the cached decl gets a valid start location.
298 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000299 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000300 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000301 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000302 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000303 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000304 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000305 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000306 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000307 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000308 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000309 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000310 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000312 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000313 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
314 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000315 PDecl->setLocEnd(EndProtoLoc);
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
318 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000319 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000320}
321
322/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000323/// issues an error if they are not declared. It returns list of
324/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000325void
Chris Lattnere13b9592008-07-26 04:03:38 +0000326Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000327 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000328 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000329 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000330 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000331 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
332 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000333 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000334 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
335 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000336 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000337 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
338 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
339 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000340 Diag(PDecl->getLocation(), diag::note_previous_decl)
341 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000342 }
343 }
344
345 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000346 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000347 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000348 continue;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000351 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000352
353 // If this is a forward declaration and we are supposed to warn in this
354 // case, do it.
355 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000357 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000358 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000359 }
360}
361
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000362/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000363/// a class method in its extension.
364///
Mike Stump1eb44332009-09-09 15:08:12 +0000365void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000366 ObjCInterfaceDecl *ID) {
367 if (!ID)
368 return; // Possibly due to previous error
369
370 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000371 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
372 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000373 ObjCMethodDecl *MD = *i;
374 MethodMap[MD->getSelector()] = MD;
375 }
376
377 if (MethodMap.empty())
378 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000379 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
380 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000381 ObjCMethodDecl *Method = *i;
382 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
383 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
384 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
385 << Method->getDeclName();
386 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
387 }
388 }
389}
390
Chris Lattner58fe03b2009-04-12 08:43:13 +0000391/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000392Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000393Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000394 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000395 unsigned NumElts,
396 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000397 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000398 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner4d391482007-12-12 07:09:47 +0000400 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000401 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000402 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000403 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000404 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000405 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000406 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000407 PushOnScopeChains(PDecl, TUScope, false);
408 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000409 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000410 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000411 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000412 if (!isNew)
413 PDecl->setChangedSinceDeserialization(true);
414 }
Chris Lattner4d391482007-12-12 07:09:47 +0000415 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000416 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
419 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000420 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000421 Protocols.data(), Protocols.size(),
422 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000423 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000424 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000425 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000426}
427
John McCalld226f652010-08-21 09:40:31 +0000428Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000429ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
430 IdentifierInfo *ClassName, SourceLocation ClassLoc,
431 IdentifierInfo *CategoryName,
432 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000433 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000434 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000435 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000436 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000437 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000438 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000439
440 /// Check that class of this category is already completely declared.
441 if (!IDecl || IDecl->isForwardDecl()) {
442 // Create an invalid ObjCCategoryDecl to serve as context for
443 // the enclosing method declarations. We mark the decl invalid
444 // to make it clear that this isn't a valid AST.
445 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
446 ClassLoc, CategoryLoc, CategoryName);
447 CDecl->setInvalidDecl();
448 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000449 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000450 }
451
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000452 if (!CategoryName && IDecl->getImplementation()) {
453 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
454 Diag(IDecl->getImplementation()->getLocation(),
455 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000456 }
457
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000458 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
459 ClassLoc, CategoryLoc, CategoryName);
460 // FIXME: PushOnScopeChains?
461 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000462
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000463 CDecl->setClassInterface(IDecl);
464 // Insert class extension to the list of class's categories.
465 if (!CategoryName)
466 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner16b34b42009-02-16 21:30:01 +0000468 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000469 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000470
Fariborz Jahanian25760612010-02-15 21:55:26 +0000471 if (CategoryName) {
472 /// Check for duplicate interface declaration for this category
473 ObjCCategoryDecl *CDeclChain;
474 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
475 CDeclChain = CDeclChain->getNextClassCategory()) {
476 if (CDeclChain->getIdentifier() == CategoryName) {
477 // Class extensions can be declared multiple times.
478 Diag(CategoryLoc, diag::warn_dup_category_def)
479 << ClassName << CategoryName;
480 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
481 break;
482 }
Chris Lattner70f19542009-02-16 21:26:43 +0000483 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000484 if (!CDeclChain)
485 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000486 }
Chris Lattner70f19542009-02-16 21:26:43 +0000487
Chris Lattner4d391482007-12-12 07:09:47 +0000488 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000489 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000490 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000491 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000492 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000493 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000494 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Anders Carlsson15281452008-11-04 16:57:32 +0000497 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000498 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000499}
500
501/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000502/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000503/// object.
John McCalld226f652010-08-21 09:40:31 +0000504Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000505 SourceLocation AtCatImplLoc,
506 IdentifierInfo *ClassName, SourceLocation ClassLoc,
507 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000508 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000509 ObjCCategoryDecl *CatIDecl = 0;
510 if (IDecl) {
511 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
512 if (!CatIDecl) {
513 // Category @implementation with no corresponding @interface.
514 // Create and install one.
515 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000516 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000517 CatName);
518 CatIDecl->setClassInterface(IDecl);
519 CatIDecl->insertNextClassCategory();
520 }
521 }
522
Mike Stump1eb44332009-09-09 15:08:12 +0000523 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000524 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
525 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 /// Check that class of this category is already completely declared.
527 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000528 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000529
Douglas Gregord0434102009-01-09 00:49:46 +0000530 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000531 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000532
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000533 /// Check that CatName, category name, is not used in another implementation.
534 if (CatIDecl) {
535 if (CatIDecl->getImplementation()) {
536 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
537 << CatName;
538 Diag(CatIDecl->getImplementation()->getLocation(),
539 diag::note_previous_definition);
540 } else
541 CatIDecl->setImplementation(CDecl);
542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Anders Carlsson15281452008-11-04 16:57:32 +0000544 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000545 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000546}
547
John McCalld226f652010-08-21 09:40:31 +0000548Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000549 SourceLocation AtClassImplLoc,
550 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000551 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000552 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000553 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000554 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000555 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000556 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
557 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000558 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000559 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000560 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000561 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
562 // If this is a forward declaration of an interface, warn.
563 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000564 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000565 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000566 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000567 } else {
568 // We did not find anything with the name ClassName; try to correct for
569 // typos in the class name.
570 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000571 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000572 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000573 // Suggest the (potentially) correct interface name. However, put the
574 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000575 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000576 // provide a code-modification hint or use the typo name for recovery,
577 // because this is just a warning. The program may actually be correct.
578 Diag(ClassLoc, diag::warn_undef_interface_suggest)
579 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000580 Diag(IDecl->getLocation(), diag::note_previous_decl)
581 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000582 << FixItHint::CreateReplacement(ClassLoc,
583 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000584 IDecl = 0;
585 } else {
586 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
587 }
Chris Lattner4d391482007-12-12 07:09:47 +0000588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner4d391482007-12-12 07:09:47 +0000590 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000592 if (SuperClassname) {
593 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000594 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
595 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000596 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000597 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
598 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000599 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000600 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000601 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000603 Diag(SuperClassLoc, diag::err_undef_superclass)
604 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000605 else if (IDecl && IDecl->getSuperClass() != SDecl) {
606 // This implementation and its interface do not have the same
607 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000608 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000609 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000610 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000611 }
612 }
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattner4d391482007-12-12 07:09:47 +0000615 if (!IDecl) {
616 // Legacy case of @implementation with no corresponding @interface.
617 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000618
Mike Stump390b4cc2009-05-16 07:39:55 +0000619 // FIXME: Do we support attributes on the @implementation? If so we should
620 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000621 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000622 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000623 IDecl->setSuperClass(SDecl);
624 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000625
626 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000627 } else {
628 // Mark the interface as being completed, even if it was just as
629 // @class ....;
630 // declaration; the user cannot reopen it.
631 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 ObjCImplementationDecl* IMPDecl =
635 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000636 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson15281452008-11-04 16:57:32 +0000638 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000639 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Chris Lattner4d391482007-12-12 07:09:47 +0000641 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000642 if (IDecl->getImplementation()) {
643 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000644 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000645 Diag(IDecl->getImplementation()->getLocation(),
646 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000647 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000648 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000649 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000650 }
John McCalld226f652010-08-21 09:40:31 +0000651 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000652}
653
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
655 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000656 SourceLocation RBrace) {
657 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000658 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000659 if (!IDecl)
660 return;
661 /// Check case of non-existing @interface decl.
662 /// (legacy objective-c @implementation decl without an @interface decl).
663 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000664 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000665 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000666 // Add ivar's to class's DeclContext.
667 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000668 ivars[i]->setLexicalDeclContext(ImpDecl);
669 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000670 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000671 }
672
Chris Lattner4d391482007-12-12 07:09:47 +0000673 return;
674 }
675 // If implementation has empty ivar list, just return.
676 if (numIvars == 0)
677 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattner4d391482007-12-12 07:09:47 +0000679 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000680 if (LangOpts.ObjCNonFragileABI2) {
681 if (ImpDecl->getSuperClass())
682 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
683 for (unsigned i = 0; i < numIvars; i++) {
684 ObjCIvarDecl* ImplIvar = ivars[i];
685 if (const ObjCIvarDecl *ClsIvar =
686 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
687 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
688 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
689 continue;
690 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000691 // Instance ivar to Implementation's DeclContext.
692 ImplIvar->setLexicalDeclContext(ImpDecl);
693 IDecl->makeDeclVisibleInContext(ImplIvar, false);
694 ImpDecl->addDecl(ImplIvar);
695 }
696 return;
697 }
Chris Lattner4d391482007-12-12 07:09:47 +0000698 // Check interface's Ivar list against those in the implementation.
699 // names and types must match.
700 //
Chris Lattner4d391482007-12-12 07:09:47 +0000701 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000702 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000703 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
704 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000705 ObjCIvarDecl* ImplIvar = ivars[j++];
706 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000707 assert (ImplIvar && "missing implementation ivar");
708 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Steve Naroffca331292009-03-03 14:49:36 +0000710 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000711 if (Context.getCanonicalType(ImplIvar->getType()) !=
712 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000713 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000714 << ImplIvar->getIdentifier()
715 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000716 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000717 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
718 Expr *ImplBitWidth = ImplIvar->getBitWidth();
719 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000720 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
721 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000722 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
723 << ImplIvar->getIdentifier();
724 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726 }
Steve Naroffca331292009-03-03 14:49:36 +0000727 // Make sure the names are identical.
728 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000729 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000730 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000731 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000732 }
733 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattner609e4c72007-12-12 18:11:49 +0000736 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000737 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000738 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000739 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000740}
741
Steve Naroff3c2eb662008-02-10 21:38:56 +0000742void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000743 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000744 if (!IncompleteImpl) {
745 Diag(ImpLoc, diag::warn_incomplete_impl);
746 IncompleteImpl = true;
747 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000748 if (DiagID == diag::warn_unimplemented_protocol_method)
749 Diag(ImpLoc, DiagID) << method->getDeclName();
750 else
751 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000752}
753
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000754/// Determines if type B can be substituted for type A. Returns true if we can
755/// guarantee that anything that the user will do to an object of type A can
756/// also be done to an object of type B. This is trivially true if the two
757/// types are the same, or if B is a subclass of A. It becomes more complex
758/// in cases where protocols are involved.
759///
760/// Object types in Objective-C describe the minimum requirements for an
761/// object, rather than providing a complete description of a type. For
762/// example, if A is a subclass of B, then B* may refer to an instance of A.
763/// The principle of substitutability means that we may use an instance of A
764/// anywhere that we may use an instance of B - it will implement all of the
765/// ivars of B and all of the methods of B.
766///
767/// This substitutability is important when type checking methods, because
768/// the implementation may have stricter type definitions than the interface.
769/// The interface specifies minimum requirements, but the implementation may
770/// have more accurate ones. For example, a method may privately accept
771/// instances of B, but only publish that it accepts instances of A. Any
772/// object passed to it will be type checked against B, and so will implicitly
773/// by a valid A*. Similarly, a method may return a subclass of the class that
774/// it is declared as returning.
775///
776/// This is most important when considering subclassing. A method in a
777/// subclass must accept any object as an argument that its superclass's
778/// implementation accepts. It may, however, accept a more general type
779/// without breaking substitutability (i.e. you can still use the subclass
780/// anywhere that you can use the superclass, but not vice versa). The
781/// converse requirement applies to return types: the return type for a
782/// subclass method must be a valid object of the kind that the superclass
783/// advertises, but it may be specified more accurately. This avoids the need
784/// for explicit down-casting by callers.
785///
786/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000787static bool isObjCTypeSubstitutable(ASTContext &Context,
788 const ObjCObjectPointerType *A,
789 const ObjCObjectPointerType *B,
790 bool rejectId) {
791 // Reject a protocol-unqualified id.
792 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000793
794 // If B is a qualified id, then A must also be a qualified id and it must
795 // implement all of the protocols in B. It may not be a qualified class.
796 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
797 // stricter definition so it is not substitutable for id<A>.
798 if (B->isObjCQualifiedIdType()) {
799 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000800 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
801 QualType(B,0),
802 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000803 }
804
805 /*
806 // id is a special type that bypasses type checking completely. We want a
807 // warning when it is used in one place but not another.
808 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
809
810
811 // If B is a qualified id, then A must also be a qualified id (which it isn't
812 // if we've got this far)
813 if (B->isObjCQualifiedIdType()) return false;
814 */
815
816 // Now we know that A and B are (potentially-qualified) class types. The
817 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000818 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000819}
820
John McCall10302c02010-10-28 02:34:38 +0000821static SourceRange getTypeRange(TypeSourceInfo *TSI) {
822 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
823}
824
825static void CheckMethodOverrideReturn(Sema &S,
826 ObjCMethodDecl *MethodImpl,
827 ObjCMethodDecl *MethodIface) {
828 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
829 MethodIface->getResultType()))
830 return;
831
832 unsigned DiagID = diag::warn_conflicting_ret_types;
833
834 // Mismatches between ObjC pointers go into a different warning
835 // category, and sometimes they're even completely whitelisted.
836 if (const ObjCObjectPointerType *ImplPtrTy =
837 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
838 if (const ObjCObjectPointerType *IfacePtrTy =
839 MethodIface->getResultType()->getAs<ObjCObjectPointerType>()) {
840 // Allow non-matching return types as long as they don't violate
841 // the principle of substitutability. Specifically, we permit
842 // return types that are subclasses of the declared return type,
843 // or that are more-qualified versions of the declared type.
844 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
845 return;
846
847 DiagID = diag::warn_non_covariant_ret_types;
848 }
849 }
850
851 S.Diag(MethodImpl->getLocation(), DiagID)
852 << MethodImpl->getDeclName()
853 << MethodIface->getResultType()
854 << MethodImpl->getResultType()
855 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
856 S.Diag(MethodIface->getLocation(), diag::note_previous_definition)
857 << getTypeRange(MethodIface->getResultTypeSourceInfo());
858}
859
860static void CheckMethodOverrideParam(Sema &S,
861 ObjCMethodDecl *MethodImpl,
862 ObjCMethodDecl *MethodIface,
863 ParmVarDecl *ImplVar,
864 ParmVarDecl *IfaceVar) {
865 QualType ImplTy = ImplVar->getType();
866 QualType IfaceTy = IfaceVar->getType();
867 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
868 return;
869
870 unsigned DiagID = diag::warn_conflicting_param_types;
871
872 // Mismatches between ObjC pointers go into a different warning
873 // category, and sometimes they're even completely whitelisted.
874 if (const ObjCObjectPointerType *ImplPtrTy =
875 ImplTy->getAs<ObjCObjectPointerType>()) {
876 if (const ObjCObjectPointerType *IfacePtrTy =
877 IfaceTy->getAs<ObjCObjectPointerType>()) {
878 // Allow non-matching argument types as long as they don't
879 // violate the principle of substitutability. Specifically, the
880 // implementation must accept any objects that the superclass
881 // accepts, however it may also accept others.
882 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
883 return;
884
885 DiagID = diag::warn_non_contravariant_param_types;
886 }
887 }
888
889 S.Diag(ImplVar->getLocation(), DiagID)
890 << getTypeRange(ImplVar->getTypeSourceInfo())
891 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
892 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
893 << getTypeRange(IfaceVar->getTypeSourceInfo());
894}
895
896
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000897void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
898 ObjCMethodDecl *IntfMethodDecl) {
John McCall10302c02010-10-28 02:34:38 +0000899 CheckMethodOverrideReturn(*this, ImpMethodDecl, IntfMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner3aff9192009-04-11 19:58:42 +0000901 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
902 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +0000903 IM != EM; ++IM, ++IF)
904 CheckMethodOverrideParam(*this, ImpMethodDecl, IntfMethodDecl, *IM, *IF);
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000906 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
907 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
908 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
909 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000910}
911
Mike Stump390b4cc2009-05-16 07:39:55 +0000912/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
913/// improve the efficiency of selector lookups and type checking by associating
914/// with each protocol / interface / category the flattened instance tables. If
915/// we used an immutable set to keep the table then it wouldn't add significant
916/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000917
Steve Naroffefe7f362008-02-08 22:06:17 +0000918/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000919/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000920void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
921 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000922 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000923 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000924 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000925 ObjCContainerDecl *CDecl) {
926 ObjCInterfaceDecl *IDecl;
927 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
928 IDecl = C->getClassInterface();
929 else
930 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
931 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
932
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000933 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000934 ObjCInterfaceDecl *NSIDecl = 0;
935 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000936 // check to see if class implements forwardInvocation method and objects
937 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000938 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000939 // Under such conditions, which means that every method possible is
940 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000941 // found" warnings.
942 // FIXME: Use a general GetUnarySelector method for this.
943 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
944 Selector fISelector = Context.Selectors.getSelector(1, &II);
945 if (InsMap.count(fISelector))
946 // Is IDecl derived from 'NSProxy'? If so, no instance methods
947 // need be implemented in the implementation.
948 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000951 // If a method lookup fails locally we still need to look and see if
952 // the method was implemented by a base class or an inherited
953 // protocol. This lookup is slow, but occurs rarely in correct code
954 // and otherwise would terminate in a warning.
955
Chris Lattner4d391482007-12-12 07:09:47 +0000956 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000957 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000958 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000959 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000960 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000961 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000962 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000963 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000964 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000965 // Ugly, but necessary. Method declared in protcol might have
966 // have been synthesized due to a property declared in the class which
967 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000968 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000969 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000970 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000971 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000972 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
973 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000974 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000975 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +0000976 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
977 << PDecl->getDeclName();
978 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000979 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000980 }
981 }
Chris Lattner4d391482007-12-12 07:09:47 +0000982 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000983 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000985 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000987 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
988 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000989 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000990 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000991 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000992 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000993 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +0000994 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
995 PDecl->getDeclName();
996 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000997 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000998 }
Chris Lattner780f3292008-07-21 21:32:27 +0000999 // Check on this protocols's referenced protocols, recursively.
1000 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1001 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001002 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001003}
1004
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001005/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1006/// or protocol against those declared in their implementations.
1007///
1008void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1009 const llvm::DenseSet<Selector> &ClsMap,
1010 llvm::DenseSet<Selector> &InsMapSeen,
1011 llvm::DenseSet<Selector> &ClsMapSeen,
1012 ObjCImplDecl* IMPDecl,
1013 ObjCContainerDecl* CDecl,
1014 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001015 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001016 // Check and see if instance methods in class interface have been
1017 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001018 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1019 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001020 if (InsMapSeen.count((*I)->getSelector()))
1021 continue;
1022 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001023 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001024 !InsMap.count((*I)->getSelector())) {
1025 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001026 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1027 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001028 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001029 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001030 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001031 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001032 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001034 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001035 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1036 // ImpMethodDecl may be null as in a @dynamic property.
1037 if (ImpMethodDecl)
1038 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1039 }
1040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001042 // Check and see if class methods in class interface have been
1043 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001044 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001045 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001046 if (ClsMapSeen.count((*I)->getSelector()))
1047 continue;
1048 ClsMapSeen.insert((*I)->getSelector());
1049 if (!ClsMap.count((*I)->getSelector())) {
1050 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001051 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1052 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001053 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 ObjCMethodDecl *ImpMethodDecl =
1055 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001056 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001057 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001058 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1059 }
1060 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001061
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001062 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001063 // Also methods in class extensions need be looked at next.
1064 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1065 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1066 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1067 IMPDecl,
1068 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1069 IncompleteImpl, false);
1070
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001071 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001072 for (ObjCInterfaceDecl::all_protocol_iterator
1073 PI = I->all_referenced_protocol_begin(),
1074 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001075 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1076 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001077 (*PI), IncompleteImpl, false);
1078 if (I->getSuperClass())
1079 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001080 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001081 I->getSuperClass(), IncompleteImpl, false);
1082 }
1083}
1084
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001085void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001086 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001087 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001088 llvm::DenseSet<Selector> InsMap;
1089 // Check and see if instance methods in class interface have been
1090 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001091 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001092 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001093 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001095 // Check and see if properties declared in the interface have either 1)
1096 // an implementation or 2) there is a @synthesize/@dynamic implementation
1097 // of the property in the @implementation.
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001098 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001099 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001100
Chris Lattner4d391482007-12-12 07:09:47 +00001101 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001102 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001103 I = IMPDecl->classmeth_begin(),
1104 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001105 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001107 // Check for type conflict of methods declared in a class/protocol and
1108 // its implementation; if any.
1109 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001110 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1111 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001112 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chris Lattner4d391482007-12-12 07:09:47 +00001114 // Check the protocol list for unimplemented methods in the @implementation
1115 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001116 // Check and see if class methods in class interface have been
1117 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattnercddc8882009-03-01 00:56:52 +00001119 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001120 for (ObjCInterfaceDecl::all_protocol_iterator
1121 PI = I->all_referenced_protocol_begin(),
1122 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001123 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001124 InsMap, ClsMap, I);
1125 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001126 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1127 Categories; Categories = Categories->getNextClassExtension())
1128 ImplMethodsVsClassMethods(S, IMPDecl,
1129 const_cast<ObjCCategoryDecl*>(Categories),
1130 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001131 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001132 // For extended class, unimplemented methods in its protocols will
1133 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001134 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001135 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1136 E = C->protocol_end(); PI != E; ++PI)
1137 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001138 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001139 // Report unimplemented properties in the category as well.
1140 // When reporting on missing setter/getters, do not report when
1141 // setter/getter is implemented in category's primary class
1142 // implementation.
1143 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1144 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1145 for (ObjCImplementationDecl::instmeth_iterator
1146 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1147 InsMap.insert((*I)->getSelector());
1148 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001149 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001150 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001151 } else
1152 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001153}
1154
Mike Stump1eb44332009-09-09 15:08:12 +00001155/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001156Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001157Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001158 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001159 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001160 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001161 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Chris Lattner4d391482007-12-12 07:09:47 +00001163 for (unsigned i = 0; i != NumElts; ++i) {
1164 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001165 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001166 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001167 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001168 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001169 // Maybe we will complain about the shadowed template parameter.
1170 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1171 // Just pretend that we didn't see the previous declaration.
1172 PrevDecl = 0;
1173 }
1174
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001175 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001176 // GCC apparently allows the following idiom:
1177 //
1178 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1179 // @class XCElementToggler;
1180 //
Mike Stump1eb44332009-09-09 15:08:12 +00001181 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001182 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001183 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001184 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001185 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001186 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001187 // a forward class declaration matching a typedef name of a class refers
1188 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001189 if (const ObjCObjectType *OI =
1190 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1191 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001192 }
Chris Lattner4d391482007-12-12 07:09:47 +00001193 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001194 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1195 if (!IDecl) { // Not already seen? Make a forward decl.
1196 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1197 IdentList[i], IdentLocs[i], true);
1198
1199 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1200 // the current DeclContext. This prevents clients that walk DeclContext
1201 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1202 // declared later (if at all). We also take care to explicitly make
1203 // sure this declaration is visible for name lookup.
1204 PushOnScopeChains(IDecl, TUScope, false);
1205 CurContext->makeDeclVisibleInContext(IDecl, true);
1206 }
Chris Lattner4d391482007-12-12 07:09:47 +00001207
1208 Interfaces.push_back(IDecl);
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek321c22f2009-11-18 00:28:11 +00001211 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001212 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001213 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001214 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001215 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001216 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001217 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001218}
1219
1220
1221/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1222/// returns true, or false, accordingly.
1223/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001224bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001225 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001226 bool matchBasedOnSizeAndAlignment,
1227 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001228 QualType T1 = Context.getCanonicalType(Method->getResultType());
1229 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001231 if (T1 != T2) {
1232 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001233 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001234 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001235 // Incomplete types don't have a size and alignment.
1236 if (T1->isIncompleteType() || T2->isIncompleteType())
1237 return false;
1238 // Check is based on size and alignment.
1239 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1240 return false;
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Chris Lattner89951a82009-02-20 18:43:26 +00001243 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1244 E = Method->param_end();
1245 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Chris Lattner89951a82009-02-20 18:43:26 +00001247 for (; ParamI != E; ++ParamI, ++PrevI) {
1248 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1249 T1 = Context.getCanonicalType((*ParamI)->getType());
1250 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001251 if (T1 != T2) {
1252 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001253 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001254 return false;
1255 // Incomplete types don't have a size and alignment.
1256 if (T1->isIncompleteType() || T2->isIncompleteType())
1257 return false;
1258 // Check is based on size and alignment.
1259 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1260 return false;
1261 }
Chris Lattner4d391482007-12-12 07:09:47 +00001262 }
1263 return true;
1264}
1265
Sebastian Redldb9d2142010-08-02 23:18:59 +00001266/// \brief Read the contents of the method pool for a given selector from
1267/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001268///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001269/// This routine should only be called once, when the method pool has no entry
1270/// for this selector.
1271Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001272 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001273 assert(MethodPool.find(Sel) == MethodPool.end() &&
1274 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001275
1276 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001277 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Sebastian Redldb9d2142010-08-02 23:18:59 +00001279 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001280}
1281
Sebastian Redldb9d2142010-08-02 23:18:59 +00001282void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1283 bool instance) {
1284 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1285 if (Pos == MethodPool.end()) {
1286 if (ExternalSource)
1287 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001288 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001289 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1290 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001291 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001292 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001293 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001294 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001295 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001296 Entry.Method = Method;
1297 Entry.Next = 0;
1298 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Chris Lattnerb25df352009-03-04 05:16:45 +00001301 // We've seen a method with this name, see if we have already seen this type
1302 // signature.
1303 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001304 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001305 ObjCMethodDecl *PrevObjCMethod = List->Method;
1306 PrevObjCMethod->setDefined(impl);
1307 // If a method is deprecated, push it in the global pool.
1308 // This is used for better diagnostics.
1309 if (Method->getAttr<DeprecatedAttr>()) {
1310 if (!PrevObjCMethod->getAttr<DeprecatedAttr>())
1311 List->Method = Method;
1312 }
1313 // If new method is unavailable, push it into global pool
1314 // unless previous one is deprecated.
1315 if (Method->getAttr<UnavailableAttr>()) {
1316 if (!PrevObjCMethod->getAttr<UnavailableAttr>() &&
1317 !PrevObjCMethod->getAttr<DeprecatedAttr>())
1318 List->Method = Method;
1319 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001320 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Chris Lattnerb25df352009-03-04 05:16:45 +00001323 // We have a new signature for an existing method - add it.
1324 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001325 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1326 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001327}
1328
Sebastian Redldb9d2142010-08-02 23:18:59 +00001329ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001330 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001331 bool warn, bool instance) {
1332 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1333 if (Pos == MethodPool.end()) {
1334 if (ExternalSource)
1335 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001336 else
1337 return 0;
1338 }
1339
Sebastian Redldb9d2142010-08-02 23:18:59 +00001340 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001342 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001343 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1344 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001345 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001346 if (warn && MethList.Method && MethList.Next) {
1347 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001348 if (strictSelectorMatch)
1349 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1350 // This checks if the methods differ in type mismatch.
1351 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1352 issueWarning = true;
1353 }
1354
1355 if (!issueWarning)
1356 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1357 // This checks if the methods differ by size & alignment.
1358 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1359 issueWarning = true;
1360 }
1361
Sebastian Redldb9d2142010-08-02 23:18:59 +00001362 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001363 if (strictSelectorMatch)
1364 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1365 else
1366 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001367 Diag(MethList.Method->getLocStart(), diag::note_using)
1368 << MethList.Method->getSourceRange();
1369 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1370 Diag(Next->Method->getLocStart(), diag::note_also_found)
1371 << Next->Method->getSourceRange();
1372 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001373 }
1374 return MethList.Method;
1375}
1376
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001377ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001378 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1379 if (Pos == MethodPool.end())
1380 return 0;
1381
1382 GlobalMethods &Methods = Pos->second;
1383
1384 if (Methods.first.Method && Methods.first.Method->isDefined())
1385 return Methods.first.Method;
1386 if (Methods.second.Method && Methods.second.Method->isDefined())
1387 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001388 return 0;
1389}
1390
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001391/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1392/// identical selector names in current and its super classes and issues
1393/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001394void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1395 ObjCMethodDecl *Method,
1396 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001397 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1398 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001400 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001401 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001402 SD->lookupMethod(Method->getSelector(), IsInstance);
1403 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001404 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001405 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001406 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001407 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1408 E = Method->param_end();
1409 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1410 for (; ParamI != E; ++ParamI, ++PrevI) {
1411 // Number of parameters are the same and is guaranteed by selector match.
1412 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1413 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1414 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1415 // If type of arguement of method in this class does not match its
1416 // respective argument type in the super class method, issue warning;
1417 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001419 << T1 << T2;
1420 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1421 return;
1422 }
1423 }
1424 ID = SD;
1425 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001426}
1427
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001428/// DiagnoseDuplicateIvars -
1429/// Check for duplicate ivars in the entire class at the start of
1430/// @implementation. This becomes necesssary because class extension can
1431/// add ivars to a class in random order which will not be known until
1432/// class's @implementation is seen.
1433void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1434 ObjCInterfaceDecl *SID) {
1435 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1436 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1437 ObjCIvarDecl* Ivar = (*IVI);
1438 if (Ivar->isInvalidDecl())
1439 continue;
1440 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1441 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1442 if (prevIvar) {
1443 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1444 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1445 Ivar->setInvalidDecl();
1446 }
1447 }
1448 }
1449}
1450
Steve Naroffa56f6162007-12-18 01:30:32 +00001451// Note: For class/category implemenations, allMethods/allProperties is
1452// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001453void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001454 Decl *ClassDecl,
1455 Decl **allMethods, unsigned allNum,
1456 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001457 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001458 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1459 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001460 // should be true.
1461 if (!ClassDecl)
1462 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001463
Mike Stump1eb44332009-09-09 15:08:12 +00001464 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001465 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1466 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001467 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001468
Ted Kremenek782f2f52010-01-07 01:20:12 +00001469 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1470 // FIXME: This is wrong. We shouldn't be pretending that there is
1471 // an '@end' in the declaration.
1472 SourceLocation L = ClassDecl->getLocation();
1473 AtEnd.setBegin(L);
1474 AtEnd.setEnd(L);
1475 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001476 }
1477
Steve Naroff0701bbb2009-01-08 17:28:14 +00001478 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001479
1480 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1481 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1482 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1483
Chris Lattner4d391482007-12-12 07:09:47 +00001484 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001486 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001487
1488 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001489 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001490 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001492 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001493 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001494 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001495 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001496 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001497 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001498 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001499 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001500 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001501 InsMap[Method->getSelector()] = Method;
1502 /// The following allows us to typecheck messages to "id".
1503 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001504 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001505 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001506 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001507 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001508 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001509 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001511 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001512 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001513 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001514 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001515 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001516 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001517 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001518 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001519 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001520 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001521 /// The following allows us to typecheck messages to "Class".
1522 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001523 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001524 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001525 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001526 }
1527 }
1528 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001529 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001530 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001531 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001532 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001533 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001534 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001535 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001537 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001538
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001539 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001540 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001541 if (C->IsClassExtension()) {
1542 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1543 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001544 }
Chris Lattner4d391482007-12-12 07:09:47 +00001545 }
Steve Naroff09c47192009-01-09 15:36:25 +00001546 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001547 if (CDecl->getIdentifier())
1548 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1549 // user-defined setter/getter. It also synthesizes setter/getter methods
1550 // and adds them to the DeclContext and global method pools.
1551 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1552 E = CDecl->prop_end();
1553 I != E; ++I)
1554 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001555 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001556 }
1557 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001558 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001559 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001560 // Any property declared in a class extension might have user
1561 // declared setter or getter in current class extension or one
1562 // of the other class extensions. Mark them as synthesized as
1563 // property will be synthesized when property with same name is
1564 // seen in the @implementation.
1565 for (const ObjCCategoryDecl *ClsExtDecl =
1566 IDecl->getFirstClassExtension();
1567 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1568 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1569 E = ClsExtDecl->prop_end(); I != E; ++I) {
1570 ObjCPropertyDecl *Property = (*I);
1571 // Skip over properties declared @dynamic
1572 if (const ObjCPropertyImplDecl *PIDecl
1573 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1574 if (PIDecl->getPropertyImplementation()
1575 == ObjCPropertyImplDecl::Dynamic)
1576 continue;
1577
1578 for (const ObjCCategoryDecl *CExtDecl =
1579 IDecl->getFirstClassExtension();
1580 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1581 if (ObjCMethodDecl *GetterMethod =
1582 CExtDecl->getInstanceMethod(Property->getGetterName()))
1583 GetterMethod->setSynthesized(true);
1584 if (!Property->isReadOnly())
1585 if (ObjCMethodDecl *SetterMethod =
1586 CExtDecl->getInstanceMethod(Property->getSetterName()))
1587 SetterMethod->setSynthesized(true);
1588 }
1589 }
1590 }
1591
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001592 if (LangOpts.ObjCNonFragileABI2)
1593 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001594 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001595 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001596
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001597 if (LangOpts.ObjCNonFragileABI2)
1598 while (IDecl->getSuperClass()) {
1599 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1600 IDecl = IDecl->getSuperClass();
1601 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001602 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001603 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001604 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001605 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001606 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Chris Lattner4d391482007-12-12 07:09:47 +00001608 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001609 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001610 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001612 Categories; Categories = Categories->getNextClassCategory()) {
1613 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001614 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001615 break;
1616 }
1617 }
1618 }
1619 }
Chris Lattner682bf922009-03-29 16:50:03 +00001620 if (isInterfaceDeclKind) {
1621 // Reject invalid vardecls.
1622 for (unsigned i = 0; i != tuvNum; i++) {
1623 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1624 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1625 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001626 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001627 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001628 }
Chris Lattner682bf922009-03-29 16:50:03 +00001629 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001630 }
Chris Lattner4d391482007-12-12 07:09:47 +00001631}
1632
1633
1634/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1635/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001636static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001637CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1638 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1639 if (PQTVal & ObjCDeclSpec::DQ_In)
1640 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1641 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1642 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1643 if (PQTVal & ObjCDeclSpec::DQ_Out)
1644 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1645 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1646 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1647 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1648 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1649 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1650 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001651
1652 return ret;
1653}
1654
Ted Kremenek422bae72010-04-18 04:59:38 +00001655static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001656bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001657 // The 'ibaction' attribute is allowed on method definitions because of
1658 // how the IBAction macro is used on both method declarations and definitions.
1659 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001660 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1661 if ((*i)->getKind() != attr::IBAction)
1662 return true;
1663 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001664}
1665
John McCalld226f652010-08-21 09:40:31 +00001666Decl *Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001667 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001668 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001669 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001670 Selector Sel,
1671 // optional arguments. The number of types/arguments is obtained
1672 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001673 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001674 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001675 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1676 bool isVariadic) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001677 // Make sure we can establish a context for the method.
1678 if (!ClassDecl) {
1679 Diag(MethodLoc, diag::error_missing_method_context);
John McCall781472f2010-08-25 08:40:02 +00001680 getCurFunction()->LabelMap.clear();
John McCalld226f652010-08-21 09:40:31 +00001681 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001682 }
Chris Lattner4d391482007-12-12 07:09:47 +00001683 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001685 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001686 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001687 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Steve Naroffccef3712009-02-20 22:59:16 +00001689 // Methods cannot return interface types. All ObjC objects are
1690 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001691 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001692 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1693 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001694 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001695 }
1696 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001697 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001698
1699 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001700 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001701 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001702 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001703 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001704 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001705 MethodDeclKind == tok::objc_optional ?
1706 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001707 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Chris Lattner0ed844b2008-04-04 06:12:32 +00001709 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Chris Lattner7db638d2009-04-11 19:42:43 +00001711 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001712 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001713 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Chris Lattnere294d3f2009-04-11 18:57:04 +00001715 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001716 ArgType = Context.getObjCIdType();
1717 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001718 } else {
John McCall58e46772009-10-23 21:48:59 +00001719 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001720 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001721 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001722 }
Mike Stump1eb44332009-09-09 15:08:12 +00001723
John McCall58e46772009-10-23 21:48:59 +00001724 ParmVarDecl* Param
1725 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1726 ArgInfo[i].Name, ArgType, DI,
John McCalld931b082010-08-26 03:08:43 +00001727 SC_None, SC_None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
John McCallc12c5bb2010-05-15 11:32:37 +00001729 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001730 Diag(ArgInfo[i].NameLoc,
1731 diag::err_object_cannot_be_passed_returned_by_value)
1732 << 1 << ArgType;
1733 Param->setInvalidDecl();
1734 }
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Chris Lattner0ed844b2008-04-04 06:12:32 +00001736 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001737 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001739 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001740 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Chris Lattner0ed844b2008-04-04 06:12:32 +00001742 Params.push_back(Param);
1743 }
1744
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001745 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001746 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001747 QualType ArgType = Param->getType();
1748 if (ArgType.isNull())
1749 ArgType = Context.getObjCIdType();
1750 else
1751 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1752 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001753 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001754 Diag(Param->getLocation(),
1755 diag::err_object_cannot_be_passed_returned_by_value)
1756 << 1 << ArgType;
1757 Param->setInvalidDecl();
1758 }
1759 Param->setDeclContext(ObjCMethod);
Douglas Gregor59c451e2010-08-07 12:29:18 +00001760 if (Param->getDeclName())
1761 IdResolver.RemoveDecl(Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001762 Params.push_back(Param);
1763 }
1764
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001765 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1766 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001767 ObjCMethod->setObjCDeclQualifier(
1768 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1769 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001770
1771 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001772 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
John McCall54abf7d2009-11-04 02:18:39 +00001774 const ObjCMethodDecl *InterfaceMD = 0;
1775
Mike Stump1eb44332009-09-09 15:08:12 +00001776 // For implementations (which can be very "coarse grain"), we add the
1777 // method now. This allows the AST to implement lookup methods that work
1778 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001779 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001780 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001781 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001782 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001783 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1784 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001785 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001786 PrevMethod = ImpDecl->getClassMethod(Sel);
1787 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001788 }
John McCall54abf7d2009-11-04 02:18:39 +00001789 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1790 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001791 if (ObjCMethod->hasAttrs() &&
1792 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001793 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001794 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001795 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001796 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001797 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1798 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001799 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001800 PrevMethod = CatImpDecl->getClassMethod(Sel);
1801 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001802 }
Sean Huntcf807c42010-08-18 23:23:40 +00001803 if (ObjCMethod->hasAttrs() &&
1804 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001805 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001806 }
1807 if (PrevMethod) {
1808 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001809 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001810 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001811 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001812 }
John McCall54abf7d2009-11-04 02:18:39 +00001813
1814 // If the interface declared this method, and it was deprecated there,
1815 // mark it deprecated here.
Sean Huntcf807c42010-08-18 23:23:40 +00001816 if (InterfaceMD)
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001817 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>()) {
1818 StringLiteral *SE = StringLiteral::CreateEmpty(Context, 1);
1819 ObjCMethod->addAttr(::new (Context)
1820 DeprecatedAttr(DA->getLocation(),
1821 Context,
1822 SE->getString()));
1823 }
John McCall54abf7d2009-11-04 02:18:39 +00001824
John McCalld226f652010-08-21 09:40:31 +00001825 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001826}
1827
Chris Lattnercc98eac2008-12-17 07:13:27 +00001828bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001829 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001830 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Anders Carlsson15281452008-11-04 16:57:32 +00001832 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1833 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Anders Carlsson15281452008-11-04 16:57:32 +00001835 return true;
1836}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001837
Chris Lattnercc98eac2008-12-17 07:13:27 +00001838/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1839/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001840void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001841 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001842 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001843 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001844 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001845 if (!Class) {
1846 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1847 return;
1848 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001849 if (LangOpts.ObjCNonFragileABI) {
1850 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1851 return;
1852 }
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Chris Lattnercc98eac2008-12-17 07:13:27 +00001854 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001855 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1856 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001857 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001858 for (unsigned i = 0; i < Ivars.size(); i++) {
1859 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001860 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001861 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1862 ID->getIdentifier(), ID->getType(),
1863 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001864 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001865 }
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Chris Lattnercc98eac2008-12-17 07:13:27 +00001867 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001868 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001869 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001870 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001871 if (getLangOptions().CPlusPlus)
1872 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001873 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001874 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001875 }
1876}
1877
Douglas Gregor160b5632010-04-26 17:32:49 +00001878/// \brief Build a type-check a new Objective-C exception variable declaration.
1879VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1880 QualType T,
1881 IdentifierInfo *Name,
1882 SourceLocation NameLoc,
1883 bool Invalid) {
1884 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1885 // duration shall not be qualified by an address-space qualifier."
1886 // Since all parameters have automatic store duration, they can not have
1887 // an address space.
1888 if (T.getAddressSpace() != 0) {
1889 Diag(NameLoc, diag::err_arg_with_address_space);
1890 Invalid = true;
1891 }
1892
1893 // An @catch parameter must be an unqualified object pointer type;
1894 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1895 if (Invalid) {
1896 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001897 } else if (T->isDependentType()) {
1898 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001899 } else if (!T->isObjCObjectPointerType()) {
1900 Invalid = true;
1901 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1902 } else if (T->isObjCQualifiedIdType()) {
1903 Invalid = true;
1904 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1905 }
1906
1907 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
John McCalld931b082010-08-26 03:08:43 +00001908 SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00001909 New->setExceptionVariable(true);
1910
Douglas Gregor160b5632010-04-26 17:32:49 +00001911 if (Invalid)
1912 New->setInvalidDecl();
1913 return New;
1914}
1915
John McCalld226f652010-08-21 09:40:31 +00001916Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001917 const DeclSpec &DS = D.getDeclSpec();
1918
1919 // We allow the "register" storage class on exception variables because
1920 // GCC did, but we drop it completely. Any other storage class is an error.
1921 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1922 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1923 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1924 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1925 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1926 << DS.getStorageClassSpec();
1927 }
1928 if (D.getDeclSpec().isThreadSpecified())
1929 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1930 D.getMutableDeclSpec().ClearStorageClassSpecs();
1931
1932 DiagnoseFunctionSpecifiers(D);
1933
1934 // Check that there are no default arguments inside the type of this
1935 // exception object (C++ only).
1936 if (getLangOptions().CPlusPlus)
1937 CheckExtraCXXDefaultArguments(D);
1938
Douglas Gregor160b5632010-04-26 17:32:49 +00001939 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001940 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1941 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001942
1943 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1944 // Objective-C++: Types shall not be defined in exception types.
1945 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1946 << Context.getTypeDeclType(OwnedDecl);
1947 }
1948
1949 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1950 D.getIdentifierLoc(),
1951 D.isInvalidType());
1952
1953 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1954 if (D.getCXXScopeSpec().isSet()) {
1955 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1956 << D.getCXXScopeSpec().getRange();
1957 New->setInvalidDecl();
1958 }
1959
1960 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00001961 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00001962 if (D.getIdentifier())
1963 IdResolver.AddDecl(New);
1964
1965 ProcessDeclAttributes(S, New, D);
1966
1967 if (New->hasAttr<BlocksAttr>())
1968 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00001969 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001970}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001971
1972/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001973/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001974void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001975 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001976 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
1977 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001978 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001979 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001980 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001981 }
1982}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001983
1984void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1985 CXXBaseOrMemberInitializer ** initializers,
1986 unsigned numInitializers) {
1987 if (numInitializers > 0) {
1988 NumIvarInitializers = numInitializers;
1989 CXXBaseOrMemberInitializer **ivarInitializers =
1990 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1991 memcpy(ivarInitializers, initializers,
1992 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1993 IvarInitializers = ivarInitializers;
1994 }
1995}
1996
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001997void Sema::DiagnoseUseOfUnimplementedSelectors() {
1998 if (ReferencedSelectors.empty())
1999 return;
2000 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2001 ReferencedSelectors.begin(),
2002 E = ReferencedSelectors.end(); S != E; ++S) {
2003 Selector Sel = (*S).first;
2004 if (!LookupImplementedMethodInGlobalPool(Sel))
2005 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2006 }
2007 return;
2008}