blob: c30a7b26e1438950ebed1a7463c239b2d433cd00 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner855e51f2007-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"
Steve Naroff58d0d6f2009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroff8df46022009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000030
Chris Lattnerc92920c2009-04-19 05:21:20 +000031 CurFunctionNeedsScopeChecking = false;
32
Steve Narofffe9eb6a2007-12-18 01:30:32 +000033 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000038
39 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000041
42 // Create Decl objects for each parameter, entrring them in the scope for
43 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000047
Daniel Dunbareaf91c32008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000050
Chris Lattner97316c02008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000052 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
53 E = MDecl->param_end(); PI != E; ++PI)
54 if ((*PI)->getIdentifier())
55 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056}
57
Chris Lattner5261d0c2009-03-28 19:18:32 +000058Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000059ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000062 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000069 // Maybe we will complain about the shadowed template parameter.
70 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
71 // Just pretend that we didn't see the previous declaration.
72 PrevDecl = 0;
73 }
74
Ted Kremenek42730c52008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000078 }
79
Ted Kremenek42730c52008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroff1422a622008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +000090 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +000091 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000094 }
Chris Lattner5cece462008-07-21 07:06:49 +000095 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff104956f2009-04-23 15:15:40 +0000101 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 }
103
104 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000106 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000107
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000108 ObjCInterfaceDecl *SuperClassDecl =
109 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000110
111 // Diagnose classes that inherit from deprecated classes.
112 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000113 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000114
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000115 if (PrevDecl && SuperClassDecl == 0) {
116 // The previous declaration was not a class decl. Check if we have a
117 // typedef. If we do, get the underlying class type.
118 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
119 QualType T = TDecl->getUnderlyingType();
120 if (T->isObjCInterfaceType()) {
121 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
122 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
123 }
124 }
Chris Lattner7699a532009-02-16 21:33:09 +0000125
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000126 // This handles the following case:
127 //
128 // typedef int SuperClass;
129 // @interface MyClass : SuperClass {} @end
130 //
131 if (!SuperClassDecl) {
132 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
133 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
134 }
135 }
Chris Lattner7699a532009-02-16 21:33:09 +0000136
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000137 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
138 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000139 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000140 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000141 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000143 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000144 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000145 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000146 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000147 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000148 IDecl->setLocEnd(SuperLoc);
149 } else { // we have a root class.
150 IDecl->setLocEnd(ClassLoc);
151 }
152
Steve Naroff1422a622008-11-18 19:15:30 +0000153 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000154 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000155 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
156 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 IDecl->setLocEnd(EndProtoLoc);
158 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000159
160 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000161 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000162}
163
164/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000165/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000166Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
167 IdentifierInfo *AliasName,
168 SourceLocation AliasLocation,
169 IdentifierInfo *ClassName,
170 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000171 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000172 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000173 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000174 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000176 else
Chris Lattner65cae292008-11-19 08:23:25 +0000177 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000178 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000179 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000180 }
181 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000182 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000183 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
184 QualType T = TDecl->getUnderlyingType();
185 if (T->isObjCInterfaceType()) {
186 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
187 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000188 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000189 }
190 }
191 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000192 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
193 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000194 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000195 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000196 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000197 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000198 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000199
200 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000202 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000203
204 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000205
Steve Naroffe00554f2009-04-23 17:46:47 +0000206 // FIXME: PushOnScopeChains?
207 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000208 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000209 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000210
Chris Lattner5261d0c2009-03-28 19:18:32 +0000211 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000212}
213
Steve Narofffe56a632009-03-05 15:22:01 +0000214void Sema::CheckForwardProtocolDeclarationForCircularDependency(
215 IdentifierInfo *PName,
216 SourceLocation &Ploc, SourceLocation PrevLoc,
217 const ObjCList<ObjCProtocolDecl> &PList)
218{
219 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
220 E = PList.end(); I != E; ++I) {
221
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000222 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-03-05 15:22:01 +0000223 if (PDecl->getIdentifier() == PName) {
224 Diag(Ploc, diag::err_protocol_has_circular_dependency);
225 Diag(PrevLoc, diag::note_previous_definition);
226 }
227 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
228 PDecl->getLocation(), PDecl->getReferencedProtocols());
229 }
230 }
231}
232
Chris Lattner5261d0c2009-03-28 19:18:32 +0000233Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000234Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
235 IdentifierInfo *ProtocolName,
236 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000237 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000238 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000239 SourceLocation EndProtoLoc,
240 AttributeList *AttrList) {
241 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000242 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000243 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000244 if (PDecl) {
245 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000246 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000247 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000248 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000249 // Just return the protocol we already had.
250 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000251 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000252 }
Steve Narofffe56a632009-03-05 15:22:01 +0000253 ObjCList<ObjCProtocolDecl> PList;
254 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
255 CheckForwardProtocolDeclarationForCircularDependency(
256 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
257 PList.Destroy(Context);
258
Steve Naroff9a9e5212008-08-13 16:39:22 +0000259 // Make sure the cached decl gets a valid start location.
260 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000261 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000262 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000263 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
264 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000265 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000266 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000267 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000268 if (AttrList)
269 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000270 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000271 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000272 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000273 PDecl->setLocEnd(EndProtoLoc);
274 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000275
276 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000277 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000278}
279
280/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000281/// issues an error if they are not declared. It returns list of
282/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000283void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000284Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000285 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000286 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000287 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000288 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000289 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000290 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000291 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000292 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000293 continue;
294 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000295
Douglas Gregoraa57e862009-02-18 21:56:37 +0000296 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000297
298 // If this is a forward declaration and we are supposed to warn in this
299 // case, do it.
300 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000301 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000302 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000303 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000304 }
305}
306
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000307/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000308/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000309///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000310void
311Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
312 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000313 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000314 ObjCPropertyDecl::PropertyAttributeKind CAttr =
315 Property->getPropertyAttributes();
316 ObjCPropertyDecl::PropertyAttributeKind SAttr =
317 SuperProperty->getPropertyAttributes();
318 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
319 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000320 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000321 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000322 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
323 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000324 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000325 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
327 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000328 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000329 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000330
331 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
332 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000334 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000335 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000337 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000338 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000339 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000340 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000341
342 QualType LHSType =
343 Context.getCanonicalType(SuperProperty->getType());
344 QualType RHSType =
345 Context.getCanonicalType(Property->getType());
346
347 if (!Context.typesAreCompatible(LHSType, RHSType)) {
348 // FIXME: Incorporate this test with typesAreCompatible.
349 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
350 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
351 return;
352 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
353 << Property->getType() << SuperProperty->getType() << inheritedName;
354 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000355}
356
357/// ComparePropertiesInBaseAndSuper - This routine compares property
358/// declarations in base and its super class, if any, and issues
359/// diagnostics in a variety of inconsistant situations.
360///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000361void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000362 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
363 if (!SDecl)
364 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000365 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000366 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
367 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000368 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000369 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000370 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
371 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000372 ObjCPropertyDecl *PDecl = (*I);
373 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000374 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000375 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000376 }
377 }
378}
379
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000380/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
381/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000382/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000383void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000384Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000385 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000386 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
387 if (!IDecl) {
388 // Category
389 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
390 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000391 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
392 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000393 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000394 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000395 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000396 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000397 CP != CE; ++CP)
398 if ((*CP)->getIdentifier() == Pr->getIdentifier())
399 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000400 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401 // Property protocol already exist in class. Diagnose any mismatch.
402 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
403 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000404 return;
405 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000406 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
407 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000408 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000409 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000410 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000411 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 CP != CE; ++CP)
413 if ((*CP)->getIdentifier() == Pr->getIdentifier())
414 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000415 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000416 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000417 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000418 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000419}
420
421/// MergeProtocolPropertiesIntoClass - This routine merges properties
422/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000423/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000424///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000425void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000426 DeclPtrTy MergeItsProtocols) {
427 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000428 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
429
430 if (!IDecl) {
431 // Category
432 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
433 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
434 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
435 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
436 E = MDecl->protocol_end(); P != E; ++P)
437 // Merge properties of category (*P) into IDECL's
438 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
439
440 // Go thru the list of protocols for this category and recursively merge
441 // their properties into this class as well.
442 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
443 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000444 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000445 } else {
446 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
447 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
448 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000449 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000450 }
451 return;
452 }
453
Chris Lattner5cece462008-07-21 07:06:49 +0000454 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000455 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
456 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000457 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000458 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
459
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000460 // Go thru the list of protocols for this class and recursively merge
461 // their properties into this class as well.
462 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
463 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000464 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000465 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000466 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
467 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
468 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000469 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000470 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000471}
472
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000473/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000474/// a class method in its extension.
475///
476void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
477 ObjCInterfaceDecl *ID) {
478 if (!ID)
479 return; // Possibly due to previous error
480
481 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000482 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
483 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000484 ObjCMethodDecl *MD = *i;
485 MethodMap[MD->getSelector()] = MD;
486 }
487
488 if (MethodMap.empty())
489 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000490 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
491 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000492 ObjCMethodDecl *Method = *i;
493 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
494 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
495 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
496 << Method->getDeclName();
497 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
498 }
499 }
500}
501
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000502/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000503Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000504Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000505 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000506 unsigned NumElts,
507 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000508 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000509
510 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000511 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000512 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000513 if (PDecl == 0) { // Not already seen?
514 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
515 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000516 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000517 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000518 if (attrList)
519 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 Protocols.push_back(PDecl);
521 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000522
523 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000524 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000525 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000526 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000527 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000528 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000529}
530
Chris Lattner5261d0c2009-03-28 19:18:32 +0000531Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000532ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
533 IdentifierInfo *ClassName, SourceLocation ClassLoc,
534 IdentifierInfo *CategoryName,
535 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000536 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000537 unsigned NumProtoRefs,
538 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000539 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000540 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
541 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000542 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000543
544 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000545 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000546 if (!IDecl || IDecl->isForwardDecl()) {
547 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000548 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000549 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000550 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000551
Chris Lattner1fa7f622009-02-16 21:26:43 +0000552 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000553
554 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000555 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000556
557 /// Check for duplicate interface declaration for this category
558 ObjCCategoryDecl *CDeclChain;
559 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
560 CDeclChain = CDeclChain->getNextClassCategory()) {
561 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
562 Diag(CategoryLoc, diag::warn_dup_category_def)
563 << ClassName << CategoryName;
564 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
565 break;
566 }
567 }
568 if (!CDeclChain)
569 CDecl->insertNextClassCategory();
570
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000572 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000573 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000574 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000575
576 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000577 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000578}
579
580/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000581/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000582/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000583Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000584 SourceLocation AtCatImplLoc,
585 IdentifierInfo *ClassName, SourceLocation ClassLoc,
586 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000588 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000589 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
590 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000591 /// Check that class of this category is already completely declared.
592 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000593 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000594
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000595 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000596 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000597
Chris Lattner855e51f2007-12-12 07:09:47 +0000598 /// TODO: Check that CatName, category name, is not used in another
599 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000600 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000601
602 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000603 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000604}
605
Chris Lattner5261d0c2009-03-28 19:18:32 +0000606Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 SourceLocation AtClassImplLoc,
608 IdentifierInfo *ClassName, SourceLocation ClassLoc,
609 IdentifierInfo *SuperClassname,
610 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000611 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000613 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000615 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000616 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000617 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000620 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000622 IDecl = 0;
623 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 }
625
626 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000628 if (SuperClassname) {
629 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000630 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000631 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000632 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
633 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000634 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000635 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000636 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000637 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000638 Diag(SuperClassLoc, diag::err_undef_superclass)
639 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 else if (IDecl && IDecl->getSuperClass() != SDecl) {
641 // This implementation and its interface do not have the same
642 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000643 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000644 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000645 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000646 }
647 }
648 }
649
650 if (!IDecl) {
651 // Legacy case of @implementation with no corresponding @interface.
652 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000653
654 // FIXME: Do we support attributes on the @implementation? If so
655 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000656 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
657 ClassName, ClassLoc, false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000658 IDecl->setSuperClass(SDecl);
659 IDecl->setLocEnd(ClassLoc);
Douglas Gregor453e11d2009-04-24 00:16:12 +0000660
661 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000662 } else {
663 // Mark the interface as being completed, even if it was just as
664 // @class ....;
665 // declaration; the user cannot reopen it.
666 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000667 }
668
Ted Kremenek42730c52008-01-07 19:49:32 +0000669 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000670 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000671 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000672
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000673 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000674 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000675
Chris Lattner855e51f2007-12-12 07:09:47 +0000676 // Check that there is no duplicate implementation of this class.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000677 if (LookupObjCImplementation(ClassName))
Chris Lattner1b6de332008-03-16 20:53:07 +0000678 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000679 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000680 else // add it to the list.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000681 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000682 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000683}
684
Ted Kremenek42730c52008-01-07 19:49:32 +0000685void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
686 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000687 SourceLocation RBrace) {
688 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000689 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 if (!IDecl)
691 return;
692 /// Check case of non-existing @interface decl.
693 /// (legacy objective-c @implementation decl without an @interface decl).
694 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000695 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000696 IDecl->setIVarList(ivars, numIvars, Context);
697 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000698 return;
699 }
700 // If implementation has empty ivar list, just return.
701 if (numIvars == 0)
702 return;
703
704 assert(ivars && "missing @implementation ivars");
705
706 // Check interface's Ivar list against those in the implementation.
707 // names and types must match.
708 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000709 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000710 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000711 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
712 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000713 ObjCIvarDecl* ImplIvar = ivars[j++];
714 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000715 assert (ImplIvar && "missing implementation ivar");
716 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000717
718 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000719 if (Context.getCanonicalType(ImplIvar->getType()) !=
720 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000721 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000722 << ImplIvar->getIdentifier()
723 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000724 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000725 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
726 Expr *ImplBitWidth = ImplIvar->getBitWidth();
727 Expr *ClsBitWidth = ClsIvar->getBitWidth();
728 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
729 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
730 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
731 << ImplIvar->getIdentifier();
732 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
733 }
734 }
735 // Make sure the names are identical.
736 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000737 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000738 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000739 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000740 }
741 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000743
744 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000745 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000746 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000747 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000748}
749
Steve Naroffb4f48512008-02-10 21:38:56 +0000750void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
751 bool &IncompleteImpl) {
752 if (!IncompleteImpl) {
753 Diag(ImpLoc, diag::warn_incomplete_impl);
754 IncompleteImpl = true;
755 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000756 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000757}
758
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000759void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
760 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000761 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000762 ImpMethodDecl->getResultType())) {
763 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
764 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
765 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000766 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
767 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000768
769 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
770 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
771 IM != EM; ++IM, ++IF) {
772 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
773 continue;
774
775 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
776 << ImpMethodDecl->getDeclName() << (*IF)->getType()
777 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000778 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000779 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000780}
781
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000782/// isPropertyReadonly - Return true if property is readonly, by searching
783/// for the property in the class and in its categories and implementations
784///
785bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000786 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000787 // by far the most common case.
788 if (!PDecl->isReadOnly())
789 return false;
790 // Even if property is ready only, if interface has a user defined setter,
791 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000792 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000793 return false;
794
795 // Main class has the property as 'readonly'. Must search
796 // through the category list to see if the property's
797 // attribute has been over-ridden to 'readwrite'.
798 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
799 Category; Category = Category->getNextClassCategory()) {
800 // Even if property is ready only, if a category has a user defined setter,
801 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000802 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000803 return false;
804 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000805 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000806 if (P && !P->isReadOnly())
807 return false;
808 }
809
810 // Also, check for definition of a setter method in the implementation if
811 // all else failed.
812 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
813 if (ObjCImplementationDecl *IMD =
814 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000815 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000816 return false;
817 }
818 else if (ObjCCategoryImplDecl *CIMD =
819 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000820 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000821 return false;
822 }
823 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000824 // Lastly, look through the implementation (if one is in scope).
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000825 if (ObjCImplementationDecl *ImpDecl
826 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000827 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000828 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000829 // If all fails, look at the super class.
830 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
831 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000832 return true;
833}
834
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000835/// FIXME: Type hierarchies in Objective-C can be deep. We could most
836/// likely improve the efficiency of selector lookups and type
837/// checking by associating with each protocol / interface / category
838/// the flattened instance tables. If we used an immutable set to keep
839/// the table then it wouldn't add significant memory cost and it
840/// would be handy for lookups.
841
Steve Naroffb268d2a2008-02-08 22:06:17 +0000842/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000843/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000844void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
845 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000846 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000847 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000848 const llvm::DenseSet<Selector> &ClsMap,
849 ObjCInterfaceDecl *IDecl) {
850 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
851
852 // If a method lookup fails locally we still need to look and see if
853 // the method was implemented by a base class or an inherited
854 // protocol. This lookup is slow, but occurs rarely in correct code
855 // and otherwise would terminate in a warning.
856
Chris Lattner855e51f2007-12-12 07:09:47 +0000857 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000858 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
859 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000860 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000861 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000862 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000863 (!Super ||
864 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000865 // Ugly, but necessary. Method declared in protcol might have
866 // have been synthesized due to a property declared in the class which
867 // uses the protocol.
868 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000869 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000870 if (!MethodInClass || !MethodInClass->isSynthesized())
871 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
872 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000873 }
874 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000875 for (ObjCProtocolDecl::classmeth_iterator
876 I = PDecl->classmeth_begin(Context),
877 E = PDecl->classmeth_end(Context);
878 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000879 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000880 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
881 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000882 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000883 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000884 }
Chris Lattner0be08822008-07-21 21:32:27 +0000885 // Check on this protocols's referenced protocols, recursively.
886 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
887 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000888 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000889}
890
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000891void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
892 ObjCContainerDecl* CDecl,
893 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 llvm::DenseSet<Selector> InsMap;
895 // Check and see if instance methods in class interface have been
896 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000897 for (ObjCImplementationDecl::instmeth_iterator
898 I = IMPDecl->instmeth_begin(Context),
899 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000900 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000901
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000902 // Check and see if properties declared in the interface have either 1)
903 // an implementation or 2) there is a @synthesize/@dynamic implementation
904 // of the property in the @implementation.
905 if (isa<ObjCInterfaceDecl>(CDecl))
906 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
907 E = CDecl->prop_end(Context); P != E; ++P) {
908 ObjCPropertyDecl *Prop = (*P);
909 if (Prop->isInvalidDecl())
910 continue;
911 ObjCPropertyImplDecl *PI = 0;
912 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +0000913 for (ObjCImplDecl::propimpl_iterator
914 I = IMPDecl->propimpl_begin(Context),
915 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000916 if ((*I)->getPropertyDecl() == Prop) {
917 PI = (*I);
918 break;
919 }
920 if (PI)
921 continue;
922 if (!InsMap.count(Prop->getGetterName())) {
923 Diag(Prop->getLocation(),
924 diag::warn_setter_getter_impl_required)
925 << Prop->getDeclName() << Prop->getGetterName();
926 Diag(IMPDecl->getLocation(),
927 diag::note_property_impl_required);
928 }
929
930 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
931 Diag(Prop->getLocation(),
932 diag::warn_setter_getter_impl_required)
933 << Prop->getDeclName() << Prop->getSetterName();
934 Diag(IMPDecl->getLocation(),
935 diag::note_property_impl_required);
936 }
937 }
938
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000939 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
940 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000941 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000942 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000943 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000944 }
Chris Lattner28400082009-02-16 19:25:52 +0000945
946 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +0000947 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000948 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000949 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000950 assert(IntfMethodDecl &&
951 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
952 // ImpMethodDecl may be null as in a @dynamic property.
953 if (ImpMethodDecl)
954 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
955 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000956
Chris Lattner855e51f2007-12-12 07:09:47 +0000957 llvm::DenseSet<Selector> ClsMap;
958 // Check and see if class methods in class interface have been
959 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000960 for (ObjCImplementationDecl::classmeth_iterator
961 I = IMPDecl->classmeth_begin(Context),
962 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000963 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000964
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000965 for (ObjCInterfaceDecl::classmeth_iterator
966 I = CDecl->classmeth_begin(Context),
967 E = CDecl->classmeth_end(Context);
968 I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000969 if (!ClsMap.count((*I)->getSelector()))
970 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000971 else {
972 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +0000973 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000974 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000975 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000976 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
977 }
978
Chris Lattner855e51f2007-12-12 07:09:47 +0000979
980 // Check the protocol list for unimplemented methods in the @implementation
981 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000982 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
983 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
984 E = I->protocol_end(); PI != E; ++PI)
985 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
986 InsMap, ClsMap, I);
987 // Check class extensions (unnamed categories)
988 for (ObjCCategoryDecl *Categories = I->getCategoryList();
989 Categories; Categories = Categories->getNextClassCategory()) {
990 if (!Categories->getIdentifier()) {
991 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
992 break;
993 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000994 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000995 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
996 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
997 E = C->protocol_end(); PI != E; ++PI)
998 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
999 InsMap, ClsMap, C->getClassInterface());
1000 } else
1001 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001002}
1003
1004/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001005Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001006Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001007 IdentifierInfo **IdentList,
1008 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001009 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001010
1011 for (unsigned i = 0; i != NumElts; ++i) {
1012 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001013 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001014 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001015 // Maybe we will complain about the shadowed template parameter.
1016 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1017 // Just pretend that we didn't see the previous declaration.
1018 PrevDecl = 0;
1019 }
1020
Ted Kremenek42730c52008-01-07 19:49:32 +00001021 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001022 // GCC apparently allows the following idiom:
1023 //
1024 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1025 // @class XCElementToggler;
1026 //
1027 // FIXME: Make an extension?
1028 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1029 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001030 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001031 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001032 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001033 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001034 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001035 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001036 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1037 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001038 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001039 }
1040
1041 Interfaces.push_back(IDecl);
1042 }
1043
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001044 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001045 &Interfaces[0],
1046 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001047 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001048 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001049 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001050}
1051
1052
1053/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1054/// returns true, or false, accordingly.
1055/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001056bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001057 const ObjCMethodDecl *PrevMethod,
1058 bool matchBasedOnSizeAndAlignment) {
1059 QualType T1 = Context.getCanonicalType(Method->getResultType());
1060 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1061
1062 if (T1 != T2) {
1063 // The result types are different.
1064 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001065 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001066 // Incomplete types don't have a size and alignment.
1067 if (T1->isIncompleteType() || T2->isIncompleteType())
1068 return false;
1069 // Check is based on size and alignment.
1070 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1071 return false;
1072 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001073
1074 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1075 E = Method->param_end();
1076 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1077
1078 for (; ParamI != E; ++ParamI, ++PrevI) {
1079 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1080 T1 = Context.getCanonicalType((*ParamI)->getType());
1081 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001082 if (T1 != T2) {
1083 // The result types are different.
1084 if (!matchBasedOnSizeAndAlignment)
1085 return false;
1086 // Incomplete types don't have a size and alignment.
1087 if (T1->isIncompleteType() || T2->isIncompleteType())
1088 return false;
1089 // Check is based on size and alignment.
1090 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1091 return false;
1092 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001093 }
1094 return true;
1095}
1096
Ted Kremenek42730c52008-01-07 19:49:32 +00001097void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001098 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1099 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001100 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001101 Entry.Method = Method;
1102 Entry.Next = 0;
1103 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001104 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001105
1106 // We've seen a method with this name, see if we have already seen this type
1107 // signature.
1108 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1109 if (MatchTwoMethodDeclarations(Method, List->Method))
1110 return;
1111
1112 // We have a new signature for an existing method - add it.
1113 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1114 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001115}
1116
Steve Naroffec7e0882008-10-21 10:50:19 +00001117// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001118ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1119 SourceRange R) {
1120 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001121 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001122
1123 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001124 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1125 // This checks if the methods differ by size & alignment.
1126 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1127 issueWarning = true;
1128 }
1129 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001130 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001131 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001132 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001133 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001134 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001135 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001136 }
1137 return MethList.Method;
1138}
1139
Ted Kremenek42730c52008-01-07 19:49:32 +00001140void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1141 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001142 if (!FirstMethod.Method) {
1143 // Haven't seen a method with this selector name yet - add it.
1144 FirstMethod.Method = Method;
1145 FirstMethod.Next = 0;
1146 } else {
1147 // We've seen a method with this name, now check the type signature(s).
1148 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1149
Ted Kremenek42730c52008-01-07 19:49:32 +00001150 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001151 Next = Next->Next)
1152 match = MatchTwoMethodDeclarations(Method, Next->Method);
1153
1154 if (!match) {
1155 // We have a new signature for an existing method - add it.
1156 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001157 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001158 FirstMethod.Next = OMI;
1159 }
1160 }
1161}
1162
Steve Naroffab63fd62009-01-08 17:28:14 +00001163/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1164/// have the property type and issue diagnostics if they don't.
1165/// Also synthesize a getter/setter method if none exist (and update the
1166/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1167/// methods is the "right" thing to do.
1168void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1169 ObjCContainerDecl *CD) {
1170 ObjCMethodDecl *GetterMethod, *SetterMethod;
1171
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001172 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1173 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001174
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001175 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001176 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001177 Diag(property->getLocation(),
1178 diag::err_accessor_property_type_mismatch)
1179 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001180 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001181 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1182 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001183
1184 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001185 if (Context.getCanonicalType(SetterMethod->getResultType())
1186 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001187 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001188 if (SetterMethod->param_size() != 1 ||
1189 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001190 Diag(property->getLocation(),
1191 diag::err_accessor_property_type_mismatch)
1192 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001193 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001194 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1195 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001196 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001197
1198 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001199 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001200 // FIXME: The synthesized property we set here is misleading. We
1201 // almost always synthesize these methods unless the user explicitly
1202 // provided prototypes (which is odd, but allowed). Sema should be
1203 // typechecking that the declarations jive in that situation (which
1204 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001205 if (!GetterMethod) {
1206 // No instance method of same name as property getter name was found.
1207 // Declare a getter method and add it to the list of methods
1208 // for this class.
1209 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1210 property->getLocation(), property->getGetterName(),
1211 property->getType(), CD, true, false, true,
1212 (property->getPropertyImplementation() ==
1213 ObjCPropertyDecl::Optional) ?
1214 ObjCMethodDecl::Optional :
1215 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001216 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001217 } else
1218 // A user declared getter will be synthesize when @synthesize of
1219 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001220 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001221 property->setGetterMethodDecl(GetterMethod);
1222
1223 // Skip setter if property is read-only.
1224 if (!property->isReadOnly()) {
1225 // Find the default setter and if one not found, add one.
1226 if (!SetterMethod) {
1227 // No instance method of same name as property setter name was found.
1228 // Declare a setter method and add it to the list of methods
1229 // for this class.
1230 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1231 property->getLocation(),
1232 property->getSetterName(),
1233 Context.VoidTy, CD, true, false, true,
1234 (property->getPropertyImplementation() ==
1235 ObjCPropertyDecl::Optional) ?
1236 ObjCMethodDecl::Optional :
1237 ObjCMethodDecl::Required);
1238 // Invent the arguments for the setter. We don't bother making a
1239 // nice name for the argument.
1240 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001241 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001242 property->getIdentifier(),
1243 property->getType(),
1244 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001245 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001246 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001247 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001248 } else
1249 // A user declared setter will be synthesize when @synthesize of
1250 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001251 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001252 property->setSetterMethodDecl(SetterMethod);
1253 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001254 // Add any synthesized methods to the global pool. This allows us to
1255 // handle the following, which is supported by GCC (and part of the design).
1256 //
1257 // @interface Foo
1258 // @property double bar;
1259 // @end
1260 //
1261 // void thisIsUnfortunate() {
1262 // id foo;
1263 // double bar = [foo bar];
1264 // }
1265 //
Douglas Gregord1675382009-01-09 19:42:16 +00001266 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001267 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001268 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001269 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001270}
1271
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001272// Note: For class/category implemenations, allMethods/allProperties is
1273// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001274void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1275 DeclPtrTy *allMethods, unsigned allNum,
1276 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001277 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001278 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001279
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001280 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1281 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001282 // should be true.
1283 if (!ClassDecl)
1284 return;
1285
Chris Lattner855e51f2007-12-12 07:09:47 +00001286 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001287 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1288 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001289 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001290
Steve Naroffab63fd62009-01-08 17:28:14 +00001291 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001292
1293 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1294 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1295 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1296
Chris Lattner855e51f2007-12-12 07:09:47 +00001297 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001298 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001299 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001300
1301 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001302 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001303 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001304 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001305 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1306 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001307 if ((isInterfaceDeclKind && PrevMethod && !match)
1308 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001309 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001310 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001311 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001312 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001313 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001314 InsMap[Method->getSelector()] = Method;
1315 /// The following allows us to typecheck messages to "id".
1316 AddInstanceMethodToGlobalPool(Method);
1317 }
1318 }
1319 else {
1320 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001321 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001322 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1323 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001324 if ((isInterfaceDeclKind && PrevMethod && !match)
1325 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001326 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001327 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001328 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001329 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001330 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001331 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001332 /// The following allows us to typecheck messages to "Class".
1333 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001334 }
1335 }
1336 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001337 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001338 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001339 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001340 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001341 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001342 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001343 // Categories are used to extend the class by declaring new methods.
1344 // By the same token, they are also used to add new properties. No
1345 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001346
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001347 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001348 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001349 if (C->getIdentifier() == 0)
1350 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001351 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001352 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1353 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1354 // user-defined setter/getter. It also synthesizes setter/getter methods
1355 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001356 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1357 E = CDecl->prop_end(Context);
1358 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001359 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001360 CDecl->setAtEndLoc(AtEndLoc);
1361 }
1362 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001363 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001364 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001365 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001366 } else if (ObjCCategoryImplDecl* CatImplClass =
1367 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001368 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001369
Chris Lattner855e51f2007-12-12 07:09:47 +00001370 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001371 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001372 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001373 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001374 Categories; Categories = Categories->getNextClassCategory()) {
1375 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001376 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001377 break;
1378 }
1379 }
1380 }
1381 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001382 if (isInterfaceDeclKind) {
1383 // Reject invalid vardecls.
1384 for (unsigned i = 0; i != tuvNum; i++) {
1385 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1386 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1387 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001388 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001389 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001390 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001391 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001392 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001393}
1394
1395
1396/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1397/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001398static Decl::ObjCDeclQualifier
1399CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1400 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1401 if (PQTVal & ObjCDeclSpec::DQ_In)
1402 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1403 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1404 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1405 if (PQTVal & ObjCDeclSpec::DQ_Out)
1406 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1407 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1408 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1409 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1410 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1411 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1412 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001413
1414 return ret;
1415}
1416
Chris Lattner5261d0c2009-03-28 19:18:32 +00001417Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001418 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001419 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001420 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001421 Selector Sel,
1422 // optional arguments. The number of types/arguments is obtained
1423 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001424 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001425 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001426 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1427 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001428 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001429
1430 // Make sure we can establish a context for the method.
1431 if (!ClassDecl) {
1432 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001433 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001434 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001435 QualType resultDeclType;
1436
Steve Naroffa442ad92009-02-20 22:59:16 +00001437 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001438 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001439
1440 // Methods cannot return interface types. All ObjC objects are
1441 // passed by reference.
1442 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001443 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1444 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001445 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001446 }
1447 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001448 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001449
Chris Lattner114add62008-03-16 00:49:28 +00001450 ObjCMethodDecl* ObjCMethod =
1451 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001452 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001453 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001454 false,
Chris Lattner114add62008-03-16 00:49:28 +00001455 MethodDeclKind == tok::objc_optional ?
1456 ObjCMethodDecl::Optional :
1457 ObjCMethodDecl::Required);
1458
Chris Lattnereee57c02008-04-04 06:12:32 +00001459 llvm::SmallVector<ParmVarDecl*, 16> Params;
1460
Chris Lattner4f0496b2009-04-11 19:42:43 +00001461 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001462 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001463
Chris Lattner612a2f72009-04-11 18:57:04 +00001464 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001465 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001466 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001467 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001468 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001469 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001470 }
1471
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001472 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001473 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001474 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001475 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001476 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001477 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001478 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001479 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001480 ArgInfo[i].Name, ArgType,
1481 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001482 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001483
Chris Lattner761b72f2009-04-11 19:34:56 +00001484 if (ArgType->isObjCInterfaceType()) {
1485 Diag(ArgInfo[i].NameLoc,
1486 diag::err_object_cannot_be_passed_returned_by_value)
1487 << 1 << ArgType;
1488 Param->setInvalidDecl();
1489 }
1490
Chris Lattnereee57c02008-04-04 06:12:32 +00001491 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001492 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001493
1494 // Apply the attributes to the parameter.
1495 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1496
Chris Lattnereee57c02008-04-04 06:12:32 +00001497 Params.push_back(Param);
1498 }
1499
Steve Naroff79ea0e02009-04-20 15:06:07 +00001500 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001501 ObjCMethod->setObjCDeclQualifier(
1502 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1503 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001504
1505 if (AttrList)
1506 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001507
1508 // For implementations (which can be very "coarse grain"), we add the
1509 // method now. This allows the AST to implement lookup methods that work
1510 // incrementally (without waiting until we parse the @end). It also allows
1511 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001512 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001513 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001514 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001515 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1516 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001517 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001518 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1519 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001520 }
1521 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001522 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001523 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001524 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001525 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1526 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001527 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001528 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1529 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001530 }
1531 }
1532 if (PrevMethod) {
1533 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001534 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001535 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001536 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001537 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001538 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001539}
1540
Daniel Dunbar540ff472008-09-23 21:53:23 +00001541void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1542 SourceLocation Loc,
1543 unsigned &Attributes) {
1544 // FIXME: Improve the reported location.
1545
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001546 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001547 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001548 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1549 ObjCDeclSpec::DQ_PR_assign |
1550 ObjCDeclSpec::DQ_PR_copy |
1551 ObjCDeclSpec::DQ_PR_retain))) {
1552 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1553 "readwrite" :
1554 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1555 "assign" :
1556 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1557 "copy" : "retain";
1558
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001559 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001560 diag::err_objc_property_attr_mutually_exclusive :
1561 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001562 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001563 }
1564
1565 // Check for copy or retain on non-object types.
1566 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1567 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001568 Diag(Loc, diag::err_objc_property_requires_object)
1569 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001570 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1571 }
1572
1573 // Check for more than one of { assign, copy, retain }.
1574 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1575 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001576 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1577 << "assign" << "copy";
1578 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001579 }
1580 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001581 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1582 << "assign" << "retain";
1583 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001584 }
1585 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1586 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001587 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1588 << "copy" << "retain";
1589 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001590 }
1591 }
1592
1593 // Warn if user supplied no assignment attribute, property is
1594 // readwrite, and this is an object type.
1595 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1596 ObjCDeclSpec::DQ_PR_retain)) &&
1597 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1598 Context.isObjCObjectPointerType(PropertyTy)) {
1599 // Skip this warning in gc-only mode.
1600 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1601 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1602
1603 // If non-gc code warn that this is likely inappropriate.
1604 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1605 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1606
1607 // FIXME: Implement warning dependent on NSCopying being
1608 // implemented. See also:
1609 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1610 // (please trim this list while you are at it).
1611 }
1612}
1613
Chris Lattner5261d0c2009-03-28 19:18:32 +00001614Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1615 FieldDeclarator &FD,
1616 ObjCDeclSpec &ODS,
1617 Selector GetterSel,
1618 Selector SetterSel,
1619 DeclPtrTy ClassCategory,
1620 bool *isOverridingProperty,
1621 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001622 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001623 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1624 // default is readwrite!
1625 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1626 // property is defaulted to 'assign' if it is readwrite and is
1627 // not retain or copy
1628 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1629 (isReadWrite &&
1630 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1631 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1632 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001633 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001634 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001635 // May modify Attributes.
1636 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001637 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1638 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001639 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001640 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001641 if ((CCPrimary = CDecl->getClassInterface())) {
1642 // Find the property in continuation class's primary class only.
1643 ObjCPropertyDecl *PIDecl = 0;
1644 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001645 for (ObjCInterfaceDecl::prop_iterator
1646 I = CCPrimary->prop_begin(Context),
1647 E = CCPrimary->prop_end(Context);
1648 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001649 if ((*I)->getIdentifier() == PropertyId) {
1650 PIDecl = *I;
1651 break;
1652 }
1653
1654 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001655 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001656 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001657 unsigned PIkind = PIDecl->getPropertyAttributes();
1658 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001659 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001660 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1661 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001662 PIDecl->makeitReadWriteAttribute();
1663 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1664 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1665 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1666 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1667 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001668 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001669 else
1670 Diag(AtLoc, diag::err_use_continuation_class)
1671 << CCPrimary->getDeclName();
1672 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001673 // Make sure setter decl is synthesized, and added to primary
1674 // class's list.
1675 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001676 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001677 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001678 // No matching property found in the primary class. Just fall thru
1679 // and add property to continuation class's primary class.
1680 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001681 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001682 Diag(CDecl->getLocation(), diag::err_continuation_class);
1683 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001684 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001685 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001686 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001687
Steve Naroffdcf1e842009-01-11 12:47:58 +00001688 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1689 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001690 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1691 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001692 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001693 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001694
Chris Lattner6e48be22009-04-11 20:14:49 +00001695 if (T->isArrayType() || T->isFunctionType()) {
1696 Diag(AtLoc, diag::err_property_type) << T;
1697 PDecl->setInvalidDecl();
1698 }
1699
Chris Lattner22063402009-02-16 18:32:47 +00001700 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001701
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001702 // Regardless of setter/getter attribute, we save the default getter/setter
1703 // selector names in anticipation of declaration of setter/getter methods.
1704 PDecl->setGetterName(GetterSel);
1705 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001706
Daniel Dunbar540ff472008-09-23 21:53:23 +00001707 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001708 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001709
Daniel Dunbar540ff472008-09-23 21:53:23 +00001710 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001711 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001712
Daniel Dunbar540ff472008-09-23 21:53:23 +00001713 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001714 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001715
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001716 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001717 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001718
Daniel Dunbar540ff472008-09-23 21:53:23 +00001719 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001720 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001721
Daniel Dunbar540ff472008-09-23 21:53:23 +00001722 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001723 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001724
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001725 if (isAssign)
1726 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1727
Daniel Dunbar540ff472008-09-23 21:53:23 +00001728 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001729 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001730
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001731 if (MethodImplKind == tok::objc_required)
1732 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1733 else if (MethodImplKind == tok::objc_optional)
1734 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001735 // A case of continuation class adding a new property in the class. This
1736 // is not what it was meant for. However, gcc supports it and so should we.
1737 // Make sure setter/getters are declared here.
1738 if (CCPrimary)
1739 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001740
Chris Lattner5261d0c2009-03-28 19:18:32 +00001741 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001742}
1743
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001744/// ActOnPropertyImplDecl - This routine performs semantic checks and
1745/// builds the AST node for a property implementation declaration; declared
1746/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001747///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001748Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1749 SourceLocation PropertyLoc,
1750 bool Synthesize,
1751 DeclPtrTy ClassCatImpDecl,
1752 IdentifierInfo *PropertyId,
1753 IdentifierInfo *PropertyIvar) {
1754 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001755 // Make sure we have a context for the property implementation declaration.
1756 if (!ClassImpDecl) {
1757 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001758 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001759 }
1760 ObjCPropertyDecl *property = 0;
1761 ObjCInterfaceDecl* IDecl = 0;
1762 // Find the class or category class where this property must have
1763 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001764 ObjCImplementationDecl *IC = 0;
1765 ObjCCategoryImplDecl* CatImplClass = 0;
1766 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001767 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001768 // We always synthesize an interface for an implementation
1769 // without an interface decl. So, IDecl is always non-zero.
1770 assert(IDecl &&
1771 "ActOnPropertyImplDecl - @implementation without @interface");
1772
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001773 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001774 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001775 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001776 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001777 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001778 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001779 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001780 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001781 if (Synthesize) {
1782 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001783 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001784 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001785 IDecl = CatImplClass->getClassInterface();
1786 if (!IDecl) {
1787 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001788 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001789 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001790 ObjCCategoryDecl *Category =
1791 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1792
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001793 // If category for this implementation not found, it is an error which
1794 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001795 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001796 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001797 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001798 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001799 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001800 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001801 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001802 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001803 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001804 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001805 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001806 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001807 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001808 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001809 // Check that we have a valid, previously declared ivar for @synthesize
1810 if (Synthesize) {
1811 // @synthesize
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001812 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001813 if (!PropertyIvar)
1814 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001815 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001816 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001817 ObjCInterfaceDecl *ClassDeclared;
1818 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001819 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001820 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1821 PropertyIvar, PropType,
1822 ObjCIvarDecl::Public,
1823 (Expr *)0);
1824 property->setPropertyIvarDecl(Ivar);
1825 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001826 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001827 // Note! I deliberately want it to fall thru so, we have a
1828 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001829 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001830 else if (getLangOptions().ObjCNonFragileABI &&
1831 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1832 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1833 << property->getDeclName() << Ivar->getDeclName()
1834 << ClassDeclared->getDeclName();
1835 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1836 << Ivar << Ivar->getNameAsCString();
1837 // Note! I deliberately want it to fall thru so more errors are caught.
1838 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001839 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1840
Steve Naroff631e3922008-09-30 00:24:17 +00001841 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001842 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001843 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001844 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001845 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001846 // Note! I deliberately want it to fall thru so, we have a
1847 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001848 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001849
1850 // FIXME! Rules for properties are somewhat different that those
1851 // for assignments. Use a new routine to consolidate all cases;
1852 // specifically for property redeclarations as well as for ivars.
1853 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1854 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1855 if (lhsType != rhsType &&
1856 lhsType->isArithmeticType()) {
1857 Diag(PropertyLoc, diag::error_property_ivar_type)
1858 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001859 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001860 }
1861 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00001862 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1863 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001864 Diag(PropertyLoc, diag::error_weak_property)
1865 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001866 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001867 }
1868 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00001869 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1870 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001871 Diag(PropertyLoc, diag::error_strong_property)
1872 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001873 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001874 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001875 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001876 } else if (PropertyIvar)
1877 // @dynamic
1878 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001879 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001880 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001881 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1882 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001883 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001884 ObjCPropertyImplDecl::Synthesize
1885 : ObjCPropertyImplDecl::Dynamic),
1886 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001887 if (IC) {
1888 if (Synthesize)
1889 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001890 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001891 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1892 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1893 << PropertyIvar;
1894 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1895 }
1896
Douglas Gregorcd19b572009-04-23 01:02:12 +00001897 if (ObjCPropertyImplDecl *PPIDecl
1898 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001899 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1900 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001901 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001902 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001903 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001904 }
1905 else {
1906 if (Synthesize)
1907 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001908 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001909 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1910 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1911 << PropertyIvar;
1912 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1913 }
1914
1915 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001916 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001917 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1918 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001919 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001920 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001921 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001922 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001923
Chris Lattner5261d0c2009-03-28 19:18:32 +00001924 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001925}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001926
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001927bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001928 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001929 return false;
1930
1931 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1932 D->setInvalidDecl();
1933
1934 return true;
1935}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001936
1937/// Collect the instance variables declared in an Objective-C object. Used in
1938/// the creation of structures from objects using the @defs directive.
1939/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1940/// part of the AST generation logic of @defs.
1941static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1942 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001943 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001944 if (Class->getSuperClass())
1945 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1946
1947 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001948 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1949 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001950 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001951 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1952 ID->getIdentifier(), ID->getType(),
1953 ID->getBitWidth());
1954 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001955 }
1956}
1957
1958/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1959/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001960void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001961 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001962 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001963 // Check that ClassName is a valid class
1964 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1965 if (!Class) {
1966 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1967 return;
1968 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00001969 if (LangOpts.ObjCNonFragileABI) {
1970 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1971 return;
1972 }
1973
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001974 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00001975 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001976
1977 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001978 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001979 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001980 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001981 if (getLangOptions().CPlusPlus)
1982 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001983 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001984 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001985 }
1986}
1987