blob: e98163e0c42bee13e5897bef9b84ceb761d49eea [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
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Sema.h"
15#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Steve Naroffebf64432009-02-28 16:59:13 +000023/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000024/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000028
Steve Naroff394f3f42008-07-25 17:57:26 +000029 // If we don't have a valid method decl, simply return.
30 if (!MDecl)
31 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000032
33 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000035 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000036 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000037 AddFactoryMethodToGlobalPool(MDecl, true);
38
Chris Lattner4d391482007-12-12 07:09:47 +000039 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000041 PushFunctionScope();
42
Chris Lattner4d391482007-12-12 07:09:47 +000043 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000048
Daniel Dunbar451318c2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000051
Chris Lattner8123a952008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000057}
58
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000064 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000065 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000066 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner4d391482007-12-12 07:09:47 +000068 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +000070 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +000071
Ted Kremeneka526c5c2008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000075 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Douglas Gregordeacbdc2010-08-11 12:19:30 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
78 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +000079 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +000080 if (!IDecl->isForwardDecl()) {
81 IDecl->setInvalidDecl();
82 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
83 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +000084
Douglas Gregordeacbdc2010-08-11 12:19:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
87 return DeclPtrTy::make(IDecl);
88 } else {
89 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
91 IDecl->setClassLoc(ClassLoc);
92
93 // Since this ObjCInterfaceDecl was created by a forward declaration,
94 // we now add it to the DeclContext since it wasn't added before
95 // (see ActOnForwardClassDeclaration).
96 IDecl->setLexicalDeclContext(CurContext);
97 CurContext->addDecl(IDecl);
98
99 if (AttrList)
100 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000102 } else {
103 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
104 ClassName, ClassLoc);
105 if (AttrList)
106 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
107
108 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattner4d391482007-12-12 07:09:47 +0000111 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000112 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000113 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
114 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000115
116 if (!PrevDecl) {
117 // Try to correct for a typo in the superclass name.
118 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000119 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000120 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
121 Diag(SuperLoc, diag::err_undef_superclass_suggest)
122 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000123 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
124 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000125 }
126 }
127
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000128 if (PrevDecl == IDecl) {
129 Diag(SuperLoc, diag::err_recursive_superclass)
130 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
131 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000132 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000135
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000140 if (PrevDecl && SuperClassDecl == 0) {
141 // The previous declaration was not a class decl. Check if we have a
142 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000144 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000145 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000146 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000148 }
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 // This handles the following case:
152 //
153 // typedef int SuperClass;
154 // @interface MyClass : SuperClass {} @end
155 //
156 if (!SuperClassDecl) {
157 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
158 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000159 }
160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000162 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
163 if (!SuperClassDecl)
164 Diag(SuperLoc, diag::err_undef_superclass)
165 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
166 else if (SuperClassDecl->isForwardDecl())
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperClassDecl->getDeclName() << ClassName
169 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000170 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000174 }
Chris Lattner4d391482007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000182 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Anders Carlsson15281452008-11-04 16:57:32 +0000186 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000193 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000198 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000199 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000200 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000201 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000202 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000203 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000204 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000205 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000206 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000207 }
208 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000209 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000210 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000211 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
212 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000213 if (T->isObjCObjectType()) {
214 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000215 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000216 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000217 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000218 }
219 }
220 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000221 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
222 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000223 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000224 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000225 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000226 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000229 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000230 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000231 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Anders Carlsson15281452008-11-04 16:57:32 +0000233 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000234 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000235
Chris Lattnerb28317a2009-03-28 19:18:32 +0000236 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000237}
238
Steve Naroff61d68522009-03-05 15:22:01 +0000239void Sema::CheckForwardProtocolDeclarationForCircularDependency(
240 IdentifierInfo *PName,
241 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000242 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000243 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
244 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Douglas Gregorc83c6872010-04-15 22:33:43 +0000246 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
247 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000248 if (PDecl->getIdentifier() == PName) {
249 Diag(Ploc, diag::err_protocol_has_circular_dependency);
250 Diag(PrevLoc, diag::note_previous_definition);
251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000253 PDecl->getLocation(), PDecl->getReferencedProtocols());
254 }
255 }
256}
257
Chris Lattnerb28317a2009-03-28 19:18:32 +0000258Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000259Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
260 IdentifierInfo *ProtocolName,
261 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000262 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000263 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000264 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000265 SourceLocation EndProtoLoc,
266 AttributeList *AttrList) {
267 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000268 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000269 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000270 if (PDecl) {
271 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000272 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000273 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000274 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000275 // Just return the protocol we already had.
276 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000277 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 }
Steve Naroff61d68522009-03-05 15:22:01 +0000279 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000280 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000281 CheckForwardProtocolDeclarationForCircularDependency(
282 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Steve Narofff11b5082008-08-13 16:39:22 +0000284 // Make sure the cached decl gets a valid start location.
285 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000286 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000287 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000288 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000289 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000290 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000291 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000292 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000293 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000294 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000295 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000296 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000297 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
298 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000299 PDecl->setLocEnd(EndProtoLoc);
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
302 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000303 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000304}
305
306/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000307/// issues an error if they are not declared. It returns list of
308/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000309void
Chris Lattnere13b9592008-07-26 04:03:38 +0000310Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000311 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000312 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000313 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000314 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000315 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
316 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000317 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000318 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
319 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000320 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000321 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
322 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
323 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000324 Diag(PDecl->getLocation(), diag::note_previous_decl)
325 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000326 }
327 }
328
329 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000330 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000331 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000332 continue;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000335 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000336
337 // If this is a forward declaration and we are supposed to warn in this
338 // case, do it.
339 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000341 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000342 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000343 }
344}
345
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000346/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000347/// a class method in its extension.
348///
Mike Stump1eb44332009-09-09 15:08:12 +0000349void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000350 ObjCInterfaceDecl *ID) {
351 if (!ID)
352 return; // Possibly due to previous error
353
354 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000355 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
356 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000357 ObjCMethodDecl *MD = *i;
358 MethodMap[MD->getSelector()] = MD;
359 }
360
361 if (MethodMap.empty())
362 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000363 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
364 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000365 ObjCMethodDecl *Method = *i;
366 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
367 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
368 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
369 << Method->getDeclName();
370 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
371 }
372 }
373}
374
Chris Lattner58fe03b2009-04-12 08:43:13 +0000375/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000376Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000377Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000378 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000379 unsigned NumElts,
380 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000381 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000382 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattner4d391482007-12-12 07:09:47 +0000384 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000385 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000386 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Douglas Gregord0434102009-01-09 00:49:46 +0000387 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000388 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000389 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000390 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000391 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000392 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000393 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000394 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000395 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
398 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000399 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000400 Protocols.data(), Protocols.size(),
401 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000402 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000403 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000404 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000405}
406
Chris Lattnerb28317a2009-03-28 19:18:32 +0000407Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000408ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
409 IdentifierInfo *ClassName, SourceLocation ClassLoc,
410 IdentifierInfo *CategoryName,
411 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000412 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000413 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000414 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000415 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000416 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000417 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000418
419 /// Check that class of this category is already completely declared.
420 if (!IDecl || IDecl->isForwardDecl()) {
421 // Create an invalid ObjCCategoryDecl to serve as context for
422 // the enclosing method declarations. We mark the decl invalid
423 // to make it clear that this isn't a valid AST.
424 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
425 ClassLoc, CategoryLoc, CategoryName);
426 CDecl->setInvalidDecl();
427 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
428 return DeclPtrTy::make(CDecl);
429 }
430
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000431 if (!CategoryName && IDecl->getImplementation()) {
432 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
433 Diag(IDecl->getImplementation()->getLocation(),
434 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000435 }
436
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000437 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
438 ClassLoc, CategoryLoc, CategoryName);
439 // FIXME: PushOnScopeChains?
440 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000441
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000442 CDecl->setClassInterface(IDecl);
443 // Insert class extension to the list of class's categories.
444 if (!CategoryName)
445 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner16b34b42009-02-16 21:30:01 +0000447 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000448 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000449
Fariborz Jahanian25760612010-02-15 21:55:26 +0000450 if (CategoryName) {
451 /// Check for duplicate interface declaration for this category
452 ObjCCategoryDecl *CDeclChain;
453 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
454 CDeclChain = CDeclChain->getNextClassCategory()) {
455 if (CDeclChain->getIdentifier() == CategoryName) {
456 // Class extensions can be declared multiple times.
457 Diag(CategoryLoc, diag::warn_dup_category_def)
458 << ClassName << CategoryName;
459 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
460 break;
461 }
Chris Lattner70f19542009-02-16 21:26:43 +0000462 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000463 if (!CDeclChain)
464 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000465 }
Chris Lattner70f19542009-02-16 21:26:43 +0000466
Chris Lattner4d391482007-12-12 07:09:47 +0000467 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000468 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000469 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000470 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000471 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000472 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000473 NumProtoRefs, ProtoLocs,
474 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Anders Carlsson15281452008-11-04 16:57:32 +0000477 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000478 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000479}
480
481/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000483/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000484Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000485 SourceLocation AtCatImplLoc,
486 IdentifierInfo *ClassName, SourceLocation ClassLoc,
487 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000488 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000489 ObjCCategoryDecl *CatIDecl = 0;
490 if (IDecl) {
491 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
492 if (!CatIDecl) {
493 // Category @implementation with no corresponding @interface.
494 // Create and install one.
495 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000496 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000497 CatName);
498 CatIDecl->setClassInterface(IDecl);
499 CatIDecl->insertNextClassCategory();
500 }
501 }
502
Mike Stump1eb44332009-09-09 15:08:12 +0000503 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000504 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
505 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000506 /// Check that class of this category is already completely declared.
507 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000508 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000509
Douglas Gregord0434102009-01-09 00:49:46 +0000510 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000511 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000512
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000513 /// Check that CatName, category name, is not used in another implementation.
514 if (CatIDecl) {
515 if (CatIDecl->getImplementation()) {
516 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
517 << CatName;
518 Diag(CatIDecl->getImplementation()->getLocation(),
519 diag::note_previous_definition);
520 } else
521 CatIDecl->setImplementation(CDecl);
522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Anders Carlsson15281452008-11-04 16:57:32 +0000524 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000525 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000526}
527
Chris Lattnerb28317a2009-03-28 19:18:32 +0000528Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000529 SourceLocation AtClassImplLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000531 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000532 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000533 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000534 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000535 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000536 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
537 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000539 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000540 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000541 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
542 // If this is a forward declaration of an interface, warn.
543 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000544 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000545 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000546 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000547 } else {
548 // We did not find anything with the name ClassName; try to correct for
549 // typos in the class name.
550 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000551 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000552 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000553 // Suggest the (potentially) correct interface name. However, put the
554 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000555 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000556 // provide a code-modification hint or use the typo name for recovery,
557 // because this is just a warning. The program may actually be correct.
558 Diag(ClassLoc, diag::warn_undef_interface_suggest)
559 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000560 Diag(IDecl->getLocation(), diag::note_previous_decl)
561 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000562 << FixItHint::CreateReplacement(ClassLoc,
563 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000564 IDecl = 0;
565 } else {
566 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
567 }
Chris Lattner4d391482007-12-12 07:09:47 +0000568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner4d391482007-12-12 07:09:47 +0000570 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000572 if (SuperClassname) {
573 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000574 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
575 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000576 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000577 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
578 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000579 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000580 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000581 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000583 Diag(SuperClassLoc, diag::err_undef_superclass)
584 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000585 else if (IDecl && IDecl->getSuperClass() != SDecl) {
586 // This implementation and its interface do not have the same
587 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000588 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000589 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000590 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000591 }
592 }
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner4d391482007-12-12 07:09:47 +0000595 if (!IDecl) {
596 // Legacy case of @implementation with no corresponding @interface.
597 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000598
Mike Stump390b4cc2009-05-16 07:39:55 +0000599 // FIXME: Do we support attributes on the @implementation? If so we should
600 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000601 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000602 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000603 IDecl->setSuperClass(SDecl);
604 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000605
606 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000607 } else {
608 // Mark the interface as being completed, even if it was just as
609 // @class ....;
610 // declaration; the user cannot reopen it.
611 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
614 ObjCImplementationDecl* IMPDecl =
615 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000616 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson15281452008-11-04 16:57:32 +0000618 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000619 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner4d391482007-12-12 07:09:47 +0000621 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000622 if (IDecl->getImplementation()) {
623 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000625 Diag(IDecl->getImplementation()->getLocation(),
626 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000627 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000628 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000629 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000630 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000631 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000632}
633
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000634void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
635 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000636 SourceLocation RBrace) {
637 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000638 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000639 if (!IDecl)
640 return;
641 /// Check case of non-existing @interface decl.
642 /// (legacy objective-c @implementation decl without an @interface decl).
643 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000644 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000645 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000646 // Add ivar's to class's DeclContext.
647 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000648 ivars[i]->setLexicalDeclContext(ImpDecl);
649 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000650 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000651 }
652
Chris Lattner4d391482007-12-12 07:09:47 +0000653 return;
654 }
655 // If implementation has empty ivar list, just return.
656 if (numIvars == 0)
657 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattner4d391482007-12-12 07:09:47 +0000659 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000660 if (LangOpts.ObjCNonFragileABI2) {
661 if (ImpDecl->getSuperClass())
662 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
663 for (unsigned i = 0; i < numIvars; i++) {
664 ObjCIvarDecl* ImplIvar = ivars[i];
665 if (const ObjCIvarDecl *ClsIvar =
666 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
667 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
668 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
669 continue;
670 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000671 // Instance ivar to Implementation's DeclContext.
672 ImplIvar->setLexicalDeclContext(ImpDecl);
673 IDecl->makeDeclVisibleInContext(ImplIvar, false);
674 ImpDecl->addDecl(ImplIvar);
675 }
676 return;
677 }
Chris Lattner4d391482007-12-12 07:09:47 +0000678 // Check interface's Ivar list against those in the implementation.
679 // names and types must match.
680 //
Chris Lattner4d391482007-12-12 07:09:47 +0000681 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000682 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000683 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
684 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000685 ObjCIvarDecl* ImplIvar = ivars[j++];
686 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000687 assert (ImplIvar && "missing implementation ivar");
688 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Steve Naroffca331292009-03-03 14:49:36 +0000690 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000691 if (Context.getCanonicalType(ImplIvar->getType()) !=
692 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000693 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000694 << ImplIvar->getIdentifier()
695 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000696 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000697 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
698 Expr *ImplBitWidth = ImplIvar->getBitWidth();
699 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000700 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
701 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000702 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
703 << ImplIvar->getIdentifier();
704 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706 }
Steve Naroffca331292009-03-03 14:49:36 +0000707 // Make sure the names are identical.
708 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000709 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000710 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000711 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000712 }
713 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattner609e4c72007-12-12 18:11:49 +0000716 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000717 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000718 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000719 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000720}
721
Steve Naroff3c2eb662008-02-10 21:38:56 +0000722void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000723 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000724 if (!IncompleteImpl) {
725 Diag(ImpLoc, diag::warn_incomplete_impl);
726 IncompleteImpl = true;
727 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000728 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000729 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000730}
731
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000732void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
733 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000734 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000735 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000736 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
737 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000738 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000739 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
740 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000741 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Chris Lattner3aff9192009-04-11 19:58:42 +0000744 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
745 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
746 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000747 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
748 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
749 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
750 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000751 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000752
753 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000754 << ImpMethodDecl->getDeclName() << (*IF)->getType()
755 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000756 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000757 }
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000758 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
759 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
760 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
761 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000762}
763
Mike Stump390b4cc2009-05-16 07:39:55 +0000764/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
765/// improve the efficiency of selector lookups and type checking by associating
766/// with each protocol / interface / category the flattened instance tables. If
767/// we used an immutable set to keep the table then it wouldn't add significant
768/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000769
Steve Naroffefe7f362008-02-08 22:06:17 +0000770/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000771/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000772void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
773 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000774 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000775 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000776 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000777 ObjCContainerDecl *CDecl) {
778 ObjCInterfaceDecl *IDecl;
779 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
780 IDecl = C->getClassInterface();
781 else
782 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
783 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
784
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000785 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000786 ObjCInterfaceDecl *NSIDecl = 0;
787 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000788 // check to see if class implements forwardInvocation method and objects
789 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000790 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000791 // Under such conditions, which means that every method possible is
792 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000793 // found" warnings.
794 // FIXME: Use a general GetUnarySelector method for this.
795 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
796 Selector fISelector = Context.Selectors.getSelector(1, &II);
797 if (InsMap.count(fISelector))
798 // Is IDecl derived from 'NSProxy'? If so, no instance methods
799 // need be implemented in the implementation.
800 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000803 // If a method lookup fails locally we still need to look and see if
804 // the method was implemented by a base class or an inherited
805 // protocol. This lookup is slow, but occurs rarely in correct code
806 // and otherwise would terminate in a warning.
807
Chris Lattner4d391482007-12-12 07:09:47 +0000808 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000809 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000810 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000811 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000812 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000813 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000814 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000815 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000817 // Ugly, but necessary. Method declared in protcol might have
818 // have been synthesized due to a property declared in the class which
819 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000820 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000821 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000822 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000823 unsigned DIAG = diag::warn_unimplemented_protocol_method;
824 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
825 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
826 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
827 << PDecl->getDeclName();
828 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000829 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000830 }
831 }
Chris Lattner4d391482007-12-12 07:09:47 +0000832 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000833 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000834 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000835 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000837 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
838 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000839 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000840 unsigned DIAG = diag::warn_unimplemented_protocol_method;
841 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
842 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
843 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
844 PDecl->getDeclName();
845 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000846 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000847 }
Chris Lattner780f3292008-07-21 21:32:27 +0000848 // Check on this protocols's referenced protocols, recursively.
849 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
850 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000851 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000852}
853
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000854/// MatchAllMethodDeclarations - Check methods declaraed in interface or
855/// or protocol against those declared in their implementations.
856///
857void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
858 const llvm::DenseSet<Selector> &ClsMap,
859 llvm::DenseSet<Selector> &InsMapSeen,
860 llvm::DenseSet<Selector> &ClsMapSeen,
861 ObjCImplDecl* IMPDecl,
862 ObjCContainerDecl* CDecl,
863 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000864 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000865 // Check and see if instance methods in class interface have been
866 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000867 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
868 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000869 if (InsMapSeen.count((*I)->getSelector()))
870 continue;
871 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000872 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000873 !InsMap.count((*I)->getSelector())) {
874 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000875 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
876 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000877 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000878 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000879 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000880 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000881 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000883 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000884 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
885 // ImpMethodDecl may be null as in a @dynamic property.
886 if (ImpMethodDecl)
887 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
888 }
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000891 // Check and see if class methods in class interface have been
892 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000893 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000894 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000895 if (ClsMapSeen.count((*I)->getSelector()))
896 continue;
897 ClsMapSeen.insert((*I)->getSelector());
898 if (!ClsMap.count((*I)->getSelector())) {
899 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000900 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
901 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000902 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000903 ObjCMethodDecl *ImpMethodDecl =
904 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000905 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000906 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000907 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
908 }
909 }
910 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
911 // Check for any implementation of a methods declared in protocol.
912 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
913 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000914 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
915 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000916 (*PI), IncompleteImpl, false);
917 if (I->getSuperClass())
918 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000919 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000920 I->getSuperClass(), IncompleteImpl, false);
921 }
922}
923
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000924void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000925 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000926 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000927 llvm::DenseSet<Selector> InsMap;
928 // Check and see if instance methods in class interface have been
929 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000930 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000932 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000934 // Check and see if properties declared in the interface have either 1)
935 // an implementation or 2) there is a @synthesize/@dynamic implementation
936 // of the property in the @implementation.
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000937 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000938 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000939
Chris Lattner4d391482007-12-12 07:09:47 +0000940 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000941 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 I = IMPDecl->classmeth_begin(),
943 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000944 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000946 // Check for type conflict of methods declared in a class/protocol and
947 // its implementation; if any.
948 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000949 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
950 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000951 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner4d391482007-12-12 07:09:47 +0000953 // Check the protocol list for unimplemented methods in the @implementation
954 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000955 // Check and see if class methods in class interface have been
956 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattnercddc8882009-03-01 00:56:52 +0000958 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000959 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000960 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000962 InsMap, ClsMap, I);
963 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000964 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
965 Categories; Categories = Categories->getNextClassExtension())
966 ImplMethodsVsClassMethods(S, IMPDecl,
967 const_cast<ObjCCategoryDecl*>(Categories),
968 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +0000969 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000970 // For extended class, unimplemented methods in its protocols will
971 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000972 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000973 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
974 E = C->protocol_end(); PI != E; ++PI)
975 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000976 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000977 // Report unimplemented properties in the category as well.
978 // When reporting on missing setter/getters, do not report when
979 // setter/getter is implemented in category's primary class
980 // implementation.
981 if (ObjCInterfaceDecl *ID = C->getClassInterface())
982 if (ObjCImplDecl *IMP = ID->getImplementation()) {
983 for (ObjCImplementationDecl::instmeth_iterator
984 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
985 InsMap.insert((*I)->getSelector());
986 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000987 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000988 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000989 } else
990 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000991}
992
Mike Stump1eb44332009-09-09 15:08:12 +0000993/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000994Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000995Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000996 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000997 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000998 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000999 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner4d391482007-12-12 07:09:47 +00001001 for (unsigned i = 0; i != NumElts; ++i) {
1002 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001003 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001004 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001005 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001006 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001007 // Maybe we will complain about the shadowed template parameter.
1008 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1009 // Just pretend that we didn't see the previous declaration.
1010 PrevDecl = 0;
1011 }
1012
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001014 // GCC apparently allows the following idiom:
1015 //
1016 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1017 // @class XCElementToggler;
1018 //
Mike Stump1eb44332009-09-09 15:08:12 +00001019 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001020 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001021 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001022 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001023 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001024 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001025 // a forward class declaration matching a typedef name of a class refers
1026 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001027 if (const ObjCObjectType *OI =
1028 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1029 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001030 }
Chris Lattner4d391482007-12-12 07:09:47 +00001031 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001032 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1033 if (!IDecl) { // Not already seen? Make a forward decl.
1034 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1035 IdentList[i], IdentLocs[i], true);
1036
1037 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1038 // the current DeclContext. This prevents clients that walk DeclContext
1039 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1040 // declared later (if at all). We also take care to explicitly make
1041 // sure this declaration is visible for name lookup.
1042 PushOnScopeChains(IDecl, TUScope, false);
1043 CurContext->makeDeclVisibleInContext(IDecl, true);
1044 }
Chris Lattner4d391482007-12-12 07:09:47 +00001045
1046 Interfaces.push_back(IDecl);
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Ted Kremenek321c22f2009-11-18 00:28:11 +00001049 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001050 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001051 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001052 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001053 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001054 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001055 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001056}
1057
1058
1059/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1060/// returns true, or false, accordingly.
1061/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001062bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001063 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001064 bool matchBasedOnSizeAndAlignment,
1065 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001066 QualType T1 = Context.getCanonicalType(Method->getResultType());
1067 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001069 if (T1 != T2) {
1070 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001071 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001072 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001073 // Incomplete types don't have a size and alignment.
1074 if (T1->isIncompleteType() || T2->isIncompleteType())
1075 return false;
1076 // Check is based on size and alignment.
1077 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1078 return false;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner89951a82009-02-20 18:43:26 +00001081 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1082 E = Method->param_end();
1083 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner89951a82009-02-20 18:43:26 +00001085 for (; ParamI != E; ++ParamI, ++PrevI) {
1086 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1087 T1 = Context.getCanonicalType((*ParamI)->getType());
1088 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001089 if (T1 != T2) {
1090 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001091 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001092 return false;
1093 // Incomplete types don't have a size and alignment.
1094 if (T1->isIncompleteType() || T2->isIncompleteType())
1095 return false;
1096 // Check is based on size and alignment.
1097 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1098 return false;
1099 }
Chris Lattner4d391482007-12-12 07:09:47 +00001100 }
1101 return true;
1102}
1103
Sebastian Redldb9d2142010-08-02 23:18:59 +00001104/// \brief Read the contents of the method pool for a given selector from
1105/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001106///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001107/// This routine should only be called once, when the method pool has no entry
1108/// for this selector.
1109Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001110 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001111 assert(MethodPool.find(Sel) == MethodPool.end() &&
1112 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001113
1114 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001115 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Sebastian Redldb9d2142010-08-02 23:18:59 +00001117 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001118}
1119
Sebastian Redldb9d2142010-08-02 23:18:59 +00001120void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1121 bool instance) {
1122 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1123 if (Pos == MethodPool.end()) {
1124 if (ExternalSource)
1125 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001126 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001127 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1128 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001129 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001130 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001131 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001132 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001133 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001134 Entry.Method = Method;
1135 Entry.Next = 0;
1136 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattnerb25df352009-03-04 05:16:45 +00001139 // We've seen a method with this name, see if we have already seen this type
1140 // signature.
1141 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001142 if (MatchTwoMethodDeclarations(Method, List->Method)) {
1143 List->Method->setDefined(impl);
Chris Lattnerb25df352009-03-04 05:16:45 +00001144 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Chris Lattnerb25df352009-03-04 05:16:45 +00001147 // We have a new signature for an existing method - add it.
1148 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001149 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1150 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001151}
1152
Sebastian Redldb9d2142010-08-02 23:18:59 +00001153ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001154 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001155 bool warn, bool instance) {
1156 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1157 if (Pos == MethodPool.end()) {
1158 if (ExternalSource)
1159 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001160 else
1161 return 0;
1162 }
1163
Sebastian Redldb9d2142010-08-02 23:18:59 +00001164 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001166 bool strictSelectorMatch = receiverIdOrClass && warn &&
1167 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) !=
1168 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001169 if (warn && MethList.Method && MethList.Next) {
1170 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001171 if (strictSelectorMatch)
1172 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1173 // This checks if the methods differ in type mismatch.
1174 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1175 issueWarning = true;
1176 }
1177
1178 if (!issueWarning)
1179 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1180 // This checks if the methods differ by size & alignment.
1181 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1182 issueWarning = true;
1183 }
1184
Sebastian Redldb9d2142010-08-02 23:18:59 +00001185 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001186 if (strictSelectorMatch)
1187 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1188 else
1189 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001190 Diag(MethList.Method->getLocStart(), diag::note_using)
1191 << MethList.Method->getSourceRange();
1192 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1193 Diag(Next->Method->getLocStart(), diag::note_also_found)
1194 << Next->Method->getSourceRange();
1195 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001196 }
1197 return MethList.Method;
1198}
1199
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001200ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001201 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1202 if (Pos == MethodPool.end())
1203 return 0;
1204
1205 GlobalMethods &Methods = Pos->second;
1206
1207 if (Methods.first.Method && Methods.first.Method->isDefined())
1208 return Methods.first.Method;
1209 if (Methods.second.Method && Methods.second.Method->isDefined())
1210 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001211 return 0;
1212}
1213
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001214/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1215/// identical selector names in current and its super classes and issues
1216/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001217void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1218 ObjCMethodDecl *Method,
1219 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001220 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1221 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001223 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001224 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001225 SD->lookupMethod(Method->getSelector(), IsInstance);
1226 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001227 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001228 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001229 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001230 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1231 E = Method->param_end();
1232 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1233 for (; ParamI != E; ++ParamI, ++PrevI) {
1234 // Number of parameters are the same and is guaranteed by selector match.
1235 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1236 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1237 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1238 // If type of arguement of method in this class does not match its
1239 // respective argument type in the super class method, issue warning;
1240 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001241 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001242 << T1 << T2;
1243 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1244 return;
1245 }
1246 }
1247 ID = SD;
1248 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001249}
1250
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001251/// DiagnoseDuplicateIvars -
1252/// Check for duplicate ivars in the entire class at the start of
1253/// @implementation. This becomes necesssary because class extension can
1254/// add ivars to a class in random order which will not be known until
1255/// class's @implementation is seen.
1256void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1257 ObjCInterfaceDecl *SID) {
1258 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1259 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1260 ObjCIvarDecl* Ivar = (*IVI);
1261 if (Ivar->isInvalidDecl())
1262 continue;
1263 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1264 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1265 if (prevIvar) {
1266 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1267 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1268 Ivar->setInvalidDecl();
1269 }
1270 }
1271 }
1272}
1273
Steve Naroffa56f6162007-12-18 01:30:32 +00001274// Note: For class/category implemenations, allMethods/allProperties is
1275// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001276void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
Ted Kremenek782f2f52010-01-07 01:20:12 +00001277 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001278 DeclPtrTy *allMethods, unsigned allNum,
1279 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001280 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001281 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001282
Steve Naroffa56f6162007-12-18 01:30:32 +00001283 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1284 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001285 // should be true.
1286 if (!ClassDecl)
1287 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001288
Mike Stump1eb44332009-09-09 15:08:12 +00001289 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001290 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1291 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001293
Ted Kremenek782f2f52010-01-07 01:20:12 +00001294 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1295 // FIXME: This is wrong. We shouldn't be pretending that there is
1296 // an '@end' in the declaration.
1297 SourceLocation L = ClassDecl->getLocation();
1298 AtEnd.setBegin(L);
1299 AtEnd.setEnd(L);
1300 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001301 }
1302
Steve Naroff0701bbb2009-01-08 17:28:14 +00001303 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001304
1305 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1306 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1307 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1308
Chris Lattner4d391482007-12-12 07:09:47 +00001309 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001310 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001311 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001312
1313 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001314 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001315 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001316 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001317 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001318 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001319 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001320 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001321 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001322 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001323 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001324 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001325 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001326 InsMap[Method->getSelector()] = Method;
1327 /// The following allows us to typecheck messages to "id".
1328 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001329 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001330 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001331 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001332 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001333 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001334 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001335 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001336 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001337 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001338 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001339 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001340 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001341 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001342 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001343 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001344 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001345 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001346 /// The following allows us to typecheck messages to "Class".
1347 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001348 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001349 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001350 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001351 }
1352 }
1353 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001354 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001355 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001356 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001357 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001358 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001359 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001360 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001362 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001363
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001364 // Compare protocol properties with those in category
1365 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001366 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001367 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001368 }
Steve Naroff09c47192009-01-09 15:36:25 +00001369 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001370 if (CDecl->getIdentifier())
1371 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1372 // user-defined setter/getter. It also synthesizes setter/getter methods
1373 // and adds them to the DeclContext and global method pools.
1374 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1375 E = CDecl->prop_end();
1376 I != E; ++I)
1377 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001378 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001379 }
1380 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001381 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001382 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001383 if (LangOpts.ObjCNonFragileABI2)
1384 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001385 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001386 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001387 if (LangOpts.ObjCNonFragileABI2)
1388 while (IDecl->getSuperClass()) {
1389 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1390 IDecl = IDecl->getSuperClass();
1391 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001392 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001393 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001394 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001395 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001396 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Chris Lattner4d391482007-12-12 07:09:47 +00001398 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001399 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001400 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001402 Categories; Categories = Categories->getNextClassCategory()) {
1403 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001404 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001405 break;
1406 }
1407 }
1408 }
1409 }
Chris Lattner682bf922009-03-29 16:50:03 +00001410 if (isInterfaceDeclKind) {
1411 // Reject invalid vardecls.
1412 for (unsigned i = 0; i != tuvNum; i++) {
1413 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1414 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1415 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001416 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001417 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001418 }
Chris Lattner682bf922009-03-29 16:50:03 +00001419 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001420 }
Chris Lattner4d391482007-12-12 07:09:47 +00001421}
1422
1423
1424/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1425/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001426static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001427CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1428 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1429 if (PQTVal & ObjCDeclSpec::DQ_In)
1430 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1431 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1432 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1433 if (PQTVal & ObjCDeclSpec::DQ_Out)
1434 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1435 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1436 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1437 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1438 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1439 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1440 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001441
1442 return ret;
1443}
1444
Ted Kremenek422bae72010-04-18 04:59:38 +00001445static inline
1446bool containsInvalidMethodImplAttribute(const AttributeList *A) {
1447 // The 'ibaction' attribute is allowed on method definitions because of
1448 // how the IBAction macro is used on both method declarations and definitions.
1449 // If the method definitions contains any other attributes, return true.
1450 while (A && A->getKind() == AttributeList::AT_IBAction)
1451 A = A->getNext();
1452 return A != NULL;
1453}
1454
Chris Lattnerb28317a2009-03-28 19:18:32 +00001455Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001456 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001457 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001458 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001459 Selector Sel,
1460 // optional arguments. The number of types/arguments is obtained
1461 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001462 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001463 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001464 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1465 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001466 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001467
1468 // Make sure we can establish a context for the method.
1469 if (!ClassDecl) {
1470 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001471 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001472 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001473 }
Chris Lattner4d391482007-12-12 07:09:47 +00001474 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001476 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001477 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001478 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Steve Naroffccef3712009-02-20 22:59:16 +00001480 // Methods cannot return interface types. All ObjC objects are
1481 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001482 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001483 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1484 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001485 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001486 }
1487 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001488 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001489
1490 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001491 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001492 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001493 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001494 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001495 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001496 MethodDeclKind == tok::objc_optional ?
1497 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001498 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Chris Lattner0ed844b2008-04-04 06:12:32 +00001500 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattner7db638d2009-04-11 19:42:43 +00001502 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001503 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001504 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Chris Lattnere294d3f2009-04-11 18:57:04 +00001506 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001507 ArgType = Context.getObjCIdType();
1508 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001509 } else {
John McCall58e46772009-10-23 21:48:59 +00001510 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001511 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001512 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
John McCall58e46772009-10-23 21:48:59 +00001515 ParmVarDecl* Param
1516 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1517 ArgInfo[i].Name, ArgType, DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001518 VarDecl::None, VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
John McCallc12c5bb2010-05-15 11:32:37 +00001520 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001521 Diag(ArgInfo[i].NameLoc,
1522 diag::err_object_cannot_be_passed_returned_by_value)
1523 << 1 << ArgType;
1524 Param->setInvalidDecl();
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattner0ed844b2008-04-04 06:12:32 +00001527 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001528 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001530 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001531 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattner0ed844b2008-04-04 06:12:32 +00001533 Params.push_back(Param);
1534 }
1535
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001536 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
1537 ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>();
1538 QualType ArgType = Param->getType();
1539 if (ArgType.isNull())
1540 ArgType = Context.getObjCIdType();
1541 else
1542 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1543 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001544 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001545 Diag(Param->getLocation(),
1546 diag::err_object_cannot_be_passed_returned_by_value)
1547 << 1 << ArgType;
1548 Param->setInvalidDecl();
1549 }
1550 Param->setDeclContext(ObjCMethod);
Douglas Gregor59c451e2010-08-07 12:29:18 +00001551 if (Param->getDeclName())
1552 IdResolver.RemoveDecl(Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001553 Params.push_back(Param);
1554 }
1555
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001556 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1557 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001558 ObjCMethod->setObjCDeclQualifier(
1559 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1560 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001561
1562 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001563 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001564
John McCall54abf7d2009-11-04 02:18:39 +00001565 const ObjCMethodDecl *InterfaceMD = 0;
1566
Mike Stump1eb44332009-09-09 15:08:12 +00001567 // For implementations (which can be very "coarse grain"), we add the
1568 // method now. This allows the AST to implement lookup methods that work
1569 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001570 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001571 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001572 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001573 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001574 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1575 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001576 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001577 PrevMethod = ImpDecl->getClassMethod(Sel);
1578 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001579 }
John McCall54abf7d2009-11-04 02:18:39 +00001580 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1581 MethodType == tok::minus);
Ted Kremenek422bae72010-04-18 04:59:38 +00001582 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001583 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001584 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001585 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001586 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1588 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001589 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 PrevMethod = CatImpDecl->getClassMethod(Sel);
1591 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001592 }
Ted Kremenek422bae72010-04-18 04:59:38 +00001593 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001594 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001595 }
1596 if (PrevMethod) {
1597 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001598 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001599 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001600 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001601 }
John McCall54abf7d2009-11-04 02:18:39 +00001602
1603 // If the interface declared this method, and it was deprecated there,
1604 // mark it deprecated here.
1605 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1606 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1607
Chris Lattnerb28317a2009-03-28 19:18:32 +00001608 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001609}
1610
Chris Lattnercc98eac2008-12-17 07:13:27 +00001611bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001612 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001613 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Anders Carlsson15281452008-11-04 16:57:32 +00001615 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1616 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Anders Carlsson15281452008-11-04 16:57:32 +00001618 return true;
1619}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001620
Chris Lattnercc98eac2008-12-17 07:13:27 +00001621/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1622/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00001623void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001624 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001625 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001626 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001627 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001628 if (!Class) {
1629 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1630 return;
1631 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001632 if (LangOpts.ObjCNonFragileABI) {
1633 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1634 return;
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattnercc98eac2008-12-17 07:13:27 +00001637 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00001638 llvm::SmallVector<FieldDecl*, 32> RecFields;
1639 Context.CollectObjCIvars(Class, RecFields);
1640 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1641 for (unsigned i = 0; i < RecFields.size(); i++) {
1642 FieldDecl* ID = RecFields[i];
1643 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1644 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1645 ID->getIdentifier(), ID->getType(),
1646 ID->getBitWidth());
1647 Decls.push_back(Sema::DeclPtrTy::make(FD));
1648 }
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Chris Lattnercc98eac2008-12-17 07:13:27 +00001650 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001651 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001652 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001653 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001654 if (getLangOptions().CPlusPlus)
1655 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001656 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001657 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001658 }
1659}
1660
Douglas Gregor160b5632010-04-26 17:32:49 +00001661/// \brief Build a type-check a new Objective-C exception variable declaration.
1662VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1663 QualType T,
1664 IdentifierInfo *Name,
1665 SourceLocation NameLoc,
1666 bool Invalid) {
1667 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1668 // duration shall not be qualified by an address-space qualifier."
1669 // Since all parameters have automatic store duration, they can not have
1670 // an address space.
1671 if (T.getAddressSpace() != 0) {
1672 Diag(NameLoc, diag::err_arg_with_address_space);
1673 Invalid = true;
1674 }
1675
1676 // An @catch parameter must be an unqualified object pointer type;
1677 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1678 if (Invalid) {
1679 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001680 } else if (T->isDependentType()) {
1681 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001682 } else if (!T->isObjCObjectPointerType()) {
1683 Invalid = true;
1684 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1685 } else if (T->isObjCQualifiedIdType()) {
1686 Invalid = true;
1687 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1688 }
1689
1690 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor324b54d2010-05-03 18:51:14 +00001691 VarDecl::None, VarDecl::None);
1692 New->setExceptionVariable(true);
1693
Douglas Gregor160b5632010-04-26 17:32:49 +00001694 if (Invalid)
1695 New->setInvalidDecl();
1696 return New;
1697}
1698
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001699Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001700 const DeclSpec &DS = D.getDeclSpec();
1701
1702 // We allow the "register" storage class on exception variables because
1703 // GCC did, but we drop it completely. Any other storage class is an error.
1704 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1705 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1706 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1707 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1708 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1709 << DS.getStorageClassSpec();
1710 }
1711 if (D.getDeclSpec().isThreadSpecified())
1712 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1713 D.getMutableDeclSpec().ClearStorageClassSpecs();
1714
1715 DiagnoseFunctionSpecifiers(D);
1716
1717 // Check that there are no default arguments inside the type of this
1718 // exception object (C++ only).
1719 if (getLangOptions().CPlusPlus)
1720 CheckExtraCXXDefaultArguments(D);
1721
Douglas Gregor160b5632010-04-26 17:32:49 +00001722 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001723 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1724 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001725
1726 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1727 // Objective-C++: Types shall not be defined in exception types.
1728 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1729 << Context.getTypeDeclType(OwnedDecl);
1730 }
1731
1732 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1733 D.getIdentifierLoc(),
1734 D.isInvalidType());
1735
1736 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1737 if (D.getCXXScopeSpec().isSet()) {
1738 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1739 << D.getCXXScopeSpec().getRange();
1740 New->setInvalidDecl();
1741 }
1742
1743 // Add the parameter declaration into this scope.
1744 S->AddDecl(DeclPtrTy::make(New));
1745 if (D.getIdentifier())
1746 IdResolver.AddDecl(New);
1747
1748 ProcessDeclAttributes(S, New, D);
1749
1750 if (New->hasAttr<BlocksAttr>())
1751 Diag(New->getLocation(), diag::err_block_on_nonlocal);
1752 return DeclPtrTy::make(New);
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001753}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001754
1755/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001756/// initialization.
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001757void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001758 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001759 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1760 E = OI->ivar_end(); I != E; ++I) {
1761 ObjCIvarDecl *Iv = (*I);
1762 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001763 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001764 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001765 }
1766
1767 // Find ivars to construct/destruct in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001768 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1769 CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001770 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1771 E = CDecl->ivar_end(); I != E; ++I) {
1772 ObjCIvarDecl *Iv = (*I);
1773 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001774 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001775 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001776 }
1777 }
1778
1779 // Also add any ivar defined in this class's implementation. This
1780 // includes synthesized ivars.
1781 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
1782 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1783 E = ImplDecl->ivar_end(); I != E; ++I) {
1784 ObjCIvarDecl *Iv = (*I);
1785 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001786 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001787 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001788 }
1789 }
1790}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001791
1792void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1793 CXXBaseOrMemberInitializer ** initializers,
1794 unsigned numInitializers) {
1795 if (numInitializers > 0) {
1796 NumIvarInitializers = numInitializers;
1797 CXXBaseOrMemberInitializer **ivarInitializers =
1798 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1799 memcpy(ivarInitializers, initializers,
1800 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1801 IvarInitializers = ivarInitializers;
1802 }
1803}
1804
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001805void Sema::DiagnoseUseOfUnimplementedSelectors() {
1806 if (ReferencedSelectors.empty())
1807 return;
1808 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
1809 ReferencedSelectors.begin(),
1810 E = ReferencedSelectors.end(); S != E; ++S) {
1811 Selector Sel = (*S).first;
1812 if (!LookupImplementedMethodInGlobalPool(Sel))
1813 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
1814 }
1815 return;
1816}