blob: 898ecbb47168dda197eacfe8a42f05c58142eb1c [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Steve Naroff58d0d6f2009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroff8df46022009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000045
Daniel Dunbareaf91c32008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000048
Chris Lattner97316c02008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000050 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
51 E = MDecl->param_end(); PI != E; ++PI)
52 if ((*PI)->getIdentifier())
53 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000054}
55
Chris Lattner5261d0c2009-03-28 19:18:32 +000056Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000057ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000060 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremenek42730c52008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000076 }
77
Ted Kremenek42730c52008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000082 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000083 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000084 Diag(IDecl->getLocation(), diag::note_previous_definition);
85
Steve Naroff1422a622008-11-18 19:15:30 +000086 // Return the previous class interface.
87 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +000088 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +000089 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000090 IDecl->setLocation(AtInterfaceLoc);
91 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000092 }
Chris Lattner5cece462008-07-21 07:06:49 +000093 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000094 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000095 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000096 if (AttrList)
97 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000098
Steve Naroff15208162008-04-02 18:30:49 +000099 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000100 // FIXME: PushOnScopeChains
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000101 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000103 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 }
105
106 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000107 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000108 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000109
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000110 ObjCInterfaceDecl *SuperClassDecl =
111 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000112
113 // Diagnose classes that inherit from deprecated classes.
114 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000115 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000116
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000117 if (PrevDecl && SuperClassDecl == 0) {
118 // The previous declaration was not a class decl. Check if we have a
119 // typedef. If we do, get the underlying class type.
120 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
121 QualType T = TDecl->getUnderlyingType();
122 if (T->isObjCInterfaceType()) {
123 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
124 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
125 }
126 }
Chris Lattner7699a532009-02-16 21:33:09 +0000127
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000128 // This handles the following case:
129 //
130 // typedef int SuperClass;
131 // @interface MyClass : SuperClass {} @end
132 //
133 if (!SuperClassDecl) {
134 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
135 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
136 }
137 }
Chris Lattner7699a532009-02-16 21:33:09 +0000138
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000139 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
140 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000141 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000142 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000143 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000144 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000145 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000146 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000148 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000149 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 IDecl->setLocEnd(SuperLoc);
151 } else { // we have a root class.
152 IDecl->setLocEnd(ClassLoc);
153 }
154
Steve Naroff1422a622008-11-18 19:15:30 +0000155 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000156 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000157 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
158 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000159 IDecl->setLocEnd(EndProtoLoc);
160 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000161
162 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000163 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000164}
165
166/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000167/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000168Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
169 IdentifierInfo *AliasName,
170 SourceLocation AliasLocation,
171 IdentifierInfo *ClassName,
172 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000173 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000174 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000176 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000178 else
Chris Lattner65cae292008-11-19 08:23:25 +0000179 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000180 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000182 }
183 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000184 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000185 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
186 QualType T = TDecl->getUnderlyingType();
187 if (T->isObjCInterfaceType()) {
188 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
189 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000190 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000191 }
192 }
193 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000194 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
195 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000196 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000197 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000198 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000201
202 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000203 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000204 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000205
206 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000207
208 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000209 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000210 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000211 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000212
Chris Lattner5261d0c2009-03-28 19:18:32 +0000213 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000214}
215
Steve Narofffe56a632009-03-05 15:22:01 +0000216void Sema::CheckForwardProtocolDeclarationForCircularDependency(
217 IdentifierInfo *PName,
218 SourceLocation &Ploc, SourceLocation PrevLoc,
219 const ObjCList<ObjCProtocolDecl> &PList)
220{
221 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
222 E = PList.end(); I != E; ++I) {
223
224 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
225 if (PDecl->getIdentifier() == PName) {
226 Diag(Ploc, diag::err_protocol_has_circular_dependency);
227 Diag(PrevLoc, diag::note_previous_definition);
228 }
229 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
230 PDecl->getLocation(), PDecl->getReferencedProtocols());
231 }
232 }
233}
234
Chris Lattner5261d0c2009-03-28 19:18:32 +0000235Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000236Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
237 IdentifierInfo *ProtocolName,
238 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000239 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000240 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000241 SourceLocation EndProtoLoc,
242 AttributeList *AttrList) {
243 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000244 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000246 if (PDecl) {
247 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000248 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000249 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000250 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000251 // Just return the protocol we already had.
252 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000253 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000254 }
Steve Narofffe56a632009-03-05 15:22:01 +0000255 ObjCList<ObjCProtocolDecl> PList;
256 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
257 CheckForwardProtocolDeclarationForCircularDependency(
258 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
259 PList.Destroy(Context);
260
Steve Naroff9a9e5212008-08-13 16:39:22 +0000261 // Make sure the cached decl gets a valid start location.
262 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000263 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000264 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000265 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
266 AtProtoInterfaceLoc,ProtocolName);
267 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000268 CurContext->addDecl(Context, PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000269 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000271 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000272 if (AttrList)
273 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000274 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000275 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000276 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000277 PDecl->setLocEnd(EndProtoLoc);
278 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000279
280 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000281 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000282}
283
284/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000285/// issues an error if they are not declared. It returns list of
286/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000287void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000288Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000289 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000290 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000291 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000292 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000293 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
294 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000295 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000296 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000297 continue;
298 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000299
Douglas Gregoraa57e862009-02-18 21:56:37 +0000300 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000301
302 // If this is a forward declaration and we are supposed to warn in this
303 // case, do it.
304 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000305 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000306 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000307 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000308 }
309}
310
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000312/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000313///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000314void
315Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
316 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000317 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000318 ObjCPropertyDecl::PropertyAttributeKind CAttr =
319 Property->getPropertyAttributes();
320 ObjCPropertyDecl::PropertyAttributeKind SAttr =
321 SuperProperty->getPropertyAttributes();
322 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
323 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000324 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000325 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
327 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000328 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000329 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000330 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
331 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000332 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000333 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000334
335 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
336 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000337 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000338 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000339 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000340 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000341 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000342 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000343 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000344 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000345
346 QualType LHSType =
347 Context.getCanonicalType(SuperProperty->getType());
348 QualType RHSType =
349 Context.getCanonicalType(Property->getType());
350
351 if (!Context.typesAreCompatible(LHSType, RHSType)) {
352 // FIXME: Incorporate this test with typesAreCompatible.
353 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
354 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
355 return;
356 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
357 << Property->getType() << SuperProperty->getType() << inheritedName;
358 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000359}
360
361/// ComparePropertiesInBaseAndSuper - This routine compares property
362/// declarations in base and its super class, if any, and issues
363/// diagnostics in a variety of inconsistant situations.
364///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000365void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000366 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
367 if (!SDecl)
368 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000369 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000370 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
371 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000372 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000373 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000374 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
375 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000376 ObjCPropertyDecl *PDecl = (*I);
377 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000378 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000379 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000380 }
381 }
382}
383
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000384/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
385/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000386/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000387void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000388Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000389 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000390 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
391 if (!IDecl) {
392 // Category
393 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
394 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000395 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
396 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000397 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000398 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000399 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000400 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401 CP != CE; ++CP)
402 if ((*CP)->getIdentifier() == Pr->getIdentifier())
403 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000404 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000405 // Property protocol already exist in class. Diagnose any mismatch.
406 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
407 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000408 return;
409 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000410 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
411 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000413 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000414 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000415 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000416 CP != CE; ++CP)
417 if ((*CP)->getIdentifier() == Pr->getIdentifier())
418 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000419 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000420 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000421 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000422 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000423}
424
425/// MergeProtocolPropertiesIntoClass - This routine merges properties
426/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000427/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000429void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000430 DeclPtrTy MergeItsProtocols) {
431 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000432 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
433
434 if (!IDecl) {
435 // Category
436 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
437 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
438 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
439 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
440 E = MDecl->protocol_end(); P != E; ++P)
441 // Merge properties of category (*P) into IDECL's
442 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
443
444 // Go thru the list of protocols for this category and recursively merge
445 // their properties into this class as well.
446 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
447 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000448 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000449 } else {
450 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
451 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
452 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000453 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000454 }
455 return;
456 }
457
Chris Lattner5cece462008-07-21 07:06:49 +0000458 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000459 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
460 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000461 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000462 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
463
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000464 // Go thru the list of protocols for this class and recursively merge
465 // their properties into this class as well.
466 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
467 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000468 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000469 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000470 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
471 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
472 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000473 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000474 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000475}
476
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000477/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000478/// a class method in its extension.
479///
480void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
481 ObjCInterfaceDecl *ID) {
482 if (!ID)
483 return; // Possibly due to previous error
484
485 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000486 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
487 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000488 ObjCMethodDecl *MD = *i;
489 MethodMap[MD->getSelector()] = MD;
490 }
491
492 if (MethodMap.empty())
493 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000494 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
495 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000496 ObjCMethodDecl *Method = *i;
497 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
498 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
499 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
500 << Method->getDeclName();
501 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
502 }
503 }
504}
505
Chris 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 Gregorc55b0b02009-04-09 21:40:53 +0000521 CurContext->addDecl(Context, PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000522 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000523 if (attrList)
524 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000525 Protocols.push_back(PDecl);
526 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000527
528 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000529 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000530 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000531 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000532 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000533 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000534}
535
Chris Lattner5261d0c2009-03-28 19:18:32 +0000536Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000537ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
538 IdentifierInfo *ClassName, SourceLocation ClassLoc,
539 IdentifierInfo *CategoryName,
540 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000541 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000542 unsigned NumProtoRefs,
543 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000544 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000545 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
546 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000547 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000548
549 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000550 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000551 if (!IDecl || IDecl->isForwardDecl()) {
552 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000553 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000554 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000555 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000556
Chris Lattner1fa7f622009-02-16 21:26:43 +0000557 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000558
559 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000560 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000561
562 /// Check for duplicate interface declaration for this category
563 ObjCCategoryDecl *CDeclChain;
564 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
565 CDeclChain = CDeclChain->getNextClassCategory()) {
566 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
567 Diag(CategoryLoc, diag::warn_dup_category_def)
568 << ClassName << CategoryName;
569 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
570 break;
571 }
572 }
573 if (!CDeclChain)
574 CDecl->insertNextClassCategory();
575
Chris Lattner855e51f2007-12-12 07:09:47 +0000576 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000577 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000578 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000580
581 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000582 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000583}
584
585/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000586/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000587/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000588Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000589 SourceLocation AtCatImplLoc,
590 IdentifierInfo *ClassName, SourceLocation ClassLoc,
591 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000592 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000593 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000594 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
595 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596 /// Check that class of this category is already completely declared.
597 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000598 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000599
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000600 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000601 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000602
Chris Lattner855e51f2007-12-12 07:09:47 +0000603 /// TODO: Check that CatName, category name, is not used in another
604 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000605 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000606
607 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000608 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000609}
610
Chris Lattner5261d0c2009-03-28 19:18:32 +0000611Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 SourceLocation AtClassImplLoc,
613 IdentifierInfo *ClassName, SourceLocation ClassLoc,
614 IdentifierInfo *SuperClassname,
615 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000618 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000620 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000621 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000622 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000626 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 }
628
629 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 if (SuperClassname) {
632 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000633 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000634 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
636 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000637 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000638 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000641 Diag(SuperClassLoc, diag::err_undef_superclass)
642 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000643 else if (IDecl && IDecl->getSuperClass() != SDecl) {
644 // This implementation and its interface do not have the same
645 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000646 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000647 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000648 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 }
650 }
651 }
652
653 if (!IDecl) {
654 // Legacy case of @implementation with no corresponding @interface.
655 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000656
657 // FIXME: Do we support attributes on the @implementation? If so
658 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000659 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
660 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000661 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 IDecl->setSuperClass(SDecl);
663 IDecl->setLocEnd(ClassLoc);
664
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000665 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000666 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000667 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000668 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000669 }
670
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000672 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000673 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000675 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000676 CurContext->addDecl(Context, IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000677
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000678 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000679 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000680
Chris Lattner855e51f2007-12-12 07:09:47 +0000681 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000683 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000684 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000685 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000686 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000687 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000688}
689
Ted Kremenek42730c52008-01-07 19:49:32 +0000690void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
691 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000692 SourceLocation RBrace) {
693 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000694 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000695 if (!IDecl)
696 return;
697 /// Check case of non-existing @interface decl.
698 /// (legacy objective-c @implementation decl without an @interface decl).
699 /// Add implementations's ivar to the synthesize class's ivar list.
700 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000701 IDecl->setIVarList(ivars, numIvars, Context);
702 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000703 return;
704 }
705 // If implementation has empty ivar list, just return.
706 if (numIvars == 0)
707 return;
708
709 assert(ivars && "missing @implementation ivars");
710
711 // Check interface's Ivar list against those in the implementation.
712 // names and types must match.
713 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000714 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000716 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
717 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 ObjCIvarDecl* ImplIvar = ivars[j++];
719 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000720 assert (ImplIvar && "missing implementation ivar");
721 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000722
723 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000724 if (Context.getCanonicalType(ImplIvar->getType()) !=
725 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000726 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000727 << ImplIvar->getIdentifier()
728 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000729 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000730 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
731 Expr *ImplBitWidth = ImplIvar->getBitWidth();
732 Expr *ClsBitWidth = ClsIvar->getBitWidth();
733 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
734 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
735 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
736 << ImplIvar->getIdentifier();
737 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
738 }
739 }
740 // Make sure the names are identical.
741 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000742 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000743 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000744 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000745 }
746 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000748
749 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000750 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000751 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000752 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000753}
754
Steve Naroffb4f48512008-02-10 21:38:56 +0000755void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
756 bool &IncompleteImpl) {
757 if (!IncompleteImpl) {
758 Diag(ImpLoc, diag::warn_incomplete_impl);
759 IncompleteImpl = true;
760 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000761 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000762}
763
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000764void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
765 ObjCMethodDecl *IntfMethodDecl) {
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.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000800 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000801 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.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000810 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000811 return false;
812 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000813 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000814 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.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000866 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
867 E = PDecl->instmeth_end(Context); 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()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000871 (!Super ||
872 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000873 // Ugly, but necessary. Method declared in protcol might have
874 // have been synthesized due to a property declared in the class which
875 // uses the protocol.
876 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000877 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000878 if (!MethodInClass || !MethodInClass->isSynthesized())
879 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
880 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000881 }
882 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000883 for (ObjCProtocolDecl::classmeth_iterator
884 I = PDecl->classmeth_begin(Context),
885 E = PDecl->classmeth_end(Context);
886 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000888 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
889 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000890 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000891 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000892 }
Chris Lattner0be08822008-07-21 21:32:27 +0000893 // Check on this protocols's referenced protocols, recursively.
894 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
895 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000896 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000897}
898
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000899void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
900 ObjCContainerDecl* CDecl,
901 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000902 llvm::DenseSet<Selector> InsMap;
903 // Check and see if instance methods in class interface have been
904 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000905 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000906 E = IMPDecl->instmeth_end(); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000907 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000908
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000909 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
910 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000911 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000912 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000913 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000914 }
Chris Lattner28400082009-02-16 19:25:52 +0000915
916 ObjCMethodDecl *ImpMethodDecl =
917 IMPDecl->getInstanceMethod((*I)->getSelector());
918 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000919 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000920 assert(IntfMethodDecl &&
921 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
922 // ImpMethodDecl may be null as in a @dynamic property.
923 if (ImpMethodDecl)
924 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
925 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000926
Chris Lattner855e51f2007-12-12 07:09:47 +0000927 llvm::DenseSet<Selector> ClsMap;
928 // Check and see if class methods in class interface have been
929 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000931 E = IMPDecl->classmeth_end(); I != E; ++I)
932 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000933
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000934 for (ObjCInterfaceDecl::classmeth_iterator
935 I = CDecl->classmeth_begin(Context),
936 E = CDecl->classmeth_end(Context);
937 I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000938 if (!ClsMap.count((*I)->getSelector()))
939 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000940 else {
941 ObjCMethodDecl *ImpMethodDecl =
942 IMPDecl->getClassMethod((*I)->getSelector());
943 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000944 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000945 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
946 }
947
Chris Lattner855e51f2007-12-12 07:09:47 +0000948
949 // Check the protocol list for unimplemented methods in the @implementation
950 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000951 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
952 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
953 E = I->protocol_end(); PI != E; ++PI)
954 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
955 InsMap, ClsMap, I);
956 // Check class extensions (unnamed categories)
957 for (ObjCCategoryDecl *Categories = I->getCategoryList();
958 Categories; Categories = Categories->getNextClassCategory()) {
959 if (!Categories->getIdentifier()) {
960 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
961 break;
962 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000963 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000964 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
965 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
966 E = C->protocol_end(); PI != E; ++PI)
967 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
968 InsMap, ClsMap, C->getClassInterface());
969 } else
970 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +0000971}
972
973/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +0000974Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000975Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000976 IdentifierInfo **IdentList,
977 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000978 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000979
980 for (unsigned i = 0; i != NumElts; ++i) {
981 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000982 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000983 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000984 // Maybe we will complain about the shadowed template parameter.
985 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
986 // Just pretend that we didn't see the previous declaration.
987 PrevDecl = 0;
988 }
989
Ted Kremenek42730c52008-01-07 19:49:32 +0000990 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000991 // GCC apparently allows the following idiom:
992 //
993 // typedef NSObject < XCElementTogglerP > XCElementToggler;
994 // @class XCElementToggler;
995 //
996 // FIXME: Make an extension?
997 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
998 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000999 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001000 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001001 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001002 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001004 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001005 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1006 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +00001007 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +00001008
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001009 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001010 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001011 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001012 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +00001013 }
1014
1015 Interfaces.push_back(IDecl);
1016 }
1017
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001018 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001019 &Interfaces[0],
1020 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001021 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001022 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001023 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001024}
1025
1026
1027/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1028/// returns true, or false, accordingly.
1029/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001030bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001031 const ObjCMethodDecl *PrevMethod,
1032 bool matchBasedOnSizeAndAlignment) {
1033 QualType T1 = Context.getCanonicalType(Method->getResultType());
1034 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1035
1036 if (T1 != T2) {
1037 // The result types are different.
1038 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001039 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001040 // Incomplete types don't have a size and alignment.
1041 if (T1->isIncompleteType() || T2->isIncompleteType())
1042 return false;
1043 // Check is based on size and alignment.
1044 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1045 return false;
1046 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001047
1048 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1049 E = Method->param_end();
1050 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1051
1052 for (; ParamI != E; ++ParamI, ++PrevI) {
1053 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1054 T1 = Context.getCanonicalType((*ParamI)->getType());
1055 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001056 if (T1 != T2) {
1057 // The result types are different.
1058 if (!matchBasedOnSizeAndAlignment)
1059 return false;
1060 // Incomplete types don't have a size and alignment.
1061 if (T1->isIncompleteType() || T2->isIncompleteType())
1062 return false;
1063 // Check is based on size and alignment.
1064 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1065 return false;
1066 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001067 }
1068 return true;
1069}
1070
Ted Kremenek42730c52008-01-07 19:49:32 +00001071void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001072 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1073 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001074 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001075 Entry.Method = Method;
1076 Entry.Next = 0;
1077 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001078 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001079
1080 // We've seen a method with this name, see if we have already seen this type
1081 // signature.
1082 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1083 if (MatchTwoMethodDeclarations(Method, List->Method))
1084 return;
1085
1086 // We have a new signature for an existing method - add it.
1087 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1088 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001089}
1090
Steve Naroffec7e0882008-10-21 10:50:19 +00001091// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001092ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1093 SourceRange R) {
1094 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001095 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001096
1097 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001098 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1099 // This checks if the methods differ by size & alignment.
1100 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1101 issueWarning = true;
1102 }
1103 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001104 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001105 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001106 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001107 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001108 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001109 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001110 }
1111 return MethList.Method;
1112}
1113
Ted Kremenek42730c52008-01-07 19:49:32 +00001114void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1115 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001116 if (!FirstMethod.Method) {
1117 // Haven't seen a method with this selector name yet - add it.
1118 FirstMethod.Method = Method;
1119 FirstMethod.Next = 0;
1120 } else {
1121 // We've seen a method with this name, now check the type signature(s).
1122 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1123
Ted Kremenek42730c52008-01-07 19:49:32 +00001124 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001125 Next = Next->Next)
1126 match = MatchTwoMethodDeclarations(Method, Next->Method);
1127
1128 if (!match) {
1129 // We have a new signature for an existing method - add it.
1130 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001131 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001132 FirstMethod.Next = OMI;
1133 }
1134 }
1135}
1136
Steve Naroffab63fd62009-01-08 17:28:14 +00001137/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1138/// have the property type and issue diagnostics if they don't.
1139/// Also synthesize a getter/setter method if none exist (and update the
1140/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1141/// methods is the "right" thing to do.
1142void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1143 ObjCContainerDecl *CD) {
1144 ObjCMethodDecl *GetterMethod, *SetterMethod;
1145
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001146 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1147 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001148
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001149 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001150 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001151 Diag(property->getLocation(),
1152 diag::err_accessor_property_type_mismatch)
1153 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001154 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001155 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1156 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001157
1158 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001159 if (Context.getCanonicalType(SetterMethod->getResultType())
1160 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001161 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001162 if (SetterMethod->param_size() != 1 ||
1163 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001164 Diag(property->getLocation(),
1165 diag::err_accessor_property_type_mismatch)
1166 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001167 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001168 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1169 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001170 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001171
1172 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001173 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001174 // FIXME: The synthesized property we set here is misleading. We
1175 // almost always synthesize these methods unless the user explicitly
1176 // provided prototypes (which is odd, but allowed). Sema should be
1177 // typechecking that the declarations jive in that situation (which
1178 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001179 if (!GetterMethod) {
1180 // No instance method of same name as property getter name was found.
1181 // Declare a getter method and add it to the list of methods
1182 // for this class.
1183 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1184 property->getLocation(), property->getGetterName(),
1185 property->getType(), CD, true, false, true,
1186 (property->getPropertyImplementation() ==
1187 ObjCPropertyDecl::Optional) ?
1188 ObjCMethodDecl::Optional :
1189 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001190 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001191 } else
1192 // A user declared getter will be synthesize when @synthesize of
1193 // the property with the same name is seen in the @implementation
1194 GetterMethod->setIsSynthesized();
1195 property->setGetterMethodDecl(GetterMethod);
1196
1197 // Skip setter if property is read-only.
1198 if (!property->isReadOnly()) {
1199 // Find the default setter and if one not found, add one.
1200 if (!SetterMethod) {
1201 // No instance method of same name as property setter name was found.
1202 // Declare a setter method and add it to the list of methods
1203 // for this class.
1204 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1205 property->getLocation(),
1206 property->getSetterName(),
1207 Context.VoidTy, CD, true, false, true,
1208 (property->getPropertyImplementation() ==
1209 ObjCPropertyDecl::Optional) ?
1210 ObjCMethodDecl::Optional :
1211 ObjCMethodDecl::Required);
1212 // Invent the arguments for the setter. We don't bother making a
1213 // nice name for the argument.
1214 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1215 SourceLocation(),
1216 property->getIdentifier(),
1217 property->getType(),
1218 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001219 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001220 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001221 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001222 } else
1223 // A user declared setter will be synthesize when @synthesize of
1224 // the property with the same name is seen in the @implementation
1225 SetterMethod->setIsSynthesized();
1226 property->setSetterMethodDecl(SetterMethod);
1227 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001228 // Add any synthesized methods to the global pool. This allows us to
1229 // handle the following, which is supported by GCC (and part of the design).
1230 //
1231 // @interface Foo
1232 // @property double bar;
1233 // @end
1234 //
1235 // void thisIsUnfortunate() {
1236 // id foo;
1237 // double bar = [foo bar];
1238 // }
1239 //
Douglas Gregord1675382009-01-09 19:42:16 +00001240 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001241 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001242 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001243 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001244}
1245
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001246// Note: For class/category implemenations, allMethods/allProperties is
1247// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001248void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1249 DeclPtrTy *allMethods, unsigned allNum,
1250 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001251 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001252 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001253
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001254 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1255 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001256 // should be true.
1257 if (!ClassDecl)
1258 return;
1259
Chris Lattner855e51f2007-12-12 07:09:47 +00001260 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001261 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1262 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001263 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001264
Steve Naroffab63fd62009-01-08 17:28:14 +00001265 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001266
1267 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1268 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1269 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1270
Chris Lattner855e51f2007-12-12 07:09:47 +00001271 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001272 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001273 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001274
1275 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001276 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001277 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001278 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001279 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1280 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001281 if ((isInterfaceDeclKind && PrevMethod && !match)
1282 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001283 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001284 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001285 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001286 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001287 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001288 InsMap[Method->getSelector()] = Method;
1289 /// The following allows us to typecheck messages to "id".
1290 AddInstanceMethodToGlobalPool(Method);
1291 }
1292 }
1293 else {
1294 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001295 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001296 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1297 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001298 if ((isInterfaceDeclKind && PrevMethod && !match)
1299 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001300 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001301 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001302 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001303 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001304 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001305 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001306 /// The following allows us to typecheck messages to "Class".
1307 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001308 }
1309 }
1310 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001311 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001312 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001313 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001314 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001315 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001316 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001317 // Categories are used to extend the class by declaring new methods.
1318 // By the same token, they are also used to add new properties. No
1319 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001320
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001321 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001322 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001323 if (C->getIdentifier() == 0)
1324 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001325 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001326 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1327 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1328 // user-defined setter/getter. It also synthesizes setter/getter methods
1329 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001330 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1331 E = CDecl->prop_end(Context);
1332 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001333 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001334 CDecl->setAtEndLoc(AtEndLoc);
1335 }
1336 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001337 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001338 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001339 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001340 } else if (ObjCCategoryImplDecl* CatImplClass =
1341 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001342 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001343
Chris Lattner855e51f2007-12-12 07:09:47 +00001344 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001345 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001346 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001347 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001348 Categories; Categories = Categories->getNextClassCategory()) {
1349 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001350 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001351 break;
1352 }
1353 }
1354 }
1355 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001356 if (isInterfaceDeclKind) {
1357 // Reject invalid vardecls.
1358 for (unsigned i = 0; i != tuvNum; i++) {
1359 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1360 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1361 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
1362 if (VDecl->getStorageClass() != VarDecl::Extern &&
1363 VDecl->getStorageClass() != VarDecl::PrivateExtern)
1364 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass)
Ted Kremeneka550a642009-04-10 18:25:37 +00001365 << cast<NamedDecl>(ClassDecl)->getDeclName();
Fariborz Jahanianca277322009-03-21 18:06:45 +00001366 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001367 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001368 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001369}
1370
1371
1372/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1373/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001374static Decl::ObjCDeclQualifier
1375CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1376 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1377 if (PQTVal & ObjCDeclSpec::DQ_In)
1378 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1379 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1380 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1381 if (PQTVal & ObjCDeclSpec::DQ_Out)
1382 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1383 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1384 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1385 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1386 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1387 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1388 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001389
1390 return ret;
1391}
1392
Chris Lattner5261d0c2009-03-28 19:18:32 +00001393Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001394 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001395 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001397 Selector Sel,
1398 // optional arguments. The number of types/arguments is obtained
1399 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001400 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001401 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001402 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1403 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001404 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001405
1406 // Make sure we can establish a context for the method.
1407 if (!ClassDecl) {
1408 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001409 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001410 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001411 QualType resultDeclType;
1412
Steve Naroffa442ad92009-02-20 22:59:16 +00001413 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001414 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001415
1416 // Methods cannot return interface types. All ObjC objects are
1417 // passed by reference.
1418 if (resultDeclType->isObjCInterfaceType()) {
1419 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1420 << "returned";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001421 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001422 }
1423 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001424 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001425
Chris Lattner114add62008-03-16 00:49:28 +00001426 ObjCMethodDecl* ObjCMethod =
1427 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001428 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001429 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001430 false,
Chris Lattner114add62008-03-16 00:49:28 +00001431 MethodDeclKind == tok::objc_optional ?
1432 ObjCMethodDecl::Optional :
1433 ObjCMethodDecl::Required);
1434
Chris Lattnereee57c02008-04-04 06:12:32 +00001435 llvm::SmallVector<ParmVarDecl*, 16> Params;
1436
1437 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1438 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001439 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001440
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001441 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001442 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001443 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001444 if (argType->isArrayType()) { // (char *[]) -> (char **)
1445 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001446 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001447 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001448 else if (argType->isFunctionType())
1449 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001450 else if (argType->isObjCInterfaceType()) {
1451 // FIXME! provide more precise location for the parameter
Steve Naroffa442ad92009-02-20 22:59:16 +00001452 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1453 << "passed";
1454 ObjCMethod->setInvalidDecl();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001455 return DeclPtrTy();
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001456 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001457 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001458 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001459 ParmVarDecl* Param;
1460 if (originalArgType.isNull())
1461 Param = ParmVarDecl::Create(Context, ObjCMethod,
1462 SourceLocation(/*FIXME*/),
1463 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001464 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001465 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001466 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1467 SourceLocation(/*FIXME*/),
1468 ArgNames[i], argType, originalArgType,
1469 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001470
Chris Lattnereee57c02008-04-04 06:12:32 +00001471 Param->setObjCDeclQualifier(
1472 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1473 Params.push_back(Param);
1474 }
1475
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001476 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001477 ObjCMethod->setObjCDeclQualifier(
1478 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1479 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001480
1481 if (AttrList)
1482 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001483
1484 // For implementations (which can be very "coarse grain"), we add the
1485 // method now. This allows the AST to implement lookup methods that work
1486 // incrementally (without waiting until we parse the @end). It also allows
1487 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001488 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001489 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001490 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001491 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001492 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001493 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001494 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001495 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001496 }
1497 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001498 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001499 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001500 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001501 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001502 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001503 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001504 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001505 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001506 }
1507 }
1508 if (PrevMethod) {
1509 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001510 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001511 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001512 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001513 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001514 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001515}
1516
Daniel Dunbar540ff472008-09-23 21:53:23 +00001517void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1518 SourceLocation Loc,
1519 unsigned &Attributes) {
1520 // FIXME: Improve the reported location.
1521
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001522 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001523 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001524 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1525 ObjCDeclSpec::DQ_PR_assign |
1526 ObjCDeclSpec::DQ_PR_copy |
1527 ObjCDeclSpec::DQ_PR_retain))) {
1528 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1529 "readwrite" :
1530 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1531 "assign" :
1532 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1533 "copy" : "retain";
1534
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001535 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001536 diag::err_objc_property_attr_mutually_exclusive :
1537 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001538 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001539 }
1540
1541 // Check for copy or retain on non-object types.
1542 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1543 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001544 Diag(Loc, diag::err_objc_property_requires_object)
1545 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001546 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1547 }
1548
1549 // Check for more than one of { assign, copy, retain }.
1550 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1551 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001552 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1553 << "assign" << "copy";
1554 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001555 }
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 << "assign" << "retain";
1559 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001560 }
1561 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1562 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001563 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1564 << "copy" << "retain";
1565 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001566 }
1567 }
1568
1569 // Warn if user supplied no assignment attribute, property is
1570 // readwrite, and this is an object type.
1571 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1572 ObjCDeclSpec::DQ_PR_retain)) &&
1573 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1574 Context.isObjCObjectPointerType(PropertyTy)) {
1575 // Skip this warning in gc-only mode.
1576 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1577 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1578
1579 // If non-gc code warn that this is likely inappropriate.
1580 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1581 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1582
1583 // FIXME: Implement warning dependent on NSCopying being
1584 // implemented. See also:
1585 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1586 // (please trim this list while you are at it).
1587 }
1588}
1589
Chris Lattner5261d0c2009-03-28 19:18:32 +00001590Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1591 FieldDeclarator &FD,
1592 ObjCDeclSpec &ODS,
1593 Selector GetterSel,
1594 Selector SetterSel,
1595 DeclPtrTy ClassCategory,
1596 bool *isOverridingProperty,
1597 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001598 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001599 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1600 // default is readwrite!
1601 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1602 // property is defaulted to 'assign' if it is readwrite and is
1603 // not retain or copy
1604 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1605 (isReadWrite &&
1606 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1607 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1608 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001609 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001610 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001611 // May modify Attributes.
1612 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001613 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1614 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001615 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001616 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001617 if ((CCPrimary = CDecl->getClassInterface())) {
1618 // Find the property in continuation class's primary class only.
1619 ObjCPropertyDecl *PIDecl = 0;
1620 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001621 for (ObjCInterfaceDecl::prop_iterator
1622 I = CCPrimary->prop_begin(Context),
1623 E = CCPrimary->prop_end(Context);
1624 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001625 if ((*I)->getIdentifier() == PropertyId) {
1626 PIDecl = *I;
1627 break;
1628 }
1629
1630 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001631 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001632 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001633 unsigned PIkind = PIDecl->getPropertyAttributes();
1634 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001635 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001636 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1637 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001638 PIDecl->makeitReadWriteAttribute();
1639 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1640 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1641 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1642 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1643 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001644 // FIXME: use a common routine with addPropertyMethods.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001645 ObjCMethodDecl *SetterDecl =
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001646 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1647 Context.VoidTy,
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001648 CCPrimary,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001649 true, false, true,
1650 ObjCMethodDecl::Required);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001651 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001652 SourceLocation(),
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001653 PropertyId,
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001654 T, VarDecl::None, 0);
1655 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001656 PIDecl->setSetterMethodDecl(SetterDecl);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001657 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001658 else
1659 Diag(AtLoc, diag::err_use_continuation_class)
1660 << CCPrimary->getDeclName();
1661 *isOverridingProperty = true;
1662 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001663 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001664 // No matching property found in the primary class. Just fall thru
1665 // and add property to continuation class's primary class.
1666 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001667 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001668 Diag(CDecl->getLocation(), diag::err_continuation_class);
1669 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001670 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001671 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001672 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001673
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001674 Type *t = T.getTypePtr();
1675 if (t->isArrayType() || t->isFunctionType())
1676 Diag(AtLoc, diag::err_property_type) << T;
1677
Steve Naroffdcf1e842009-01-11 12:47:58 +00001678 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1679 assert(DC && "ClassDecl is not a DeclContext");
1680 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001681 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001682 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001683
1684 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001685
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001686 // Regardless of setter/getter attribute, we save the default getter/setter
1687 // selector names in anticipation of declaration of setter/getter methods.
1688 PDecl->setGetterName(GetterSel);
1689 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001690
Daniel Dunbar540ff472008-09-23 21:53:23 +00001691 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001692 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001693
Daniel Dunbar540ff472008-09-23 21:53:23 +00001694 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001695 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001696
Daniel Dunbar540ff472008-09-23 21:53:23 +00001697 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001698 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001699
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001700 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001701 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001702
Daniel Dunbar540ff472008-09-23 21:53:23 +00001703 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001704 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001705
Daniel Dunbar540ff472008-09-23 21:53:23 +00001706 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001707 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001708
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001709 if (isAssign)
1710 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1711
Daniel Dunbar540ff472008-09-23 21:53:23 +00001712 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001713 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001714
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001715 if (MethodImplKind == tok::objc_required)
1716 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1717 else if (MethodImplKind == tok::objc_optional)
1718 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001719 // A case of continuation class adding a new property in the class. This
1720 // is not what it was meant for. However, gcc supports it and so should we.
1721 // Make sure setter/getters are declared here.
1722 if (CCPrimary)
1723 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001724
Chris Lattner5261d0c2009-03-28 19:18:32 +00001725 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001726}
1727
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001728/// ActOnPropertyImplDecl - This routine performs semantic checks and
1729/// builds the AST node for a property implementation declaration; declared
1730/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001731///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001732Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1733 SourceLocation PropertyLoc,
1734 bool Synthesize,
1735 DeclPtrTy ClassCatImpDecl,
1736 IdentifierInfo *PropertyId,
1737 IdentifierInfo *PropertyIvar) {
1738 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001739 // Make sure we have a context for the property implementation declaration.
1740 if (!ClassImpDecl) {
1741 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001742 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001743 }
1744 ObjCPropertyDecl *property = 0;
1745 ObjCInterfaceDecl* IDecl = 0;
1746 // Find the class or category class where this property must have
1747 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001748 ObjCImplementationDecl *IC = 0;
1749 ObjCCategoryImplDecl* CatImplClass = 0;
1750 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001751 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001752 // We always synthesize an interface for an implementation
1753 // without an interface decl. So, IDecl is always non-zero.
1754 assert(IDecl &&
1755 "ActOnPropertyImplDecl - @implementation without @interface");
1756
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001757 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001758 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001759 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001760 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001761 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001762 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001763 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001764 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001765 if (Synthesize) {
1766 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001767 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001768 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001769 IDecl = CatImplClass->getClassInterface();
1770 if (!IDecl) {
1771 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001772 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001773 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001774 ObjCCategoryDecl *Category =
1775 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1776
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001777 // If category for this implementation not found, it is an error which
1778 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001779 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001780 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001781 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001782 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001783 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001784 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001785 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001786 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001787 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001788 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001789 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001790 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001791 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001792 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001793 // Check that we have a valid, previously declared ivar for @synthesize
1794 if (Synthesize) {
1795 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001796 if (!PropertyIvar)
1797 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001798 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001799 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001800 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001801 if (!Ivar) {
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001802 if (getLangOptions().ObjCNonFragileABI) {
1803 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1804 PropertyIvar, PropType,
Fariborz Jahanian745fd892009-04-06 18:30:00 +00001805 ObjCIvarDecl::Public,
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001806 (Expr *)0);
1807 property->setPropertyIvarDecl(Ivar);
1808 }
1809 else {
Steve Naroff92aeef32009-03-03 22:09:41 +00001810 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001811 return DeclPtrTy();
1812 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001813 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001814 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1815
Steve Naroff631e3922008-09-30 00:24:17 +00001816 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001817 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001818 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001819 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001820 << property->getDeclName() << Ivar->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001821 return DeclPtrTy();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001822 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001823
1824 // FIXME! Rules for properties are somewhat different that those
1825 // for assignments. Use a new routine to consolidate all cases;
1826 // specifically for property redeclarations as well as for ivars.
1827 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1828 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1829 if (lhsType != rhsType &&
1830 lhsType->isArithmeticType()) {
1831 Diag(PropertyLoc, diag::error_property_ivar_type)
1832 << property->getDeclName() << Ivar->getDeclName();
1833 return DeclPtrTy();
1834 }
1835 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00001836 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1837 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001838 Diag(PropertyLoc, diag::error_weak_property)
1839 << property->getDeclName() << Ivar->getDeclName();
1840 return DeclPtrTy();
1841 }
1842 if ((Context.isObjCObjectPointerType(property->getType()) ||
1843 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1844 Diag(PropertyLoc, diag::error_strong_property)
1845 << property->getDeclName() << Ivar->getDeclName();
1846 return DeclPtrTy();
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001847 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001848 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001849 } else if (PropertyIvar) {
1850 // @dynamic
1851 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001852 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001853 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001854 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001855 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001856 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1857 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001858 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001859 ObjCPropertyImplDecl::Synthesize
1860 : ObjCPropertyImplDecl::Dynamic),
1861 Ivar);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001862 CurContext->addDecl(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001863 if (IC) {
1864 if (Synthesize)
1865 if (ObjCPropertyImplDecl *PPIDecl =
1866 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1867 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1868 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1869 << PropertyIvar;
1870 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1871 }
1872
1873 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1874 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1875 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001876 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001877 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001878 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001879 }
1880 else {
1881 if (Synthesize)
1882 if (ObjCPropertyImplDecl *PPIDecl =
1883 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1884 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1885 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1886 << PropertyIvar;
1887 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1888 }
1889
1890 if (ObjCPropertyImplDecl *PPIDecl =
1891 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1892 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1893 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001894 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001895 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001896 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001897 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001898
Chris Lattner5261d0c2009-03-28 19:18:32 +00001899 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001900}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001901
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001902bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001903 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001904 return false;
1905
1906 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1907 D->setInvalidDecl();
1908
1909 return true;
1910}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001911
1912/// Collect the instance variables declared in an Objective-C object. Used in
1913/// the creation of structures from objects using the @defs directive.
1914/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1915/// part of the AST generation logic of @defs.
1916static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1917 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001918 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001919 if (Class->getSuperClass())
1920 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1921
1922 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001923 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1924 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001925 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001926 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1927 ID->getIdentifier(), ID->getType(),
1928 ID->getBitWidth());
1929 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001930 }
1931}
1932
1933/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1934/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001935void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001936 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001937 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001938 // Check that ClassName is a valid class
1939 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1940 if (!Class) {
1941 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1942 return;
1943 }
1944 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00001945 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001946
1947 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001948 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001949 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001950 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001951 if (getLangOptions().CPlusPlus)
1952 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001953 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001954 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001955 }
1956}
1957