blob: 17de0a506786d51888bd20fadf0e29b20175bb2b [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 Gregor03b2ad22009-01-12 23:27:07 +0000101 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000209 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000268 CurContext->addDecl(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)
Steve Naroff451f83c2009-01-09 15:36:25 +0000370 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
371 E = SDecl->prop_end(); 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?
Steve Naroff451f83c2009-01-09 15:36:25 +0000374 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
375 E = IDecl->prop_end(); 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");
Steve Naroff451f83c2009-01-09 15:36:25 +0000395 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
396 E = PDecl->prop_end(); 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?
Steve Naroff451f83c2009-01-09 15:36:25 +0000400 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
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 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000410 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
411 E = PDecl->prop_end(); 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?
Steve Naroff451f83c2009-01-09 15:36:25 +0000415 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
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;
486 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
487 e = ID->meth_end(); i != e; ++i) {
488 ObjCMethodDecl *MD = *i;
489 MethodMap[MD->getSelector()] = MD;
490 }
491
492 if (MethodMap.empty())
493 return;
494 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
495 e = CAT->meth_end(); i != e; ++i) {
496 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 Lattner855e51f2007-12-12 07:09:47 +0000506/// ActOnForwardProtocolDeclaration -
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 Gregor03b2ad22009-01-12 23:27:07 +0000521 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000531 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000547 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000601 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000666 CurContext->addDecl(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 Gregor03b2ad22009-01-12 23:27:07 +0000676 CurContext->addDecl(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) {
766 bool err = false;
767 QualType ImpMethodQType =
768 Context.getCanonicalType(ImpMethodDecl->getResultType());
769 QualType IntfMethodQType =
770 Context.getCanonicalType(IntfMethodDecl->getResultType());
771 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
772 err = true;
773 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
774 IF=IntfMethodDecl->param_begin(),
775 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
776 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
777 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
778 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
779 err = true;
780 break;
781 }
782 }
783 if (err) {
784 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
785 << ImpMethodDecl->getDeclName();
786 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
787 }
788}
789
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000790/// isPropertyReadonly - Return true if property is readonly, by searching
791/// for the property in the class and in its categories and implementations
792///
793bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000794 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000795 // by far the most common case.
796 if (!PDecl->isReadOnly())
797 return false;
798 // Even if property is ready only, if interface has a user defined setter,
799 // it is not considered read only.
800 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
801 return false;
802
803 // Main class has the property as 'readonly'. Must search
804 // through the category list to see if the property's
805 // attribute has been over-ridden to 'readwrite'.
806 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
807 Category; Category = Category->getNextClassCategory()) {
808 // Even if property is ready only, if a category has a user defined setter,
809 // it is not considered read only.
810 if (Category->getInstanceMethod(PDecl->getSetterName()))
811 return false;
812 ObjCPropertyDecl *P =
813 Category->FindPropertyDeclaration(PDecl->getIdentifier());
814 if (P && !P->isReadOnly())
815 return false;
816 }
817
818 // Also, check for definition of a setter method in the implementation if
819 // all else failed.
820 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
821 if (ObjCImplementationDecl *IMD =
822 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
823 if (IMD->getInstanceMethod(PDecl->getSetterName()))
824 return false;
825 }
826 else if (ObjCCategoryImplDecl *CIMD =
827 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
828 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
829 return false;
830 }
831 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000832 // Lastly, look through the implementation (if one is in scope).
833 if (ObjCImplementationDecl *ImpDecl =
834 ObjCImplementations[IDecl->getIdentifier()])
835 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
836 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000837 // If all fails, look at the super class.
838 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
839 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000840 return true;
841}
842
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000843/// FIXME: Type hierarchies in Objective-C can be deep. We could most
844/// likely improve the efficiency of selector lookups and type
845/// checking by associating with each protocol / interface / category
846/// the flattened instance tables. If we used an immutable set to keep
847/// the table then it wouldn't add significant memory cost and it
848/// would be handy for lookups.
849
Steve Naroffb268d2a2008-02-08 22:06:17 +0000850/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000851/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000852void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
853 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000854 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000855 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000856 const llvm::DenseSet<Selector> &ClsMap,
857 ObjCInterfaceDecl *IDecl) {
858 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
859
860 // If a method lookup fails locally we still need to look and see if
861 // the method was implemented by a base class or an inherited
862 // protocol. This lookup is slow, but occurs rarely in correct code
863 // and otherwise would terminate in a warning.
864
Chris Lattner855e51f2007-12-12 07:09:47 +0000865 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000867 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000868 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000869 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000870 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000871 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
872 // Ugly, but necessary. Method declared in protcol might have
873 // have been synthesized due to a property declared in the class which
874 // uses the protocol.
875 ObjCMethodDecl *MethodInClass =
876 IDecl->lookupInstanceMethod(method->getSelector());
877 if (!MethodInClass || !MethodInClass->isSynthesized())
878 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
879 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000880 }
881 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000882 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000883 E = PDecl->classmeth_end(); 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()) &&
887 (!Super || !Super->lookupClassMethod(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(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000903 E = IMPDecl->instmeth_end(); I != E; ++I)
904 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000905
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000906 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
907 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000908 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000909 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000910 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000911 }
Chris Lattner28400082009-02-16 19:25:52 +0000912
913 ObjCMethodDecl *ImpMethodDecl =
914 IMPDecl->getInstanceMethod((*I)->getSelector());
915 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000916 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000917 assert(IntfMethodDecl &&
918 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
919 // ImpMethodDecl may be null as in a @dynamic property.
920 if (ImpMethodDecl)
921 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
922 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000923
Chris Lattner855e51f2007-12-12 07:09:47 +0000924 llvm::DenseSet<Selector> ClsMap;
925 // Check and see if class methods in class interface have been
926 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000927 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000928 E = IMPDecl->classmeth_end(); I != E; ++I)
929 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000930
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000931 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
932 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000933 if (!ClsMap.count((*I)->getSelector()))
934 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000935 else {
936 ObjCMethodDecl *ImpMethodDecl =
937 IMPDecl->getClassMethod((*I)->getSelector());
938 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000939 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000940 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
941 }
942
Chris Lattner855e51f2007-12-12 07:09:47 +0000943
944 // Check the protocol list for unimplemented methods in the @implementation
945 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000946 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
947 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
948 E = I->protocol_end(); PI != E; ++PI)
949 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
950 InsMap, ClsMap, I);
951 // Check class extensions (unnamed categories)
952 for (ObjCCategoryDecl *Categories = I->getCategoryList();
953 Categories; Categories = Categories->getNextClassCategory()) {
954 if (!Categories->getIdentifier()) {
955 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
956 break;
957 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000958 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000959 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
960 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
961 E = C->protocol_end(); PI != E; ++PI)
962 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
963 InsMap, ClsMap, C->getClassInterface());
964 } else
965 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +0000966}
967
968/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +0000969Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000970Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000971 IdentifierInfo **IdentList,
972 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000973 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000974
975 for (unsigned i = 0; i != NumElts; ++i) {
976 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000977 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000978 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000979 // Maybe we will complain about the shadowed template parameter.
980 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
981 // Just pretend that we didn't see the previous declaration.
982 PrevDecl = 0;
983 }
984
Ted Kremenek42730c52008-01-07 19:49:32 +0000985 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000986 // GCC apparently allows the following idiom:
987 //
988 // typedef NSObject < XCElementTogglerP > XCElementToggler;
989 // @class XCElementToggler;
990 //
991 // FIXME: Make an extension?
992 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
993 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000994 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000995 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000996 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000997 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000998 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000999 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001000 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1001 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +00001002 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +00001003
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001004 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001005 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001006 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001007 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +00001008 }
1009
1010 Interfaces.push_back(IDecl);
1011 }
1012
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001013 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001014 &Interfaces[0],
1015 Interfaces.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001016 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001017 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001018 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001019}
1020
1021
1022/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1023/// returns true, or false, accordingly.
1024/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001025bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001026 const ObjCMethodDecl *PrevMethod,
1027 bool matchBasedOnSizeAndAlignment) {
1028 QualType T1 = Context.getCanonicalType(Method->getResultType());
1029 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1030
1031 if (T1 != T2) {
1032 // The result types are different.
1033 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001034 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001035 // Incomplete types don't have a size and alignment.
1036 if (T1->isIncompleteType() || T2->isIncompleteType())
1037 return false;
1038 // Check is based on size and alignment.
1039 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1040 return false;
1041 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001042
1043 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1044 E = Method->param_end();
1045 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1046
1047 for (; ParamI != E; ++ParamI, ++PrevI) {
1048 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1049 T1 = Context.getCanonicalType((*ParamI)->getType());
1050 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001051 if (T1 != T2) {
1052 // The result types are different.
1053 if (!matchBasedOnSizeAndAlignment)
1054 return false;
1055 // Incomplete types don't have a size and alignment.
1056 if (T1->isIncompleteType() || T2->isIncompleteType())
1057 return false;
1058 // Check is based on size and alignment.
1059 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1060 return false;
1061 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001062 }
1063 return true;
1064}
1065
Ted Kremenek42730c52008-01-07 19:49:32 +00001066void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001067 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1068 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001069 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001070 Entry.Method = Method;
1071 Entry.Next = 0;
1072 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001073 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001074
1075 // We've seen a method with this name, see if we have already seen this type
1076 // signature.
1077 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1078 if (MatchTwoMethodDeclarations(Method, List->Method))
1079 return;
1080
1081 // We have a new signature for an existing method - add it.
1082 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1083 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001084}
1085
Steve Naroffec7e0882008-10-21 10:50:19 +00001086// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001087ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1088 SourceRange R) {
1089 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001090 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001091
1092 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001093 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1094 // This checks if the methods differ by size & alignment.
1095 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1096 issueWarning = true;
1097 }
1098 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001099 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001100 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001101 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001102 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001103 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001104 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001105 }
1106 return MethList.Method;
1107}
1108
Ted Kremenek42730c52008-01-07 19:49:32 +00001109void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1110 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001111 if (!FirstMethod.Method) {
1112 // Haven't seen a method with this selector name yet - add it.
1113 FirstMethod.Method = Method;
1114 FirstMethod.Next = 0;
1115 } else {
1116 // We've seen a method with this name, now check the type signature(s).
1117 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1118
Ted Kremenek42730c52008-01-07 19:49:32 +00001119 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001120 Next = Next->Next)
1121 match = MatchTwoMethodDeclarations(Method, Next->Method);
1122
1123 if (!match) {
1124 // We have a new signature for an existing method - add it.
1125 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001126 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001127 FirstMethod.Next = OMI;
1128 }
1129 }
1130}
1131
Steve Naroffab63fd62009-01-08 17:28:14 +00001132/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1133/// have the property type and issue diagnostics if they don't.
1134/// Also synthesize a getter/setter method if none exist (and update the
1135/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1136/// methods is the "right" thing to do.
1137void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1138 ObjCContainerDecl *CD) {
1139 ObjCMethodDecl *GetterMethod, *SetterMethod;
1140
1141 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1142 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1143
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001144 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001145 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001146 Diag(property->getLocation(),
1147 diag::err_accessor_property_type_mismatch)
1148 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001149 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001150 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1151 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001152
1153 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001154 if (Context.getCanonicalType(SetterMethod->getResultType())
1155 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001156 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001157 if (SetterMethod->param_size() != 1 ||
1158 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001159 Diag(property->getLocation(),
1160 diag::err_accessor_property_type_mismatch)
1161 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001162 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001163 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1164 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001165 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001166
1167 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001168 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001169 // FIXME: The synthesized property we set here is misleading. We
1170 // almost always synthesize these methods unless the user explicitly
1171 // provided prototypes (which is odd, but allowed). Sema should be
1172 // typechecking that the declarations jive in that situation (which
1173 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001174 if (!GetterMethod) {
1175 // No instance method of same name as property getter name was found.
1176 // Declare a getter method and add it to the list of methods
1177 // for this class.
1178 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1179 property->getLocation(), property->getGetterName(),
1180 property->getType(), CD, true, false, true,
1181 (property->getPropertyImplementation() ==
1182 ObjCPropertyDecl::Optional) ?
1183 ObjCMethodDecl::Optional :
1184 ObjCMethodDecl::Required);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001185 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001186 } else
1187 // A user declared getter will be synthesize when @synthesize of
1188 // the property with the same name is seen in the @implementation
1189 GetterMethod->setIsSynthesized();
1190 property->setGetterMethodDecl(GetterMethod);
1191
1192 // Skip setter if property is read-only.
1193 if (!property->isReadOnly()) {
1194 // Find the default setter and if one not found, add one.
1195 if (!SetterMethod) {
1196 // No instance method of same name as property setter name was found.
1197 // Declare a setter method and add it to the list of methods
1198 // for this class.
1199 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1200 property->getLocation(),
1201 property->getSetterName(),
1202 Context.VoidTy, CD, true, false, true,
1203 (property->getPropertyImplementation() ==
1204 ObjCPropertyDecl::Optional) ?
1205 ObjCMethodDecl::Optional :
1206 ObjCMethodDecl::Required);
1207 // Invent the arguments for the setter. We don't bother making a
1208 // nice name for the argument.
1209 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1210 SourceLocation(),
1211 property->getIdentifier(),
1212 property->getType(),
1213 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001214 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001215 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001216 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001217 } else
1218 // A user declared setter will be synthesize when @synthesize of
1219 // the property with the same name is seen in the @implementation
1220 SetterMethod->setIsSynthesized();
1221 property->setSetterMethodDecl(SetterMethod);
1222 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001223 // Add any synthesized methods to the global pool. This allows us to
1224 // handle the following, which is supported by GCC (and part of the design).
1225 //
1226 // @interface Foo
1227 // @property double bar;
1228 // @end
1229 //
1230 // void thisIsUnfortunate() {
1231 // id foo;
1232 // double bar = [foo bar];
1233 // }
1234 //
Douglas Gregord1675382009-01-09 19:42:16 +00001235 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001236 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001237 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001238 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001239}
1240
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001241// Note: For class/category implemenations, allMethods/allProperties is
1242// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001243void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1244 DeclPtrTy *allMethods, unsigned allNum,
1245 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001246 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001247 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001248
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001249 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1250 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001251 // should be true.
1252 if (!ClassDecl)
1253 return;
1254
Chris Lattner855e51f2007-12-12 07:09:47 +00001255 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001256 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1257 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001258 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001259
Steve Naroffab63fd62009-01-08 17:28:14 +00001260 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001261
1262 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1263 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1264 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1265
Chris Lattner855e51f2007-12-12 07:09:47 +00001266 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001268 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001269
1270 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001271 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001272 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001273 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001274 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1275 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001276 if ((isInterfaceDeclKind && PrevMethod && !match)
1277 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001278 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001279 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001280 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001281 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001282 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001283 InsMap[Method->getSelector()] = Method;
1284 /// The following allows us to typecheck messages to "id".
1285 AddInstanceMethodToGlobalPool(Method);
1286 }
1287 }
1288 else {
1289 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001290 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001291 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1292 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001293 if ((isInterfaceDeclKind && PrevMethod && !match)
1294 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001295 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001296 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001297 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001298 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001299 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001300 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001301 /// The following allows us to typecheck messages to "Class".
1302 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001303 }
1304 }
1305 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001307 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001308 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001309 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001310 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001311 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001312 // Categories are used to extend the class by declaring new methods.
1313 // By the same token, they are also used to add new properties. No
1314 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001315
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001316 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001317 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001318 if (C->getIdentifier() == 0)
1319 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001320 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001321 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1322 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1323 // user-defined setter/getter. It also synthesizes setter/getter methods
1324 // and adds them to the DeclContext and global method pools.
Chris Lattner22063402009-02-16 18:32:47 +00001325 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1326 E = CDecl->prop_end(); I != E; ++I)
1327 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001328 CDecl->setAtEndLoc(AtEndLoc);
1329 }
1330 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001331 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001332 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001333 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001334 } else if (ObjCCategoryImplDecl* CatImplClass =
1335 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001336 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001337
Chris Lattner855e51f2007-12-12 07:09:47 +00001338 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001339 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001340 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001341 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001342 Categories; Categories = Categories->getNextClassCategory()) {
1343 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001344 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001345 break;
1346 }
1347 }
1348 }
1349 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001350 if (isInterfaceDeclKind) {
1351 // Reject invalid vardecls.
1352 for (unsigned i = 0; i != tuvNum; i++) {
1353 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1354 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1355 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
1356 if (VDecl->getStorageClass() != VarDecl::Extern &&
1357 VDecl->getStorageClass() != VarDecl::PrivateExtern)
1358 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass)
1359 << cast<NamedDecl>(ClassDecl)->getIdentifier();
Fariborz Jahanianca277322009-03-21 18:06:45 +00001360 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001361 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001362 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001363}
1364
1365
1366/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1367/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001368static Decl::ObjCDeclQualifier
1369CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1370 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1371 if (PQTVal & ObjCDeclSpec::DQ_In)
1372 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1373 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1374 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1375 if (PQTVal & ObjCDeclSpec::DQ_Out)
1376 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1377 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1378 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1379 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1380 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1381 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1382 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001383
1384 return ret;
1385}
1386
Chris Lattner5261d0c2009-03-28 19:18:32 +00001387Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001388 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001389 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001390 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001391 Selector Sel,
1392 // optional arguments. The number of types/arguments is obtained
1393 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001395 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001396 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1397 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001398 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001399
1400 // Make sure we can establish a context for the method.
1401 if (!ClassDecl) {
1402 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001403 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001404 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001405 QualType resultDeclType;
1406
Steve Naroffa442ad92009-02-20 22:59:16 +00001407 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001408 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001409
1410 // Methods cannot return interface types. All ObjC objects are
1411 // passed by reference.
1412 if (resultDeclType->isObjCInterfaceType()) {
1413 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1414 << "returned";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001415 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001416 }
1417 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001418 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001419
Chris Lattner114add62008-03-16 00:49:28 +00001420 ObjCMethodDecl* ObjCMethod =
1421 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001422 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001423 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001424 false,
Chris Lattner114add62008-03-16 00:49:28 +00001425 MethodDeclKind == tok::objc_optional ?
1426 ObjCMethodDecl::Optional :
1427 ObjCMethodDecl::Required);
1428
Chris Lattnereee57c02008-04-04 06:12:32 +00001429 llvm::SmallVector<ParmVarDecl*, 16> Params;
1430
1431 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1432 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001433 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001434
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001435 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001436 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001437 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001438 if (argType->isArrayType()) { // (char *[]) -> (char **)
1439 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001440 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001441 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001442 else if (argType->isFunctionType())
1443 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001444 else if (argType->isObjCInterfaceType()) {
1445 // FIXME! provide more precise location for the parameter
Steve Naroffa442ad92009-02-20 22:59:16 +00001446 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1447 << "passed";
1448 ObjCMethod->setInvalidDecl();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001449 return DeclPtrTy();
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001450 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001451 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001452 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001453 ParmVarDecl* Param;
1454 if (originalArgType.isNull())
1455 Param = ParmVarDecl::Create(Context, ObjCMethod,
1456 SourceLocation(/*FIXME*/),
1457 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001458 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001459 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001460 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1461 SourceLocation(/*FIXME*/),
1462 ArgNames[i], argType, originalArgType,
1463 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001464
Chris Lattnereee57c02008-04-04 06:12:32 +00001465 Param->setObjCDeclQualifier(
1466 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1467 Params.push_back(Param);
1468 }
1469
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001470 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001471 ObjCMethod->setObjCDeclQualifier(
1472 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1473 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001474
1475 if (AttrList)
1476 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001477
1478 // For implementations (which can be very "coarse grain"), we add the
1479 // method now. This allows the AST to implement lookup methods that work
1480 // incrementally (without waiting until we parse the @end). It also allows
1481 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001482 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001483 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001484 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001485 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001486 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001487 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001488 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001489 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001490 }
1491 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001492 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001493 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001494 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001495 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001496 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001497 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001498 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001499 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001500 }
1501 }
1502 if (PrevMethod) {
1503 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001504 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001505 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001506 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001507 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001508 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001509}
1510
Daniel Dunbar540ff472008-09-23 21:53:23 +00001511void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1512 SourceLocation Loc,
1513 unsigned &Attributes) {
1514 // FIXME: Improve the reported location.
1515
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001516 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001517 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001518 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1519 ObjCDeclSpec::DQ_PR_assign |
1520 ObjCDeclSpec::DQ_PR_copy |
1521 ObjCDeclSpec::DQ_PR_retain))) {
1522 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1523 "readwrite" :
1524 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1525 "assign" :
1526 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1527 "copy" : "retain";
1528
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001529 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001530 diag::err_objc_property_attr_mutually_exclusive :
1531 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001532 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001533 }
1534
1535 // Check for copy or retain on non-object types.
1536 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1537 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001538 Diag(Loc, diag::err_objc_property_requires_object)
1539 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001540 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1541 }
1542
1543 // Check for more than one of { assign, copy, retain }.
1544 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1545 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001546 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1547 << "assign" << "copy";
1548 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001549 }
1550 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001551 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1552 << "assign" << "retain";
1553 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001554 }
1555 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1556 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001557 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1558 << "copy" << "retain";
1559 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001560 }
1561 }
1562
1563 // Warn if user supplied no assignment attribute, property is
1564 // readwrite, and this is an object type.
1565 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1566 ObjCDeclSpec::DQ_PR_retain)) &&
1567 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1568 Context.isObjCObjectPointerType(PropertyTy)) {
1569 // Skip this warning in gc-only mode.
1570 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1571 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1572
1573 // If non-gc code warn that this is likely inappropriate.
1574 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1575 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1576
1577 // FIXME: Implement warning dependent on NSCopying being
1578 // implemented. See also:
1579 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1580 // (please trim this list while you are at it).
1581 }
1582}
1583
Chris Lattner5261d0c2009-03-28 19:18:32 +00001584Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1585 FieldDeclarator &FD,
1586 ObjCDeclSpec &ODS,
1587 Selector GetterSel,
1588 Selector SetterSel,
1589 DeclPtrTy ClassCategory,
1590 bool *isOverridingProperty,
1591 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001592 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001593 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1594 // default is readwrite!
1595 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1596 // property is defaulted to 'assign' if it is readwrite and is
1597 // not retain or copy
1598 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1599 (isReadWrite &&
1600 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1601 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1602 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001603 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001604 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001605 // May modify Attributes.
1606 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001607 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1608 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001609 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001610 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001611 if ((CCPrimary = CDecl->getClassInterface())) {
1612 // Find the property in continuation class's primary class only.
1613 ObjCPropertyDecl *PIDecl = 0;
1614 IdentifierInfo *PropertyId = FD.D.getIdentifier();
1615 for (ObjCInterfaceDecl::prop_iterator I = CCPrimary->prop_begin(),
1616 E = CCPrimary->prop_end(); I != E; ++I)
1617 if ((*I)->getIdentifier() == PropertyId) {
1618 PIDecl = *I;
1619 break;
1620 }
1621
1622 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001623 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001624 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001625 unsigned PIkind = PIDecl->getPropertyAttributes();
1626 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001627 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001628 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1629 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001630 PIDecl->makeitReadWriteAttribute();
1631 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1632 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1633 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1634 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1635 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001636 // FIXME: use a common routine with addPropertyMethods.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001637 ObjCMethodDecl *SetterDecl =
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001638 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1639 Context.VoidTy,
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001640 CCPrimary,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001641 true, false, true,
1642 ObjCMethodDecl::Required);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001643 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001644 SourceLocation(),
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001645 PropertyId,
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001646 T, VarDecl::None, 0);
1647 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001648 PIDecl->setSetterMethodDecl(SetterDecl);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001649 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001650 else
1651 Diag(AtLoc, diag::err_use_continuation_class)
1652 << CCPrimary->getDeclName();
1653 *isOverridingProperty = true;
1654 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001655 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001656 // No matching property found in the primary class. Just fall thru
1657 // and add property to continuation class's primary class.
1658 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001659 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001660 Diag(CDecl->getLocation(), diag::err_continuation_class);
1661 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001662 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001663 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001664 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001665
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001666 Type *t = T.getTypePtr();
1667 if (t->isArrayType() || t->isFunctionType())
1668 Diag(AtLoc, diag::err_property_type) << T;
1669
Steve Naroffdcf1e842009-01-11 12:47:58 +00001670 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1671 assert(DC && "ClassDecl is not a DeclContext");
1672 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001673 FD.D.getIdentifier(), T);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001674 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001675
1676 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001677
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001678 // Regardless of setter/getter attribute, we save the default getter/setter
1679 // selector names in anticipation of declaration of setter/getter methods.
1680 PDecl->setGetterName(GetterSel);
1681 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001682
Daniel Dunbar540ff472008-09-23 21:53:23 +00001683 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001684 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001685
Daniel Dunbar540ff472008-09-23 21:53:23 +00001686 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001687 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001688
Daniel Dunbar540ff472008-09-23 21:53:23 +00001689 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001690 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001691
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001692 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001693 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001694
Daniel Dunbar540ff472008-09-23 21:53:23 +00001695 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001697
Daniel Dunbar540ff472008-09-23 21:53:23 +00001698 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001699 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001700
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001701 if (isAssign)
1702 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1703
Daniel Dunbar540ff472008-09-23 21:53:23 +00001704 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001705 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001706
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001707 if (MethodImplKind == tok::objc_required)
1708 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1709 else if (MethodImplKind == tok::objc_optional)
1710 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001711 // A case of continuation class adding a new property in the class. This
1712 // is not what it was meant for. However, gcc supports it and so should we.
1713 // Make sure setter/getters are declared here.
1714 if (CCPrimary)
1715 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001716
Chris Lattner5261d0c2009-03-28 19:18:32 +00001717 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001718}
1719
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001720/// ActOnPropertyImplDecl - This routine performs semantic checks and
1721/// builds the AST node for a property implementation declaration; declared
1722/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001723///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001724Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1725 SourceLocation PropertyLoc,
1726 bool Synthesize,
1727 DeclPtrTy ClassCatImpDecl,
1728 IdentifierInfo *PropertyId,
1729 IdentifierInfo *PropertyIvar) {
1730 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001731 // Make sure we have a context for the property implementation declaration.
1732 if (!ClassImpDecl) {
1733 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001734 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001735 }
1736 ObjCPropertyDecl *property = 0;
1737 ObjCInterfaceDecl* IDecl = 0;
1738 // Find the class or category class where this property must have
1739 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001740 ObjCImplementationDecl *IC = 0;
1741 ObjCCategoryImplDecl* CatImplClass = 0;
1742 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001743 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001744 // We always synthesize an interface for an implementation
1745 // without an interface decl. So, IDecl is always non-zero.
1746 assert(IDecl &&
1747 "ActOnPropertyImplDecl - @implementation without @interface");
1748
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001749 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001750 property = IDecl->FindPropertyDeclaration(PropertyId);
1751 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001752 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001753 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001754 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001755 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001756 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001757 if (Synthesize) {
1758 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001759 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001760 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001761 IDecl = CatImplClass->getClassInterface();
1762 if (!IDecl) {
1763 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001764 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001765 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001766 ObjCCategoryDecl *Category =
1767 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1768
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001769 // If category for this implementation not found, it is an error which
1770 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001771 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001772 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001773 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001774 property = Category->FindPropertyDeclaration(PropertyId);
1775 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001776 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001777 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001778 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001779 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001780 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001781 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001782 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001783 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001784 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001785 // Check that we have a valid, previously declared ivar for @synthesize
1786 if (Synthesize) {
1787 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001788 if (!PropertyIvar)
1789 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001790 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001791 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianbeae78e2009-02-16 19:35:27 +00001792 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001793 if (!Ivar) {
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001794 if (getLangOptions().ObjCNonFragileABI) {
1795 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1796 PropertyIvar, PropType,
Fariborz Jahanian745fd892009-04-06 18:30:00 +00001797 ObjCIvarDecl::Public,
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001798 (Expr *)0);
1799 property->setPropertyIvarDecl(Ivar);
1800 }
1801 else {
Steve Naroff92aeef32009-03-03 22:09:41 +00001802 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001803 return DeclPtrTy();
1804 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001805 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001806 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1807
Steve Naroff631e3922008-09-30 00:24:17 +00001808 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001809 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001810 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001811 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001812 << property->getDeclName() << Ivar->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001813 return DeclPtrTy();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001814 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001815
1816 // FIXME! Rules for properties are somewhat different that those
1817 // for assignments. Use a new routine to consolidate all cases;
1818 // specifically for property redeclarations as well as for ivars.
1819 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1820 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1821 if (lhsType != rhsType &&
1822 lhsType->isArithmeticType()) {
1823 Diag(PropertyLoc, diag::error_property_ivar_type)
1824 << property->getDeclName() << Ivar->getDeclName();
1825 return DeclPtrTy();
1826 }
1827 // __weak is explicit. So it works on Canonical type.
1828 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1829 Diag(PropertyLoc, diag::error_weak_property)
1830 << property->getDeclName() << Ivar->getDeclName();
1831 return DeclPtrTy();
1832 }
1833 if ((Context.isObjCObjectPointerType(property->getType()) ||
1834 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1835 Diag(PropertyLoc, diag::error_strong_property)
1836 << property->getDeclName() << Ivar->getDeclName();
1837 return DeclPtrTy();
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001838 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001839 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001840 } else if (PropertyIvar) {
1841 // @dynamic
1842 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001843 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001844 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001845 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001846 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001847 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1848 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001849 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001850 ObjCPropertyImplDecl::Synthesize
1851 : ObjCPropertyImplDecl::Dynamic),
1852 Ivar);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001853 CurContext->addDecl(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001854 if (IC) {
1855 if (Synthesize)
1856 if (ObjCPropertyImplDecl *PPIDecl =
1857 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1858 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1859 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1860 << PropertyIvar;
1861 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1862 }
1863
1864 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1865 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1866 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001867 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001868 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001869 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001870 }
1871 else {
1872 if (Synthesize)
1873 if (ObjCPropertyImplDecl *PPIDecl =
1874 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1875 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1876 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1877 << PropertyIvar;
1878 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1879 }
1880
1881 if (ObjCPropertyImplDecl *PPIDecl =
1882 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1883 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1884 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001885 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001886 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001887 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001888 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001889
Chris Lattner5261d0c2009-03-28 19:18:32 +00001890 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001891}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001892
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001893bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001894 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001895 return false;
1896
1897 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1898 D->setInvalidDecl();
1899
1900 return true;
1901}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001902
1903/// Collect the instance variables declared in an Objective-C object. Used in
1904/// the creation of structures from objects using the @defs directive.
1905/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1906/// part of the AST generation logic of @defs.
1907static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1908 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001909 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001910 if (Class->getSuperClass())
1911 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1912
1913 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001914 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1915 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001916 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001917 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1918 ID->getIdentifier(), ID->getType(),
1919 ID->getBitWidth());
1920 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001921 }
1922}
1923
1924/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1925/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001926void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001927 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001928 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001929 // Check that ClassName is a valid class
1930 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1931 if (!Class) {
1932 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1933 return;
1934 }
1935 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00001936 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001937
1938 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001939 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001940 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001941 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001942 if (getLangOptions().CPlusPlus)
1943 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001944 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001945 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001946 }
1947}
1948