blob: 208562f8f12dc959d26f157e3a335d06f9ed95fc [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
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000045
Daniel Dunbareaf91c32008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000048
Chris Lattner97316c02008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000050 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
51 E = MDecl->param_end(); PI != E; ++PI)
52 if ((*PI)->getIdentifier())
53 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000054}
55
Chris Lattner5261d0c2009-03-28 19:18:32 +000056Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000057ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000060 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremenek42730c52008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000076 }
77
Ted Kremenek42730c52008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000082 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000083 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000084 Diag(IDecl->getLocation(), diag::note_previous_definition);
85
Steve Naroff1422a622008-11-18 19:15:30 +000086 // Return the previous class interface.
87 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +000088 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +000089 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000090 IDecl->setLocation(AtInterfaceLoc);
91 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000092 }
Chris Lattner5cece462008-07-21 07:06:49 +000093 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000094 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000095 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000096 if (AttrList)
97 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000098
Steve Naroff15208162008-04-02 18:30:49 +000099 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000100 // FIXME: PushOnScopeChains
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000101 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000103 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 }
105
106 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000107 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000108 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000109
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000110 ObjCInterfaceDecl *SuperClassDecl =
111 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000112
113 // Diagnose classes that inherit from deprecated classes.
114 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000115 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000116
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000117 if (PrevDecl && SuperClassDecl == 0) {
118 // The previous declaration was not a class decl. Check if we have a
119 // typedef. If we do, get the underlying class type.
120 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
121 QualType T = TDecl->getUnderlyingType();
122 if (T->isObjCInterfaceType()) {
123 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
124 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
125 }
126 }
Chris Lattner7699a532009-02-16 21:33:09 +0000127
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000128 // This handles the following case:
129 //
130 // typedef int SuperClass;
131 // @interface MyClass : SuperClass {} @end
132 //
133 if (!SuperClassDecl) {
134 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
135 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
136 }
137 }
Chris Lattner7699a532009-02-16 21:33:09 +0000138
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000139 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
140 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000141 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000142 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000143 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000144 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000145 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000146 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000148 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000149 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 IDecl->setLocEnd(SuperLoc);
151 } else { // we have a root class.
152 IDecl->setLocEnd(ClassLoc);
153 }
154
Steve Naroff1422a622008-11-18 19:15:30 +0000155 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000156 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000157 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
158 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000159 IDecl->setLocEnd(EndProtoLoc);
160 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000161
162 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000163 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000164}
165
166/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000167/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000168Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
169 IdentifierInfo *AliasName,
170 SourceLocation AliasLocation,
171 IdentifierInfo *ClassName,
172 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000173 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000174 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000176 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000178 else
Chris Lattner65cae292008-11-19 08:23:25 +0000179 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000180 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000182 }
183 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000184 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000185 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
186 QualType T = TDecl->getUnderlyingType();
187 if (T->isObjCInterfaceType()) {
188 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
189 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000190 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000191 }
192 }
193 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000194 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
195 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000196 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000197 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000198 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000201
202 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000203 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000204 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000205
206 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000207
208 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000209 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000210 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000211 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000212
Chris Lattner5261d0c2009-03-28 19:18:32 +0000213 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000214}
215
Steve Narofffe56a632009-03-05 15:22:01 +0000216void Sema::CheckForwardProtocolDeclarationForCircularDependency(
217 IdentifierInfo *PName,
218 SourceLocation &Ploc, SourceLocation PrevLoc,
219 const ObjCList<ObjCProtocolDecl> &PList)
220{
221 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
222 E = PList.end(); I != E; ++I) {
223
224 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
225 if (PDecl->getIdentifier() == PName) {
226 Diag(Ploc, diag::err_protocol_has_circular_dependency);
227 Diag(PrevLoc, diag::note_previous_definition);
228 }
229 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
230 PDecl->getLocation(), PDecl->getReferencedProtocols());
231 }
232 }
233}
234
Chris Lattner5261d0c2009-03-28 19:18:32 +0000235Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000236Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
237 IdentifierInfo *ProtocolName,
238 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000239 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000240 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000241 SourceLocation EndProtoLoc,
242 AttributeList *AttrList) {
243 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000244 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000246 if (PDecl) {
247 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000248 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000249 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000250 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000251 // Just return the protocol we already had.
252 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000253 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000254 }
Steve Narofffe56a632009-03-05 15:22:01 +0000255 ObjCList<ObjCProtocolDecl> PList;
256 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
257 CheckForwardProtocolDeclarationForCircularDependency(
258 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
259 PList.Destroy(Context);
260
Steve Naroff9a9e5212008-08-13 16:39:22 +0000261 // Make sure the cached decl gets a valid start location.
262 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000263 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000264 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000265 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
266 AtProtoInterfaceLoc,ProtocolName);
267 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000268 CurContext->addDecl(Context, PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000269 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000271 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000272 if (AttrList)
273 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000274 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000275 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000276 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000277 PDecl->setLocEnd(EndProtoLoc);
278 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000279
280 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000281 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000282}
283
284/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000285/// issues an error if they are not declared. It returns list of
286/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000287void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000288Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000289 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000290 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000291 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000292 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000293 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
294 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000295 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000296 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000297 continue;
298 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000299
Douglas Gregoraa57e862009-02-18 21:56:37 +0000300 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000301
302 // If this is a forward declaration and we are supposed to warn in this
303 // case, do it.
304 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000305 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000306 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000307 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000308 }
309}
310
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000312/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000313///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000314void
315Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
316 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000317 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000318 ObjCPropertyDecl::PropertyAttributeKind CAttr =
319 Property->getPropertyAttributes();
320 ObjCPropertyDecl::PropertyAttributeKind SAttr =
321 SuperProperty->getPropertyAttributes();
322 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
323 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000324 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000325 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
327 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000330 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
331 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000332 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000333 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000334
335 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
336 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000337 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000338 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000339 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000340 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000341 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000342 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000343 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000344 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000345
346 QualType LHSType =
347 Context.getCanonicalType(SuperProperty->getType());
348 QualType RHSType =
349 Context.getCanonicalType(Property->getType());
350
351 if (!Context.typesAreCompatible(LHSType, RHSType)) {
352 // FIXME: Incorporate this test with typesAreCompatible.
353 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
354 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
355 return;
356 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
357 << Property->getType() << SuperProperty->getType() << inheritedName;
358 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000359}
360
361/// ComparePropertiesInBaseAndSuper - This routine compares property
362/// declarations in base and its super class, if any, and issues
363/// diagnostics in a variety of inconsistant situations.
364///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000365void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000366 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
367 if (!SDecl)
368 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000369 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000370 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
371 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000372 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000373 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000374 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
375 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000376 ObjCPropertyDecl *PDecl = (*I);
377 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000378 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000379 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000380 }
381 }
382}
383
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000384/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
385/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000386/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000387void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000388Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000389 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000390 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
391 if (!IDecl) {
392 // Category
393 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
394 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000395 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
396 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000397 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000398 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000399 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000400 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401 CP != CE; ++CP)
402 if ((*CP)->getIdentifier() == Pr->getIdentifier())
403 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000404 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000405 // Property protocol already exist in class. Diagnose any mismatch.
406 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
407 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000408 return;
409 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000410 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
411 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000413 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000414 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000415 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000416 CP != CE; ++CP)
417 if ((*CP)->getIdentifier() == Pr->getIdentifier())
418 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000419 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000420 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000421 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000422 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000423}
424
425/// MergeProtocolPropertiesIntoClass - This routine merges properties
426/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000427/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000429void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000430 DeclPtrTy MergeItsProtocols) {
431 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000432 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
433
434 if (!IDecl) {
435 // Category
436 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
437 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
438 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
439 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
440 E = MDecl->protocol_end(); P != E; ++P)
441 // Merge properties of category (*P) into IDECL's
442 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
443
444 // Go thru the list of protocols for this category and recursively merge
445 // their properties into this class as well.
446 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
447 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000448 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000449 } else {
450 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
451 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
452 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000453 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000454 }
455 return;
456 }
457
Chris Lattner5cece462008-07-21 07:06:49 +0000458 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000459 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
460 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000461 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000462 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
463
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000464 // Go thru the list of protocols for this class and recursively merge
465 // their properties into this class as well.
466 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
467 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000468 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000469 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000470 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
471 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
472 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000473 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000474 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000475}
476
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000477/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000478/// a class method in its extension.
479///
480void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
481 ObjCInterfaceDecl *ID) {
482 if (!ID)
483 return; // Possibly due to previous error
484
485 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000486 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
487 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000488 ObjCMethodDecl *MD = *i;
489 MethodMap[MD->getSelector()] = MD;
490 }
491
492 if (MethodMap.empty())
493 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000494 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
495 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000496 ObjCMethodDecl *Method = *i;
497 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
498 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
499 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
500 << Method->getDeclName();
501 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
502 }
503 }
504}
505
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000506/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000507Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000508Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000509 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000510 unsigned NumElts,
511 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000512 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000513
514 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000515 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000516 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000517 if (PDecl == 0) { // Not already seen?
518 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
519 IdentList[i].second, Ident);
520 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000521 CurContext->addDecl(Context, PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000522 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000523 if (attrList)
524 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000525 Protocols.push_back(PDecl);
526 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000527
528 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000529 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000530 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000531 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000532 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000533 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000534}
535
Chris Lattner5261d0c2009-03-28 19:18:32 +0000536Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000537ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
538 IdentifierInfo *ClassName, SourceLocation ClassLoc,
539 IdentifierInfo *CategoryName,
540 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000541 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000542 unsigned NumProtoRefs,
543 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000544 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000545 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
546 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000547 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000548
549 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000550 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000551 if (!IDecl || IDecl->isForwardDecl()) {
552 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000553 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000554 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000555 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000556
Chris Lattner1fa7f622009-02-16 21:26:43 +0000557 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000558
559 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000560 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000561
562 /// Check for duplicate interface declaration for this category
563 ObjCCategoryDecl *CDeclChain;
564 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
565 CDeclChain = CDeclChain->getNextClassCategory()) {
566 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
567 Diag(CategoryLoc, diag::warn_dup_category_def)
568 << ClassName << CategoryName;
569 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
570 break;
571 }
572 }
573 if (!CDeclChain)
574 CDecl->insertNextClassCategory();
575
Chris Lattner855e51f2007-12-12 07:09:47 +0000576 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000577 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000578 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000580
581 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000582 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000583}
584
585/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000586/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000587/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000588Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000589 SourceLocation AtCatImplLoc,
590 IdentifierInfo *ClassName, SourceLocation ClassLoc,
591 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000592 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000593 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000594 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
595 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596 /// Check that class of this category is already completely declared.
597 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000598 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000599
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000600 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000601 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000602
Chris Lattner855e51f2007-12-12 07:09:47 +0000603 /// TODO: Check that CatName, category name, is not used in another
604 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000605 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000606
607 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000608 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000609}
610
Chris Lattner5261d0c2009-03-28 19:18:32 +0000611Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 SourceLocation AtClassImplLoc,
613 IdentifierInfo *ClassName, SourceLocation ClassLoc,
614 IdentifierInfo *SuperClassname,
615 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000618 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000620 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000621 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000622 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000626 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 }
628
629 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 if (SuperClassname) {
632 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000633 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000634 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
636 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000637 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000638 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000641 Diag(SuperClassLoc, diag::err_undef_superclass)
642 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000643 else if (IDecl && IDecl->getSuperClass() != SDecl) {
644 // This implementation and its interface do not have the same
645 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000646 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000647 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000648 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 }
650 }
651 }
652
653 if (!IDecl) {
654 // Legacy case of @implementation with no corresponding @interface.
655 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000656
657 // FIXME: Do we support attributes on the @implementation? If so
658 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000659 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
660 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000661 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 IDecl->setSuperClass(SDecl);
663 IDecl->setLocEnd(ClassLoc);
664
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000665 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000666 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000667 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000668 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000669 }
670
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000672 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000673 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000675 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000676 CurContext->addDecl(Context, IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000677
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000678 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000679 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000680
Chris Lattner855e51f2007-12-12 07:09:47 +0000681 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000683 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000684 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000685 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000686 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000687 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000688}
689
Ted Kremenek42730c52008-01-07 19:49:32 +0000690void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
691 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000692 SourceLocation RBrace) {
693 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000694 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000695 if (!IDecl)
696 return;
697 /// Check case of non-existing @interface decl.
698 /// (legacy objective-c @implementation decl without an @interface decl).
699 /// Add implementations's ivar to the synthesize class's ivar list.
700 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000701 IDecl->setIVarList(ivars, numIvars, Context);
702 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000703 return;
704 }
705 // If implementation has empty ivar list, just return.
706 if (numIvars == 0)
707 return;
708
709 assert(ivars && "missing @implementation ivars");
710
711 // Check interface's Ivar list against those in the implementation.
712 // names and types must match.
713 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000714 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000716 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
717 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 ObjCIvarDecl* ImplIvar = ivars[j++];
719 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000720 assert (ImplIvar && "missing implementation ivar");
721 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000722
723 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000724 if (Context.getCanonicalType(ImplIvar->getType()) !=
725 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000726 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000727 << ImplIvar->getIdentifier()
728 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000729 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000730 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
731 Expr *ImplBitWidth = ImplIvar->getBitWidth();
732 Expr *ClsBitWidth = ClsIvar->getBitWidth();
733 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
734 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
735 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
736 << ImplIvar->getIdentifier();
737 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
738 }
739 }
740 // Make sure the names are identical.
741 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000742 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000743 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000744 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000745 }
746 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000748
749 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000750 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000751 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000752 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000753}
754
Steve Naroffb4f48512008-02-10 21:38:56 +0000755void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
756 bool &IncompleteImpl) {
757 if (!IncompleteImpl) {
758 Diag(ImpLoc, diag::warn_incomplete_impl);
759 IncompleteImpl = true;
760 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000761 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000762}
763
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000764void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
765 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000766 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000767 ImpMethodDecl->getResultType())) {
768 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
769 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
770 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000771 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
772 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000773
774 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
775 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
776 IM != EM; ++IM, ++IF) {
777 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
778 continue;
779
780 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
781 << ImpMethodDecl->getDeclName() << (*IF)->getType()
782 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000783 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000784 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000785}
786
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000787/// isPropertyReadonly - Return true if property is readonly, by searching
788/// for the property in the class and in its categories and implementations
789///
790bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000791 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000792 // by far the most common case.
793 if (!PDecl->isReadOnly())
794 return false;
795 // Even if property is ready only, if interface has a user defined setter,
796 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000797 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000798 return false;
799
800 // Main class has the property as 'readonly'. Must search
801 // through the category list to see if the property's
802 // attribute has been over-ridden to 'readwrite'.
803 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
804 Category; Category = Category->getNextClassCategory()) {
805 // Even if property is ready only, if a category has a user defined setter,
806 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000807 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000808 return false;
809 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000810 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000811 if (P && !P->isReadOnly())
812 return false;
813 }
814
815 // Also, check for definition of a setter method in the implementation if
816 // all else failed.
817 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
818 if (ObjCImplementationDecl *IMD =
819 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
820 if (IMD->getInstanceMethod(PDecl->getSetterName()))
821 return false;
822 }
823 else if (ObjCCategoryImplDecl *CIMD =
824 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
825 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
826 return false;
827 }
828 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000829 // Lastly, look through the implementation (if one is in scope).
830 if (ObjCImplementationDecl *ImpDecl =
831 ObjCImplementations[IDecl->getIdentifier()])
832 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
833 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000834 // If all fails, look at the super class.
835 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
836 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000837 return true;
838}
839
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000840/// FIXME: Type hierarchies in Objective-C can be deep. We could most
841/// likely improve the efficiency of selector lookups and type
842/// checking by associating with each protocol / interface / category
843/// the flattened instance tables. If we used an immutable set to keep
844/// the table then it wouldn't add significant memory cost and it
845/// would be handy for lookups.
846
Steve Naroffb268d2a2008-02-08 22:06:17 +0000847/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000848/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000849void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
850 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000851 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000852 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000853 const llvm::DenseSet<Selector> &ClsMap,
854 ObjCInterfaceDecl *IDecl) {
855 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
856
857 // If a method lookup fails locally we still need to look and see if
858 // the method was implemented by a base class or an inherited
859 // protocol. This lookup is slow, but occurs rarely in correct code
860 // and otherwise would terminate in a warning.
861
Chris Lattner855e51f2007-12-12 07:09:47 +0000862 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000863 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
864 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000866 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000867 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000868 (!Super ||
869 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000870 // Ugly, but necessary. Method declared in protcol might have
871 // have been synthesized due to a property declared in the class which
872 // uses the protocol.
873 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000874 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000875 if (!MethodInClass || !MethodInClass->isSynthesized())
876 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
877 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000878 }
879 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000880 for (ObjCProtocolDecl::classmeth_iterator
881 I = PDecl->classmeth_begin(Context),
882 E = PDecl->classmeth_end(Context);
883 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000884 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000885 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
886 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000887 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000888 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000889 }
Chris Lattner0be08822008-07-21 21:32:27 +0000890 // Check on this protocols's referenced protocols, recursively.
891 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
892 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000893 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000894}
895
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000896void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
897 ObjCContainerDecl* CDecl,
898 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 llvm::DenseSet<Selector> InsMap;
900 // Check and see if instance methods in class interface have been
901 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000902 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000903 E = IMPDecl->instmeth_end(); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000904 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000905
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000906 // Check and see if properties declared in the interface have either 1)
907 // an implementation or 2) there is a @synthesize/@dynamic implementation
908 // of the property in the @implementation.
909 if (isa<ObjCInterfaceDecl>(CDecl))
910 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
911 E = CDecl->prop_end(Context); P != E; ++P) {
912 ObjCPropertyDecl *Prop = (*P);
913 if (Prop->isInvalidDecl())
914 continue;
915 ObjCPropertyImplDecl *PI = 0;
916 // Is there a matching propery synthesize/dynamic?
917 for (ObjCImplDecl::propimpl_iterator I = IMPDecl->propimpl_begin(),
918 EI = IMPDecl->propimpl_end(); I != EI; ++I)
919 if ((*I)->getPropertyDecl() == Prop) {
920 PI = (*I);
921 break;
922 }
923 if (PI)
924 continue;
925 if (!InsMap.count(Prop->getGetterName())) {
926 Diag(Prop->getLocation(),
927 diag::warn_setter_getter_impl_required)
928 << Prop->getDeclName() << Prop->getGetterName();
929 Diag(IMPDecl->getLocation(),
930 diag::note_property_impl_required);
931 }
932
933 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
934 Diag(Prop->getLocation(),
935 diag::warn_setter_getter_impl_required)
936 << Prop->getDeclName() << Prop->getSetterName();
937 Diag(IMPDecl->getLocation(),
938 diag::note_property_impl_required);
939 }
940 }
941
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000942 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
943 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000944 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000945 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000946 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000947 }
Chris Lattner28400082009-02-16 19:25:52 +0000948
949 ObjCMethodDecl *ImpMethodDecl =
950 IMPDecl->getInstanceMethod((*I)->getSelector());
951 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000952 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000953 assert(IntfMethodDecl &&
954 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
955 // ImpMethodDecl may be null as in a @dynamic property.
956 if (ImpMethodDecl)
957 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
958 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000959
Chris Lattner855e51f2007-12-12 07:09:47 +0000960 llvm::DenseSet<Selector> ClsMap;
961 // Check and see if class methods in class interface have been
962 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000963 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000964 E = IMPDecl->classmeth_end(); I != E; ++I)
965 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000966
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000967 for (ObjCInterfaceDecl::classmeth_iterator
968 I = CDecl->classmeth_begin(Context),
969 E = CDecl->classmeth_end(Context);
970 I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000971 if (!ClsMap.count((*I)->getSelector()))
972 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000973 else {
974 ObjCMethodDecl *ImpMethodDecl =
975 IMPDecl->getClassMethod((*I)->getSelector());
976 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000977 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000978 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
979 }
980
Chris Lattner855e51f2007-12-12 07:09:47 +0000981
982 // Check the protocol list for unimplemented methods in the @implementation
983 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000984 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
985 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
986 E = I->protocol_end(); PI != E; ++PI)
987 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
988 InsMap, ClsMap, I);
989 // Check class extensions (unnamed categories)
990 for (ObjCCategoryDecl *Categories = I->getCategoryList();
991 Categories; Categories = Categories->getNextClassCategory()) {
992 if (!Categories->getIdentifier()) {
993 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
994 break;
995 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000996 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000997 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
998 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
999 E = C->protocol_end(); PI != E; ++PI)
1000 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1001 InsMap, ClsMap, C->getClassInterface());
1002 } else
1003 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001004}
1005
1006/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001007Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001008Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001009 IdentifierInfo **IdentList,
1010 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001011 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001012
1013 for (unsigned i = 0; i != NumElts; ++i) {
1014 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001015 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001016 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001017 // Maybe we will complain about the shadowed template parameter.
1018 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1019 // Just pretend that we didn't see the previous declaration.
1020 PrevDecl = 0;
1021 }
1022
Ted Kremenek42730c52008-01-07 19:49:32 +00001023 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001024 // GCC apparently allows the following idiom:
1025 //
1026 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1027 // @class XCElementToggler;
1028 //
1029 // FIXME: Make an extension?
1030 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1031 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001032 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001033 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001034 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001035 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001037 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001038 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1039 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +00001040 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +00001041
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001042 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001043 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001044 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001045 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +00001046 }
1047
1048 Interfaces.push_back(IDecl);
1049 }
1050
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001051 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001052 &Interfaces[0],
1053 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001054 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001055 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001056 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001057}
1058
1059
1060/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1061/// returns true, or false, accordingly.
1062/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001063bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001064 const ObjCMethodDecl *PrevMethod,
1065 bool matchBasedOnSizeAndAlignment) {
1066 QualType T1 = Context.getCanonicalType(Method->getResultType());
1067 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1068
1069 if (T1 != T2) {
1070 // The result types are different.
1071 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001072 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001073 // Incomplete types don't have a size and alignment.
1074 if (T1->isIncompleteType() || T2->isIncompleteType())
1075 return false;
1076 // Check is based on size and alignment.
1077 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1078 return false;
1079 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001080
1081 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1082 E = Method->param_end();
1083 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1084
1085 for (; ParamI != E; ++ParamI, ++PrevI) {
1086 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1087 T1 = Context.getCanonicalType((*ParamI)->getType());
1088 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001089 if (T1 != T2) {
1090 // The result types are different.
1091 if (!matchBasedOnSizeAndAlignment)
1092 return false;
1093 // Incomplete types don't have a size and alignment.
1094 if (T1->isIncompleteType() || T2->isIncompleteType())
1095 return false;
1096 // Check is based on size and alignment.
1097 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1098 return false;
1099 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001100 }
1101 return true;
1102}
1103
Ted Kremenek42730c52008-01-07 19:49:32 +00001104void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001105 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1106 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001107 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001108 Entry.Method = Method;
1109 Entry.Next = 0;
1110 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001111 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001112
1113 // We've seen a method with this name, see if we have already seen this type
1114 // signature.
1115 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1116 if (MatchTwoMethodDeclarations(Method, List->Method))
1117 return;
1118
1119 // We have a new signature for an existing method - add it.
1120 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1121 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001122}
1123
Steve Naroffec7e0882008-10-21 10:50:19 +00001124// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001125ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1126 SourceRange R) {
1127 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001128 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001129
1130 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001131 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1132 // This checks if the methods differ by size & alignment.
1133 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1134 issueWarning = true;
1135 }
1136 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001137 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001138 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001139 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001140 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001141 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001142 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001143 }
1144 return MethList.Method;
1145}
1146
Ted Kremenek42730c52008-01-07 19:49:32 +00001147void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1148 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001149 if (!FirstMethod.Method) {
1150 // Haven't seen a method with this selector name yet - add it.
1151 FirstMethod.Method = Method;
1152 FirstMethod.Next = 0;
1153 } else {
1154 // We've seen a method with this name, now check the type signature(s).
1155 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1156
Ted Kremenek42730c52008-01-07 19:49:32 +00001157 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001158 Next = Next->Next)
1159 match = MatchTwoMethodDeclarations(Method, Next->Method);
1160
1161 if (!match) {
1162 // We have a new signature for an existing method - add it.
1163 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001164 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001165 FirstMethod.Next = OMI;
1166 }
1167 }
1168}
1169
Steve Naroffab63fd62009-01-08 17:28:14 +00001170/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1171/// have the property type and issue diagnostics if they don't.
1172/// Also synthesize a getter/setter method if none exist (and update the
1173/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1174/// methods is the "right" thing to do.
1175void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1176 ObjCContainerDecl *CD) {
1177 ObjCMethodDecl *GetterMethod, *SetterMethod;
1178
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001179 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1180 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001181
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001182 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001183 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001184 Diag(property->getLocation(),
1185 diag::err_accessor_property_type_mismatch)
1186 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001187 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001188 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1189 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001190
1191 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001192 if (Context.getCanonicalType(SetterMethod->getResultType())
1193 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001194 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001195 if (SetterMethod->param_size() != 1 ||
1196 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001197 Diag(property->getLocation(),
1198 diag::err_accessor_property_type_mismatch)
1199 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001200 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001201 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1202 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001203 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001204
1205 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001206 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001207 // FIXME: The synthesized property we set here is misleading. We
1208 // almost always synthesize these methods unless the user explicitly
1209 // provided prototypes (which is odd, but allowed). Sema should be
1210 // typechecking that the declarations jive in that situation (which
1211 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001212 if (!GetterMethod) {
1213 // No instance method of same name as property getter name was found.
1214 // Declare a getter method and add it to the list of methods
1215 // for this class.
1216 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1217 property->getLocation(), property->getGetterName(),
1218 property->getType(), CD, true, false, true,
1219 (property->getPropertyImplementation() ==
1220 ObjCPropertyDecl::Optional) ?
1221 ObjCMethodDecl::Optional :
1222 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001223 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001224 } else
1225 // A user declared getter will be synthesize when @synthesize of
1226 // the property with the same name is seen in the @implementation
1227 GetterMethod->setIsSynthesized();
1228 property->setGetterMethodDecl(GetterMethod);
1229
1230 // Skip setter if property is read-only.
1231 if (!property->isReadOnly()) {
1232 // Find the default setter and if one not found, add one.
1233 if (!SetterMethod) {
1234 // No instance method of same name as property setter name was found.
1235 // Declare a setter method and add it to the list of methods
1236 // for this class.
1237 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1238 property->getLocation(),
1239 property->getSetterName(),
1240 Context.VoidTy, CD, true, false, true,
1241 (property->getPropertyImplementation() ==
1242 ObjCPropertyDecl::Optional) ?
1243 ObjCMethodDecl::Optional :
1244 ObjCMethodDecl::Required);
1245 // Invent the arguments for the setter. We don't bother making a
1246 // nice name for the argument.
1247 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001248 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001249 property->getIdentifier(),
1250 property->getType(),
1251 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001252 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001253 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001254 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001255 } else
1256 // A user declared setter will be synthesize when @synthesize of
1257 // the property with the same name is seen in the @implementation
1258 SetterMethod->setIsSynthesized();
1259 property->setSetterMethodDecl(SetterMethod);
1260 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001261 // Add any synthesized methods to the global pool. This allows us to
1262 // handle the following, which is supported by GCC (and part of the design).
1263 //
1264 // @interface Foo
1265 // @property double bar;
1266 // @end
1267 //
1268 // void thisIsUnfortunate() {
1269 // id foo;
1270 // double bar = [foo bar];
1271 // }
1272 //
Douglas Gregord1675382009-01-09 19:42:16 +00001273 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001274 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001275 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001276 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001277}
1278
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001279// Note: For class/category implemenations, allMethods/allProperties is
1280// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001281void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1282 DeclPtrTy *allMethods, unsigned allNum,
1283 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001284 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001285 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001286
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001287 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1288 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001289 // should be true.
1290 if (!ClassDecl)
1291 return;
1292
Chris Lattner855e51f2007-12-12 07:09:47 +00001293 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001294 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1295 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001296 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001297
Steve Naroffab63fd62009-01-08 17:28:14 +00001298 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001299
1300 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1301 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1302 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1303
Chris Lattner855e51f2007-12-12 07:09:47 +00001304 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001305 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001306 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001307
1308 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001309 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001310 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001311 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001312 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1313 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001314 if ((isInterfaceDeclKind && PrevMethod && !match)
1315 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001316 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001317 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001318 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001319 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001320 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001321 InsMap[Method->getSelector()] = Method;
1322 /// The following allows us to typecheck messages to "id".
1323 AddInstanceMethodToGlobalPool(Method);
1324 }
1325 }
1326 else {
1327 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001328 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001329 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1330 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001331 if ((isInterfaceDeclKind && PrevMethod && !match)
1332 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001333 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001334 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001335 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001336 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001337 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001338 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001339 /// The following allows us to typecheck messages to "Class".
1340 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001341 }
1342 }
1343 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001344 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001345 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001346 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001347 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001348 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001349 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001350 // Categories are used to extend the class by declaring new methods.
1351 // By the same token, they are also used to add new properties. No
1352 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001353
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001354 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001355 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001356 if (C->getIdentifier() == 0)
1357 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001358 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001359 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1360 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1361 // user-defined setter/getter. It also synthesizes setter/getter methods
1362 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001363 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1364 E = CDecl->prop_end(Context);
1365 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001366 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001367 CDecl->setAtEndLoc(AtEndLoc);
1368 }
1369 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001370 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001371 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001372 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001373 } else if (ObjCCategoryImplDecl* CatImplClass =
1374 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001375 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001376
Chris Lattner855e51f2007-12-12 07:09:47 +00001377 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001378 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001379 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001380 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001381 Categories; Categories = Categories->getNextClassCategory()) {
1382 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001383 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001384 break;
1385 }
1386 }
1387 }
1388 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001389 if (isInterfaceDeclKind) {
1390 // Reject invalid vardecls.
1391 for (unsigned i = 0; i != tuvNum; i++) {
1392 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1393 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1394 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001395 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001396 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001397 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001398 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001399 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001400}
1401
1402
1403/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1404/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001405static Decl::ObjCDeclQualifier
1406CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1407 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1408 if (PQTVal & ObjCDeclSpec::DQ_In)
1409 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1410 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1411 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1412 if (PQTVal & ObjCDeclSpec::DQ_Out)
1413 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1414 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1415 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1416 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1417 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1418 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1419 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001420
1421 return ret;
1422}
1423
Chris Lattner5261d0c2009-03-28 19:18:32 +00001424Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001425 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001426 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001427 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001428 Selector Sel,
1429 // optional arguments. The number of types/arguments is obtained
1430 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001431 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001432 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001433 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1434 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001435 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001436
1437 // Make sure we can establish a context for the method.
1438 if (!ClassDecl) {
1439 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001440 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001441 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001442 QualType resultDeclType;
1443
Steve Naroffa442ad92009-02-20 22:59:16 +00001444 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001445 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001446
1447 // Methods cannot return interface types. All ObjC objects are
1448 // passed by reference.
1449 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001450 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1451 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001452 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001453 }
1454 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001455 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001456
Chris Lattner114add62008-03-16 00:49:28 +00001457 ObjCMethodDecl* ObjCMethod =
1458 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001459 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001460 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001461 false,
Chris Lattner114add62008-03-16 00:49:28 +00001462 MethodDeclKind == tok::objc_optional ?
1463 ObjCMethodDecl::Optional :
1464 ObjCMethodDecl::Required);
1465
Chris Lattnereee57c02008-04-04 06:12:32 +00001466 llvm::SmallVector<ParmVarDecl*, 16> Params;
1467
Chris Lattner4f0496b2009-04-11 19:42:43 +00001468 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001469 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001470
Chris Lattner612a2f72009-04-11 18:57:04 +00001471 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001472 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001473 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001474 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001475 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001476 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001477 }
1478
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001479 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001480 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001481 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001482 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001483 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001484 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001485 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001486 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001487 ArgInfo[i].Name, ArgType,
1488 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001489 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001490
Chris Lattner761b72f2009-04-11 19:34:56 +00001491 if (ArgType->isObjCInterfaceType()) {
1492 Diag(ArgInfo[i].NameLoc,
1493 diag::err_object_cannot_be_passed_returned_by_value)
1494 << 1 << ArgType;
1495 Param->setInvalidDecl();
1496 }
1497
Chris Lattnereee57c02008-04-04 06:12:32 +00001498 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001499 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001500
1501 // Apply the attributes to the parameter.
1502 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1503
Chris Lattnereee57c02008-04-04 06:12:32 +00001504 Params.push_back(Param);
1505 }
1506
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001507 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001508 ObjCMethod->setObjCDeclQualifier(
1509 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1510 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001511
1512 if (AttrList)
1513 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001514
1515 // For implementations (which can be very "coarse grain"), we add the
1516 // method now. This allows the AST to implement lookup methods that work
1517 // incrementally (without waiting until we parse the @end). It also allows
1518 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001519 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001520 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001521 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001522 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001523 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001524 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001525 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001526 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001527 }
1528 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001529 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001530 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001531 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001532 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001533 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001534 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001535 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001536 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001537 }
1538 }
1539 if (PrevMethod) {
1540 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001541 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001542 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001543 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001544 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001545 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001546}
1547
Daniel Dunbar540ff472008-09-23 21:53:23 +00001548void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1549 SourceLocation Loc,
1550 unsigned &Attributes) {
1551 // FIXME: Improve the reported location.
1552
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001553 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001554 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001555 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1556 ObjCDeclSpec::DQ_PR_assign |
1557 ObjCDeclSpec::DQ_PR_copy |
1558 ObjCDeclSpec::DQ_PR_retain))) {
1559 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1560 "readwrite" :
1561 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1562 "assign" :
1563 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1564 "copy" : "retain";
1565
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001566 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001567 diag::err_objc_property_attr_mutually_exclusive :
1568 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001569 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001570 }
1571
1572 // Check for copy or retain on non-object types.
1573 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1574 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001575 Diag(Loc, diag::err_objc_property_requires_object)
1576 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001577 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1578 }
1579
1580 // Check for more than one of { assign, copy, retain }.
1581 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1582 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001583 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1584 << "assign" << "copy";
1585 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001586 }
1587 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001588 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1589 << "assign" << "retain";
1590 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001591 }
1592 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1593 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001594 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1595 << "copy" << "retain";
1596 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001597 }
1598 }
1599
1600 // Warn if user supplied no assignment attribute, property is
1601 // readwrite, and this is an object type.
1602 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1603 ObjCDeclSpec::DQ_PR_retain)) &&
1604 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1605 Context.isObjCObjectPointerType(PropertyTy)) {
1606 // Skip this warning in gc-only mode.
1607 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1608 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1609
1610 // If non-gc code warn that this is likely inappropriate.
1611 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1612 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1613
1614 // FIXME: Implement warning dependent on NSCopying being
1615 // implemented. See also:
1616 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1617 // (please trim this list while you are at it).
1618 }
1619}
1620
Chris Lattner5261d0c2009-03-28 19:18:32 +00001621Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1622 FieldDeclarator &FD,
1623 ObjCDeclSpec &ODS,
1624 Selector GetterSel,
1625 Selector SetterSel,
1626 DeclPtrTy ClassCategory,
1627 bool *isOverridingProperty,
1628 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001629 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001630 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1631 // default is readwrite!
1632 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1633 // property is defaulted to 'assign' if it is readwrite and is
1634 // not retain or copy
1635 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1636 (isReadWrite &&
1637 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1638 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1639 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001640 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001641 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001642 // May modify Attributes.
1643 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001644 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1645 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001646 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001647 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001648 if ((CCPrimary = CDecl->getClassInterface())) {
1649 // Find the property in continuation class's primary class only.
1650 ObjCPropertyDecl *PIDecl = 0;
1651 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001652 for (ObjCInterfaceDecl::prop_iterator
1653 I = CCPrimary->prop_begin(Context),
1654 E = CCPrimary->prop_end(Context);
1655 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001656 if ((*I)->getIdentifier() == PropertyId) {
1657 PIDecl = *I;
1658 break;
1659 }
1660
1661 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001662 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001663 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001664 unsigned PIkind = PIDecl->getPropertyAttributes();
1665 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001666 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001667 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1668 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001669 PIDecl->makeitReadWriteAttribute();
1670 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1671 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1672 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1673 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1674 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001675 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001676 else
1677 Diag(AtLoc, diag::err_use_continuation_class)
1678 << CCPrimary->getDeclName();
1679 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001680 // Make sure setter decl is synthesized, and added to primary
1681 // class's list.
1682 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001683 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001684 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001685 // No matching property found in the primary class. Just fall thru
1686 // and add property to continuation class's primary class.
1687 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001688 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001689 Diag(CDecl->getLocation(), diag::err_continuation_class);
1690 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001691 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001692 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001693 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001694
Steve Naroffdcf1e842009-01-11 12:47:58 +00001695 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1696 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001697 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1698 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001699 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001700 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001701
Chris Lattner6e48be22009-04-11 20:14:49 +00001702 if (T->isArrayType() || T->isFunctionType()) {
1703 Diag(AtLoc, diag::err_property_type) << T;
1704 PDecl->setInvalidDecl();
1705 }
1706
Chris Lattner22063402009-02-16 18:32:47 +00001707 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001708
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001709 // Regardless of setter/getter attribute, we save the default getter/setter
1710 // selector names in anticipation of declaration of setter/getter methods.
1711 PDecl->setGetterName(GetterSel);
1712 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001713
Daniel Dunbar540ff472008-09-23 21:53:23 +00001714 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001715 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001716
Daniel Dunbar540ff472008-09-23 21:53:23 +00001717 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001718 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001719
Daniel Dunbar540ff472008-09-23 21:53:23 +00001720 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001721 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001722
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001723 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001724 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001725
Daniel Dunbar540ff472008-09-23 21:53:23 +00001726 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001727 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001728
Daniel Dunbar540ff472008-09-23 21:53:23 +00001729 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001730 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001731
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001732 if (isAssign)
1733 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1734
Daniel Dunbar540ff472008-09-23 21:53:23 +00001735 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001736 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001737
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001738 if (MethodImplKind == tok::objc_required)
1739 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1740 else if (MethodImplKind == tok::objc_optional)
1741 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001742 // A case of continuation class adding a new property in the class. This
1743 // is not what it was meant for. However, gcc supports it and so should we.
1744 // Make sure setter/getters are declared here.
1745 if (CCPrimary)
1746 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001747
Chris Lattner5261d0c2009-03-28 19:18:32 +00001748 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001749}
1750
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001751/// ActOnPropertyImplDecl - This routine performs semantic checks and
1752/// builds the AST node for a property implementation declaration; declared
1753/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001754///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001755Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1756 SourceLocation PropertyLoc,
1757 bool Synthesize,
1758 DeclPtrTy ClassCatImpDecl,
1759 IdentifierInfo *PropertyId,
1760 IdentifierInfo *PropertyIvar) {
1761 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001762 // Make sure we have a context for the property implementation declaration.
1763 if (!ClassImpDecl) {
1764 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001765 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001766 }
1767 ObjCPropertyDecl *property = 0;
1768 ObjCInterfaceDecl* IDecl = 0;
1769 // Find the class or category class where this property must have
1770 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001771 ObjCImplementationDecl *IC = 0;
1772 ObjCCategoryImplDecl* CatImplClass = 0;
1773 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001774 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001775 // We always synthesize an interface for an implementation
1776 // without an interface decl. So, IDecl is always non-zero.
1777 assert(IDecl &&
1778 "ActOnPropertyImplDecl - @implementation without @interface");
1779
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001780 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001781 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001782 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001783 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001784 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001785 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001786 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001787 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001788 if (Synthesize) {
1789 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001790 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001791 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001792 IDecl = CatImplClass->getClassInterface();
1793 if (!IDecl) {
1794 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001795 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001796 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001797 ObjCCategoryDecl *Category =
1798 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1799
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001800 // If category for this implementation not found, it is an error which
1801 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001802 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001803 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001804 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001805 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001806 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001807 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001808 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001809 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001810 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001811 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001812 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001813 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001814 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001815 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001816 // Check that we have a valid, previously declared ivar for @synthesize
1817 if (Synthesize) {
1818 // @synthesize
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001819 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001820 if (!PropertyIvar)
1821 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001822 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001823 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001824 ObjCInterfaceDecl *ClassDeclared;
1825 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001826 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001827 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1828 PropertyIvar, PropType,
1829 ObjCIvarDecl::Public,
1830 (Expr *)0);
1831 property->setPropertyIvarDecl(Ivar);
1832 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001833 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001834 // Note! I deliberately want it to fall thru so, we have a
1835 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001836 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001837 else if (getLangOptions().ObjCNonFragileABI &&
1838 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1839 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1840 << property->getDeclName() << Ivar->getDeclName()
1841 << ClassDeclared->getDeclName();
1842 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1843 << Ivar << Ivar->getNameAsCString();
1844 // Note! I deliberately want it to fall thru so more errors are caught.
1845 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001846 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1847
Steve Naroff631e3922008-09-30 00:24:17 +00001848 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001849 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001850 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001851 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001852 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001853 // Note! I deliberately want it to fall thru so, we have a
1854 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001855 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001856
1857 // FIXME! Rules for properties are somewhat different that those
1858 // for assignments. Use a new routine to consolidate all cases;
1859 // specifically for property redeclarations as well as for ivars.
1860 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1861 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1862 if (lhsType != rhsType &&
1863 lhsType->isArithmeticType()) {
1864 Diag(PropertyLoc, diag::error_property_ivar_type)
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 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00001869 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1870 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001871 Diag(PropertyLoc, diag::error_weak_property)
1872 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001873 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001874 }
1875 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00001876 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1877 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001878 Diag(PropertyLoc, diag::error_strong_property)
1879 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001880 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001881 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001882 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001883 } else if (PropertyIvar)
1884 // @dynamic
1885 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001886 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001887 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001888 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1889 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001890 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001891 ObjCPropertyImplDecl::Synthesize
1892 : ObjCPropertyImplDecl::Dynamic),
1893 Ivar);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001894 CurContext->addDecl(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001895 if (IC) {
1896 if (Synthesize)
1897 if (ObjCPropertyImplDecl *PPIDecl =
1898 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1899 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1900 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1901 << PropertyIvar;
1902 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1903 }
1904
1905 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1906 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1907 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001908 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001909 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001910 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001911 }
1912 else {
1913 if (Synthesize)
1914 if (ObjCPropertyImplDecl *PPIDecl =
1915 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1916 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1917 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1918 << PropertyIvar;
1919 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1920 }
1921
1922 if (ObjCPropertyImplDecl *PPIDecl =
1923 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1924 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1925 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001926 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001927 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001928 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001929 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001930
Chris Lattner5261d0c2009-03-28 19:18:32 +00001931 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001932}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001933
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001934bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001935 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001936 return false;
1937
1938 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1939 D->setInvalidDecl();
1940
1941 return true;
1942}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001943
1944/// Collect the instance variables declared in an Objective-C object. Used in
1945/// the creation of structures from objects using the @defs directive.
1946/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1947/// part of the AST generation logic of @defs.
1948static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1949 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001950 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001951 if (Class->getSuperClass())
1952 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1953
1954 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001955 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1956 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001957 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001958 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1959 ID->getIdentifier(), ID->getType(),
1960 ID->getBitWidth());
1961 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001962 }
1963}
1964
1965/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1966/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001967void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001968 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001969 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001970 // Check that ClassName is a valid class
1971 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1972 if (!Class) {
1973 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1974 return;
1975 }
1976 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00001977 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001978
1979 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001980 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001981 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001982 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001983 if (getLangOptions().CPlusPlus)
1984 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001985 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001986 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001987 }
1988}
1989