blob: ef43c051f7bc11cefe3d02a86ee9dc12f78dfb2a [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda463fe2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregor35b0bac2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorc78d3462009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000021using namespace clang;
22
Steve Naroff70f41d62009-02-28 16:59:13 +000023/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000024/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000028
Steve Naroff542cd5d2008-07-25 17:57:26 +000029 // If we don't have a valid method decl, simply return.
30 if (!MDecl)
31 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000032
33 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000038
Chris Lattnerda463fe2007-12-12 07:09:47 +000039 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +000041 PushFunctionScope();
42
Chris Lattnerda463fe2007-12-12 07:09:47 +000043 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000048
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000051
Chris Lattner58258242008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +000057}
58
Chris Lattner83f095c2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +000064 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000065 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000066 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattnerda463fe2007-12-12 07:09:47 +000068 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +000070 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +000071
Ted Kremenek1b0ea822008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenek1b0ea822008-01-07 19:49:32 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +000078 if (IDecl) {
79 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +000080 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +000081 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +000082 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +000083 Diag(IDecl->getLocation(), diag::note_previous_definition);
84
Steve Naroff119f60e2008-11-18 19:15:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +000087 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +000088 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +000089 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +000091 IDecl->setClassLoc(ClassLoc);
Ted Kremenek707ece62009-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 Gregor53080572010-04-09 21:30:38 +000096 IDecl->setLexicalDeclContext(CurContext);
Ted Kremenek707ece62009-11-17 22:58:30 +000097 CurContext->addDecl(IDecl);
98
Fariborz Jahaniand3612392009-11-17 19:08:08 +000099 if (AttrList)
100 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000101 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000102 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000103 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000104 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000105 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000106 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Steve Naroff3c301dc2009-04-23 15:15:40 +0000108 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerda463fe2007-12-12 07:09:47 +0000111 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000112 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000113 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
114 LookupOrdinaryName);
Douglas Gregor35b0bac2010-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 Gregor280e1ee2010-04-14 20:04:41 +0000119 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000120 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
121 Diag(SuperLoc, diag::err_undef_superclass_suggest)
122 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor6da83622010-01-07 00:17:44 +0000123 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
124 << PrevDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000125 }
126 }
127
Fariborz Jahanian5582f232009-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 Stump12b8ce12009-08-04 21:02:39 +0000132 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000133 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000135
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Fariborz Jahanian5582f232009-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 McCall8b07ec22010-05-15 11:32:37 +0000145 if (T->isObjCObjectType()) {
146 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
148 }
149 }
Mike Stump11289f42009-09-09 15:08:12 +0000150
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000159 }
160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000170 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000174 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
Mike Stump11289f42009-09-09 15:08:12 +0000178
Steve Naroff119f60e2008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000182 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Mike Stump11289f42009-09-09 15:08:12 +0000185
Anders Carlssona6b508a2008-11-04 16:57:32 +0000186 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000193 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000198 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000199 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000200 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000201 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000202 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000203 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000204 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000205 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000206 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000207 }
208 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000209 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000210 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000211 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
212 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000213 if (T->isObjCObjectType()) {
214 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000215 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000216 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000217 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000218 }
219 }
220 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000221 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
222 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000223 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000224 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000225 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000226 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner219b3e92008-03-16 21:17:37 +0000229 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000230 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000231 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000232
Anders Carlssona6b508a2008-11-04 16:57:32 +0000233 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000234 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000235
Chris Lattner83f095c2009-03-28 19:18:32 +0000236 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000237}
238
Steve Naroff41d09ad2009-03-05 15:22:01 +0000239void Sema::CheckForwardProtocolDeclarationForCircularDependency(
240 IdentifierInfo *PName,
241 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000242 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000243 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
244 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000245
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000246 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
247 Ploc)) {
Steve Naroff41d09ad2009-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 Stump11289f42009-09-09 15:08:12 +0000252 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000253 PDecl->getLocation(), PDecl->getReferencedProtocols());
254 }
255 }
256}
257
Chris Lattner83f095c2009-03-28 19:18:32 +0000258Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000259Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
260 IdentifierInfo *ProtocolName,
261 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000262 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000263 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000264 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000265 SourceLocation EndProtoLoc,
266 AttributeList *AttrList) {
267 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000268 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000269 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000270 if (PDecl) {
271 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000272 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000273 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000274 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000275 // Just return the protocol we already had.
276 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000277 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000278 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000279 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000280 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000281 CheckForwardProtocolDeclarationForCircularDependency(
282 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
283 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000284
Steve Naroffeb03dac2008-08-13 16:39:22 +0000285 // Make sure the cached decl gets a valid start location.
286 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000287 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000288 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000289 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000290 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000291 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000292 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000293 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000294 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000295 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000296 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000297 /// Check then save referenced protocols.
Douglas Gregor002b6712010-01-16 15:02:53 +0000298 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
299 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000300 PDecl->setLocEnd(EndProtoLoc);
301 }
Mike Stump11289f42009-09-09 15:08:12 +0000302
303 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000304 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000305}
306
307/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-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 Lattnerda463fe2007-12-12 07:09:47 +0000310void
Chris Lattner3bbae002008-07-26 04:03:38 +0000311Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000312 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000313 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000314 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000315 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000316 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
317 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000318 if (!PDecl) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000319 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
320 LookupObjCProtocolName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000321 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-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 Gregor6da83622010-01-07 00:17:44 +0000325 Diag(PDecl->getLocation(), diag::note_previous_decl)
326 << PDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000327 }
328 }
329
330 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000331 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000332 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000333 continue;
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Douglas Gregor171c45a2009-02-18 21:56:37 +0000336 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-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 Lattner3b054132008-11-19 05:08:23 +0000341 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000342 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000343 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000344 }
345}
346
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000347/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000348/// a class method in its extension.
349///
Mike Stump11289f42009-09-09 15:08:12 +0000350void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000356 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
357 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000358 ObjCMethodDecl *MD = *i;
359 MethodMap[MD->getSelector()] = MD;
360 }
361
362 if (MethodMap.empty())
363 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000364 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
365 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-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 Lattnerdac168d2009-04-12 08:43:13 +0000376/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000377Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000378Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000379 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000380 unsigned NumElts,
381 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000382 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000383 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattnerda463fe2007-12-12 07:09:47 +0000385 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000386 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000387 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000388 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000389 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000390 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000391 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000392 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000393 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000394 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000395 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000396 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
399 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000400 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000401 Protocols.data(), Protocols.size(),
402 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000403 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000404 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000405 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000406}
407
Chris Lattner83f095c2009-03-28 19:18:32 +0000408Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000409ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
410 IdentifierInfo *ClassName, SourceLocation ClassLoc,
411 IdentifierInfo *CategoryName,
412 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000413 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000414 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000415 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000416 SourceLocation EndProtoLoc) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000417 ObjCCategoryDecl *CDecl = 0;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000418 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-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
432 if (!CategoryName) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000433 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek514ff702010-02-23 19:39:46 +0000434 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000435 CDecl = IDecl->getClassExtension();
Fariborz Jahaniane16cdb42010-04-02 20:53:05 +0000436 if (IDecl->getImplementation()) {
437 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
438 Diag(IDecl->getImplementation()->getLocation(),
439 diag::note_implementation_declared);
440 }
Ted Kremenek514ff702010-02-23 19:39:46 +0000441 }
442
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000443 if (!CDecl) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000444 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
445 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000446 // FIXME: PushOnScopeChains?
447 CurContext->addDecl(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000448
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000449 CDecl->setClassInterface(IDecl);
450 // Insert first use of class extension to the list of class's categories.
451 if (!CategoryName)
452 CDecl->insertNextClassCategory();
453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerced903b2009-02-16 21:30:01 +0000455 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000456 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000457
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000458 if (CategoryName) {
459 /// Check for duplicate interface declaration for this category
460 ObjCCategoryDecl *CDeclChain;
461 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
462 CDeclChain = CDeclChain->getNextClassCategory()) {
463 if (CDeclChain->getIdentifier() == CategoryName) {
464 // Class extensions can be declared multiple times.
465 Diag(CategoryLoc, diag::warn_dup_category_def)
466 << ClassName << CategoryName;
467 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
468 break;
469 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000470 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000471 if (!CDeclChain)
472 CDecl->insertNextClassCategory();
Chris Lattner9018ca82009-02-16 21:26:43 +0000473 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000474
Chris Lattnerda463fe2007-12-12 07:09:47 +0000475 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000476 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000477 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000478 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000479 if (CDecl->IsClassExtension())
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000480 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000481 NumProtoRefs, ProtoLocs,
482 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Anders Carlssona6b508a2008-11-04 16:57:32 +0000485 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000486 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000487}
488
489/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000490/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000491/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000492Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000493 SourceLocation AtCatImplLoc,
494 IdentifierInfo *ClassName, SourceLocation ClassLoc,
495 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000496 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000497 ObjCCategoryDecl *CatIDecl = 0;
498 if (IDecl) {
499 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
500 if (!CatIDecl) {
501 // Category @implementation with no corresponding @interface.
502 // Create and install one.
503 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000504 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000505 CatName);
506 CatIDecl->setClassInterface(IDecl);
507 CatIDecl->insertNextClassCategory();
508 }
509 }
510
Mike Stump11289f42009-09-09 15:08:12 +0000511 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000512 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
513 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000514 /// Check that class of this category is already completely declared.
515 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000516 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000517
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000518 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000519 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000520
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000521 /// Check that CatName, category name, is not used in another implementation.
522 if (CatIDecl) {
523 if (CatIDecl->getImplementation()) {
524 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
525 << CatName;
526 Diag(CatIDecl->getImplementation()->getLocation(),
527 diag::note_previous_definition);
528 } else
529 CatIDecl->setImplementation(CDecl);
530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Anders Carlssona6b508a2008-11-04 16:57:32 +0000532 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000533 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000534}
535
Chris Lattner83f095c2009-03-28 19:18:32 +0000536Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000537 SourceLocation AtClassImplLoc,
538 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000539 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000540 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000541 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000542 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000543 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000544 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
545 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000546 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000547 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000548 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor40f7a002010-01-04 17:27:12 +0000549 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
550 // If this is a forward declaration of an interface, warn.
551 if (IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000552 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000553 IDecl = 0;
554 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000555 } else {
556 // We did not find anything with the name ClassName; try to correct for
557 // typos in the class name.
558 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000559 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor40f7a002010-01-04 17:27:12 +0000560 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000561 // Suggest the (potentially) correct interface name. However, put the
562 // fix-it hint itself in a separate note, since changing the name in
563 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000564 // provide a code-modification hint or use the typo name for recovery,
565 // because this is just a warning. The program may actually be correct.
566 Diag(ClassLoc, diag::warn_undef_interface_suggest)
567 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000568 Diag(IDecl->getLocation(), diag::note_previous_decl)
569 << R.getLookupName()
Douglas Gregora771f462010-03-31 17:46:05 +0000570 << FixItHint::CreateReplacement(ClassLoc,
571 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000572 IDecl = 0;
573 } else {
574 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
575 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000579 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000580 if (SuperClassname) {
581 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000582 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
583 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000584 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000585 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
586 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000587 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000588 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000589 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000590 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000591 Diag(SuperClassLoc, diag::err_undef_superclass)
592 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000593 else if (IDecl && IDecl->getSuperClass() != SDecl) {
594 // This implementation and its interface do not have the same
595 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000596 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000597 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000598 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000599 }
600 }
601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattnerda463fe2007-12-12 07:09:47 +0000603 if (!IDecl) {
604 // Legacy case of @implementation with no corresponding @interface.
605 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000606
Mike Stump87c57ac2009-05-16 07:39:55 +0000607 // FIXME: Do we support attributes on the @implementation? If so we should
608 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000609 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000610 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000611 IDecl->setSuperClass(SDecl);
612 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000613
614 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000615 } else {
616 // Mark the interface as being completed, even if it was just as
617 // @class ....;
618 // declaration; the user cannot reopen it.
619 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000620 }
Mike Stump11289f42009-09-09 15:08:12 +0000621
622 ObjCImplementationDecl* IMPDecl =
623 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000624 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Anders Carlssona6b508a2008-11-04 16:57:32 +0000626 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000627 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Chris Lattnerda463fe2007-12-12 07:09:47 +0000629 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000630 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000631 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000632 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000633 Diag(IDecl->getImplementation()->getLocation(),
634 diag::note_previous_definition);
635 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000636 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000637 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000638 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000639 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000640}
641
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000642void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
643 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000644 SourceLocation RBrace) {
645 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000646 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000647 if (!IDecl)
648 return;
649 /// Check case of non-existing @interface decl.
650 /// (legacy objective-c @implementation decl without an @interface decl).
651 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000652 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000653 IDecl->setLocEnd(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000654 // Add ivar's to class's DeclContext.
655 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +0000656 ivars[i]->setLexicalDeclContext(ImpDecl);
657 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000658 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000659 }
660
Chris Lattnerda463fe2007-12-12 07:09:47 +0000661 return;
662 }
663 // If implementation has empty ivar list, just return.
664 if (numIvars == 0)
665 return;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattnerda463fe2007-12-12 07:09:47 +0000667 assert(ivars && "missing @implementation ivars");
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000668 if (LangOpts.ObjCNonFragileABI2) {
669 if (ImpDecl->getSuperClass())
670 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
671 for (unsigned i = 0; i < numIvars; i++) {
672 ObjCIvarDecl* ImplIvar = ivars[i];
673 if (const ObjCIvarDecl *ClsIvar =
674 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
675 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
676 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
677 continue;
678 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000679 // Instance ivar to Implementation's DeclContext.
680 ImplIvar->setLexicalDeclContext(ImpDecl);
681 IDecl->makeDeclVisibleInContext(ImplIvar, false);
682 ImpDecl->addDecl(ImplIvar);
683 }
684 return;
685 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000686 // Check interface's Ivar list against those in the implementation.
687 // names and types must match.
688 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000689 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000690 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000691 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
692 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000693 ObjCIvarDecl* ImplIvar = ivars[j++];
694 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000695 assert (ImplIvar && "missing implementation ivar");
696 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000697
Steve Naroff157599f2009-03-03 14:49:36 +0000698 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000699 if (Context.getCanonicalType(ImplIvar->getType()) !=
700 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000701 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000702 << ImplIvar->getIdentifier()
703 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000704 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000705 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
706 Expr *ImplBitWidth = ImplIvar->getBitWidth();
707 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000708 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
709 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000710 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
711 << ImplIvar->getIdentifier();
712 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714 }
Steve Naroff157599f2009-03-03 14:49:36 +0000715 // Make sure the names are identical.
716 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000717 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000718 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000719 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000720 }
721 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner0f29d982007-12-12 18:11:49 +0000724 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000725 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000726 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000727 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000728}
729
Steve Naroff15833ed2008-02-10 21:38:56 +0000730void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000731 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff15833ed2008-02-10 21:38:56 +0000732 if (!IncompleteImpl) {
733 Diag(ImpLoc, diag::warn_incomplete_impl);
734 IncompleteImpl = true;
735 }
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000736 Diag(method->getLocation(), DiagID)
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000737 << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000738}
739
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000740void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
741 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000742 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000743 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000744 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
745 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000746 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000747 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
748 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000749 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattner67f35b02009-04-11 19:58:42 +0000752 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
753 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
754 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000755 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
756 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
757 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
758 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000759 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000760
761 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000762 << ImpMethodDecl->getDeclName() << (*IF)->getType()
763 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000764 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000765 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000766}
767
Mike Stump87c57ac2009-05-16 07:39:55 +0000768/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
769/// improve the efficiency of selector lookups and type checking by associating
770/// with each protocol / interface / category the flattened instance tables. If
771/// we used an immutable set to keep the table then it wouldn't add significant
772/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000773
Steve Naroffa36992242008-02-08 22:06:17 +0000774/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000775/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000776void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
777 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000778 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000779 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000780 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +0000781 ObjCContainerDecl *CDecl) {
782 ObjCInterfaceDecl *IDecl;
783 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
784 IDecl = C->getClassInterface();
785 else
786 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
787 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
788
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000789 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000790 ObjCInterfaceDecl *NSIDecl = 0;
791 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000792 // check to see if class implements forwardInvocation method and objects
793 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000794 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000795 // Under such conditions, which means that every method possible is
796 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000797 // found" warnings.
798 // FIXME: Use a general GetUnarySelector method for this.
799 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
800 Selector fISelector = Context.Selectors.getSelector(1, &II);
801 if (InsMap.count(fISelector))
802 // Is IDecl derived from 'NSProxy'? If so, no instance methods
803 // need be implemented in the implementation.
804 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000807 // If a method lookup fails locally we still need to look and see if
808 // the method was implemented by a base class or an inherited
809 // protocol. This lookup is slow, but occurs rarely in correct code
810 // and otherwise would terminate in a warning.
811
Chris Lattnerda463fe2007-12-12 07:09:47 +0000812 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000813 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000814 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000815 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000816 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000817 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000818 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000819 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000820 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000821 // Ugly, but necessary. Method declared in protcol might have
822 // have been synthesized due to a property declared in the class which
823 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000824 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000825 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000826 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000827 unsigned DIAG = diag::warn_unimplemented_protocol_method;
828 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
829 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
830 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
831 << PDecl->getDeclName();
832 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000833 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000834 }
835 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000836 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000837 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000838 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000839 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000840 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000841 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
842 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000843 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000844 unsigned DIAG = diag::warn_unimplemented_protocol_method;
845 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
846 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
847 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
848 PDecl->getDeclName();
849 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000850 }
Steve Naroff3ce37a62007-12-14 23:37:57 +0000851 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000852 // Check on this protocols's referenced protocols, recursively.
853 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
854 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000855 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000856}
857
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000858/// MatchAllMethodDeclarations - Check methods declaraed in interface or
859/// or protocol against those declared in their implementations.
860///
861void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
862 const llvm::DenseSet<Selector> &ClsMap,
863 llvm::DenseSet<Selector> &InsMapSeen,
864 llvm::DenseSet<Selector> &ClsMapSeen,
865 ObjCImplDecl* IMPDecl,
866 ObjCContainerDecl* CDecl,
867 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000868 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000869 // Check and see if instance methods in class interface have been
870 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000871 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
872 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000873 if (InsMapSeen.count((*I)->getSelector()))
874 continue;
875 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000876 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000877 !InsMap.count((*I)->getSelector())) {
878 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000879 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
880 diag::note_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000881 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000882 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000883 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000884 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000885 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000886 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000887 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000888 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
889 // ImpMethodDecl may be null as in a @dynamic property.
890 if (ImpMethodDecl)
891 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
892 }
893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000895 // Check and see if class methods in class interface have been
896 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000897 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000898 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000899 if (ClsMapSeen.count((*I)->getSelector()))
900 continue;
901 ClsMapSeen.insert((*I)->getSelector());
902 if (!ClsMap.count((*I)->getSelector())) {
903 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000904 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
905 diag::note_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +0000906 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000907 ObjCMethodDecl *ImpMethodDecl =
908 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000909 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000910 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000911 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
912 }
913 }
914 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
915 // Check for any implementation of a methods declared in protocol.
916 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
917 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000918 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
919 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000920 (*PI), IncompleteImpl, false);
921 if (I->getSuperClass())
922 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +0000923 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000924 I->getSuperClass(), IncompleteImpl, false);
925 }
926}
927
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000928void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000929 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000930 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000931 llvm::DenseSet<Selector> InsMap;
932 // Check and see if instance methods in class interface have been
933 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000934 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000935 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000936 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000937
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +0000938 // Check and see if properties declared in the interface have either 1)
939 // an implementation or 2) there is a @synthesize/@dynamic implementation
940 // of the property in the @implementation.
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +0000941 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000942 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian98609b32010-01-20 01:51:55 +0000943
Chris Lattnerda463fe2007-12-12 07:09:47 +0000944 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +0000945 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000946 I = IMPDecl->classmeth_begin(),
947 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000948 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000949
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000950 // Check for type conflict of methods declared in a class/protocol and
951 // its implementation; if any.
952 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +0000953 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
954 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000955 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattnerda463fe2007-12-12 07:09:47 +0000957 // Check the protocol list for unimplemented methods in the @implementation
958 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000959 // Check and see if class methods in class interface have been
960 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner9ef10f42009-03-01 00:56:52 +0000962 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000963 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +0000964 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000965 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000966 InsMap, ClsMap, I);
967 // Check class extensions (unnamed categories)
968 for (ObjCCategoryDecl *Categories = I->getCategoryList();
969 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000970 if (Categories->IsClassExtension()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000971 ImplMethodsVsClassMethods(S, IMPDecl, Categories, IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +0000972 break;
973 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000974 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000975 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000976 // For extended class, unimplemented methods in its protocols will
977 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000978 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000979 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
980 E = C->protocol_end(); PI != E; ++PI)
981 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +0000982 InsMap, ClsMap, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000983 // Report unimplemented properties in the category as well.
984 // When reporting on missing setter/getters, do not report when
985 // setter/getter is implemented in category's primary class
986 // implementation.
987 if (ObjCInterfaceDecl *ID = C->getClassInterface())
988 if (ObjCImplDecl *IMP = ID->getImplementation()) {
989 for (ObjCImplementationDecl::instmeth_iterator
990 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
991 InsMap.insert((*I)->getSelector());
992 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000993 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +0000994 }
Chris Lattner9ef10f42009-03-01 00:56:52 +0000995 } else
996 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +0000997}
998
Mike Stump11289f42009-09-09 15:08:12 +0000999/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001000Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001001Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001002 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001003 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001004 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001005 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001006
Chris Lattnerda463fe2007-12-12 07:09:47 +00001007 for (unsigned i = 0; i != NumElts; ++i) {
1008 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001009 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001010 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001011 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001012 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001013 // Maybe we will complain about the shadowed template parameter.
1014 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1015 // Just pretend that we didn't see the previous declaration.
1016 PrevDecl = 0;
1017 }
1018
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001019 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001020 // GCC apparently allows the following idiom:
1021 //
1022 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1023 // @class XCElementToggler;
1024 //
Mike Stump11289f42009-09-09 15:08:12 +00001025 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001026 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00001027 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001028 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001029 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00001030 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00001031 // a forward class declaration matching a typedef name of a class refers
1032 // to the underlying class.
John McCall8b07ec22010-05-15 11:32:37 +00001033 if (const ObjCObjectType *OI =
1034 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1035 PrevDecl = OI->getInterface();
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001036 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001037 }
Mike Stump11289f42009-09-09 15:08:12 +00001038 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001039 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001040 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001041 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001042
1043 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1044 // the current DeclContext. This prevents clients that walk DeclContext
1045 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1046 // declared later (if at all). We also take care to explicitly make
1047 // sure this declaration is visible for name lookup.
1048 PushOnScopeChains(IDecl, TUScope, false);
1049 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001050 }
1051
1052 Interfaces.push_back(IDecl);
1053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Ted Kremenek9b124e12009-11-18 00:28:11 +00001055 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001056 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001057 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001058 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001059 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001060 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001061 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001062}
1063
1064
1065/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1066/// returns true, or false, accordingly.
1067/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001068bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001069 const ObjCMethodDecl *PrevMethod,
1070 bool matchBasedOnSizeAndAlignment) {
1071 QualType T1 = Context.getCanonicalType(Method->getResultType());
1072 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001073
Steve Naroffa0ed1652008-10-21 10:37:50 +00001074 if (T1 != T2) {
1075 // The result types are different.
1076 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001077 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001078 // Incomplete types don't have a size and alignment.
1079 if (T1->isIncompleteType() || T2->isIncompleteType())
1080 return false;
1081 // Check is based on size and alignment.
1082 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1083 return false;
1084 }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattnera4997152009-02-20 18:43:26 +00001086 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1087 E = Method->param_end();
1088 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattnera4997152009-02-20 18:43:26 +00001090 for (; ParamI != E; ++ParamI, ++PrevI) {
1091 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1092 T1 = Context.getCanonicalType((*ParamI)->getType());
1093 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001094 if (T1 != T2) {
1095 // The result types are different.
1096 if (!matchBasedOnSizeAndAlignment)
1097 return false;
1098 // Incomplete types don't have a size and alignment.
1099 if (T1->isIncompleteType() || T2->isIncompleteType())
1100 return false;
1101 // Check is based on size and alignment.
1102 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1103 return false;
1104 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001105 }
1106 return true;
1107}
1108
Douglas Gregorc78d3462009-04-24 21:10:55 +00001109/// \brief Read the contents of the instance and factory method pools
1110/// for a given selector from external storage.
1111///
1112/// This routine should only be called once, when neither the instance
1113/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001114Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001115 bool isInstance) {
1116 assert(ExternalSource && "We need an external AST source");
1117 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1118 "Selector data already loaded into the instance method pool");
1119 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1120 "Selector data already loaded into the factory method pool");
1121
1122 // Read the method list from the external source.
1123 std::pair<ObjCMethodList, ObjCMethodList> Methods
1124 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001125
Douglas Gregorc78d3462009-04-24 21:10:55 +00001126 if (isInstance) {
1127 if (Methods.second.Method)
1128 FactoryMethodPool[Sel] = Methods.second;
1129 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001130 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001131
1132 if (Methods.first.Method)
1133 InstanceMethodPool[Sel] = Methods.first;
1134
1135 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1136}
1137
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001138void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001139 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1140 = InstanceMethodPool.find(Method->getSelector());
1141 if (Pos == InstanceMethodPool.end()) {
1142 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1143 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1144 else
1145 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1146 ObjCMethodList())).first;
1147 }
1148
1149 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001150 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001151 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001152 Entry.Method = Method;
1153 Entry.Next = 0;
1154 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattner7b26b292009-03-04 05:16:45 +00001157 // We've seen a method with this name, see if we have already seen this type
1158 // signature.
1159 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1160 if (MatchTwoMethodDeclarations(Method, List->Method))
1161 return;
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattner7b26b292009-03-04 05:16:45 +00001163 // We have a new signature for an existing method - add it.
1164 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001165 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1166 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001167}
1168
Steve Naroff9ebc0502008-10-21 10:50:19 +00001169// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001170ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001171 SourceRange R,
1172 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001173 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1174 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001175 if (Pos == InstanceMethodPool.end()) {
1176 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001177 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1178 else
1179 return 0;
1180 }
1181
1182 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001183 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001184
Steve Naroff4a82d812008-09-30 14:38:43 +00001185 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001186 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1187 // This checks if the methods differ by size & alignment.
1188 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001189 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001190 }
1191 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001192 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001193 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001194 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001195 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001196 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001197 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001198 }
1199 return MethList.Method;
1200}
1201
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001202void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001203 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1204 = FactoryMethodPool.find(Method->getSelector());
1205 if (Pos == FactoryMethodPool.end()) {
1206 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1207 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1208 else
1209 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1210 ObjCMethodList())).first;
1211 }
1212
1213 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001214 if (!FirstMethod.Method) {
1215 // Haven't seen a method with this selector name yet - add it.
1216 FirstMethod.Method = Method;
1217 FirstMethod.Next = 0;
1218 } else {
1219 // We've seen a method with this name, now check the type signature(s).
1220 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001221
1222 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001223 Next = Next->Next)
1224 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Chris Lattnerda463fe2007-12-12 07:09:47 +00001226 if (!match) {
1227 // We have a new signature for an existing method - add it.
1228 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001229 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1230 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001231 FirstMethod.Next = OMI;
1232 }
1233 }
1234}
1235
Mike Stump11289f42009-09-09 15:08:12 +00001236ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001237 SourceRange R) {
1238 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1239 = FactoryMethodPool.find(Sel);
1240 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001241 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001242 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1243 else
1244 return 0;
1245 }
1246
1247 ObjCMethodList &MethList = Pos->second;
1248 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001249
Douglas Gregorc78d3462009-04-24 21:10:55 +00001250 if (MethList.Method && MethList.Next) {
1251 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1252 // This checks if the methods differ by size & alignment.
1253 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1254 issueWarning = true;
1255 }
1256 if (issueWarning && (MethList.Method && MethList.Next)) {
1257 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001258 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001259 << MethList.Method->getSourceRange();
1260 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001261 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001262 << Next->Method->getSourceRange();
1263 }
1264 return MethList.Method;
1265}
1266
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001267/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1268/// identical selector names in current and its super classes and issues
1269/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001270void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1271 ObjCMethodDecl *Method,
1272 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001273 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1274 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001276 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001277 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001278 SD->lookupMethod(Method->getSelector(), IsInstance);
1279 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001280 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001281 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001282 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001283 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1284 E = Method->param_end();
1285 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1286 for (; ParamI != E; ++ParamI, ++PrevI) {
1287 // Number of parameters are the same and is guaranteed by selector match.
1288 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1289 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1290 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1291 // If type of arguement of method in this class does not match its
1292 // respective argument type in the super class method, issue warning;
1293 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001294 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001295 << T1 << T2;
1296 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1297 return;
1298 }
1299 }
1300 ID = SD;
1301 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001302}
1303
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001304/// DiagnoseDuplicateIvars -
1305/// Check for duplicate ivars in the entire class at the start of
1306/// @implementation. This becomes necesssary because class extension can
1307/// add ivars to a class in random order which will not be known until
1308/// class's @implementation is seen.
1309void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1310 ObjCInterfaceDecl *SID) {
1311 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1312 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1313 ObjCIvarDecl* Ivar = (*IVI);
1314 if (Ivar->isInvalidDecl())
1315 continue;
1316 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1317 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1318 if (prevIvar) {
1319 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1320 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1321 Ivar->setInvalidDecl();
1322 }
1323 }
1324 }
1325}
1326
Steve Naroff1d2538c2007-12-18 01:30:32 +00001327// Note: For class/category implemenations, allMethods/allProperties is
1328// always null.
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001329void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
Ted Kremenekc7c64312010-01-07 01:20:12 +00001330 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001331 DeclPtrTy *allMethods, unsigned allNum,
1332 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001333 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001334 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001335
Steve Naroff1d2538c2007-12-18 01:30:32 +00001336 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1337 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001338 // should be true.
1339 if (!ClassDecl)
1340 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001341
Mike Stump11289f42009-09-09 15:08:12 +00001342 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001343 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1344 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001345 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001346
Ted Kremenekc7c64312010-01-07 01:20:12 +00001347 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1348 // FIXME: This is wrong. We shouldn't be pretending that there is
1349 // an '@end' in the declaration.
1350 SourceLocation L = ClassDecl->getLocation();
1351 AtEnd.setBegin(L);
1352 AtEnd.setEnd(L);
1353 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001354 }
1355
Steve Naroff35c62ae2009-01-08 17:28:14 +00001356 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001357
1358 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1359 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1360 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1361
Chris Lattnerda463fe2007-12-12 07:09:47 +00001362 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001363 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001364 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001365
1366 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001367 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001368 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001369 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001370 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001371 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001372 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001373 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001374 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001375 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001376 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001377 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001378 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001379 InsMap[Method->getSelector()] = Method;
1380 /// The following allows us to typecheck messages to "id".
1381 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001382 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001383 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001384 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001385 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001386 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001387 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001388 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001389 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001390 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001391 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001392 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001393 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001394 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001395 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001396 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001397 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001398 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001399 /// The following allows us to typecheck messages to "Class".
1400 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001401 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001402 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001403 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001404 }
1405 }
1406 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001407 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001408 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001409 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001410 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001411 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001412 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001413 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001414 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001415 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001416
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001417 // Compare protocol properties with those in category
1418 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001419 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001420 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001421 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001422 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001423 if (CDecl->getIdentifier())
1424 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1425 // user-defined setter/getter. It also synthesizes setter/getter methods
1426 // and adds them to the DeclContext and global method pools.
1427 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1428 E = CDecl->prop_end();
1429 I != E; ++I)
1430 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001431 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001432 }
1433 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001434 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001435 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001436 if (LangOpts.ObjCNonFragileABI2)
1437 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001438 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001439 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001440 if (LangOpts.ObjCNonFragileABI2)
1441 while (IDecl->getSuperClass()) {
1442 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1443 IDecl = IDecl->getSuperClass();
1444 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001445 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001446 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00001447 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001448 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001449 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattnerda463fe2007-12-12 07:09:47 +00001451 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001452 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001453 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001454 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001455 Categories; Categories = Categories->getNextClassCategory()) {
1456 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001457 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001458 break;
1459 }
1460 }
1461 }
1462 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001463 if (isInterfaceDeclKind) {
1464 // Reject invalid vardecls.
1465 for (unsigned i = 0; i != tuvNum; i++) {
1466 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1467 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1468 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001469 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001470 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001471 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001472 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001473 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001474}
1475
1476
1477/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1478/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001479static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001480CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1481 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1482 if (PQTVal & ObjCDeclSpec::DQ_In)
1483 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1484 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1485 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1486 if (PQTVal & ObjCDeclSpec::DQ_Out)
1487 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1488 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1489 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1490 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1491 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1492 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1493 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001494
1495 return ret;
1496}
1497
Ted Kremenek36712b22010-04-18 04:59:38 +00001498static inline
1499bool containsInvalidMethodImplAttribute(const AttributeList *A) {
1500 // The 'ibaction' attribute is allowed on method definitions because of
1501 // how the IBAction macro is used on both method declarations and definitions.
1502 // If the method definitions contains any other attributes, return true.
1503 while (A && A->getKind() == AttributeList::AT_IBAction)
1504 A = A->getNext();
1505 return A != NULL;
1506}
1507
Chris Lattner83f095c2009-03-28 19:18:32 +00001508Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001509 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001510 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001511 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001512 Selector Sel,
1513 // optional arguments. The number of types/arguments is obtained
1514 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001515 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001516 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00001517 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1518 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001519 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001520
1521 // Make sure we can establish a context for the method.
1522 if (!ClassDecl) {
1523 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9a28e842010-03-01 23:15:13 +00001524 getLabelMap().clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001525 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001526 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001527 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001528
Douglas Gregor12852d92010-03-08 14:59:44 +00001529 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00001530 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00001531 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001532
Steve Naroff32606412009-02-20 22:59:16 +00001533 // Methods cannot return interface types. All ObjC objects are
1534 // passed by reference.
John McCall8b07ec22010-05-15 11:32:37 +00001535 if (resultDeclType->isObjCObjectType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001536 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1537 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001538 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001539 }
1540 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001541 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001542
1543 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001544 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00001545 ResultTInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001546 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001547 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001548 false,
Mike Stump11289f42009-09-09 15:08:12 +00001549 MethodDeclKind == tok::objc_optional ?
1550 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001551 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001553 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattner23b0faf2009-04-11 19:42:43 +00001555 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001556 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001557 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001558
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001559 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001560 ArgType = Context.getObjCIdType();
1561 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001562 } else {
John McCall856bbea2009-10-23 21:48:59 +00001563 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001564 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001565 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001566 }
Mike Stump11289f42009-09-09 15:08:12 +00001567
John McCall856bbea2009-10-23 21:48:59 +00001568 ParmVarDecl* Param
1569 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1570 ArgInfo[i].Name, ArgType, DI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001571 VarDecl::None, VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001572
John McCall8b07ec22010-05-15 11:32:37 +00001573 if (ArgType->isObjCObjectType()) {
Chris Lattner9713a1c2009-04-11 19:34:56 +00001574 Diag(ArgInfo[i].NameLoc,
1575 diag::err_object_cannot_be_passed_returned_by_value)
1576 << 1 << ArgType;
1577 Param->setInvalidDecl();
1578 }
Mike Stump11289f42009-09-09 15:08:12 +00001579
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001580 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001581 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001582
Chris Lattner9713a1c2009-04-11 19:34:56 +00001583 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001584 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001586 Params.push_back(Param);
1587 }
1588
Fariborz Jahanian60462092010-04-08 00:30:06 +00001589 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
1590 ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>();
1591 QualType ArgType = Param->getType();
1592 if (ArgType.isNull())
1593 ArgType = Context.getObjCIdType();
1594 else
1595 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1596 ArgType = adjustParameterType(ArgType);
John McCall8b07ec22010-05-15 11:32:37 +00001597 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian60462092010-04-08 00:30:06 +00001598 Diag(Param->getLocation(),
1599 diag::err_object_cannot_be_passed_returned_by_value)
1600 << 1 << ArgType;
1601 Param->setInvalidDecl();
1602 }
1603 Param->setDeclContext(ObjCMethod);
1604 IdResolver.RemoveDecl(Param);
1605 Params.push_back(Param);
1606 }
1607
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00001608 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1609 Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001610 ObjCMethod->setObjCDeclQualifier(
1611 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1612 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001613
1614 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001615 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001616
John McCall28a6aea2009-11-04 02:18:39 +00001617 const ObjCMethodDecl *InterfaceMD = 0;
1618
Mike Stump11289f42009-09-09 15:08:12 +00001619 // For implementations (which can be very "coarse grain"), we add the
1620 // method now. This allows the AST to implement lookup methods that work
1621 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001622 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001623 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001624 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001625 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001626 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1627 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001628 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001629 PrevMethod = ImpDecl->getClassMethod(Sel);
1630 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001631 }
John McCall28a6aea2009-11-04 02:18:39 +00001632 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1633 MethodType == tok::minus);
Ted Kremenek36712b22010-04-18 04:59:38 +00001634 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001635 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001636 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001637 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001638 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001639 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1640 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001641 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001642 PrevMethod = CatImpDecl->getClassMethod(Sel);
1643 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001644 }
Ted Kremenek36712b22010-04-18 04:59:38 +00001645 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001646 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001647 }
1648 if (PrevMethod) {
1649 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001650 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001651 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001652 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001653 }
John McCall28a6aea2009-11-04 02:18:39 +00001654
1655 // If the interface declared this method, and it was deprecated there,
1656 // mark it deprecated here.
1657 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1658 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1659
Chris Lattner83f095c2009-03-28 19:18:32 +00001660 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001661}
1662
Chris Lattner438e5012008-12-17 07:13:27 +00001663bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001664 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00001665 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001666
Anders Carlssona6b508a2008-11-04 16:57:32 +00001667 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1668 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001669
Anders Carlssona6b508a2008-11-04 16:57:32 +00001670 return true;
1671}
Chris Lattner438e5012008-12-17 07:13:27 +00001672
Chris Lattner438e5012008-12-17 07:13:27 +00001673/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1674/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00001675void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00001676 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00001677 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00001678 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001679 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00001680 if (!Class) {
1681 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1682 return;
1683 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00001684 if (LangOpts.ObjCNonFragileABI) {
1685 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1686 return;
1687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattner438e5012008-12-17 07:13:27 +00001689 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001690 llvm::SmallVector<FieldDecl*, 32> RecFields;
1691 Context.CollectObjCIvars(Class, RecFields);
1692 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1693 for (unsigned i = 0; i < RecFields.size(); i++) {
1694 FieldDecl* ID = RecFields[i];
1695 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1696 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1697 ID->getIdentifier(), ID->getType(),
1698 ID->getBitWidth());
1699 Decls.push_back(Sema::DeclPtrTy::make(FD));
1700 }
Mike Stump11289f42009-09-09 15:08:12 +00001701
Chris Lattner438e5012008-12-17 07:13:27 +00001702 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00001703 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00001704 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001705 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00001706 if (getLangOptions().CPlusPlus)
1707 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001708 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001709 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00001710 }
1711}
1712
Douglas Gregorf3564192010-04-26 17:32:49 +00001713/// \brief Build a type-check a new Objective-C exception variable declaration.
1714VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1715 QualType T,
1716 IdentifierInfo *Name,
1717 SourceLocation NameLoc,
1718 bool Invalid) {
1719 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1720 // duration shall not be qualified by an address-space qualifier."
1721 // Since all parameters have automatic store duration, they can not have
1722 // an address space.
1723 if (T.getAddressSpace() != 0) {
1724 Diag(NameLoc, diag::err_arg_with_address_space);
1725 Invalid = true;
1726 }
1727
1728 // An @catch parameter must be an unqualified object pointer type;
1729 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1730 if (Invalid) {
1731 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00001732 } else if (T->isDependentType()) {
1733 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00001734 } else if (!T->isObjCObjectPointerType()) {
1735 Invalid = true;
1736 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1737 } else if (T->isObjCQualifiedIdType()) {
1738 Invalid = true;
1739 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1740 }
1741
1742 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor3f324d562010-05-03 18:51:14 +00001743 VarDecl::None, VarDecl::None);
1744 New->setExceptionVariable(true);
1745
Douglas Gregorf3564192010-04-26 17:32:49 +00001746 if (Invalid)
1747 New->setInvalidDecl();
1748 return New;
1749}
1750
Douglas Gregore11ee112010-04-23 23:01:43 +00001751Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00001752 const DeclSpec &DS = D.getDeclSpec();
1753
1754 // We allow the "register" storage class on exception variables because
1755 // GCC did, but we drop it completely. Any other storage class is an error.
1756 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1757 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1758 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1759 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1760 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1761 << DS.getStorageClassSpec();
1762 }
1763 if (D.getDeclSpec().isThreadSpecified())
1764 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1765 D.getMutableDeclSpec().ClearStorageClassSpecs();
1766
1767 DiagnoseFunctionSpecifiers(D);
1768
1769 // Check that there are no default arguments inside the type of this
1770 // exception object (C++ only).
1771 if (getLangOptions().CPlusPlus)
1772 CheckExtraCXXDefaultArguments(D);
1773
1774 TypeSourceInfo *TInfo = 0;
1775 TagDecl *OwnedDecl = 0;
1776 QualType ExceptionType = GetTypeForDeclarator(D, S, &TInfo, &OwnedDecl);
1777
1778 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1779 // Objective-C++: Types shall not be defined in exception types.
1780 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1781 << Context.getTypeDeclType(OwnedDecl);
1782 }
1783
1784 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1785 D.getIdentifierLoc(),
1786 D.isInvalidType());
1787
1788 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1789 if (D.getCXXScopeSpec().isSet()) {
1790 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1791 << D.getCXXScopeSpec().getRange();
1792 New->setInvalidDecl();
1793 }
1794
1795 // Add the parameter declaration into this scope.
1796 S->AddDecl(DeclPtrTy::make(New));
1797 if (D.getIdentifier())
1798 IdResolver.AddDecl(New);
1799
1800 ProcessDeclAttributes(S, New, D);
1801
1802 if (New->hasAttr<BlocksAttr>())
1803 Diag(New->getLocation(), diag::err_block_on_nonlocal);
1804 return DeclPtrTy::make(New);
Douglas Gregore11ee112010-04-23 23:01:43 +00001805}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001806
1807/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001808/// initialization.
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001809void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001810 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001811 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1812 E = OI->ivar_end(); I != E; ++I) {
1813 ObjCIvarDecl *Iv = (*I);
1814 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001815 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001816 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001817 }
1818
1819 // Find ivars to construct/destruct in class extension.
1820 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
1821 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1822 E = CDecl->ivar_end(); I != E; ++I) {
1823 ObjCIvarDecl *Iv = (*I);
1824 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001825 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001826 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001827 }
1828 }
1829
1830 // Also add any ivar defined in this class's implementation. This
1831 // includes synthesized ivars.
1832 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
1833 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1834 E = ImplDecl->ivar_end(); I != E; ++I) {
1835 ObjCIvarDecl *Iv = (*I);
1836 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001837 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001838 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001839 }
1840 }
1841}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001842
1843void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1844 CXXBaseOrMemberInitializer ** initializers,
1845 unsigned numInitializers) {
1846 if (numInitializers > 0) {
1847 NumIvarInitializers = numInitializers;
1848 CXXBaseOrMemberInitializer **ivarInitializers =
1849 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1850 memcpy(ivarInitializers, initializers,
1851 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1852 IvarInitializers = ivarInitializers;
1853 }
1854}
1855