blob: 21aeb59a0886242d09becc328fac68039edef22a [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
14#include "Sema.h"
Douglas Gregorf06cdae2010-01-03 18:01:57 +000015#include "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())
Steve Naroffa56f6162007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000038
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
Ted Kremeneka526c5c2008-01-07 19:49:32 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000078 if (IDecl) {
79 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000080 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000081 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000082 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000083 Diag(IDecl->getLocation(), diag::note_previous_definition);
84
Steve Naroffcfe8bf32008-11-18 19:15:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000087 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000089 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +000091 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +000092
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).
Douglas Gregor78713d82010-04-09 21:30:38 +000096 IDecl->setLexicalDeclContext(CurContext);
Ted Kremenekc32b1d82009-11-17 22:58:30 +000097 CurContext->addDecl(IDecl);
98
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +000099 if (AttrList)
100 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000102 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000103 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000104 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000105 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000106 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Steve Naroffa7503a72009-04-23 15:15:40 +0000108 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.
143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
144 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000145 if (T->isObjCObjectType()) {
146 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
148 }
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);
283 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Narofff11b5082008-08-13 16:39:22 +0000285 // Make sure the cached decl gets a valid start location.
286 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000287 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000288 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000289 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000290 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000291 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000292 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000293 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000294 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000295 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000296 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000297 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000298 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
299 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000300 PDecl->setLocEnd(EndProtoLoc);
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
303 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000304 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000305}
306
307/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000308/// issues an error if they are not declared. It returns list of
309/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000310void
Chris Lattnere13b9592008-07-26 04:03:38 +0000311Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000312 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000313 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000314 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000315 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000316 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
317 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000318 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000319 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
320 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000321 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000322 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
323 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
324 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000325 Diag(PDecl->getLocation(), diag::note_previous_decl)
326 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000327 }
328 }
329
330 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000331 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000332 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000333 continue;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000336 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000337
338 // If this is a forward declaration and we are supposed to warn in this
339 // case, do it.
340 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000341 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000342 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000343 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000344 }
345}
346
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000347/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000348/// a class method in its extension.
349///
Mike Stump1eb44332009-09-09 15:08:12 +0000350void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000351 ObjCInterfaceDecl *ID) {
352 if (!ID)
353 return; // Possibly due to previous error
354
355 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000356 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
357 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000358 ObjCMethodDecl *MD = *i;
359 MethodMap[MD->getSelector()] = MD;
360 }
361
362 if (MethodMap.empty())
363 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000364 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
365 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000366 ObjCMethodDecl *Method = *i;
367 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
368 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
369 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
370 << Method->getDeclName();
371 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
372 }
373 }
374}
375
Chris Lattner58fe03b2009-04-12 08:43:13 +0000376/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000377Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000378Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000379 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000380 unsigned NumElts,
381 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000382 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000383 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner4d391482007-12-12 07:09:47 +0000385 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000386 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000387 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Douglas Gregord0434102009-01-09 00:49:46 +0000388 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000389 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000390 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000391 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000392 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000393 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000394 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000395 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000396 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
399 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000400 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000401 Protocols.data(), Protocols.size(),
402 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000403 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000404 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000405 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000406}
407
Chris Lattnerb28317a2009-03-28 19:18:32 +0000408Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000409ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
410 IdentifierInfo *ClassName, SourceLocation ClassLoc,
411 IdentifierInfo *CategoryName,
412 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000413 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000414 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000415 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000416 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000417 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000418 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000419
420 /// Check that class of this category is already completely declared.
421 if (!IDecl || IDecl->isForwardDecl()) {
422 // Create an invalid ObjCCategoryDecl to serve as context for
423 // the enclosing method declarations. We mark the decl invalid
424 // to make it clear that this isn't a valid AST.
425 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
426 ClassLoc, CategoryLoc, CategoryName);
427 CDecl->setInvalidDecl();
428 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
429 return DeclPtrTy::make(CDecl);
430 }
431
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000432 if (!CategoryName && IDecl->getImplementation()) {
433 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
434 Diag(IDecl->getImplementation()->getLocation(),
435 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000436 }
437
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000438 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
439 ClassLoc, CategoryLoc, CategoryName);
440 // FIXME: PushOnScopeChains?
441 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000442
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000443 CDecl->setClassInterface(IDecl);
444 // Insert class extension to the list of class's categories.
445 if (!CategoryName)
446 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner16b34b42009-02-16 21:30:01 +0000448 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000449 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000450
Fariborz Jahanian25760612010-02-15 21:55:26 +0000451 if (CategoryName) {
452 /// Check for duplicate interface declaration for this category
453 ObjCCategoryDecl *CDeclChain;
454 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
455 CDeclChain = CDeclChain->getNextClassCategory()) {
456 if (CDeclChain->getIdentifier() == CategoryName) {
457 // Class extensions can be declared multiple times.
458 Diag(CategoryLoc, diag::warn_dup_category_def)
459 << ClassName << CategoryName;
460 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
461 break;
462 }
Chris Lattner70f19542009-02-16 21:26:43 +0000463 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000464 if (!CDeclChain)
465 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000466 }
Chris Lattner70f19542009-02-16 21:26:43 +0000467
Chris Lattner4d391482007-12-12 07:09:47 +0000468 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000469 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000470 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000471 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000472 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000473 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000474 NumProtoRefs, ProtoLocs,
475 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson15281452008-11-04 16:57:32 +0000478 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000479 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000480}
481
482/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000484/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000485Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000486 SourceLocation AtCatImplLoc,
487 IdentifierInfo *ClassName, SourceLocation ClassLoc,
488 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000490 ObjCCategoryDecl *CatIDecl = 0;
491 if (IDecl) {
492 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
493 if (!CatIDecl) {
494 // Category @implementation with no corresponding @interface.
495 // Create and install one.
496 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000497 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000498 CatName);
499 CatIDecl->setClassInterface(IDecl);
500 CatIDecl->insertNextClassCategory();
501 }
502 }
503
Mike Stump1eb44332009-09-09 15:08:12 +0000504 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000505 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
506 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000507 /// Check that class of this category is already completely declared.
508 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000509 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000510
Douglas Gregord0434102009-01-09 00:49:46 +0000511 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000512 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000513
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000514 /// Check that CatName, category name, is not used in another implementation.
515 if (CatIDecl) {
516 if (CatIDecl->getImplementation()) {
517 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
518 << CatName;
519 Diag(CatIDecl->getImplementation()->getLocation(),
520 diag::note_previous_definition);
521 } else
522 CatIDecl->setImplementation(CDecl);
523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson15281452008-11-04 16:57:32 +0000525 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000526 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000527}
528
Chris Lattnerb28317a2009-03-28 19:18:32 +0000529Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000530 SourceLocation AtClassImplLoc,
531 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000532 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000533 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000535 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000536 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000537 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
538 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000541 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000542 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
543 // If this is a forward declaration of an interface, warn.
544 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000545 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000546 IDecl = 0;
547 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000548 } else {
549 // We did not find anything with the name ClassName; try to correct for
550 // typos in the class name.
551 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000552 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000553 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000554 // Suggest the (potentially) correct interface name. However, put the
555 // fix-it hint itself in a separate note, since changing the name in
556 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000557 // provide a code-modification hint or use the typo name for recovery,
558 // because this is just a warning. The program may actually be correct.
559 Diag(ClassLoc, diag::warn_undef_interface_suggest)
560 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000561 Diag(IDecl->getLocation(), diag::note_previous_decl)
562 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000563 << FixItHint::CreateReplacement(ClassLoc,
564 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000565 IDecl = 0;
566 } else {
567 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
568 }
Chris Lattner4d391482007-12-12 07:09:47 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner4d391482007-12-12 07:09:47 +0000571 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000573 if (SuperClassname) {
574 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000575 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
576 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000577 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000578 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
579 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000580 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000582 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000583 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000584 Diag(SuperClassLoc, diag::err_undef_superclass)
585 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000586 else if (IDecl && IDecl->getSuperClass() != SDecl) {
587 // This implementation and its interface do not have the same
588 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000589 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000590 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000591 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000592 }
593 }
594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattner4d391482007-12-12 07:09:47 +0000596 if (!IDecl) {
597 // Legacy case of @implementation with no corresponding @interface.
598 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000599
Mike Stump390b4cc2009-05-16 07:39:55 +0000600 // FIXME: Do we support attributes on the @implementation? If so we should
601 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000602 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000603 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000604 IDecl->setSuperClass(SDecl);
605 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000606
607 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000608 } else {
609 // Mark the interface as being completed, even if it was just as
610 // @class ....;
611 // declaration; the user cannot reopen it.
612 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 ObjCImplementationDecl* IMPDecl =
616 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000617 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Anders Carlsson15281452008-11-04 16:57:32 +0000619 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000620 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Chris Lattner4d391482007-12-12 07:09:47 +0000622 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000623 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000624 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000625 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000626 Diag(IDecl->getImplementation()->getLocation(),
627 diag::note_previous_definition);
628 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000629 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000630 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000631 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000632 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633}
634
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
636 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000637 SourceLocation RBrace) {
638 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000639 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000640 if (!IDecl)
641 return;
642 /// Check case of non-existing @interface decl.
643 /// (legacy objective-c @implementation decl without an @interface decl).
644 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000645 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000646 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000647 // Add ivar's to class's DeclContext.
648 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000649 ivars[i]->setLexicalDeclContext(ImpDecl);
650 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000651 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000652 }
653
Chris Lattner4d391482007-12-12 07:09:47 +0000654 return;
655 }
656 // If implementation has empty ivar list, just return.
657 if (numIvars == 0)
658 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Chris Lattner4d391482007-12-12 07:09:47 +0000660 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000661 if (LangOpts.ObjCNonFragileABI2) {
662 if (ImpDecl->getSuperClass())
663 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
664 for (unsigned i = 0; i < numIvars; i++) {
665 ObjCIvarDecl* ImplIvar = ivars[i];
666 if (const ObjCIvarDecl *ClsIvar =
667 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
668 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
669 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
670 continue;
671 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000672 // Instance ivar to Implementation's DeclContext.
673 ImplIvar->setLexicalDeclContext(ImpDecl);
674 IDecl->makeDeclVisibleInContext(ImplIvar, false);
675 ImpDecl->addDecl(ImplIvar);
676 }
677 return;
678 }
Chris Lattner4d391482007-12-12 07:09:47 +0000679 // Check interface's Ivar list against those in the implementation.
680 // names and types must match.
681 //
Chris Lattner4d391482007-12-12 07:09:47 +0000682 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000683 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000684 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
685 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686 ObjCIvarDecl* ImplIvar = ivars[j++];
687 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000688 assert (ImplIvar && "missing implementation ivar");
689 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Steve Naroffca331292009-03-03 14:49:36 +0000691 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000692 if (Context.getCanonicalType(ImplIvar->getType()) !=
693 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000694 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000695 << ImplIvar->getIdentifier()
696 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000697 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000698 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
699 Expr *ImplBitWidth = ImplIvar->getBitWidth();
700 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000701 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
702 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000703 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
704 << ImplIvar->getIdentifier();
705 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707 }
Steve Naroffca331292009-03-03 14:49:36 +0000708 // Make sure the names are identical.
709 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000710 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000711 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000712 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000713 }
714 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Chris Lattner609e4c72007-12-12 18:11:49 +0000717 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000718 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000719 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000720 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000721}
722
Steve Naroff3c2eb662008-02-10 21:38:56 +0000723void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000724 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000725 if (!IncompleteImpl) {
726 Diag(ImpLoc, diag::warn_incomplete_impl);
727 IncompleteImpl = true;
728 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000729 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000730 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000731}
732
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000733void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
734 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000735 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000736 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000737 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
738 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000739 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000740 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
741 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000742 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner3aff9192009-04-11 19:58:42 +0000745 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
746 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
747 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000748 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
749 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
750 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
751 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000752 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000753
754 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000755 << ImpMethodDecl->getDeclName() << (*IF)->getType()
756 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000757 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000758 }
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000759 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
760 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
761 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
762 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000763}
764
Mike Stump390b4cc2009-05-16 07:39:55 +0000765/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
766/// improve the efficiency of selector lookups and type checking by associating
767/// with each protocol / interface / category the flattened instance tables. If
768/// we used an immutable set to keep the table then it wouldn't add significant
769/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000770
Steve Naroffefe7f362008-02-08 22:06:17 +0000771/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000772/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000773void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
774 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000775 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000776 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000777 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000778 ObjCContainerDecl *CDecl) {
779 ObjCInterfaceDecl *IDecl;
780 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
781 IDecl = C->getClassInterface();
782 else
783 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
784 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
785
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000786 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000787 ObjCInterfaceDecl *NSIDecl = 0;
788 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000789 // check to see if class implements forwardInvocation method and objects
790 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000791 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000792 // Under such conditions, which means that every method possible is
793 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000794 // found" warnings.
795 // FIXME: Use a general GetUnarySelector method for this.
796 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
797 Selector fISelector = Context.Selectors.getSelector(1, &II);
798 if (InsMap.count(fISelector))
799 // Is IDecl derived from 'NSProxy'? If so, no instance methods
800 // need be implemented in the implementation.
801 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000804 // If a method lookup fails locally we still need to look and see if
805 // the method was implemented by a base class or an inherited
806 // protocol. This lookup is slow, but occurs rarely in correct code
807 // and otherwise would terminate in a warning.
808
Chris Lattner4d391482007-12-12 07:09:47 +0000809 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000810 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000811 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000812 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000813 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000814 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000815 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000816 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000817 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000818 // Ugly, but necessary. Method declared in protcol might have
819 // have been synthesized due to a property declared in the class which
820 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000821 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000822 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000823 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000824 unsigned DIAG = diag::warn_unimplemented_protocol_method;
825 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
826 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
827 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
828 << PDecl->getDeclName();
829 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000830 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000831 }
832 }
Chris Lattner4d391482007-12-12 07:09:47 +0000833 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000834 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000836 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000837 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000838 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
839 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000840 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000841 unsigned DIAG = diag::warn_unimplemented_protocol_method;
842 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
843 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
844 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
845 PDecl->getDeclName();
846 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000847 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000848 }
Chris Lattner780f3292008-07-21 21:32:27 +0000849 // Check on this protocols's referenced protocols, recursively.
850 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
851 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000852 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000853}
854
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000855/// MatchAllMethodDeclarations - Check methods declaraed in interface or
856/// or protocol against those declared in their implementations.
857///
858void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
859 const llvm::DenseSet<Selector> &ClsMap,
860 llvm::DenseSet<Selector> &InsMapSeen,
861 llvm::DenseSet<Selector> &ClsMapSeen,
862 ObjCImplDecl* IMPDecl,
863 ObjCContainerDecl* CDecl,
864 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000866 // Check and see if instance methods in class interface have been
867 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000868 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
869 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000870 if (InsMapSeen.count((*I)->getSelector()))
871 continue;
872 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000873 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000874 !InsMap.count((*I)->getSelector())) {
875 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000876 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
877 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000878 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000879 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000880 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000881 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000882 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000883 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000884 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000885 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
886 // ImpMethodDecl may be null as in a @dynamic property.
887 if (ImpMethodDecl)
888 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
889 }
890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000892 // Check and see if class methods in class interface have been
893 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000894 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000895 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000896 if (ClsMapSeen.count((*I)->getSelector()))
897 continue;
898 ClsMapSeen.insert((*I)->getSelector());
899 if (!ClsMap.count((*I)->getSelector())) {
900 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000901 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
902 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000903 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000904 ObjCMethodDecl *ImpMethodDecl =
905 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000906 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000907 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000908 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
909 }
910 }
911 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
912 // Check for any implementation of a methods declared in protocol.
913 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
914 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000915 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
916 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000917 (*PI), IncompleteImpl, false);
918 if (I->getSuperClass())
919 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000920 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000921 I->getSuperClass(), IncompleteImpl, false);
922 }
923}
924
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000925void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000926 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000927 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000928 llvm::DenseSet<Selector> InsMap;
929 // Check and see if instance methods in class interface have been
930 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000931 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000933 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000935 // Check and see if properties declared in the interface have either 1)
936 // an implementation or 2) there is a @synthesize/@dynamic implementation
937 // of the property in the @implementation.
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000938 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000939 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000940
Chris Lattner4d391482007-12-12 07:09:47 +0000941 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000942 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000943 I = IMPDecl->classmeth_begin(),
944 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000945 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000947 // Check for type conflict of methods declared in a class/protocol and
948 // its implementation; if any.
949 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000950 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
951 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000952 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattner4d391482007-12-12 07:09:47 +0000954 // Check the protocol list for unimplemented methods in the @implementation
955 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000956 // Check and see if class methods in class interface have been
957 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Chris Lattnercddc8882009-03-01 00:56:52 +0000959 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000960 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000961 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000962 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000963 InsMap, ClsMap, I);
964 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000965 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
966 Categories; Categories = Categories->getNextClassExtension())
967 ImplMethodsVsClassMethods(S, IMPDecl,
968 const_cast<ObjCCategoryDecl*>(Categories),
969 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +0000970 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000971 // For extended class, unimplemented methods in its protocols will
972 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000973 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000974 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
975 E = C->protocol_end(); PI != E; ++PI)
976 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000977 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000978 // Report unimplemented properties in the category as well.
979 // When reporting on missing setter/getters, do not report when
980 // setter/getter is implemented in category's primary class
981 // implementation.
982 if (ObjCInterfaceDecl *ID = C->getClassInterface())
983 if (ObjCImplDecl *IMP = ID->getImplementation()) {
984 for (ObjCImplementationDecl::instmeth_iterator
985 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
986 InsMap.insert((*I)->getSelector());
987 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000988 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000989 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000990 } else
991 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000992}
993
Mike Stump1eb44332009-09-09 15:08:12 +0000994/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000995Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000996Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000997 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +0000998 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000999 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001000 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattner4d391482007-12-12 07:09:47 +00001002 for (unsigned i = 0; i != NumElts; ++i) {
1003 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001004 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001005 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001006 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001007 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001008 // Maybe we will complain about the shadowed template parameter.
1009 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1010 // Just pretend that we didn't see the previous declaration.
1011 PrevDecl = 0;
1012 }
1013
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001014 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001015 // GCC apparently allows the following idiom:
1016 //
1017 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1018 // @class XCElementToggler;
1019 //
Mike Stump1eb44332009-09-09 15:08:12 +00001020 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001021 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001022 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001023 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001024 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001025 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001026 // a forward class declaration matching a typedef name of a class refers
1027 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001028 if (const ObjCObjectType *OI =
1029 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1030 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001031 }
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001034 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001035 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001036 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001037
1038 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1039 // the current DeclContext. This prevents clients that walk DeclContext
1040 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1041 // declared later (if at all). We also take care to explicitly make
1042 // sure this declaration is visible for name lookup.
1043 PushOnScopeChains(IDecl, TUScope, false);
1044 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001045 }
1046
1047 Interfaces.push_back(IDecl);
1048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek321c22f2009-11-18 00:28:11 +00001050 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001051 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001052 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001053 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001055 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001056 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001057}
1058
1059
1060/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1061/// returns true, or false, accordingly.
1062/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001063bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001064 const ObjCMethodDecl *PrevMethod,
1065 bool matchBasedOnSizeAndAlignment) {
1066 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.
1071 if (!matchBasedOnSizeAndAlignment)
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.
1091 if (!matchBasedOnSizeAndAlignment)
1092 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
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001104/// \brief Read the contents of the instance and factory method pools
1105/// for a given selector from external storage.
1106///
1107/// This routine should only be called once, when neither the instance
1108/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001109Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001110 bool isInstance) {
1111 assert(ExternalSource && "We need an external AST source");
1112 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1113 "Selector data already loaded into the instance method pool");
1114 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1115 "Selector data already loaded into the factory method pool");
1116
1117 // Read the method list from the external source.
1118 std::pair<ObjCMethodList, ObjCMethodList> Methods
1119 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001121 if (isInstance) {
1122 if (Methods.second.Method)
1123 FactoryMethodPool[Sel] = Methods.second;
1124 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001125 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001126
1127 if (Methods.first.Method)
1128 InstanceMethodPool[Sel] = Methods.first;
1129
1130 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1131}
1132
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001133void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001134 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1135 = InstanceMethodPool.find(Method->getSelector());
1136 if (Pos == InstanceMethodPool.end()) {
1137 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1138 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1139 else
1140 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1141 ObjCMethodList())).first;
1142 }
1143
1144 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001145 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001146 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001147 Entry.Method = Method;
1148 Entry.Next = 0;
1149 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Chris Lattnerb25df352009-03-04 05:16:45 +00001152 // We've seen a method with this name, see if we have already seen this type
1153 // signature.
1154 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1155 if (MatchTwoMethodDeclarations(Method, List->Method))
1156 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattnerb25df352009-03-04 05:16:45 +00001158 // We have a new signature for an existing method - add it.
1159 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001160 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1161 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001162}
1163
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001164// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001165ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001166 SourceRange R,
1167 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001168 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1169 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001170 if (Pos == InstanceMethodPool.end()) {
1171 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001172 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1173 else
1174 return 0;
1175 }
1176
1177 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001178 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Steve Naroff037cda52008-09-30 14:38:43 +00001180 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001181 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1182 // This checks if the methods differ by size & alignment.
1183 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001184 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001185 }
1186 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001187 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001188 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001189 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001190 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001191 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001192 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001193 }
1194 return MethList.Method;
1195}
1196
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001197void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001198 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1199 = FactoryMethodPool.find(Method->getSelector());
1200 if (Pos == FactoryMethodPool.end()) {
1201 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1202 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1203 else
1204 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1205 ObjCMethodList())).first;
1206 }
1207
1208 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001209 if (!FirstMethod.Method) {
1210 // Haven't seen a method with this selector name yet - add it.
1211 FirstMethod.Method = Method;
1212 FirstMethod.Next = 0;
1213 } else {
1214 // We've seen a method with this name, now check the type signature(s).
1215 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001216
1217 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001218 Next = Next->Next)
1219 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner4d391482007-12-12 07:09:47 +00001221 if (!match) {
1222 // We have a new signature for an existing method - add it.
1223 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001224 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1225 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001226 FirstMethod.Next = OMI;
1227 }
1228 }
1229}
1230
Mike Stump1eb44332009-09-09 15:08:12 +00001231ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001232 SourceRange R) {
1233 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1234 = FactoryMethodPool.find(Sel);
1235 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001236 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001237 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1238 else
1239 return 0;
1240 }
1241
1242 ObjCMethodList &MethList = Pos->second;
1243 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001245 if (MethList.Method && MethList.Next) {
1246 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1247 // This checks if the methods differ by size & alignment.
1248 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1249 issueWarning = true;
1250 }
1251 if (issueWarning && (MethList.Method && MethList.Next)) {
1252 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001253 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001254 << MethList.Method->getSourceRange();
1255 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001256 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001257 << Next->Method->getSourceRange();
1258 }
1259 return MethList.Method;
1260}
1261
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001262/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1263/// identical selector names in current and its super classes and issues
1264/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001265void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1266 ObjCMethodDecl *Method,
1267 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001268 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1269 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001271 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001272 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001273 SD->lookupMethod(Method->getSelector(), IsInstance);
1274 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001275 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001276 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001277 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001278 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1279 E = Method->param_end();
1280 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1281 for (; ParamI != E; ++ParamI, ++PrevI) {
1282 // Number of parameters are the same and is guaranteed by selector match.
1283 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1284 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1285 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1286 // If type of arguement of method in this class does not match its
1287 // respective argument type in the super class method, issue warning;
1288 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001289 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001290 << T1 << T2;
1291 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1292 return;
1293 }
1294 }
1295 ID = SD;
1296 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001297}
1298
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001299/// DiagnoseDuplicateIvars -
1300/// Check for duplicate ivars in the entire class at the start of
1301/// @implementation. This becomes necesssary because class extension can
1302/// add ivars to a class in random order which will not be known until
1303/// class's @implementation is seen.
1304void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1305 ObjCInterfaceDecl *SID) {
1306 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1307 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1308 ObjCIvarDecl* Ivar = (*IVI);
1309 if (Ivar->isInvalidDecl())
1310 continue;
1311 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1312 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1313 if (prevIvar) {
1314 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1315 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1316 Ivar->setInvalidDecl();
1317 }
1318 }
1319 }
1320}
1321
Steve Naroffa56f6162007-12-18 01:30:32 +00001322// Note: For class/category implemenations, allMethods/allProperties is
1323// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001324void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
Ted Kremenek782f2f52010-01-07 01:20:12 +00001325 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001326 DeclPtrTy *allMethods, unsigned allNum,
1327 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001328 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001330
Steve Naroffa56f6162007-12-18 01:30:32 +00001331 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1332 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001333 // should be true.
1334 if (!ClassDecl)
1335 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001336
Mike Stump1eb44332009-09-09 15:08:12 +00001337 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001338 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1339 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001341
Ted Kremenek782f2f52010-01-07 01:20:12 +00001342 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1343 // FIXME: This is wrong. We shouldn't be pretending that there is
1344 // an '@end' in the declaration.
1345 SourceLocation L = ClassDecl->getLocation();
1346 AtEnd.setBegin(L);
1347 AtEnd.setEnd(L);
1348 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001349 }
1350
Steve Naroff0701bbb2009-01-08 17:28:14 +00001351 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001352
1353 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1354 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1355 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1356
Chris Lattner4d391482007-12-12 07:09:47 +00001357 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001359 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001360
1361 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001362 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001363 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001364 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001365 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001366 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001367 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001368 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001369 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001370 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001371 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001372 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001373 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001374 InsMap[Method->getSelector()] = Method;
1375 /// The following allows us to typecheck messages to "id".
1376 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001378 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001379 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001380 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001381 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001382 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001384 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001385 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001386 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001387 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001388 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001389 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001390 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001391 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001392 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001393 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001394 /// The following allows us to typecheck messages to "Class".
1395 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001396 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001397 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001398 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001399 }
1400 }
1401 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001403 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001404 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001405 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001406 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001407 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001408 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001409 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001410 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001411
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001412 // Compare protocol properties with those in category
1413 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001414 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001415 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001416 }
Steve Naroff09c47192009-01-09 15:36:25 +00001417 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001418 if (CDecl->getIdentifier())
1419 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1420 // user-defined setter/getter. It also synthesizes setter/getter methods
1421 // and adds them to the DeclContext and global method pools.
1422 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1423 E = CDecl->prop_end();
1424 I != E; ++I)
1425 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001426 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001427 }
1428 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001429 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001430 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001431 if (LangOpts.ObjCNonFragileABI2)
1432 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001433 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001434 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001435 if (LangOpts.ObjCNonFragileABI2)
1436 while (IDecl->getSuperClass()) {
1437 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1438 IDecl = IDecl->getSuperClass();
1439 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001440 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001441 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001442 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001443 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001444 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Chris Lattner4d391482007-12-12 07:09:47 +00001446 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001447 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001448 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001449 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001450 Categories; Categories = Categories->getNextClassCategory()) {
1451 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001452 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001453 break;
1454 }
1455 }
1456 }
1457 }
Chris Lattner682bf922009-03-29 16:50:03 +00001458 if (isInterfaceDeclKind) {
1459 // Reject invalid vardecls.
1460 for (unsigned i = 0; i != tuvNum; i++) {
1461 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1462 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1463 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001464 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001465 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001466 }
Chris Lattner682bf922009-03-29 16:50:03 +00001467 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001468 }
Chris Lattner4d391482007-12-12 07:09:47 +00001469}
1470
1471
1472/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1473/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001474static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1476 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1477 if (PQTVal & ObjCDeclSpec::DQ_In)
1478 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1479 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1480 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1481 if (PQTVal & ObjCDeclSpec::DQ_Out)
1482 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1483 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1484 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1485 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1486 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1487 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1488 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001489
1490 return ret;
1491}
1492
Ted Kremenek422bae72010-04-18 04:59:38 +00001493static inline
1494bool containsInvalidMethodImplAttribute(const AttributeList *A) {
1495 // The 'ibaction' attribute is allowed on method definitions because of
1496 // how the IBAction macro is used on both method declarations and definitions.
1497 // If the method definitions contains any other attributes, return true.
1498 while (A && A->getKind() == AttributeList::AT_IBAction)
1499 A = A->getNext();
1500 return A != NULL;
1501}
1502
Chris Lattnerb28317a2009-03-28 19:18:32 +00001503Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001504 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001505 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001506 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001507 Selector Sel,
1508 // optional arguments. The number of types/arguments is obtained
1509 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001510 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001511 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001512 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1513 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001514 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001515
1516 // Make sure we can establish a context for the method.
1517 if (!ClassDecl) {
1518 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001519 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001520 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001521 }
Chris Lattner4d391482007-12-12 07:09:47 +00001522 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001524 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001525 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001526 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Steve Naroffccef3712009-02-20 22:59:16 +00001528 // Methods cannot return interface types. All ObjC objects are
1529 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001530 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001531 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1532 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001533 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001534 }
1535 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001537
1538 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001539 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001540 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001541 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001542 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001543 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001544 MethodDeclKind == tok::objc_optional ?
1545 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001546 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Chris Lattner0ed844b2008-04-04 06:12:32 +00001548 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattner7db638d2009-04-11 19:42:43 +00001550 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001551 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001552 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Chris Lattnere294d3f2009-04-11 18:57:04 +00001554 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001555 ArgType = Context.getObjCIdType();
1556 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001557 } else {
John McCall58e46772009-10-23 21:48:59 +00001558 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001559 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001560 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
John McCall58e46772009-10-23 21:48:59 +00001563 ParmVarDecl* Param
1564 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1565 ArgInfo[i].Name, ArgType, DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001566 VarDecl::None, VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001567
John McCallc12c5bb2010-05-15 11:32:37 +00001568 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001569 Diag(ArgInfo[i].NameLoc,
1570 diag::err_object_cannot_be_passed_returned_by_value)
1571 << 1 << ArgType;
1572 Param->setInvalidDecl();
1573 }
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Chris Lattner0ed844b2008-04-04 06:12:32 +00001575 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001576 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001578 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001579 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Chris Lattner0ed844b2008-04-04 06:12:32 +00001581 Params.push_back(Param);
1582 }
1583
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001584 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
1585 ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>();
1586 QualType ArgType = Param->getType();
1587 if (ArgType.isNull())
1588 ArgType = Context.getObjCIdType();
1589 else
1590 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1591 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001592 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001593 Diag(Param->getLocation(),
1594 diag::err_object_cannot_be_passed_returned_by_value)
1595 << 1 << ArgType;
1596 Param->setInvalidDecl();
1597 }
1598 Param->setDeclContext(ObjCMethod);
1599 IdResolver.RemoveDecl(Param);
1600 Params.push_back(Param);
1601 }
1602
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001603 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1604 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 ObjCMethod->setObjCDeclQualifier(
1606 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1607 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001608
1609 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001610 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
John McCall54abf7d2009-11-04 02:18:39 +00001612 const ObjCMethodDecl *InterfaceMD = 0;
1613
Mike Stump1eb44332009-09-09 15:08:12 +00001614 // For implementations (which can be very "coarse grain"), we add the
1615 // method now. This allows the AST to implement lookup methods that work
1616 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001617 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001618 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001619 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001620 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001621 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1622 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001623 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001624 PrevMethod = ImpDecl->getClassMethod(Sel);
1625 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001626 }
John McCall54abf7d2009-11-04 02:18:39 +00001627 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1628 MethodType == tok::minus);
Ted Kremenek422bae72010-04-18 04:59:38 +00001629 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001630 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001631 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001632 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001633 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001634 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1635 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001636 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001637 PrevMethod = CatImpDecl->getClassMethod(Sel);
1638 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001639 }
Ted Kremenek422bae72010-04-18 04:59:38 +00001640 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001641 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001642 }
1643 if (PrevMethod) {
1644 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001645 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001646 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001647 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001648 }
John McCall54abf7d2009-11-04 02:18:39 +00001649
1650 // If the interface declared this method, and it was deprecated there,
1651 // mark it deprecated here.
1652 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1653 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1654
Chris Lattnerb28317a2009-03-28 19:18:32 +00001655 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001656}
1657
Chris Lattnercc98eac2008-12-17 07:13:27 +00001658bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001659 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001660 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Anders Carlsson15281452008-11-04 16:57:32 +00001662 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1663 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Anders Carlsson15281452008-11-04 16:57:32 +00001665 return true;
1666}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001667
Chris Lattnercc98eac2008-12-17 07:13:27 +00001668/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1669/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00001670void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001671 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001672 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001673 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001674 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001675 if (!Class) {
1676 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1677 return;
1678 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001679 if (LangOpts.ObjCNonFragileABI) {
1680 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1681 return;
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Chris Lattnercc98eac2008-12-17 07:13:27 +00001684 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00001685 llvm::SmallVector<FieldDecl*, 32> RecFields;
1686 Context.CollectObjCIvars(Class, RecFields);
1687 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1688 for (unsigned i = 0; i < RecFields.size(); i++) {
1689 FieldDecl* ID = RecFields[i];
1690 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1691 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1692 ID->getIdentifier(), ID->getType(),
1693 ID->getBitWidth());
1694 Decls.push_back(Sema::DeclPtrTy::make(FD));
1695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Chris Lattnercc98eac2008-12-17 07:13:27 +00001697 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001698 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001699 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001700 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001701 if (getLangOptions().CPlusPlus)
1702 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001703 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001704 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001705 }
1706}
1707
Douglas Gregor160b5632010-04-26 17:32:49 +00001708/// \brief Build a type-check a new Objective-C exception variable declaration.
1709VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1710 QualType T,
1711 IdentifierInfo *Name,
1712 SourceLocation NameLoc,
1713 bool Invalid) {
1714 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1715 // duration shall not be qualified by an address-space qualifier."
1716 // Since all parameters have automatic store duration, they can not have
1717 // an address space.
1718 if (T.getAddressSpace() != 0) {
1719 Diag(NameLoc, diag::err_arg_with_address_space);
1720 Invalid = true;
1721 }
1722
1723 // An @catch parameter must be an unqualified object pointer type;
1724 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1725 if (Invalid) {
1726 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001727 } else if (T->isDependentType()) {
1728 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001729 } else if (!T->isObjCObjectPointerType()) {
1730 Invalid = true;
1731 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1732 } else if (T->isObjCQualifiedIdType()) {
1733 Invalid = true;
1734 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1735 }
1736
1737 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor324b54d2010-05-03 18:51:14 +00001738 VarDecl::None, VarDecl::None);
1739 New->setExceptionVariable(true);
1740
Douglas Gregor160b5632010-04-26 17:32:49 +00001741 if (Invalid)
1742 New->setInvalidDecl();
1743 return New;
1744}
1745
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001746Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001747 const DeclSpec &DS = D.getDeclSpec();
1748
1749 // We allow the "register" storage class on exception variables because
1750 // GCC did, but we drop it completely. Any other storage class is an error.
1751 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1752 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1753 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1754 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1755 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1756 << DS.getStorageClassSpec();
1757 }
1758 if (D.getDeclSpec().isThreadSpecified())
1759 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1760 D.getMutableDeclSpec().ClearStorageClassSpecs();
1761
1762 DiagnoseFunctionSpecifiers(D);
1763
1764 // Check that there are no default arguments inside the type of this
1765 // exception object (C++ only).
1766 if (getLangOptions().CPlusPlus)
1767 CheckExtraCXXDefaultArguments(D);
1768
Douglas Gregor160b5632010-04-26 17:32:49 +00001769 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001770 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1771 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001772
1773 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1774 // Objective-C++: Types shall not be defined in exception types.
1775 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1776 << Context.getTypeDeclType(OwnedDecl);
1777 }
1778
1779 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1780 D.getIdentifierLoc(),
1781 D.isInvalidType());
1782
1783 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1784 if (D.getCXXScopeSpec().isSet()) {
1785 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1786 << D.getCXXScopeSpec().getRange();
1787 New->setInvalidDecl();
1788 }
1789
1790 // Add the parameter declaration into this scope.
1791 S->AddDecl(DeclPtrTy::make(New));
1792 if (D.getIdentifier())
1793 IdResolver.AddDecl(New);
1794
1795 ProcessDeclAttributes(S, New, D);
1796
1797 if (New->hasAttr<BlocksAttr>())
1798 Diag(New->getLocation(), diag::err_block_on_nonlocal);
1799 return DeclPtrTy::make(New);
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001800}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001801
1802/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001803/// initialization.
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001804void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001805 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001806 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1807 E = OI->ivar_end(); I != E; ++I) {
1808 ObjCIvarDecl *Iv = (*I);
1809 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001810 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001811 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001812 }
1813
1814 // Find ivars to construct/destruct in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001815 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1816 CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001817 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1818 E = CDecl->ivar_end(); I != E; ++I) {
1819 ObjCIvarDecl *Iv = (*I);
1820 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001821 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001822 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001823 }
1824 }
1825
1826 // Also add any ivar defined in this class's implementation. This
1827 // includes synthesized ivars.
1828 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
1829 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1830 E = ImplDecl->ivar_end(); I != E; ++I) {
1831 ObjCIvarDecl *Iv = (*I);
1832 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001833 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001834 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001835 }
1836 }
1837}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001838
1839void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1840 CXXBaseOrMemberInitializer ** initializers,
1841 unsigned numInitializers) {
1842 if (numInitializers > 0) {
1843 NumIvarInitializers = numInitializers;
1844 CXXBaseOrMemberInitializer **ivarInitializers =
1845 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1846 memcpy(ivarInitializers, initializers,
1847 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1848 IvarInitializers = ivarInitializers;
1849 }
1850}
1851