blob: 3c544ed377c9cda65ed9db29823b15e6bbfe4911 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner4d391482007-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 Naroffca331292009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroffebf64432009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
Chris Lattner38c5ebd2009-04-19 05:21:20 +000031 CurFunctionNeedsScopeChecking = false;
32
Steve Naroffa56f6162007-12-18 01:30:32 +000033 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000038
39 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-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 Lattner4d391482007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000047
Daniel Dunbar451318c2008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000050
Chris Lattner8123a952008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-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 Lattner4d391482007-12-12 07:09:47 +000056}
57
Chris Lattnerb28317a2009-03-28 19:18:32 +000058Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000059ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000062 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-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 Kremeneka526c5c2008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000078 }
79
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000090 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000091 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Steve Naroffa7503a72009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000104 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000105 }
106
107 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
Steve Naroff818cb9e2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000113
114 // Diagnose classes that inherit from deprecated classes.
115 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000116 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000117
Steve Naroff818cb9e2009-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 Lattnerc7984dd2009-02-16 21:33:09 +0000128
Steve Naroff818cb9e2009-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 Lattnerc7984dd2009-02-16 21:33:09 +0000139
Steve Naroff818cb9e2009-02-04 17:14:05 +0000140 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
141 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000146 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000147 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000148 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000149 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000150 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000151 IDecl->setLocEnd(SuperLoc);
152 } else { // we have a root class.
153 IDecl->setLocEnd(ClassLoc);
154 }
155
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000156 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000157 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000158 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
159 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 IDecl->setLocEnd(EndProtoLoc);
161 }
Anders Carlsson15281452008-11-04 16:57:32 +0000162
163 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000164 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000165}
166
167/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000168/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000169Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
170 IdentifierInfo *AliasName,
171 SourceLocation AliasLocation,
172 IdentifierInfo *ClassName,
173 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000174 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000175 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000176 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000178 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000179 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000180 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000181 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000182 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000183 }
184 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000185 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-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 Gregor4c921ae2009-01-30 01:04:22 +0000191 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000192 }
193 }
194 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000195 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
196 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000197 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000198 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000199 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000200 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000201 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000202
203 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000205 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000206
207 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000208
Steve Naroff04c5f4f2009-04-23 17:46:47 +0000209 // FIXME: PushOnScopeChains?
210 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000211 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregord0434102009-01-09 00:49:46 +0000213
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000215}
216
Steve Naroff61d68522009-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
225 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
226 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 Lattnerb28317a2009-03-28 19:18:32 +0000236Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000237Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
238 IdentifierInfo *ProtocolName,
239 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000240 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000241 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000242 SourceLocation EndProtoLoc,
243 AttributeList *AttrList) {
244 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000245 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000247 if (PDecl) {
248 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000249 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000250 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000251 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000252 // Just return the protocol we already had.
253 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000254 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000255 }
Steve Naroff61d68522009-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 Narofff11b5082008-08-13 16:39:22 +0000262 // Make sure the cached decl gets a valid start location.
263 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000264 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000266 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
267 AtProtoInterfaceLoc,ProtocolName);
268 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000269 CurContext->addDecl(Context, PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000270 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000271 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000272 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000273 if (AttrList)
274 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000275 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000276 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000277 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 PDecl->setLocEnd(EndProtoLoc);
279 }
Anders Carlsson15281452008-11-04 16:57:32 +0000280
281 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000282 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000283}
284
285/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000286/// issues an error if they are not declared. It returns list of
287/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000288void
Chris Lattnere13b9592008-07-26 04:03:38 +0000289Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000290 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000291 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000292 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000293 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000294 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
295 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000296 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000297 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000298 continue;
299 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000300
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000301 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000302
303 // If this is a forward declaration and we are supposed to warn in this
304 // case, do it.
305 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000306 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000307 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000309 }
310}
311
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000313/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000314///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000315void
316Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
317 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 ObjCPropertyDecl::PropertyAttributeKind CAttr =
320 Property->getPropertyAttributes();
321 ObjCPropertyDecl::PropertyAttributeKind SAttr =
322 SuperProperty->getPropertyAttributes();
323 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
324 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
328 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000329 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000330 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
332 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335
336 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
337 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000338 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000339 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000341 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000344 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000345 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000346
347 QualType LHSType =
348 Context.getCanonicalType(SuperProperty->getType());
349 QualType RHSType =
350 Context.getCanonicalType(Property->getType());
351
352 if (!Context.typesAreCompatible(LHSType, RHSType)) {
353 // FIXME: Incorporate this test with typesAreCompatible.
354 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
355 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
356 return;
357 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
358 << Property->getType() << SuperProperty->getType() << inheritedName;
359 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360}
361
362/// ComparePropertiesInBaseAndSuper - This routine compares property
363/// declarations in base and its super class, if any, and issues
364/// diagnostics in a variety of inconsistant situations.
365///
Chris Lattner70f19542009-02-16 21:26:43 +0000366void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
368 if (!SDecl)
369 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000370 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000371 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
372 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000373 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000374 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000375 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
376 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377 ObjCPropertyDecl *PDecl = (*I);
378 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000379 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000381 }
382 }
383}
384
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
386/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000387/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000388void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000390 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
392 if (!IDecl) {
393 // Category
394 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
395 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000396 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
397 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000399 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000401 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402 CP != CE; ++CP)
403 if ((*CP)->getIdentifier() == Pr->getIdentifier())
404 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000405 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000406 // Property protocol already exist in class. Diagnose any mismatch.
407 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
408 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000409 return;
410 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000411 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
412 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000414 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000416 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000417 CP != CE; ++CP)
418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424}
425
426/// MergeProtocolPropertiesIntoClass - This routine merges properties
427/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000428/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429///
Chris Lattner70f19542009-02-16 21:26:43 +0000430void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000431 DeclPtrTy MergeItsProtocols) {
432 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000433 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
434
435 if (!IDecl) {
436 // Category
437 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
438 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
439 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
440 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
441 E = MDecl->protocol_end(); P != E; ++P)
442 // Merge properties of category (*P) into IDECL's
443 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
444
445 // Go thru the list of protocols for this category and recursively merge
446 // their properties into this class as well.
447 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
448 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000449 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000450 } else {
451 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
452 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
453 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000454 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000455 }
456 return;
457 }
458
Chris Lattnerb752f282008-07-21 07:06:49 +0000459 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000460 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
461 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000463 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
464
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 // Go thru the list of protocols for this class and recursively merge
466 // their properties into this class as well.
467 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
468 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000469 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000470 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000471 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
472 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
473 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000474 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000475 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476}
477
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000478/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000479/// a class method in its extension.
480///
481void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
482 ObjCInterfaceDecl *ID) {
483 if (!ID)
484 return; // Possibly due to previous error
485
486 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000487 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
488 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000489 ObjCMethodDecl *MD = *i;
490 MethodMap[MD->getSelector()] = MD;
491 }
492
493 if (MethodMap.empty())
494 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000495 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
496 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000497 ObjCMethodDecl *Method = *i;
498 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
499 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
500 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
501 << Method->getDeclName();
502 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
503 }
504 }
505}
506
Chris Lattner58fe03b2009-04-12 08:43:13 +0000507/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000508Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000509Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000510 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000511 unsigned NumElts,
512 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000514
515 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000516 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000517 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000518 if (PDecl == 0) { // Not already seen?
519 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
520 IdentList[i].second, Ident);
521 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000522 CurContext->addDecl(Context, PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000523 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524 if (attrList)
525 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 Protocols.push_back(PDecl);
527 }
Anders Carlsson15281452008-11-04 16:57:32 +0000528
529 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000530 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000531 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000532 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000533 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000534 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Chris Lattnerb28317a2009-03-28 19:18:32 +0000537Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000538ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
539 IdentifierInfo *ClassName, SourceLocation ClassLoc,
540 IdentifierInfo *CategoryName,
541 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000542 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000543 unsigned NumProtoRefs,
544 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000545 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000546 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
547 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000548 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000549
550 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000551 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000552 if (!IDecl || IDecl->isForwardDecl()) {
553 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000555 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000556 }
Chris Lattner4d391482007-12-12 07:09:47 +0000557
Chris Lattner70f19542009-02-16 21:26:43 +0000558 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000559
560 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000561 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000562
563 /// Check for duplicate interface declaration for this category
564 ObjCCategoryDecl *CDeclChain;
565 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
566 CDeclChain = CDeclChain->getNextClassCategory()) {
567 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
568 Diag(CategoryLoc, diag::warn_dup_category_def)
569 << ClassName << CategoryName;
570 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
571 break;
572 }
573 }
574 if (!CDeclChain)
575 CDecl->insertNextClassCategory();
576
Chris Lattner4d391482007-12-12 07:09:47 +0000577 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000578 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000579 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Anders Carlsson15281452008-11-04 16:57:32 +0000581
582 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000583 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000584}
585
586/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000588/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000589Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000590 SourceLocation AtCatImplLoc,
591 IdentifierInfo *ClassName, SourceLocation ClassLoc,
592 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000594 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000595 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
596 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 /// Check that class of this category is already completely declared.
598 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000600
Douglas Gregord0434102009-01-09 00:49:46 +0000601 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000602 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000603
Chris Lattner4d391482007-12-12 07:09:47 +0000604 /// TODO: Check that CatName, category name, is not used in another
605 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000606 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000607
608 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000610}
611
Chris Lattnerb28317a2009-03-28 19:18:32 +0000612Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000613 SourceLocation AtClassImplLoc,
614 IdentifierInfo *ClassName, SourceLocation ClassLoc,
615 IdentifierInfo *SuperClassname,
616 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000618 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000619 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000622 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000623 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000624 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000626 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000627 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000628 }
629
630 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000631 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000632 if (SuperClassname) {
633 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000634 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
637 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000638 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000641 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000642 Diag(SuperClassLoc, diag::err_undef_superclass)
643 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000644 else if (IDecl && IDecl->getSuperClass() != SDecl) {
645 // This implementation and its interface do not have the same
646 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000647 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000648 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000649 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000650 }
651 }
652 }
653
654 if (!IDecl) {
655 // Legacy case of @implementation with no corresponding @interface.
656 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000657
658 // FIXME: Do we support attributes on the @implementation? If so
659 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000660 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
661 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000662 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000663 IDecl->setSuperClass(SDecl);
664 IDecl->setLocEnd(ClassLoc);
665
Douglas Gregord0434102009-01-09 00:49:46 +0000666 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000667 CurContext->addDecl(Context, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000669 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Daniel Dunbar24c89912009-04-21 21:41:56 +0000670 } else {
671 // Mark the interface as being completed, even if it was just as
672 // @class ....;
673 // declaration; the user cannot reopen it.
674 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000675 }
676
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000678 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000679 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000680
Douglas Gregord0434102009-01-09 00:49:46 +0000681 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000682 CurContext->addDecl(Context, IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000683
Anders Carlsson15281452008-11-04 16:57:32 +0000684 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000685 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000686
Chris Lattner4d391482007-12-12 07:09:47 +0000687 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000688 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000689 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000691 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000693 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000694}
695
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
697 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000698 SourceLocation RBrace) {
699 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000700 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000701 if (!IDecl)
702 return;
703 /// Check case of non-existing @interface decl.
704 /// (legacy objective-c @implementation decl without an @interface decl).
705 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000706 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000707 IDecl->setIVarList(ivars, numIvars, Context);
708 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000709 return;
710 }
711 // If implementation has empty ivar list, just return.
712 if (numIvars == 0)
713 return;
714
715 assert(ivars && "missing @implementation ivars");
716
717 // Check interface's Ivar list against those in the implementation.
718 // names and types must match.
719 //
Chris Lattner4d391482007-12-12 07:09:47 +0000720 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000722 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
723 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724 ObjCIvarDecl* ImplIvar = ivars[j++];
725 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000726 assert (ImplIvar && "missing implementation ivar");
727 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000728
729 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000730 if (Context.getCanonicalType(ImplIvar->getType()) !=
731 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000732 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000733 << ImplIvar->getIdentifier()
734 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000735 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000736 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
737 Expr *ImplBitWidth = ImplIvar->getBitWidth();
738 Expr *ClsBitWidth = ClsIvar->getBitWidth();
739 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
740 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
741 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
742 << ImplIvar->getIdentifier();
743 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
744 }
745 }
746 // Make sure the names are identical.
747 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000748 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000749 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000750 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000751 }
752 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000753 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000754
755 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000756 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000757 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000758 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000759}
760
Steve Naroff3c2eb662008-02-10 21:38:56 +0000761void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
762 bool &IncompleteImpl) {
763 if (!IncompleteImpl) {
764 Diag(ImpLoc, diag::warn_incomplete_impl);
765 IncompleteImpl = true;
766 }
Chris Lattner08631c52008-11-23 21:45:46 +0000767 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000768}
769
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000770void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
771 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000772 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000773 ImpMethodDecl->getResultType())) {
774 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
775 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
776 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000777 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
778 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000779
780 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
781 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
782 IM != EM; ++IM, ++IF) {
783 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
784 continue;
785
786 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
787 << ImpMethodDecl->getDeclName() << (*IF)->getType()
788 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000789 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000790 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000791}
792
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000793/// isPropertyReadonly - Return true if property is readonly, by searching
794/// for the property in the class and in its categories and implementations
795///
796bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000797 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000798 // by far the most common case.
799 if (!PDecl->isReadOnly())
800 return false;
801 // Even if property is ready only, if interface has a user defined setter,
802 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000803 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000804 return false;
805
806 // Main class has the property as 'readonly'. Must search
807 // through the category list to see if the property's
808 // attribute has been over-ridden to 'readwrite'.
809 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
810 Category; Category = Category->getNextClassCategory()) {
811 // Even if property is ready only, if a category has a user defined setter,
812 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000813 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000814 return false;
815 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000816 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000817 if (P && !P->isReadOnly())
818 return false;
819 }
820
821 // Also, check for definition of a setter method in the implementation if
822 // all else failed.
823 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
824 if (ObjCImplementationDecl *IMD =
825 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000826 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000827 return false;
828 }
829 else if (ObjCCategoryImplDecl *CIMD =
830 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000831 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000832 return false;
833 }
834 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000835 // Lastly, look through the implementation (if one is in scope).
836 if (ObjCImplementationDecl *ImpDecl =
837 ObjCImplementations[IDecl->getIdentifier()])
Douglas Gregor653f1b12009-04-23 01:02:12 +0000838 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000839 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000840 // If all fails, look at the super class.
841 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
842 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000843 return true;
844}
845
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000846/// FIXME: Type hierarchies in Objective-C can be deep. We could most
847/// likely improve the efficiency of selector lookups and type
848/// checking by associating with each protocol / interface / category
849/// the flattened instance tables. If we used an immutable set to keep
850/// the table then it wouldn't add significant memory cost and it
851/// would be handy for lookups.
852
Steve Naroffefe7f362008-02-08 22:06:17 +0000853/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000854/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000855void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
856 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000857 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000858 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000859 const llvm::DenseSet<Selector> &ClsMap,
860 ObjCInterfaceDecl *IDecl) {
861 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
862
863 // If a method lookup fails locally we still need to look and see if
864 // the method was implemented by a base class or an inherited
865 // protocol. This lookup is slow, but occurs rarely in correct code
866 // and otherwise would terminate in a warning.
867
Chris Lattner4d391482007-12-12 07:09:47 +0000868 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000869 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
870 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000872 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000873 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000874 (!Super ||
875 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000876 // Ugly, but necessary. Method declared in protcol might have
877 // have been synthesized due to a property declared in the class which
878 // uses the protocol.
879 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000880 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000881 if (!MethodInClass || !MethodInClass->isSynthesized())
882 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
883 }
Chris Lattner4d391482007-12-12 07:09:47 +0000884 }
885 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000886 for (ObjCProtocolDecl::classmeth_iterator
887 I = PDecl->classmeth_begin(Context),
888 E = PDecl->classmeth_end(Context);
889 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000890 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000891 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
892 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000893 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000894 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000895 }
Chris Lattner780f3292008-07-21 21:32:27 +0000896 // Check on this protocols's referenced protocols, recursively.
897 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
898 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000899 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000900}
901
Chris Lattnercddc8882009-03-01 00:56:52 +0000902void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
903 ObjCContainerDecl* CDecl,
904 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000905 llvm::DenseSet<Selector> InsMap;
906 // Check and see if instance methods in class interface have been
907 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000908 for (ObjCImplementationDecl::instmeth_iterator
909 I = IMPDecl->instmeth_begin(Context),
910 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000911 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000912
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000913 // Check and see if properties declared in the interface have either 1)
914 // an implementation or 2) there is a @synthesize/@dynamic implementation
915 // of the property in the @implementation.
916 if (isa<ObjCInterfaceDecl>(CDecl))
917 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
918 E = CDecl->prop_end(Context); P != E; ++P) {
919 ObjCPropertyDecl *Prop = (*P);
920 if (Prop->isInvalidDecl())
921 continue;
922 ObjCPropertyImplDecl *PI = 0;
923 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000924 for (ObjCImplDecl::propimpl_iterator
925 I = IMPDecl->propimpl_begin(Context),
926 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000927 if ((*I)->getPropertyDecl() == Prop) {
928 PI = (*I);
929 break;
930 }
931 if (PI)
932 continue;
933 if (!InsMap.count(Prop->getGetterName())) {
934 Diag(Prop->getLocation(),
935 diag::warn_setter_getter_impl_required)
936 << Prop->getDeclName() << Prop->getGetterName();
937 Diag(IMPDecl->getLocation(),
938 diag::note_property_impl_required);
939 }
940
941 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
942 Diag(Prop->getLocation(),
943 diag::warn_setter_getter_impl_required)
944 << Prop->getDeclName() << Prop->getSetterName();
945 Diag(IMPDecl->getLocation(),
946 diag::note_property_impl_required);
947 }
948 }
949
Douglas Gregor6ab35242009-04-09 21:40:53 +0000950 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
951 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000952 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000953 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000954 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000955 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000956
957 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000958 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000959 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000960 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000961 assert(IntfMethodDecl &&
962 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
963 // ImpMethodDecl may be null as in a @dynamic property.
964 if (ImpMethodDecl)
965 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
966 }
Chris Lattner4c525092007-12-12 17:58:05 +0000967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 llvm::DenseSet<Selector> ClsMap;
969 // Check and see if class methods in class interface have been
970 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000971 for (ObjCImplementationDecl::classmeth_iterator
972 I = IMPDecl->classmeth_begin(Context),
973 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000974 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000975
Douglas Gregor6ab35242009-04-09 21:40:53 +0000976 for (ObjCInterfaceDecl::classmeth_iterator
977 I = CDecl->classmeth_begin(Context),
978 E = CDecl->classmeth_end(Context);
979 I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000980 if (!ClsMap.count((*I)->getSelector()))
981 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000982 else {
983 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000984 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000985 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000986 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000987 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
988 }
989
Chris Lattner4d391482007-12-12 07:09:47 +0000990
991 // Check the protocol list for unimplemented methods in the @implementation
992 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000993 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
994 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
995 E = I->protocol_end(); PI != E; ++PI)
996 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
997 InsMap, ClsMap, I);
998 // Check class extensions (unnamed categories)
999 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1000 Categories; Categories = Categories->getNextClassCategory()) {
1001 if (!Categories->getIdentifier()) {
1002 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1003 break;
1004 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001005 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001006 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1007 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1008 E = C->protocol_end(); PI != E; ++PI)
1009 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1010 InsMap, ClsMap, C->getClassInterface());
1011 } else
1012 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001013}
1014
1015/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001016Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001017Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001018 IdentifierInfo **IdentList,
1019 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001020 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001021
1022 for (unsigned i = 0; i != NumElts; ++i) {
1023 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001024 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001025 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001026 // Maybe we will complain about the shadowed template parameter.
1027 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1028 // Just pretend that we didn't see the previous declaration.
1029 PrevDecl = 0;
1030 }
1031
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001033 // GCC apparently allows the following idiom:
1034 //
1035 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1036 // @class XCElementToggler;
1037 //
1038 // FIXME: Make an extension?
1039 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1040 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001041 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001042 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001043 }
Chris Lattner4d391482007-12-12 07:09:47 +00001044 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001045 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001046 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001047 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1048 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +00001049 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001050
Steve Naroff8f06f842009-04-23 16:00:56 +00001051 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001052 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001053 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +00001054 }
1055
1056 Interfaces.push_back(IDecl);
1057 }
1058
Douglas Gregord0434102009-01-09 00:49:46 +00001059 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001060 &Interfaces[0],
1061 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001062 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001063 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001064 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001065}
1066
1067
1068/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1069/// returns true, or false, accordingly.
1070/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001071bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001072 const ObjCMethodDecl *PrevMethod,
1073 bool matchBasedOnSizeAndAlignment) {
1074 QualType T1 = Context.getCanonicalType(Method->getResultType());
1075 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1076
1077 if (T1 != T2) {
1078 // The result types are different.
1079 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001080 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001081 // Incomplete types don't have a size and alignment.
1082 if (T1->isIncompleteType() || T2->isIncompleteType())
1083 return false;
1084 // Check is based on size and alignment.
1085 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1086 return false;
1087 }
Chris Lattner89951a82009-02-20 18:43:26 +00001088
1089 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1090 E = Method->param_end();
1091 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1092
1093 for (; ParamI != E; ++ParamI, ++PrevI) {
1094 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1095 T1 = Context.getCanonicalType((*ParamI)->getType());
1096 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001097 if (T1 != T2) {
1098 // The result types are different.
1099 if (!matchBasedOnSizeAndAlignment)
1100 return false;
1101 // Incomplete types don't have a size and alignment.
1102 if (T1->isIncompleteType() || T2->isIncompleteType())
1103 return false;
1104 // Check is based on size and alignment.
1105 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1106 return false;
1107 }
Chris Lattner4d391482007-12-12 07:09:47 +00001108 }
1109 return true;
1110}
1111
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001112void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001113 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1114 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001115 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001116 Entry.Method = Method;
1117 Entry.Next = 0;
1118 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001119 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001120
1121 // We've seen a method with this name, see if we have already seen this type
1122 // signature.
1123 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1124 if (MatchTwoMethodDeclarations(Method, List->Method))
1125 return;
1126
1127 // We have a new signature for an existing method - add it.
1128 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1129 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001130}
1131
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001132// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001133ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1134 SourceRange R) {
1135 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001136 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001137
1138 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001139 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1140 // This checks if the methods differ by size & alignment.
1141 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1142 issueWarning = true;
1143 }
1144 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001145 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001146 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001147 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001148 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001149 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001150 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001151 }
1152 return MethList.Method;
1153}
1154
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001155void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1156 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001157 if (!FirstMethod.Method) {
1158 // Haven't seen a method with this selector name yet - add it.
1159 FirstMethod.Method = Method;
1160 FirstMethod.Next = 0;
1161 } else {
1162 // We've seen a method with this name, now check the type signature(s).
1163 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1164
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001165 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001166 Next = Next->Next)
1167 match = MatchTwoMethodDeclarations(Method, Next->Method);
1168
1169 if (!match) {
1170 // We have a new signature for an existing method - add it.
1171 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001172 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001173 FirstMethod.Next = OMI;
1174 }
1175 }
1176}
1177
Steve Naroff0701bbb2009-01-08 17:28:14 +00001178/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1179/// have the property type and issue diagnostics if they don't.
1180/// Also synthesize a getter/setter method if none exist (and update the
1181/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1182/// methods is the "right" thing to do.
1183void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1184 ObjCContainerDecl *CD) {
1185 ObjCMethodDecl *GetterMethod, *SetterMethod;
1186
Douglas Gregor6ab35242009-04-09 21:40:53 +00001187 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1188 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001189
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001190 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001191 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001192 Diag(property->getLocation(),
1193 diag::err_accessor_property_type_mismatch)
1194 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001195 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001196 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1197 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001198
1199 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001200 if (Context.getCanonicalType(SetterMethod->getResultType())
1201 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001202 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001203 if (SetterMethod->param_size() != 1 ||
1204 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001205 Diag(property->getLocation(),
1206 diag::err_accessor_property_type_mismatch)
1207 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001208 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001209 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1210 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001211 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001212
1213 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001214 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001215 // FIXME: The synthesized property we set here is misleading. We
1216 // almost always synthesize these methods unless the user explicitly
1217 // provided prototypes (which is odd, but allowed). Sema should be
1218 // typechecking that the declarations jive in that situation (which
1219 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001220 if (!GetterMethod) {
1221 // No instance method of same name as property getter name was found.
1222 // Declare a getter method and add it to the list of methods
1223 // for this class.
1224 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1225 property->getLocation(), property->getGetterName(),
1226 property->getType(), CD, true, false, true,
1227 (property->getPropertyImplementation() ==
1228 ObjCPropertyDecl::Optional) ?
1229 ObjCMethodDecl::Optional :
1230 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001231 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001232 } else
1233 // A user declared getter will be synthesize when @synthesize of
1234 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001235 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001236 property->setGetterMethodDecl(GetterMethod);
1237
1238 // Skip setter if property is read-only.
1239 if (!property->isReadOnly()) {
1240 // Find the default setter and if one not found, add one.
1241 if (!SetterMethod) {
1242 // No instance method of same name as property setter name was found.
1243 // Declare a setter method and add it to the list of methods
1244 // for this class.
1245 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1246 property->getLocation(),
1247 property->getSetterName(),
1248 Context.VoidTy, CD, true, false, true,
1249 (property->getPropertyImplementation() ==
1250 ObjCPropertyDecl::Optional) ?
1251 ObjCMethodDecl::Optional :
1252 ObjCMethodDecl::Required);
1253 // Invent the arguments for the setter. We don't bother making a
1254 // nice name for the argument.
1255 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001256 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001257 property->getIdentifier(),
1258 property->getType(),
1259 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001260 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001261 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001262 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001263 } else
1264 // A user declared setter will be synthesize when @synthesize of
1265 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001266 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001267 property->setSetterMethodDecl(SetterMethod);
1268 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001269 // Add any synthesized methods to the global pool. This allows us to
1270 // handle the following, which is supported by GCC (and part of the design).
1271 //
1272 // @interface Foo
1273 // @property double bar;
1274 // @end
1275 //
1276 // void thisIsUnfortunate() {
1277 // id foo;
1278 // double bar = [foo bar];
1279 // }
1280 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001281 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001282 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001283 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001284 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001285}
1286
Steve Naroffa56f6162007-12-18 01:30:32 +00001287// Note: For class/category implemenations, allMethods/allProperties is
1288// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001289void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1290 DeclPtrTy *allMethods, unsigned allNum,
1291 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001292 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001293 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001294
Steve Naroffa56f6162007-12-18 01:30:32 +00001295 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1296 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001297 // should be true.
1298 if (!ClassDecl)
1299 return;
1300
Chris Lattner4d391482007-12-12 07:09:47 +00001301 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001302 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1303 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001304 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001305
Steve Naroff0701bbb2009-01-08 17:28:14 +00001306 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001307
1308 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1309 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1310 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1311
Chris Lattner4d391482007-12-12 07:09:47 +00001312 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001313 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001314 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001315
1316 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001317 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001318 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001320 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1321 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001322 if ((isInterfaceDeclKind && PrevMethod && !match)
1323 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001324 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001325 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001326 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001327 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001328 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001329 InsMap[Method->getSelector()] = Method;
1330 /// The following allows us to typecheck messages to "id".
1331 AddInstanceMethodToGlobalPool(Method);
1332 }
1333 }
1334 else {
1335 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001336 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001337 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1338 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001339 if ((isInterfaceDeclKind && PrevMethod && !match)
1340 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001341 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001342 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001343 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001344 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001345 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001346 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001347 /// The following allows us to typecheck messages to "Class".
1348 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001349 }
1350 }
1351 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001352 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001353 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001354 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001355 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001356 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001357 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001358 // Categories are used to extend the class by declaring new methods.
1359 // By the same token, they are also used to add new properties. No
1360 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001361
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001362 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001363 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001364 if (C->getIdentifier() == 0)
1365 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001366 }
Steve Naroff09c47192009-01-09 15:36:25 +00001367 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1368 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1369 // user-defined setter/getter. It also synthesizes setter/getter methods
1370 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001371 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1372 E = CDecl->prop_end(Context);
1373 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001374 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001375 CDecl->setAtEndLoc(AtEndLoc);
1376 }
1377 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001378 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001379 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001380 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001381 } else if (ObjCCategoryImplDecl* CatImplClass =
1382 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001383 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001384
Chris Lattner4d391482007-12-12 07:09:47 +00001385 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001386 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001387 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001389 Categories; Categories = Categories->getNextClassCategory()) {
1390 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001391 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001392 break;
1393 }
1394 }
1395 }
1396 }
Chris Lattner682bf922009-03-29 16:50:03 +00001397 if (isInterfaceDeclKind) {
1398 // Reject invalid vardecls.
1399 for (unsigned i = 0; i != tuvNum; i++) {
1400 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1401 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1402 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001403 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001404 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001405 }
Chris Lattner682bf922009-03-29 16:50:03 +00001406 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001407 }
Chris Lattner4d391482007-12-12 07:09:47 +00001408}
1409
1410
1411/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1412/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001413static Decl::ObjCDeclQualifier
1414CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1415 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1416 if (PQTVal & ObjCDeclSpec::DQ_In)
1417 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1418 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1419 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1420 if (PQTVal & ObjCDeclSpec::DQ_Out)
1421 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1422 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1423 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1424 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1425 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1426 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1427 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001428
1429 return ret;
1430}
1431
Chris Lattnerb28317a2009-03-28 19:18:32 +00001432Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001433 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001434 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001435 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001436 Selector Sel,
1437 // optional arguments. The number of types/arguments is obtained
1438 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001439 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001440 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001441 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1442 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001443 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001444
1445 // Make sure we can establish a context for the method.
1446 if (!ClassDecl) {
1447 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001448 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001449 }
Chris Lattner4d391482007-12-12 07:09:47 +00001450 QualType resultDeclType;
1451
Steve Naroffccef3712009-02-20 22:59:16 +00001452 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001453 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001454
1455 // Methods cannot return interface types. All ObjC objects are
1456 // passed by reference.
1457 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001458 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1459 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001460 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001461 }
1462 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001464
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001465 ObjCMethodDecl* ObjCMethod =
1466 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001467 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001468 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001469 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001470 MethodDeclKind == tok::objc_optional ?
1471 ObjCMethodDecl::Optional :
1472 ObjCMethodDecl::Required);
1473
Chris Lattner0ed844b2008-04-04 06:12:32 +00001474 llvm::SmallVector<ParmVarDecl*, 16> Params;
1475
Chris Lattner7db638d2009-04-11 19:42:43 +00001476 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001477 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001478
Chris Lattnere294d3f2009-04-11 18:57:04 +00001479 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001480 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001481 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001482 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001483 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001484 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001485 }
1486
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001487 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001488 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001489 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001490 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001491 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001492 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001493 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001494 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001495 ArgInfo[i].Name, ArgType,
1496 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001497 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001498
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001499 if (ArgType->isObjCInterfaceType()) {
1500 Diag(ArgInfo[i].NameLoc,
1501 diag::err_object_cannot_be_passed_returned_by_value)
1502 << 1 << ArgType;
1503 Param->setInvalidDecl();
1504 }
1505
Chris Lattner0ed844b2008-04-04 06:12:32 +00001506 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001507 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001508
1509 // Apply the attributes to the parameter.
1510 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1511
Chris Lattner0ed844b2008-04-04 06:12:32 +00001512 Params.push_back(Param);
1513 }
1514
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001515 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 ObjCMethod->setObjCDeclQualifier(
1517 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1518 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001519
1520 if (AttrList)
1521 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001522
1523 // For implementations (which can be very "coarse grain"), we add the
1524 // method now. This allows the AST to implement lookup methods that work
1525 // incrementally (without waiting until we parse the @end). It also allows
1526 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001527 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001528 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001529 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001530 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1531 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001532 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001533 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1534 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001535 }
1536 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001537 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001538 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001539 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001540 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1541 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001542 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001543 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1544 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001545 }
1546 }
1547 if (PrevMethod) {
1548 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001549 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001550 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001551 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001552 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001553 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001554}
1555
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001556void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1557 SourceLocation Loc,
1558 unsigned &Attributes) {
1559 // FIXME: Improve the reported location.
1560
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001561 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001562 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001563 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1564 ObjCDeclSpec::DQ_PR_assign |
1565 ObjCDeclSpec::DQ_PR_copy |
1566 ObjCDeclSpec::DQ_PR_retain))) {
1567 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1568 "readwrite" :
1569 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1570 "assign" :
1571 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1572 "copy" : "retain";
1573
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001574 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001575 diag::err_objc_property_attr_mutually_exclusive :
1576 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001577 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001578 }
1579
1580 // Check for copy or retain on non-object types.
1581 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1582 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001583 Diag(Loc, diag::err_objc_property_requires_object)
1584 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001585 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1586 }
1587
1588 // Check for more than one of { assign, copy, retain }.
1589 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1590 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001591 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1592 << "assign" << "copy";
1593 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001594 }
1595 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001596 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1597 << "assign" << "retain";
1598 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001599 }
1600 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1601 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001602 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1603 << "copy" << "retain";
1604 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001605 }
1606 }
1607
1608 // Warn if user supplied no assignment attribute, property is
1609 // readwrite, and this is an object type.
1610 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1611 ObjCDeclSpec::DQ_PR_retain)) &&
1612 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1613 Context.isObjCObjectPointerType(PropertyTy)) {
1614 // Skip this warning in gc-only mode.
1615 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1616 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1617
1618 // If non-gc code warn that this is likely inappropriate.
1619 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1620 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1621
1622 // FIXME: Implement warning dependent on NSCopying being
1623 // implemented. See also:
1624 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1625 // (please trim this list while you are at it).
1626 }
1627}
1628
Chris Lattnerb28317a2009-03-28 19:18:32 +00001629Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1630 FieldDeclarator &FD,
1631 ObjCDeclSpec &ODS,
1632 Selector GetterSel,
1633 Selector SetterSel,
1634 DeclPtrTy ClassCategory,
1635 bool *isOverridingProperty,
1636 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001637 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001638 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1639 // default is readwrite!
1640 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1641 // property is defaulted to 'assign' if it is readwrite and is
1642 // not retain or copy
1643 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1644 (isReadWrite &&
1645 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1646 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1647 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001648 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001649 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001650 // May modify Attributes.
1651 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001652 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1653 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001654 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001655 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001656 if ((CCPrimary = CDecl->getClassInterface())) {
1657 // Find the property in continuation class's primary class only.
1658 ObjCPropertyDecl *PIDecl = 0;
1659 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001660 for (ObjCInterfaceDecl::prop_iterator
1661 I = CCPrimary->prop_begin(Context),
1662 E = CCPrimary->prop_end(Context);
1663 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001664 if ((*I)->getIdentifier() == PropertyId) {
1665 PIDecl = *I;
1666 break;
1667 }
1668
1669 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001670 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001671 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001672 unsigned PIkind = PIDecl->getPropertyAttributes();
1673 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001674 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001675 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1676 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001677 PIDecl->makeitReadWriteAttribute();
1678 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1679 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1680 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1681 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1682 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001683 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001684 else
1685 Diag(AtLoc, diag::err_use_continuation_class)
1686 << CCPrimary->getDeclName();
1687 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001688 // Make sure setter decl is synthesized, and added to primary
1689 // class's list.
1690 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001691 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001692 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001693 // No matching property found in the primary class. Just fall thru
1694 // and add property to continuation class's primary class.
1695 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001696 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001697 Diag(CDecl->getLocation(), diag::err_continuation_class);
1698 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001699 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001700 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001701 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001702
Steve Naroff93983f82009-01-11 12:47:58 +00001703 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1704 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001705 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1706 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001707 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001708 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001709
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001710 if (T->isArrayType() || T->isFunctionType()) {
1711 Diag(AtLoc, diag::err_property_type) << T;
1712 PDecl->setInvalidDecl();
1713 }
1714
Chris Lattner97a58872009-02-16 18:32:47 +00001715 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001716
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001717 // Regardless of setter/getter attribute, we save the default getter/setter
1718 // selector names in anticipation of declaration of setter/getter methods.
1719 PDecl->setGetterName(GetterSel);
1720 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001721
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001722 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001723 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001724
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001725 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001726 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001727
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001728 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001729 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001730
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001731 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001732 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001733
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001734 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001735 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001736
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001737 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001738 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001739
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001740 if (isAssign)
1741 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1742
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001743 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001744 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001745
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001746 if (MethodImplKind == tok::objc_required)
1747 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1748 else if (MethodImplKind == tok::objc_optional)
1749 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001750 // A case of continuation class adding a new property in the class. This
1751 // is not what it was meant for. However, gcc supports it and so should we.
1752 // Make sure setter/getters are declared here.
1753 if (CCPrimary)
1754 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001755
Chris Lattnerb28317a2009-03-28 19:18:32 +00001756 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001757}
1758
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001759/// ActOnPropertyImplDecl - This routine performs semantic checks and
1760/// builds the AST node for a property implementation declaration; declared
1761/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001762///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001763Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1764 SourceLocation PropertyLoc,
1765 bool Synthesize,
1766 DeclPtrTy ClassCatImpDecl,
1767 IdentifierInfo *PropertyId,
1768 IdentifierInfo *PropertyIvar) {
1769 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001770 // Make sure we have a context for the property implementation declaration.
1771 if (!ClassImpDecl) {
1772 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001773 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001774 }
1775 ObjCPropertyDecl *property = 0;
1776 ObjCInterfaceDecl* IDecl = 0;
1777 // Find the class or category class where this property must have
1778 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001779 ObjCImplementationDecl *IC = 0;
1780 ObjCCategoryImplDecl* CatImplClass = 0;
1781 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001782 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001783 // We always synthesize an interface for an implementation
1784 // without an interface decl. So, IDecl is always non-zero.
1785 assert(IDecl &&
1786 "ActOnPropertyImplDecl - @implementation without @interface");
1787
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001788 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001789 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001790 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001791 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001792 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001793 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001794 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001795 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001796 if (Synthesize) {
1797 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001798 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001799 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001800 IDecl = CatImplClass->getClassInterface();
1801 if (!IDecl) {
1802 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001803 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001804 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001805 ObjCCategoryDecl *Category =
1806 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1807
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001808 // If category for this implementation not found, it is an error which
1809 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001810 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001812 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001813 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001814 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001815 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001816 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001817 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001818 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001819 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001820 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001821 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001822 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001823 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001824 // Check that we have a valid, previously declared ivar for @synthesize
1825 if (Synthesize) {
1826 // @synthesize
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001827 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001828 if (!PropertyIvar)
1829 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001830 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001831 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001832 ObjCInterfaceDecl *ClassDeclared;
1833 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001834 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001835 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1836 PropertyIvar, PropType,
1837 ObjCIvarDecl::Public,
1838 (Expr *)0);
1839 property->setPropertyIvarDecl(Ivar);
1840 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001841 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001842 // Note! I deliberately want it to fall thru so, we have a
1843 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001844 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001845 else if (getLangOptions().ObjCNonFragileABI &&
1846 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1847 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1848 << property->getDeclName() << Ivar->getDeclName()
1849 << ClassDeclared->getDeclName();
1850 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1851 << Ivar << Ivar->getNameAsCString();
1852 // Note! I deliberately want it to fall thru so more errors are caught.
1853 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001854 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1855
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001856 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001857 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001858 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001859 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001860 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001861 // Note! I deliberately want it to fall thru so, we have a
1862 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001863 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001864
1865 // FIXME! Rules for properties are somewhat different that those
1866 // for assignments. Use a new routine to consolidate all cases;
1867 // specifically for property redeclarations as well as for ivars.
1868 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1869 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1870 if (lhsType != rhsType &&
1871 lhsType->isArithmeticType()) {
1872 Diag(PropertyLoc, diag::error_property_ivar_type)
1873 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001874 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001875 }
1876 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00001877 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1878 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001879 Diag(PropertyLoc, diag::error_weak_property)
1880 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001881 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001882 }
1883 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00001884 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1885 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001886 Diag(PropertyLoc, diag::error_strong_property)
1887 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001888 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001889 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001890 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001891 } else if (PropertyIvar)
1892 // @dynamic
1893 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001894 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001895 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001896 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1897 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001898 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001899 ObjCPropertyImplDecl::Synthesize
1900 : ObjCPropertyImplDecl::Dynamic),
1901 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001902 if (IC) {
1903 if (Synthesize)
1904 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001905 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001906 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1907 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1908 << PropertyIvar;
1909 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1910 }
1911
Douglas Gregor653f1b12009-04-23 01:02:12 +00001912 if (ObjCPropertyImplDecl *PPIDecl
1913 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001914 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1915 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001916 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001917 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001918 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001919 }
1920 else {
1921 if (Synthesize)
1922 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001923 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001924 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1925 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1926 << PropertyIvar;
1927 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1928 }
1929
1930 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001931 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001932 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1933 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001934 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001935 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001936 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001937 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001938
Chris Lattnerb28317a2009-03-28 19:18:32 +00001939 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001940}
Anders Carlsson15281452008-11-04 16:57:32 +00001941
Chris Lattnercc98eac2008-12-17 07:13:27 +00001942bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001943 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001944 return false;
1945
1946 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1947 D->setInvalidDecl();
1948
1949 return true;
1950}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001951
1952/// Collect the instance variables declared in an Objective-C object. Used in
1953/// the creation of structures from objects using the @defs directive.
1954/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1955/// part of the AST generation logic of @defs.
1956static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1957 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001958 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001959 if (Class->getSuperClass())
1960 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1961
1962 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001963 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1964 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001965 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001966 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1967 ID->getIdentifier(), ID->getType(),
1968 ID->getBitWidth());
1969 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00001970 }
1971}
1972
1973/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1974/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001975void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001976 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001977 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001978 // Check that ClassName is a valid class
1979 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1980 if (!Class) {
1981 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1982 return;
1983 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001984 if (LangOpts.ObjCNonFragileABI) {
1985 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1986 return;
1987 }
1988
Chris Lattnercc98eac2008-12-17 07:13:27 +00001989 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00001990 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001991
1992 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001993 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001994 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001995 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001996 if (getLangOptions().CPlusPlus)
1997 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001998 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00001999 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002000 }
2001}
2002