blob: e0cbdce7e0d5265acf9cfb2b0d7cbeae11c7f880 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Steve Naroff58d0d6f2009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroff8df46022009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000030
Chris Lattnerc92920c2009-04-19 05:21:20 +000031 CurFunctionNeedsScopeChecking = false;
32
Steve Narofffe9eb6a2007-12-18 01:30:32 +000033 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000038
39 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000041
42 // Create Decl objects for each parameter, entrring them in the scope for
43 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000047
Daniel Dunbareaf91c32008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000050
Chris Lattner97316c02008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000052 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
53 E = MDecl->param_end(); PI != E; ++PI)
54 if ((*PI)->getIdentifier())
55 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056}
57
Chris Lattner5261d0c2009-03-28 19:18:32 +000058Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000059ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000062 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000069 // Maybe we will complain about the shadowed template parameter.
70 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
71 // Just pretend that we didn't see the previous declaration.
72 PrevDecl = 0;
73 }
74
Ted Kremenek42730c52008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000078 }
79
Ted Kremenek42730c52008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroff1422a622008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +000090 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +000091 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000094 }
Chris Lattner5cece462008-07-21 07:06:49 +000095 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Steve Naroff104956f2009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000104 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 }
106
107 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000110
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000113
114 // Diagnose classes that inherit from deprecated classes.
115 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000116 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000117
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000118 if (PrevDecl && SuperClassDecl == 0) {
119 // The previous declaration was not a class decl. Check if we have a
120 // typedef. If we do, get the underlying class type.
121 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
122 QualType T = TDecl->getUnderlyingType();
123 if (T->isObjCInterfaceType()) {
124 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
125 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
126 }
127 }
Chris Lattner7699a532009-02-16 21:33:09 +0000128
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000129 // This handles the following case:
130 //
131 // typedef int SuperClass;
132 // @interface MyClass : SuperClass {} @end
133 //
134 if (!SuperClassDecl) {
135 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
136 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
137 }
138 }
Chris Lattner7699a532009-02-16 21:33:09 +0000139
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000140 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
141 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000144 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000145 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000146 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000147 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000148 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000149 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000150 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000151 IDecl->setLocEnd(SuperLoc);
152 } else { // we have a root class.
153 IDecl->setLocEnd(ClassLoc);
154 }
155
Steve Naroff1422a622008-11-18 19:15:30 +0000156 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000157 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000158 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
159 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 IDecl->setLocEnd(EndProtoLoc);
161 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000162
163 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000164 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000165}
166
167/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000168/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000169Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
170 IdentifierInfo *AliasName,
171 SourceLocation AliasLocation,
172 IdentifierInfo *ClassName,
173 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000174 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000175 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000176 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000177 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000179 else
Chris Lattner65cae292008-11-19 08:23:25 +0000180 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000181 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000182 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 }
184 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000185 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000186 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
187 QualType T = TDecl->getUnderlyingType();
188 if (T->isObjCInterfaceType()) {
189 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
190 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000191 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000192 }
193 }
194 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000195 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
196 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000197 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000198 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000199 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000200 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000202
203 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000205 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000206
207 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000208
Steve Naroffe00554f2009-04-23 17:46:47 +0000209 // FIXME: PushOnScopeChains?
210 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000211 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000212 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000213
Chris Lattner5261d0c2009-03-28 19:18:32 +0000214 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000215}
216
Steve Narofffe56a632009-03-05 15:22:01 +0000217void Sema::CheckForwardProtocolDeclarationForCircularDependency(
218 IdentifierInfo *PName,
219 SourceLocation &Ploc, SourceLocation PrevLoc,
220 const ObjCList<ObjCProtocolDecl> &PList)
221{
222 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
223 E = PList.end(); I != E; ++I) {
224
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000225 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-03-05 15:22:01 +0000226 if (PDecl->getIdentifier() == PName) {
227 Diag(Ploc, diag::err_protocol_has_circular_dependency);
228 Diag(PrevLoc, diag::note_previous_definition);
229 }
230 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
231 PDecl->getLocation(), PDecl->getReferencedProtocols());
232 }
233 }
234}
235
Chris Lattner5261d0c2009-03-28 19:18:32 +0000236Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000237Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
238 IdentifierInfo *ProtocolName,
239 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000240 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000241 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000242 SourceLocation EndProtoLoc,
243 AttributeList *AttrList) {
244 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000245 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000246 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000247 if (PDecl) {
248 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000249 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000250 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000251 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000252 // Just return the protocol we already had.
253 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000254 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000255 }
Steve Narofffe56a632009-03-05 15:22:01 +0000256 ObjCList<ObjCProtocolDecl> PList;
257 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
258 CheckForwardProtocolDeclarationForCircularDependency(
259 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
260 PList.Destroy(Context);
261
Steve Naroff9a9e5212008-08-13 16:39:22 +0000262 // Make sure the cached decl gets a valid start location.
263 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000264 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000265 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000266 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
267 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000268 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000269 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000270 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000271 if (AttrList)
272 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000273 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000274 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000275 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000276 PDecl->setLocEnd(EndProtoLoc);
277 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000278
279 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000280 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000281}
282
283/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000284/// issues an error if they are not declared. It returns list of
285/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000286void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000287Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000288 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000289 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000290 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000291 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000292 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000293 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000294 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000295 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000296 continue;
297 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000298
Douglas Gregoraa57e862009-02-18 21:56:37 +0000299 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000300
301 // If this is a forward declaration and we are supposed to warn in this
302 // case, do it.
303 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000304 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000305 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000306 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000307 }
308}
309
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000310/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000311/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000312///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000313void
314Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
315 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000316 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000317 ObjCPropertyDecl::PropertyAttributeKind CAttr =
318 Property->getPropertyAttributes();
319 ObjCPropertyDecl::PropertyAttributeKind SAttr =
320 SuperProperty->getPropertyAttributes();
321 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
322 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000323 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000324 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000325 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
326 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000327 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000328 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000329 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
330 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000331 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000332 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000333
334 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
335 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000337 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000338 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000339 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000340 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000341 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000342 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000343 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000344
345 QualType LHSType =
346 Context.getCanonicalType(SuperProperty->getType());
347 QualType RHSType =
348 Context.getCanonicalType(Property->getType());
349
350 if (!Context.typesAreCompatible(LHSType, RHSType)) {
351 // FIXME: Incorporate this test with typesAreCompatible.
352 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
353 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
354 return;
355 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
356 << Property->getType() << SuperProperty->getType() << inheritedName;
357 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000358}
359
360/// ComparePropertiesInBaseAndSuper - This routine compares property
361/// declarations in base and its super class, if any, and issues
362/// diagnostics in a variety of inconsistant situations.
363///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000364void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000365 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
366 if (!SDecl)
367 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000368 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000369 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
370 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000371 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000372 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000373 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
374 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000375 ObjCPropertyDecl *PDecl = (*I);
376 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000377 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000378 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000379 }
380 }
381}
382
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000383/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
384/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000385/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000386void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000387Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000388 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000389 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
390 if (!IDecl) {
391 // Category
392 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
393 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000394 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
395 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000396 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000397 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000398 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000399 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000400 CP != CE; ++CP)
401 if ((*CP)->getIdentifier() == Pr->getIdentifier())
402 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000403 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000404 // Property protocol already exist in class. Diagnose any mismatch.
405 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
406 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000407 return;
408 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000409 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
410 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000411 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000412 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000413 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000414 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000415 CP != CE; ++CP)
416 if ((*CP)->getIdentifier() == Pr->getIdentifier())
417 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000418 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000419 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000420 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000421 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000422}
423
424/// MergeProtocolPropertiesIntoClass - This routine merges properties
425/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000426/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000427///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000428void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000429 DeclPtrTy MergeItsProtocols) {
430 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000431 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
432
433 if (!IDecl) {
434 // Category
435 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
436 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
437 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
438 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
439 E = MDecl->protocol_end(); P != E; ++P)
440 // Merge properties of category (*P) into IDECL's
441 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
442
443 // Go thru the list of protocols for this category and recursively merge
444 // their properties into this class as well.
445 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
446 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000447 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000448 } else {
449 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
450 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
451 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000452 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000453 }
454 return;
455 }
456
Chris Lattner5cece462008-07-21 07:06:49 +0000457 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000458 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
459 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000460 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000461 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
462
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000463 // Go thru the list of protocols for this class and recursively merge
464 // their properties into this class as well.
465 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
466 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000467 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000468 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000469 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
470 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
471 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000472 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000473 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000474}
475
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000476/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000477/// a class method in its extension.
478///
479void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
480 ObjCInterfaceDecl *ID) {
481 if (!ID)
482 return; // Possibly due to previous error
483
484 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000485 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
486 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000487 ObjCMethodDecl *MD = *i;
488 MethodMap[MD->getSelector()] = MD;
489 }
490
491 if (MethodMap.empty())
492 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000493 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
494 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000495 ObjCMethodDecl *Method = *i;
496 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
497 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
498 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
499 << Method->getDeclName();
500 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
501 }
502 }
503}
504
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000505/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000506Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000507Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000508 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000509 unsigned NumElts,
510 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000511 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000512
513 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000514 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000515 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000516 if (PDecl == 0) { // Not already seen?
517 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
518 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000519 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000520 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000521 if (attrList)
522 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000523 Protocols.push_back(PDecl);
524 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000525
526 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000527 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000528 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000529 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000530 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000531 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000532}
533
Chris Lattner5261d0c2009-03-28 19:18:32 +0000534Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000535ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
536 IdentifierInfo *ClassName, SourceLocation ClassLoc,
537 IdentifierInfo *CategoryName,
538 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000539 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000540 unsigned NumProtoRefs,
541 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000542 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000543 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
544 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000545 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000546
547 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000548 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000549 if (!IDecl || IDecl->isForwardDecl()) {
550 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000551 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000552 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000553 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000554
Chris Lattner1fa7f622009-02-16 21:26:43 +0000555 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000556
557 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000558 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000559
560 /// Check for duplicate interface declaration for this category
561 ObjCCategoryDecl *CDeclChain;
562 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
563 CDeclChain = CDeclChain->getNextClassCategory()) {
564 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
565 Diag(CategoryLoc, diag::warn_dup_category_def)
566 << ClassName << CategoryName;
567 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
568 break;
569 }
570 }
571 if (!CDeclChain)
572 CDecl->insertNextClassCategory();
573
Chris Lattner855e51f2007-12-12 07:09:47 +0000574 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000575 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000576 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000577 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000578
579 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000580 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000581}
582
583/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000584/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000585/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000586Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 SourceLocation AtCatImplLoc,
588 IdentifierInfo *ClassName, SourceLocation ClassLoc,
589 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000591 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000592 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
593 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000594 /// Check that class of this category is already completely declared.
595 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000596 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000597
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000598 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000599 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000600
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 /// TODO: Check that CatName, category name, is not used in another
602 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000603 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000604
605 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000606 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000607}
608
Chris Lattner5261d0c2009-03-28 19:18:32 +0000609Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000610 SourceLocation AtClassImplLoc,
611 IdentifierInfo *ClassName, SourceLocation ClassLoc,
612 IdentifierInfo *SuperClassname,
613 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000616 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000618 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000619 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000620 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000623 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000625 IDecl = 0;
626 }
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));
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000669 } else {
670 // Mark the interface as being completed, even if it was just as
671 // @class ....;
672 // declaration; the user cannot reopen it.
673 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674 }
675
Ted Kremenek42730c52008-01-07 19:49:32 +0000676 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000677 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000678 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000679
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000680 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000681 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000682
Chris Lattner855e51f2007-12-12 07:09:47 +0000683 // Check that there is no duplicate implementation of this class.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000684 if (LookupObjCImplementation(ClassName))
Chris Lattner1b6de332008-03-16 20:53:07 +0000685 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000686 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000687 else // add it to the list.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000688 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000689 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000690}
691
Ted Kremenek42730c52008-01-07 19:49:32 +0000692void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
693 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000694 SourceLocation RBrace) {
695 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000696 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000697 if (!IDecl)
698 return;
699 /// Check case of non-existing @interface decl.
700 /// (legacy objective-c @implementation decl without an @interface decl).
701 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000702 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000703 IDecl->setIVarList(ivars, numIvars, Context);
704 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000705 return;
706 }
707 // If implementation has empty ivar list, just return.
708 if (numIvars == 0)
709 return;
710
711 assert(ivars && "missing @implementation ivars");
712
713 // Check interface's Ivar list against those in the implementation.
714 // names and types must match.
715 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000716 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000717 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000718 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
719 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000720 ObjCIvarDecl* ImplIvar = ivars[j++];
721 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000722 assert (ImplIvar && "missing implementation ivar");
723 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000724
725 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000726 if (Context.getCanonicalType(ImplIvar->getType()) !=
727 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000728 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000729 << ImplIvar->getIdentifier()
730 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000731 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000732 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
733 Expr *ImplBitWidth = ImplIvar->getBitWidth();
734 Expr *ClsBitWidth = ClsIvar->getBitWidth();
735 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
736 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
737 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
738 << ImplIvar->getIdentifier();
739 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
740 }
741 }
742 // Make sure the names are identical.
743 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000744 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000745 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000746 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 }
748 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000750
751 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000752 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000753 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000754 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000755}
756
Steve Naroffb4f48512008-02-10 21:38:56 +0000757void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
758 bool &IncompleteImpl) {
759 if (!IncompleteImpl) {
760 Diag(ImpLoc, diag::warn_incomplete_impl);
761 IncompleteImpl = true;
762 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000763 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000764}
765
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000766void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
767 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000768 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000769 ImpMethodDecl->getResultType())) {
770 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
771 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
772 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000773 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
774 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000775
776 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
777 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
778 IM != EM; ++IM, ++IF) {
779 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
780 continue;
781
782 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
783 << ImpMethodDecl->getDeclName() << (*IF)->getType()
784 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000785 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000786 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000787}
788
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000789/// isPropertyReadonly - Return true if property is readonly, by searching
790/// for the property in the class and in its categories and implementations
791///
792bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000793 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000794 // by far the most common case.
795 if (!PDecl->isReadOnly())
796 return false;
797 // Even if property is ready only, if interface has a user defined setter,
798 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000799 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000800 return false;
801
802 // Main class has the property as 'readonly'. Must search
803 // through the category list to see if the property's
804 // attribute has been over-ridden to 'readwrite'.
805 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
806 Category; Category = Category->getNextClassCategory()) {
807 // Even if property is ready only, if a category has a user defined setter,
808 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000809 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000810 return false;
811 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000812 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000813 if (P && !P->isReadOnly())
814 return false;
815 }
816
817 // Also, check for definition of a setter method in the implementation if
818 // all else failed.
819 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
820 if (ObjCImplementationDecl *IMD =
821 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000822 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000823 return false;
824 }
825 else if (ObjCCategoryImplDecl *CIMD =
826 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000827 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000828 return false;
829 }
830 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000831 // Lastly, look through the implementation (if one is in scope).
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000832 if (ObjCImplementationDecl *ImpDecl
833 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000834 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000835 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000836 // If all fails, look at the super class.
837 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
838 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000839 return true;
840}
841
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000842/// FIXME: Type hierarchies in Objective-C can be deep. We could most
843/// likely improve the efficiency of selector lookups and type
844/// checking by associating with each protocol / interface / category
845/// the flattened instance tables. If we used an immutable set to keep
846/// the table then it wouldn't add significant memory cost and it
847/// would be handy for lookups.
848
Steve Naroffb268d2a2008-02-08 22:06:17 +0000849/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000850/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000851void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
852 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000853 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000854 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000855 const llvm::DenseSet<Selector> &ClsMap,
856 ObjCInterfaceDecl *IDecl) {
857 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
858
859 // If a method lookup fails locally we still need to look and see if
860 // the method was implemented by a base class or an inherited
861 // protocol. This lookup is slow, but occurs rarely in correct code
862 // and otherwise would terminate in a warning.
863
Chris Lattner855e51f2007-12-12 07:09:47 +0000864 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000865 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
866 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000867 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000868 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000869 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000870 (!Super ||
871 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000872 // Ugly, but necessary. Method declared in protcol might have
873 // have been synthesized due to a property declared in the class which
874 // uses the protocol.
875 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000876 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000877 if (!MethodInClass || !MethodInClass->isSynthesized())
878 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
879 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000880 }
881 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000882 for (ObjCProtocolDecl::classmeth_iterator
883 I = PDecl->classmeth_begin(Context),
884 E = PDecl->classmeth_end(Context);
885 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000887 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
888 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000889 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000890 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000891 }
Chris Lattner0be08822008-07-21 21:32:27 +0000892 // Check on this protocols's referenced protocols, recursively.
893 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
894 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000895 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000896}
897
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000898void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
899 ObjCContainerDecl* CDecl,
900 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000901 llvm::DenseSet<Selector> InsMap;
902 // Check and see if instance methods in class interface have been
903 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000904 for (ObjCImplementationDecl::instmeth_iterator
905 I = IMPDecl->instmeth_begin(Context),
906 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000907 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000908
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000909 // Check and see if properties declared in the interface have either 1)
910 // an implementation or 2) there is a @synthesize/@dynamic implementation
911 // of the property in the @implementation.
912 if (isa<ObjCInterfaceDecl>(CDecl))
913 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
914 E = CDecl->prop_end(Context); P != E; ++P) {
915 ObjCPropertyDecl *Prop = (*P);
916 if (Prop->isInvalidDecl())
917 continue;
918 ObjCPropertyImplDecl *PI = 0;
919 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +0000920 for (ObjCImplDecl::propimpl_iterator
921 I = IMPDecl->propimpl_begin(Context),
922 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000923 if ((*I)->getPropertyDecl() == Prop) {
924 PI = (*I);
925 break;
926 }
927 if (PI)
928 continue;
929 if (!InsMap.count(Prop->getGetterName())) {
930 Diag(Prop->getLocation(),
931 diag::warn_setter_getter_impl_required)
932 << Prop->getDeclName() << Prop->getGetterName();
933 Diag(IMPDecl->getLocation(),
934 diag::note_property_impl_required);
935 }
936
937 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
938 Diag(Prop->getLocation(),
939 diag::warn_setter_getter_impl_required)
940 << Prop->getDeclName() << Prop->getSetterName();
941 Diag(IMPDecl->getLocation(),
942 diag::note_property_impl_required);
943 }
944 }
945
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000946 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
947 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000948 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000949 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000950 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000951 }
Chris Lattner28400082009-02-16 19:25:52 +0000952
953 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +0000954 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000955 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000956 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000957 assert(IntfMethodDecl &&
958 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
959 // ImpMethodDecl may be null as in a @dynamic property.
960 if (ImpMethodDecl)
961 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
962 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000963
Chris Lattner855e51f2007-12-12 07:09:47 +0000964 llvm::DenseSet<Selector> ClsMap;
965 // Check and see if class methods in class interface have been
966 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000967 for (ObjCImplementationDecl::classmeth_iterator
968 I = IMPDecl->classmeth_begin(Context),
969 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000970 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000971
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000972 for (ObjCInterfaceDecl::classmeth_iterator
973 I = CDecl->classmeth_begin(Context),
974 E = CDecl->classmeth_end(Context);
975 I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000976 if (!ClsMap.count((*I)->getSelector()))
977 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000978 else {
979 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +0000980 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000981 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000982 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000983 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
984 }
985
Chris Lattner855e51f2007-12-12 07:09:47 +0000986
987 // Check the protocol list for unimplemented methods in the @implementation
988 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000989 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
990 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
991 E = I->protocol_end(); PI != E; ++PI)
992 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
993 InsMap, ClsMap, I);
994 // Check class extensions (unnamed categories)
995 for (ObjCCategoryDecl *Categories = I->getCategoryList();
996 Categories; Categories = Categories->getNextClassCategory()) {
997 if (!Categories->getIdentifier()) {
998 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
999 break;
1000 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +00001001 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001002 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1003 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1004 E = C->protocol_end(); PI != E; ++PI)
1005 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1006 InsMap, ClsMap, C->getClassInterface());
1007 } else
1008 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001009}
1010
1011/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001012Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001013Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001014 IdentifierInfo **IdentList,
1015 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001017
1018 for (unsigned i = 0; i != NumElts; ++i) {
1019 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001020 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001021 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001022 // Maybe we will complain about the shadowed template parameter.
1023 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1024 // Just pretend that we didn't see the previous declaration.
1025 PrevDecl = 0;
1026 }
1027
Ted Kremenek42730c52008-01-07 19:49:32 +00001028 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001029 // GCC apparently allows the following idiom:
1030 //
1031 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1032 // @class XCElementToggler;
1033 //
1034 // FIXME: Make an extension?
1035 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1036 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001037 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001038 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001039 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001041 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001042 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001043 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1044 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +00001045 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +00001046
Steve Naroff74b916a2009-04-23 16:00:56 +00001047 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001048 // Remember that this needs to be removed when the scope is popped.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001049 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +00001050 }
1051
1052 Interfaces.push_back(IDecl);
1053 }
1054
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001055 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001056 &Interfaces[0],
1057 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001058 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001059 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001060 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001061}
1062
1063
1064/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1065/// returns true, or false, accordingly.
1066/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001067bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001068 const ObjCMethodDecl *PrevMethod,
1069 bool matchBasedOnSizeAndAlignment) {
1070 QualType T1 = Context.getCanonicalType(Method->getResultType());
1071 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1072
1073 if (T1 != T2) {
1074 // The result types are different.
1075 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001076 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001077 // Incomplete types don't have a size and alignment.
1078 if (T1->isIncompleteType() || T2->isIncompleteType())
1079 return false;
1080 // Check is based on size and alignment.
1081 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1082 return false;
1083 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001084
1085 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1086 E = Method->param_end();
1087 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1088
1089 for (; ParamI != E; ++ParamI, ++PrevI) {
1090 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1091 T1 = Context.getCanonicalType((*ParamI)->getType());
1092 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001093 if (T1 != T2) {
1094 // The result types are different.
1095 if (!matchBasedOnSizeAndAlignment)
1096 return false;
1097 // Incomplete types don't have a size and alignment.
1098 if (T1->isIncompleteType() || T2->isIncompleteType())
1099 return false;
1100 // Check is based on size and alignment.
1101 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1102 return false;
1103 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001104 }
1105 return true;
1106}
1107
Ted Kremenek42730c52008-01-07 19:49:32 +00001108void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001109 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1110 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001111 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001112 Entry.Method = Method;
1113 Entry.Next = 0;
1114 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001115 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001116
1117 // We've seen a method with this name, see if we have already seen this type
1118 // signature.
1119 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1120 if (MatchTwoMethodDeclarations(Method, List->Method))
1121 return;
1122
1123 // We have a new signature for an existing method - add it.
1124 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1125 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001126}
1127
Steve Naroffec7e0882008-10-21 10:50:19 +00001128// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001129ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1130 SourceRange R) {
1131 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001132 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001133
1134 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001135 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1136 // This checks if the methods differ by size & alignment.
1137 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1138 issueWarning = true;
1139 }
1140 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001141 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001142 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001143 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001144 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001145 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001146 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001147 }
1148 return MethList.Method;
1149}
1150
Ted Kremenek42730c52008-01-07 19:49:32 +00001151void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1152 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001153 if (!FirstMethod.Method) {
1154 // Haven't seen a method with this selector name yet - add it.
1155 FirstMethod.Method = Method;
1156 FirstMethod.Next = 0;
1157 } else {
1158 // We've seen a method with this name, now check the type signature(s).
1159 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1160
Ted Kremenek42730c52008-01-07 19:49:32 +00001161 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001162 Next = Next->Next)
1163 match = MatchTwoMethodDeclarations(Method, Next->Method);
1164
1165 if (!match) {
1166 // We have a new signature for an existing method - add it.
1167 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001168 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001169 FirstMethod.Next = OMI;
1170 }
1171 }
1172}
1173
Steve Naroffab63fd62009-01-08 17:28:14 +00001174/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1175/// have the property type and issue diagnostics if they don't.
1176/// Also synthesize a getter/setter method if none exist (and update the
1177/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1178/// methods is the "right" thing to do.
1179void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1180 ObjCContainerDecl *CD) {
1181 ObjCMethodDecl *GetterMethod, *SetterMethod;
1182
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001183 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1184 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001185
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001186 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001187 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001188 Diag(property->getLocation(),
1189 diag::err_accessor_property_type_mismatch)
1190 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001191 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001192 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1193 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001194
1195 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001196 if (Context.getCanonicalType(SetterMethod->getResultType())
1197 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001198 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001199 if (SetterMethod->param_size() != 1 ||
1200 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001201 Diag(property->getLocation(),
1202 diag::err_accessor_property_type_mismatch)
1203 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001204 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001205 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1206 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001207 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001208
1209 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001210 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001211 // FIXME: The synthesized property we set here is misleading. We
1212 // almost always synthesize these methods unless the user explicitly
1213 // provided prototypes (which is odd, but allowed). Sema should be
1214 // typechecking that the declarations jive in that situation (which
1215 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001216 if (!GetterMethod) {
1217 // No instance method of same name as property getter name was found.
1218 // Declare a getter method and add it to the list of methods
1219 // for this class.
1220 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1221 property->getLocation(), property->getGetterName(),
1222 property->getType(), CD, true, false, true,
1223 (property->getPropertyImplementation() ==
1224 ObjCPropertyDecl::Optional) ?
1225 ObjCMethodDecl::Optional :
1226 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001227 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001228 } else
1229 // A user declared getter will be synthesize when @synthesize of
1230 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001231 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001232 property->setGetterMethodDecl(GetterMethod);
1233
1234 // Skip setter if property is read-only.
1235 if (!property->isReadOnly()) {
1236 // Find the default setter and if one not found, add one.
1237 if (!SetterMethod) {
1238 // No instance method of same name as property setter name was found.
1239 // Declare a setter method and add it to the list of methods
1240 // for this class.
1241 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1242 property->getLocation(),
1243 property->getSetterName(),
1244 Context.VoidTy, CD, true, false, true,
1245 (property->getPropertyImplementation() ==
1246 ObjCPropertyDecl::Optional) ?
1247 ObjCMethodDecl::Optional :
1248 ObjCMethodDecl::Required);
1249 // Invent the arguments for the setter. We don't bother making a
1250 // nice name for the argument.
1251 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001252 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001253 property->getIdentifier(),
1254 property->getType(),
1255 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001256 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001257 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001258 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001259 } else
1260 // A user declared setter will be synthesize when @synthesize of
1261 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001262 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001263 property->setSetterMethodDecl(SetterMethod);
1264 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001265 // Add any synthesized methods to the global pool. This allows us to
1266 // handle the following, which is supported by GCC (and part of the design).
1267 //
1268 // @interface Foo
1269 // @property double bar;
1270 // @end
1271 //
1272 // void thisIsUnfortunate() {
1273 // id foo;
1274 // double bar = [foo bar];
1275 // }
1276 //
Douglas Gregord1675382009-01-09 19:42:16 +00001277 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001278 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001279 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001280 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001281}
1282
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001283// Note: For class/category implemenations, allMethods/allProperties is
1284// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001285void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1286 DeclPtrTy *allMethods, unsigned allNum,
1287 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001288 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001289 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001290
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001291 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1292 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001293 // should be true.
1294 if (!ClassDecl)
1295 return;
1296
Chris Lattner855e51f2007-12-12 07:09:47 +00001297 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001298 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1299 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001300 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001301
Steve Naroffab63fd62009-01-08 17:28:14 +00001302 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001303
1304 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1305 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1306 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1307
Chris Lattner855e51f2007-12-12 07:09:47 +00001308 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001310 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001311
1312 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001313 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001314 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001316 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1317 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001318 if ((isInterfaceDeclKind && PrevMethod && !match)
1319 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001320 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001321 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001322 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001323 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001324 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001325 InsMap[Method->getSelector()] = Method;
1326 /// The following allows us to typecheck messages to "id".
1327 AddInstanceMethodToGlobalPool(Method);
1328 }
1329 }
1330 else {
1331 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001332 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001333 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1334 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001335 if ((isInterfaceDeclKind && PrevMethod && !match)
1336 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001337 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001338 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001339 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001340 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001341 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001342 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001343 /// The following allows us to typecheck messages to "Class".
1344 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001345 }
1346 }
1347 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001348 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001349 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001350 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001351 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001352 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001353 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001354 // Categories are used to extend the class by declaring new methods.
1355 // By the same token, they are also used to add new properties. No
1356 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001357
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001358 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001359 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001360 if (C->getIdentifier() == 0)
1361 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001362 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001363 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1364 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1365 // user-defined setter/getter. It also synthesizes setter/getter methods
1366 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001367 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1368 E = CDecl->prop_end(Context);
1369 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001370 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001371 CDecl->setAtEndLoc(AtEndLoc);
1372 }
1373 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001374 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001375 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001376 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001377 } else if (ObjCCategoryImplDecl* CatImplClass =
1378 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001379 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001380
Chris Lattner855e51f2007-12-12 07:09:47 +00001381 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001382 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001383 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001384 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001385 Categories; Categories = Categories->getNextClassCategory()) {
1386 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001387 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001388 break;
1389 }
1390 }
1391 }
1392 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001393 if (isInterfaceDeclKind) {
1394 // Reject invalid vardecls.
1395 for (unsigned i = 0; i != tuvNum; i++) {
1396 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1397 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1398 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001399 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001400 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001401 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001402 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001403 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001404}
1405
1406
1407/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1408/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001409static Decl::ObjCDeclQualifier
1410CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1411 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1412 if (PQTVal & ObjCDeclSpec::DQ_In)
1413 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1414 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1415 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1416 if (PQTVal & ObjCDeclSpec::DQ_Out)
1417 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1418 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1419 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1420 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1421 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1422 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1423 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001424
1425 return ret;
1426}
1427
Chris Lattner5261d0c2009-03-28 19:18:32 +00001428Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001429 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001430 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001431 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001432 Selector Sel,
1433 // optional arguments. The number of types/arguments is obtained
1434 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001435 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001436 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001437 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1438 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001439 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001440
1441 // Make sure we can establish a context for the method.
1442 if (!ClassDecl) {
1443 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001444 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001445 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001446 QualType resultDeclType;
1447
Steve Naroffa442ad92009-02-20 22:59:16 +00001448 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001449 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001450
1451 // Methods cannot return interface types. All ObjC objects are
1452 // passed by reference.
1453 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001454 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1455 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001456 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001457 }
1458 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001459 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001460
Chris Lattner114add62008-03-16 00:49:28 +00001461 ObjCMethodDecl* ObjCMethod =
1462 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001463 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001464 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001465 false,
Chris Lattner114add62008-03-16 00:49:28 +00001466 MethodDeclKind == tok::objc_optional ?
1467 ObjCMethodDecl::Optional :
1468 ObjCMethodDecl::Required);
1469
Chris Lattnereee57c02008-04-04 06:12:32 +00001470 llvm::SmallVector<ParmVarDecl*, 16> Params;
1471
Chris Lattner4f0496b2009-04-11 19:42:43 +00001472 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001473 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001474
Chris Lattner612a2f72009-04-11 18:57:04 +00001475 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001476 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001477 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001478 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001479 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001480 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001481 }
1482
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001483 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001484 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001485 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001486 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001487 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001488 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001489 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001490 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001491 ArgInfo[i].Name, ArgType,
1492 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001493 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001494
Chris Lattner761b72f2009-04-11 19:34:56 +00001495 if (ArgType->isObjCInterfaceType()) {
1496 Diag(ArgInfo[i].NameLoc,
1497 diag::err_object_cannot_be_passed_returned_by_value)
1498 << 1 << ArgType;
1499 Param->setInvalidDecl();
1500 }
1501
Chris Lattnereee57c02008-04-04 06:12:32 +00001502 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001503 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001504
1505 // Apply the attributes to the parameter.
1506 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1507
Chris Lattnereee57c02008-04-04 06:12:32 +00001508 Params.push_back(Param);
1509 }
1510
Steve Naroff79ea0e02009-04-20 15:06:07 +00001511 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001512 ObjCMethod->setObjCDeclQualifier(
1513 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1514 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001515
1516 if (AttrList)
1517 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001518
1519 // For implementations (which can be very "coarse grain"), we add the
1520 // method now. This allows the AST to implement lookup methods that work
1521 // incrementally (without waiting until we parse the @end). It also allows
1522 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001523 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001524 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001525 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001526 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1527 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001528 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001529 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1530 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001531 }
1532 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001533 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001534 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001535 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001536 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1537 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001538 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001539 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1540 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001541 }
1542 }
1543 if (PrevMethod) {
1544 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001545 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001546 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001547 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001548 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001549 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001550}
1551
Daniel Dunbar540ff472008-09-23 21:53:23 +00001552void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1553 SourceLocation Loc,
1554 unsigned &Attributes) {
1555 // FIXME: Improve the reported location.
1556
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001557 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001558 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001559 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1560 ObjCDeclSpec::DQ_PR_assign |
1561 ObjCDeclSpec::DQ_PR_copy |
1562 ObjCDeclSpec::DQ_PR_retain))) {
1563 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1564 "readwrite" :
1565 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1566 "assign" :
1567 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1568 "copy" : "retain";
1569
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001570 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001571 diag::err_objc_property_attr_mutually_exclusive :
1572 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001573 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001574 }
1575
1576 // Check for copy or retain on non-object types.
1577 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1578 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001579 Diag(Loc, diag::err_objc_property_requires_object)
1580 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001581 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1582 }
1583
1584 // Check for more than one of { assign, copy, retain }.
1585 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1586 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001587 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1588 << "assign" << "copy";
1589 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001590 }
1591 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001592 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1593 << "assign" << "retain";
1594 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001595 }
1596 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1597 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001598 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1599 << "copy" << "retain";
1600 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001601 }
1602 }
1603
1604 // Warn if user supplied no assignment attribute, property is
1605 // readwrite, and this is an object type.
1606 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1607 ObjCDeclSpec::DQ_PR_retain)) &&
1608 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1609 Context.isObjCObjectPointerType(PropertyTy)) {
1610 // Skip this warning in gc-only mode.
1611 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1612 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1613
1614 // If non-gc code warn that this is likely inappropriate.
1615 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1616 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1617
1618 // FIXME: Implement warning dependent on NSCopying being
1619 // implemented. See also:
1620 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1621 // (please trim this list while you are at it).
1622 }
1623}
1624
Chris Lattner5261d0c2009-03-28 19:18:32 +00001625Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1626 FieldDeclarator &FD,
1627 ObjCDeclSpec &ODS,
1628 Selector GetterSel,
1629 Selector SetterSel,
1630 DeclPtrTy ClassCategory,
1631 bool *isOverridingProperty,
1632 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001633 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001634 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1635 // default is readwrite!
1636 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1637 // property is defaulted to 'assign' if it is readwrite and is
1638 // not retain or copy
1639 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1640 (isReadWrite &&
1641 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1642 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1643 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001644 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001645 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001646 // May modify Attributes.
1647 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001648 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1649 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001650 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001651 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001652 if ((CCPrimary = CDecl->getClassInterface())) {
1653 // Find the property in continuation class's primary class only.
1654 ObjCPropertyDecl *PIDecl = 0;
1655 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001656 for (ObjCInterfaceDecl::prop_iterator
1657 I = CCPrimary->prop_begin(Context),
1658 E = CCPrimary->prop_end(Context);
1659 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001660 if ((*I)->getIdentifier() == PropertyId) {
1661 PIDecl = *I;
1662 break;
1663 }
1664
1665 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001666 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001667 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001668 unsigned PIkind = PIDecl->getPropertyAttributes();
1669 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001670 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001671 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1672 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001673 PIDecl->makeitReadWriteAttribute();
1674 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1675 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1676 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1677 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1678 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001679 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001680 else
1681 Diag(AtLoc, diag::err_use_continuation_class)
1682 << CCPrimary->getDeclName();
1683 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001684 // Make sure setter decl is synthesized, and added to primary
1685 // class's list.
1686 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001687 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001688 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001689 // No matching property found in the primary class. Just fall thru
1690 // and add property to continuation class's primary class.
1691 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001692 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001693 Diag(CDecl->getLocation(), diag::err_continuation_class);
1694 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001695 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001696 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001697 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001698
Steve Naroffdcf1e842009-01-11 12:47:58 +00001699 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1700 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001701 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1702 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001703 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001704 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001705
Chris Lattner6e48be22009-04-11 20:14:49 +00001706 if (T->isArrayType() || T->isFunctionType()) {
1707 Diag(AtLoc, diag::err_property_type) << T;
1708 PDecl->setInvalidDecl();
1709 }
1710
Chris Lattner22063402009-02-16 18:32:47 +00001711 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001712
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001713 // Regardless of setter/getter attribute, we save the default getter/setter
1714 // selector names in anticipation of declaration of setter/getter methods.
1715 PDecl->setGetterName(GetterSel);
1716 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001717
Daniel Dunbar540ff472008-09-23 21:53:23 +00001718 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001719 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001720
Daniel Dunbar540ff472008-09-23 21:53:23 +00001721 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001722 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001723
Daniel Dunbar540ff472008-09-23 21:53:23 +00001724 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001725 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001726
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001727 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001728 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001729
Daniel Dunbar540ff472008-09-23 21:53:23 +00001730 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001731 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001732
Daniel Dunbar540ff472008-09-23 21:53:23 +00001733 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001734 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001735
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001736 if (isAssign)
1737 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1738
Daniel Dunbar540ff472008-09-23 21:53:23 +00001739 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001740 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001741
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001742 if (MethodImplKind == tok::objc_required)
1743 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1744 else if (MethodImplKind == tok::objc_optional)
1745 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001746 // A case of continuation class adding a new property in the class. This
1747 // is not what it was meant for. However, gcc supports it and so should we.
1748 // Make sure setter/getters are declared here.
1749 if (CCPrimary)
1750 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001751
Chris Lattner5261d0c2009-03-28 19:18:32 +00001752 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001753}
1754
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001755/// ActOnPropertyImplDecl - This routine performs semantic checks and
1756/// builds the AST node for a property implementation declaration; declared
1757/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001758///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001759Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1760 SourceLocation PropertyLoc,
1761 bool Synthesize,
1762 DeclPtrTy ClassCatImpDecl,
1763 IdentifierInfo *PropertyId,
1764 IdentifierInfo *PropertyIvar) {
1765 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001766 // Make sure we have a context for the property implementation declaration.
1767 if (!ClassImpDecl) {
1768 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001769 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001770 }
1771 ObjCPropertyDecl *property = 0;
1772 ObjCInterfaceDecl* IDecl = 0;
1773 // Find the class or category class where this property must have
1774 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001775 ObjCImplementationDecl *IC = 0;
1776 ObjCCategoryImplDecl* CatImplClass = 0;
1777 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001778 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001779 // We always synthesize an interface for an implementation
1780 // without an interface decl. So, IDecl is always non-zero.
1781 assert(IDecl &&
1782 "ActOnPropertyImplDecl - @implementation without @interface");
1783
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001784 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001785 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001786 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001787 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001788 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001789 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001790 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001791 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001792 if (Synthesize) {
1793 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001794 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001795 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001796 IDecl = CatImplClass->getClassInterface();
1797 if (!IDecl) {
1798 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001799 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001800 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001801 ObjCCategoryDecl *Category =
1802 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1803
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001804 // If category for this implementation not found, it is an error which
1805 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001806 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001807 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001808 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001809 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001810 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001811 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001812 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001813 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001814 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001815 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001816 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001817 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001818 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001819 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001820 // Check that we have a valid, previously declared ivar for @synthesize
1821 if (Synthesize) {
1822 // @synthesize
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001823 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001824 if (!PropertyIvar)
1825 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001826 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001827 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001828 ObjCInterfaceDecl *ClassDeclared;
1829 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001830 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001831 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1832 PropertyIvar, PropType,
1833 ObjCIvarDecl::Public,
1834 (Expr *)0);
1835 property->setPropertyIvarDecl(Ivar);
1836 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001837 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001838 // Note! I deliberately want it to fall thru so, we have a
1839 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001840 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001841 else if (getLangOptions().ObjCNonFragileABI &&
1842 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1843 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1844 << property->getDeclName() << Ivar->getDeclName()
1845 << ClassDeclared->getDeclName();
1846 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1847 << Ivar << Ivar->getNameAsCString();
1848 // Note! I deliberately want it to fall thru so more errors are caught.
1849 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001850 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1851
Steve Naroff631e3922008-09-30 00:24:17 +00001852 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001853 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001854 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001855 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001856 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001857 // Note! I deliberately want it to fall thru so, we have a
1858 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001859 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001860
1861 // FIXME! Rules for properties are somewhat different that those
1862 // for assignments. Use a new routine to consolidate all cases;
1863 // specifically for property redeclarations as well as for ivars.
1864 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1865 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1866 if (lhsType != rhsType &&
1867 lhsType->isArithmeticType()) {
1868 Diag(PropertyLoc, diag::error_property_ivar_type)
1869 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001870 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001871 }
1872 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00001873 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1874 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001875 Diag(PropertyLoc, diag::error_weak_property)
1876 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001877 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001878 }
1879 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00001880 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1881 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001882 Diag(PropertyLoc, diag::error_strong_property)
1883 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001884 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001885 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001886 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001887 } else if (PropertyIvar)
1888 // @dynamic
1889 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001890 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001891 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001892 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1893 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001894 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001895 ObjCPropertyImplDecl::Synthesize
1896 : ObjCPropertyImplDecl::Dynamic),
1897 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001898 if (IC) {
1899 if (Synthesize)
1900 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001901 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001902 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1903 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1904 << PropertyIvar;
1905 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1906 }
1907
Douglas Gregorcd19b572009-04-23 01:02:12 +00001908 if (ObjCPropertyImplDecl *PPIDecl
1909 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001910 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1911 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001912 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001913 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001914 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001915 }
1916 else {
1917 if (Synthesize)
1918 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001919 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001920 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1921 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1922 << PropertyIvar;
1923 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1924 }
1925
1926 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00001927 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00001928 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1929 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001930 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00001931 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00001932 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001933 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001934
Chris Lattner5261d0c2009-03-28 19:18:32 +00001935 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001936}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001937
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001938bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001939 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001940 return false;
1941
1942 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1943 D->setInvalidDecl();
1944
1945 return true;
1946}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001947
1948/// Collect the instance variables declared in an Objective-C object. Used in
1949/// the creation of structures from objects using the @defs directive.
1950/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1951/// part of the AST generation logic of @defs.
1952static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1953 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001954 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001955 if (Class->getSuperClass())
1956 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1957
1958 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001959 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1960 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001961 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001962 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1963 ID->getIdentifier(), ID->getType(),
1964 ID->getBitWidth());
1965 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001966 }
1967}
1968
1969/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1970/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001971void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001972 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001973 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001974 // Check that ClassName is a valid class
1975 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1976 if (!Class) {
1977 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1978 return;
1979 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00001980 if (LangOpts.ObjCNonFragileABI) {
1981 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1982 return;
1983 }
1984
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001985 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00001986 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001987
1988 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001989 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001990 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001991 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001992 if (getLangOptions().CPlusPlus)
1993 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001994 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001995 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001996 }
1997}
1998