blob: c65b1402be32b4d3a9723b2c9db514c4857f2f06 [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;
Douglas Gregord0434102009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
Douglas Gregor6ab35242009-04-09 21:40:53 +0000103 CurContext->addDecl(Context, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000105 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000106 }
107
108 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000109 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000110 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000111
Steve Naroff818cb9e2009-02-04 17:14:05 +0000112 ObjCInterfaceDecl *SuperClassDecl =
113 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000114
115 // Diagnose classes that inherit from deprecated classes.
116 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000117 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000118
Steve Naroff818cb9e2009-02-04 17:14:05 +0000119 if (PrevDecl && SuperClassDecl == 0) {
120 // The previous declaration was not a class decl. Check if we have a
121 // typedef. If we do, get the underlying class type.
122 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
123 QualType T = TDecl->getUnderlyingType();
124 if (T->isObjCInterfaceType()) {
125 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
126 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
127 }
128 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000129
Steve Naroff818cb9e2009-02-04 17:14:05 +0000130 // This handles the following case:
131 //
132 // typedef int SuperClass;
133 // @interface MyClass : SuperClass {} @end
134 //
135 if (!SuperClassDecl) {
136 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
137 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
138 }
139 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000140
Steve Naroff818cb9e2009-02-04 17:14:05 +0000141 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
142 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000145 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000146 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000148 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000150 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000151 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 IDecl->setLocEnd(SuperLoc);
153 } else { // we have a root class.
154 IDecl->setLocEnd(ClassLoc);
155 }
156
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000157 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000158 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000159 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
160 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000161 IDecl->setLocEnd(EndProtoLoc);
162 }
Anders Carlsson15281452008-11-04 16:57:32 +0000163
164 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000165 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000166}
167
168/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000169/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000170Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
171 IdentifierInfo *AliasName,
172 SourceLocation AliasLocation,
173 IdentifierInfo *ClassName,
174 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000175 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000176 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000177 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000179 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000180 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000181 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000182 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000183 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000184 }
185 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000186 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000187 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
188 QualType T = TDecl->getUnderlyingType();
189 if (T->isObjCInterfaceType()) {
190 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
191 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000192 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000193 }
194 }
195 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000196 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
197 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000198 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000199 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000200 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000201 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000202 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000203
204 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000205 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000206 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000207
208 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000209
210 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000211 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000212 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000213 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregord0434102009-01-09 00:49:46 +0000214
Chris Lattnerb28317a2009-03-28 19:18:32 +0000215 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000216}
217
Steve Naroff61d68522009-03-05 15:22:01 +0000218void Sema::CheckForwardProtocolDeclarationForCircularDependency(
219 IdentifierInfo *PName,
220 SourceLocation &Ploc, SourceLocation PrevLoc,
221 const ObjCList<ObjCProtocolDecl> &PList)
222{
223 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
224 E = PList.end(); I != E; ++I) {
225
226 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
227 if (PDecl->getIdentifier() == PName) {
228 Diag(Ploc, diag::err_protocol_has_circular_dependency);
229 Diag(PrevLoc, diag::note_previous_definition);
230 }
231 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
232 PDecl->getLocation(), PDecl->getReferencedProtocols());
233 }
234 }
235}
236
Chris Lattnerb28317a2009-03-28 19:18:32 +0000237Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000238Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
239 IdentifierInfo *ProtocolName,
240 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000241 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000242 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000243 SourceLocation EndProtoLoc,
244 AttributeList *AttrList) {
245 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000246 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000248 if (PDecl) {
249 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000250 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000251 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000252 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000253 // Just return the protocol we already had.
254 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000255 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000256 }
Steve Naroff61d68522009-03-05 15:22:01 +0000257 ObjCList<ObjCProtocolDecl> PList;
258 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
259 CheckForwardProtocolDeclarationForCircularDependency(
260 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
261 PList.Destroy(Context);
262
Steve Narofff11b5082008-08-13 16:39:22 +0000263 // Make sure the cached decl gets a valid start location.
264 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000266 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000267 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
268 AtProtoInterfaceLoc,ProtocolName);
269 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000270 CurContext->addDecl(Context, PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000271 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000273 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000274 if (AttrList)
275 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000276 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000277 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000278 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000279 PDecl->setLocEnd(EndProtoLoc);
280 }
Anders Carlsson15281452008-11-04 16:57:32 +0000281
282 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000283 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000284}
285
286/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000287/// issues an error if they are not declared. It returns list of
288/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000289void
Chris Lattnere13b9592008-07-26 04:03:38 +0000290Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000291 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000292 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000293 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000294 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000295 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
296 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000297 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000298 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000299 continue;
300 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000301
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000302 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000303
304 // If this is a forward declaration and we are supposed to warn in this
305 // case, do it.
306 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000307 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000308 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000310 }
311}
312
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000313/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000314/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000315///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000316void
317Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
318 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000319 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000320 ObjCPropertyDecl::PropertyAttributeKind CAttr =
321 Property->getPropertyAttributes();
322 ObjCPropertyDecl::PropertyAttributeKind SAttr =
323 SuperProperty->getPropertyAttributes();
324 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
325 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000326 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000327 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
329 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000330 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000331 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
333 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000334 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000335 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000336
337 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
338 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000339 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000340 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000341 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000342 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000343 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000344 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000345 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000346 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000347
348 QualType LHSType =
349 Context.getCanonicalType(SuperProperty->getType());
350 QualType RHSType =
351 Context.getCanonicalType(Property->getType());
352
353 if (!Context.typesAreCompatible(LHSType, RHSType)) {
354 // FIXME: Incorporate this test with typesAreCompatible.
355 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
356 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
357 return;
358 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
359 << Property->getType() << SuperProperty->getType() << inheritedName;
360 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000361}
362
363/// ComparePropertiesInBaseAndSuper - This routine compares property
364/// declarations in base and its super class, if any, and issues
365/// diagnostics in a variety of inconsistant situations.
366///
Chris Lattner70f19542009-02-16 21:26:43 +0000367void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000368 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
369 if (!SDecl)
370 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000371 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000372 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
373 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000374 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000375 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000376 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
377 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000378 ObjCPropertyDecl *PDecl = (*I);
379 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000380 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000381 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000382 }
383 }
384}
385
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
387/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000388/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000390Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000391 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000392 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
393 if (!IDecl) {
394 // Category
395 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
396 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000397 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
398 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000399 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000400 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000401 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000402 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000403 CP != CE; ++CP)
404 if ((*CP)->getIdentifier() == Pr->getIdentifier())
405 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000406 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000407 // Property protocol already exist in class. Diagnose any mismatch.
408 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
409 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000410 return;
411 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000412 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
413 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000414 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000415 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000416 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000417 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000418 CP != CE; ++CP)
419 if ((*CP)->getIdentifier() == Pr->getIdentifier())
420 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000421 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000422 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000423 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000425}
426
427/// MergeProtocolPropertiesIntoClass - This routine merges properties
428/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000429/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430///
Chris Lattner70f19542009-02-16 21:26:43 +0000431void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000432 DeclPtrTy MergeItsProtocols) {
433 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000434 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
435
436 if (!IDecl) {
437 // Category
438 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
439 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
440 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
441 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
442 E = MDecl->protocol_end(); P != E; ++P)
443 // Merge properties of category (*P) into IDECL's
444 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
445
446 // Go thru the list of protocols for this category and recursively merge
447 // their properties into this class as well.
448 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
449 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000450 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000451 } else {
452 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
453 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
454 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000455 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000456 }
457 return;
458 }
459
Chris Lattnerb752f282008-07-21 07:06:49 +0000460 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000461 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
462 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000463 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000464 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
465
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000466 // Go thru the list of protocols for this class and recursively merge
467 // their properties into this class as well.
468 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
469 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000470 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000471 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000472 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
473 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
474 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000475 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000476 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000477}
478
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000479/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000480/// a class method in its extension.
481///
482void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
483 ObjCInterfaceDecl *ID) {
484 if (!ID)
485 return; // Possibly due to previous error
486
487 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000488 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
489 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000490 ObjCMethodDecl *MD = *i;
491 MethodMap[MD->getSelector()] = MD;
492 }
493
494 if (MethodMap.empty())
495 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000496 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
497 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000498 ObjCMethodDecl *Method = *i;
499 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
500 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
501 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
502 << Method->getDeclName();
503 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
504 }
505 }
506}
507
Chris Lattner58fe03b2009-04-12 08:43:13 +0000508/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000509Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000510Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000511 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000512 unsigned NumElts,
513 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000515
516 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000517 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000518 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000519 if (PDecl == 0) { // Not already seen?
520 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
521 IdentList[i].second, Ident);
522 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000523 CurContext->addDecl(Context, PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000524 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000525 if (attrList)
526 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000527 Protocols.push_back(PDecl);
528 }
Anders Carlsson15281452008-11-04 16:57:32 +0000529
530 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000531 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000532 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000533 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000534 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000535 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000536}
537
Chris Lattnerb28317a2009-03-28 19:18:32 +0000538Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000539ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
540 IdentifierInfo *ClassName, SourceLocation ClassLoc,
541 IdentifierInfo *CategoryName,
542 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000543 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000544 unsigned NumProtoRefs,
545 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000546 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000547 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
548 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000549 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000550
551 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000552 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000553 if (!IDecl || IDecl->isForwardDecl()) {
554 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000556 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000557 }
Chris Lattner4d391482007-12-12 07:09:47 +0000558
Chris Lattner70f19542009-02-16 21:26:43 +0000559 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000560
561 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000562 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000563
564 /// Check for duplicate interface declaration for this category
565 ObjCCategoryDecl *CDeclChain;
566 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
567 CDeclChain = CDeclChain->getNextClassCategory()) {
568 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
569 Diag(CategoryLoc, diag::warn_dup_category_def)
570 << ClassName << CategoryName;
571 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
572 break;
573 }
574 }
575 if (!CDeclChain)
576 CDecl->insertNextClassCategory();
577
Chris Lattner4d391482007-12-12 07:09:47 +0000578 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000579 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000580 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 }
Anders Carlsson15281452008-11-04 16:57:32 +0000582
583 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000584 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000585}
586
587/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000589/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000590Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000591 SourceLocation AtCatImplLoc,
592 IdentifierInfo *ClassName, SourceLocation ClassLoc,
593 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000595 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000596 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
597 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000598 /// Check that class of this category is already completely declared.
599 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000600 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000601
Douglas Gregord0434102009-01-09 00:49:46 +0000602 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000603 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000604
Chris Lattner4d391482007-12-12 07:09:47 +0000605 /// TODO: Check that CatName, category name, is not used in another
606 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000607 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000608
609 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000610 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000611}
612
Chris Lattnerb28317a2009-03-28 19:18:32 +0000613Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000614 SourceLocation AtClassImplLoc,
615 IdentifierInfo *ClassName, SourceLocation ClassLoc,
616 IdentifierInfo *SuperClassname,
617 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000619 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000620 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000622 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000623 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000624 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000625 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000627 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000629 }
630
631 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000632 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000633 if (SuperClassname) {
634 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000635 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000636 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000637 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
638 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000639 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000640 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000641 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000642 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000643 Diag(SuperClassLoc, diag::err_undef_superclass)
644 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000645 else if (IDecl && IDecl->getSuperClass() != SDecl) {
646 // This implementation and its interface do not have the same
647 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000648 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000649 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000650 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000651 }
652 }
653 }
654
655 if (!IDecl) {
656 // Legacy case of @implementation with no corresponding @interface.
657 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000658
659 // FIXME: Do we support attributes on the @implementation? If so
660 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000661 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
662 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000663 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000664 IDecl->setSuperClass(SDecl);
665 IDecl->setLocEnd(ClassLoc);
666
Douglas Gregord0434102009-01-09 00:49:46 +0000667 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000668 CurContext->addDecl(Context, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000669 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000670 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Daniel Dunbar24c89912009-04-21 21:41:56 +0000671 } else {
672 // Mark the interface as being completed, even if it was just as
673 // @class ....;
674 // declaration; the user cannot reopen it.
675 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000676 }
677
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000678 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000679 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000680 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000681
Douglas Gregord0434102009-01-09 00:49:46 +0000682 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000683 CurContext->addDecl(Context, IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000684
Anders Carlsson15281452008-11-04 16:57:32 +0000685 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000686 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000687
Chris Lattner4d391482007-12-12 07:09:47 +0000688 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000690 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000691 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000692 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000694 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000695}
696
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
698 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000699 SourceLocation RBrace) {
700 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000701 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000702 if (!IDecl)
703 return;
704 /// Check case of non-existing @interface decl.
705 /// (legacy objective-c @implementation decl without an @interface decl).
706 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000707 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000708 IDecl->setIVarList(ivars, numIvars, Context);
709 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000710 return;
711 }
712 // If implementation has empty ivar list, just return.
713 if (numIvars == 0)
714 return;
715
716 assert(ivars && "missing @implementation ivars");
717
718 // Check interface's Ivar list against those in the implementation.
719 // names and types must match.
720 //
Chris Lattner4d391482007-12-12 07:09:47 +0000721 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000723 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
724 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 ObjCIvarDecl* ImplIvar = ivars[j++];
726 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000727 assert (ImplIvar && "missing implementation ivar");
728 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000729
730 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000731 if (Context.getCanonicalType(ImplIvar->getType()) !=
732 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000733 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000734 << ImplIvar->getIdentifier()
735 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000736 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000737 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
738 Expr *ImplBitWidth = ImplIvar->getBitWidth();
739 Expr *ClsBitWidth = ClsIvar->getBitWidth();
740 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
741 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
742 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
743 << ImplIvar->getIdentifier();
744 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
745 }
746 }
747 // Make sure the names are identical.
748 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000750 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000751 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000752 }
753 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000754 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000755
756 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000757 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000758 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000759 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000760}
761
Steve Naroff3c2eb662008-02-10 21:38:56 +0000762void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
763 bool &IncompleteImpl) {
764 if (!IncompleteImpl) {
765 Diag(ImpLoc, diag::warn_incomplete_impl);
766 IncompleteImpl = true;
767 }
Chris Lattner08631c52008-11-23 21:45:46 +0000768 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000769}
770
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000771void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
772 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000773 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000774 ImpMethodDecl->getResultType())) {
775 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
776 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
777 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000778 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
779 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000780
781 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
782 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
783 IM != EM; ++IM, ++IF) {
784 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
785 continue;
786
787 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
788 << ImpMethodDecl->getDeclName() << (*IF)->getType()
789 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000790 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000791 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000792}
793
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000794/// isPropertyReadonly - Return true if property is readonly, by searching
795/// for the property in the class and in its categories and implementations
796///
797bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000798 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000799 // by far the most common case.
800 if (!PDecl->isReadOnly())
801 return false;
802 // Even if property is ready only, if interface has a user defined setter,
803 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000804 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000805 return false;
806
807 // Main class has the property as 'readonly'. Must search
808 // through the category list to see if the property's
809 // attribute has been over-ridden to 'readwrite'.
810 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
811 Category; Category = Category->getNextClassCategory()) {
812 // Even if property is ready only, if a category has a user defined setter,
813 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000814 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000815 return false;
816 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000817 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000818 if (P && !P->isReadOnly())
819 return false;
820 }
821
822 // Also, check for definition of a setter method in the implementation if
823 // all else failed.
824 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
825 if (ObjCImplementationDecl *IMD =
826 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000827 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000828 return false;
829 }
830 else if (ObjCCategoryImplDecl *CIMD =
831 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000832 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000833 return false;
834 }
835 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000836 // Lastly, look through the implementation (if one is in scope).
837 if (ObjCImplementationDecl *ImpDecl =
838 ObjCImplementations[IDecl->getIdentifier()])
Douglas Gregor653f1b12009-04-23 01:02:12 +0000839 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000840 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000841 // If all fails, look at the super class.
842 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
843 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000844 return true;
845}
846
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000847/// FIXME: Type hierarchies in Objective-C can be deep. We could most
848/// likely improve the efficiency of selector lookups and type
849/// checking by associating with each protocol / interface / category
850/// the flattened instance tables. If we used an immutable set to keep
851/// the table then it wouldn't add significant memory cost and it
852/// would be handy for lookups.
853
Steve Naroffefe7f362008-02-08 22:06:17 +0000854/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000855/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000856void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
857 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000858 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000859 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000860 const llvm::DenseSet<Selector> &ClsMap,
861 ObjCInterfaceDecl *IDecl) {
862 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
863
864 // If a method lookup fails locally we still need to look and see if
865 // the method was implemented by a base class or an inherited
866 // protocol. This lookup is slow, but occurs rarely in correct code
867 // and otherwise would terminate in a warning.
868
Chris Lattner4d391482007-12-12 07:09:47 +0000869 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000870 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
871 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000873 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000874 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000875 (!Super ||
876 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000877 // Ugly, but necessary. Method declared in protcol might have
878 // have been synthesized due to a property declared in the class which
879 // uses the protocol.
880 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000881 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000882 if (!MethodInClass || !MethodInClass->isSynthesized())
883 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
884 }
Chris Lattner4d391482007-12-12 07:09:47 +0000885 }
886 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000887 for (ObjCProtocolDecl::classmeth_iterator
888 I = PDecl->classmeth_begin(Context),
889 E = PDecl->classmeth_end(Context);
890 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000892 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
893 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000894 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000895 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000896 }
Chris Lattner780f3292008-07-21 21:32:27 +0000897 // Check on this protocols's referenced protocols, recursively.
898 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
899 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000900 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000901}
902
Chris Lattnercddc8882009-03-01 00:56:52 +0000903void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
904 ObjCContainerDecl* CDecl,
905 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000906 llvm::DenseSet<Selector> InsMap;
907 // Check and see if instance methods in class interface have been
908 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000909 for (ObjCImplementationDecl::instmeth_iterator
910 I = IMPDecl->instmeth_begin(Context),
911 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000912 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000913
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000914 // Check and see if properties declared in the interface have either 1)
915 // an implementation or 2) there is a @synthesize/@dynamic implementation
916 // of the property in the @implementation.
917 if (isa<ObjCInterfaceDecl>(CDecl))
918 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
919 E = CDecl->prop_end(Context); P != E; ++P) {
920 ObjCPropertyDecl *Prop = (*P);
921 if (Prop->isInvalidDecl())
922 continue;
923 ObjCPropertyImplDecl *PI = 0;
924 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000925 for (ObjCImplDecl::propimpl_iterator
926 I = IMPDecl->propimpl_begin(Context),
927 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000928 if ((*I)->getPropertyDecl() == Prop) {
929 PI = (*I);
930 break;
931 }
932 if (PI)
933 continue;
934 if (!InsMap.count(Prop->getGetterName())) {
935 Diag(Prop->getLocation(),
936 diag::warn_setter_getter_impl_required)
937 << Prop->getDeclName() << Prop->getGetterName();
938 Diag(IMPDecl->getLocation(),
939 diag::note_property_impl_required);
940 }
941
942 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
943 Diag(Prop->getLocation(),
944 diag::warn_setter_getter_impl_required)
945 << Prop->getDeclName() << Prop->getSetterName();
946 Diag(IMPDecl->getLocation(),
947 diag::note_property_impl_required);
948 }
949 }
950
Douglas Gregor6ab35242009-04-09 21:40:53 +0000951 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
952 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000953 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000954 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000955 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000956 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000957
958 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000959 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000960 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000961 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000962 assert(IntfMethodDecl &&
963 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
964 // ImpMethodDecl may be null as in a @dynamic property.
965 if (ImpMethodDecl)
966 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
967 }
Chris Lattner4c525092007-12-12 17:58:05 +0000968
Chris Lattner4d391482007-12-12 07:09:47 +0000969 llvm::DenseSet<Selector> ClsMap;
970 // Check and see if class methods in class interface have been
971 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000972 for (ObjCImplementationDecl::classmeth_iterator
973 I = IMPDecl->classmeth_begin(Context),
974 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000975 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000976
Douglas Gregor6ab35242009-04-09 21:40:53 +0000977 for (ObjCInterfaceDecl::classmeth_iterator
978 I = CDecl->classmeth_begin(Context),
979 E = CDecl->classmeth_end(Context);
980 I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000981 if (!ClsMap.count((*I)->getSelector()))
982 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000983 else {
984 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000985 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000986 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000987 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000988 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
989 }
990
Chris Lattner4d391482007-12-12 07:09:47 +0000991
992 // Check the protocol list for unimplemented methods in the @implementation
993 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000994 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
995 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
996 E = I->protocol_end(); PI != E; ++PI)
997 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
998 InsMap, ClsMap, I);
999 // Check class extensions (unnamed categories)
1000 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1001 Categories; Categories = Categories->getNextClassCategory()) {
1002 if (!Categories->getIdentifier()) {
1003 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1004 break;
1005 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001006 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001007 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1008 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1009 E = C->protocol_end(); PI != E; ++PI)
1010 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1011 InsMap, ClsMap, C->getClassInterface());
1012 } else
1013 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001014}
1015
1016/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001017Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001018Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001019 IdentifierInfo **IdentList,
1020 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001021 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001022
1023 for (unsigned i = 0; i != NumElts; ++i) {
1024 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001025 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001026 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001027 // Maybe we will complain about the shadowed template parameter.
1028 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1029 // Just pretend that we didn't see the previous declaration.
1030 PrevDecl = 0;
1031 }
1032
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001033 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001034 // GCC apparently allows the following idiom:
1035 //
1036 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1037 // @class XCElementToggler;
1038 //
1039 // FIXME: Make an extension?
1040 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1041 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001042 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001043 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001044 }
Chris Lattner4d391482007-12-12 07:09:47 +00001045 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001046 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001047 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001048 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1049 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +00001050 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001051
Douglas Gregord0434102009-01-09 00:49:46 +00001052 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +00001053 CurContext->addDecl(Context, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001054 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001055 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +00001056 }
1057
1058 Interfaces.push_back(IDecl);
1059 }
1060
Douglas Gregord0434102009-01-09 00:49:46 +00001061 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001062 &Interfaces[0],
1063 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001064 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001065 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001066 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001067}
1068
1069
1070/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1071/// returns true, or false, accordingly.
1072/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001073bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001074 const ObjCMethodDecl *PrevMethod,
1075 bool matchBasedOnSizeAndAlignment) {
1076 QualType T1 = Context.getCanonicalType(Method->getResultType());
1077 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1078
1079 if (T1 != T2) {
1080 // The result types are different.
1081 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001082 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001083 // Incomplete types don't have a size and alignment.
1084 if (T1->isIncompleteType() || T2->isIncompleteType())
1085 return false;
1086 // Check is based on size and alignment.
1087 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1088 return false;
1089 }
Chris Lattner89951a82009-02-20 18:43:26 +00001090
1091 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1092 E = Method->param_end();
1093 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1094
1095 for (; ParamI != E; ++ParamI, ++PrevI) {
1096 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1097 T1 = Context.getCanonicalType((*ParamI)->getType());
1098 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001099 if (T1 != T2) {
1100 // The result types are different.
1101 if (!matchBasedOnSizeAndAlignment)
1102 return false;
1103 // Incomplete types don't have a size and alignment.
1104 if (T1->isIncompleteType() || T2->isIncompleteType())
1105 return false;
1106 // Check is based on size and alignment.
1107 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1108 return false;
1109 }
Chris Lattner4d391482007-12-12 07:09:47 +00001110 }
1111 return true;
1112}
1113
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001115 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1116 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001117 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001118 Entry.Method = Method;
1119 Entry.Next = 0;
1120 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001121 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001122
1123 // We've seen a method with this name, see if we have already seen this type
1124 // signature.
1125 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1126 if (MatchTwoMethodDeclarations(Method, List->Method))
1127 return;
1128
1129 // We have a new signature for an existing method - add it.
1130 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1131 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001132}
1133
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001134// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001135ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1136 SourceRange R) {
1137 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001138 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001139
1140 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001141 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1142 // This checks if the methods differ by size & alignment.
1143 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1144 issueWarning = true;
1145 }
1146 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001147 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001148 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001149 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001150 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001151 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001152 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001153 }
1154 return MethList.Method;
1155}
1156
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001157void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1158 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001159 if (!FirstMethod.Method) {
1160 // Haven't seen a method with this selector name yet - add it.
1161 FirstMethod.Method = Method;
1162 FirstMethod.Next = 0;
1163 } else {
1164 // We've seen a method with this name, now check the type signature(s).
1165 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1166
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001167 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001168 Next = Next->Next)
1169 match = MatchTwoMethodDeclarations(Method, Next->Method);
1170
1171 if (!match) {
1172 // We have a new signature for an existing method - add it.
1173 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001174 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001175 FirstMethod.Next = OMI;
1176 }
1177 }
1178}
1179
Steve Naroff0701bbb2009-01-08 17:28:14 +00001180/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1181/// have the property type and issue diagnostics if they don't.
1182/// Also synthesize a getter/setter method if none exist (and update the
1183/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1184/// methods is the "right" thing to do.
1185void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1186 ObjCContainerDecl *CD) {
1187 ObjCMethodDecl *GetterMethod, *SetterMethod;
1188
Douglas Gregor6ab35242009-04-09 21:40:53 +00001189 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1190 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001191
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001192 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001193 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001194 Diag(property->getLocation(),
1195 diag::err_accessor_property_type_mismatch)
1196 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001197 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001198 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1199 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001200
1201 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001202 if (Context.getCanonicalType(SetterMethod->getResultType())
1203 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001204 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001205 if (SetterMethod->param_size() != 1 ||
1206 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001207 Diag(property->getLocation(),
1208 diag::err_accessor_property_type_mismatch)
1209 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001210 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001211 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1212 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001213 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001214
1215 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001216 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001217 // FIXME: The synthesized property we set here is misleading. We
1218 // almost always synthesize these methods unless the user explicitly
1219 // provided prototypes (which is odd, but allowed). Sema should be
1220 // typechecking that the declarations jive in that situation (which
1221 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001222 if (!GetterMethod) {
1223 // No instance method of same name as property getter name was found.
1224 // Declare a getter method and add it to the list of methods
1225 // for this class.
1226 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1227 property->getLocation(), property->getGetterName(),
1228 property->getType(), CD, true, false, true,
1229 (property->getPropertyImplementation() ==
1230 ObjCPropertyDecl::Optional) ?
1231 ObjCMethodDecl::Optional :
1232 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001233 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001234 } else
1235 // A user declared getter will be synthesize when @synthesize of
1236 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001237 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001238 property->setGetterMethodDecl(GetterMethod);
1239
1240 // Skip setter if property is read-only.
1241 if (!property->isReadOnly()) {
1242 // Find the default setter and if one not found, add one.
1243 if (!SetterMethod) {
1244 // No instance method of same name as property setter name was found.
1245 // Declare a setter method and add it to the list of methods
1246 // for this class.
1247 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1248 property->getLocation(),
1249 property->getSetterName(),
1250 Context.VoidTy, CD, true, false, true,
1251 (property->getPropertyImplementation() ==
1252 ObjCPropertyDecl::Optional) ?
1253 ObjCMethodDecl::Optional :
1254 ObjCMethodDecl::Required);
1255 // Invent the arguments for the setter. We don't bother making a
1256 // nice name for the argument.
1257 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001258 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001259 property->getIdentifier(),
1260 property->getType(),
1261 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001262 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001263 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001264 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001265 } else
1266 // A user declared setter will be synthesize when @synthesize of
1267 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001268 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001269 property->setSetterMethodDecl(SetterMethod);
1270 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001271 // Add any synthesized methods to the global pool. This allows us to
1272 // handle the following, which is supported by GCC (and part of the design).
1273 //
1274 // @interface Foo
1275 // @property double bar;
1276 // @end
1277 //
1278 // void thisIsUnfortunate() {
1279 // id foo;
1280 // double bar = [foo bar];
1281 // }
1282 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001283 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001284 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001285 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001286 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001287}
1288
Steve Naroffa56f6162007-12-18 01:30:32 +00001289// Note: For class/category implemenations, allMethods/allProperties is
1290// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001291void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1292 DeclPtrTy *allMethods, unsigned allNum,
1293 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001294 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001295 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001296
Steve Naroffa56f6162007-12-18 01:30:32 +00001297 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1298 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001299 // should be true.
1300 if (!ClassDecl)
1301 return;
1302
Chris Lattner4d391482007-12-12 07:09:47 +00001303 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001304 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1305 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001306 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001307
Steve Naroff0701bbb2009-01-08 17:28:14 +00001308 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001309
1310 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1311 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1312 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1313
Chris Lattner4d391482007-12-12 07:09:47 +00001314 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001315 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001316 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001317
1318 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001319 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001320 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001322 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1323 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001324 if ((isInterfaceDeclKind && PrevMethod && !match)
1325 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001326 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001327 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001328 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001329 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001330 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001331 InsMap[Method->getSelector()] = Method;
1332 /// The following allows us to typecheck messages to "id".
1333 AddInstanceMethodToGlobalPool(Method);
1334 }
1335 }
1336 else {
1337 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001338 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001339 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1340 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001341 if ((isInterfaceDeclKind && PrevMethod && !match)
1342 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001343 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001344 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001345 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001346 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001347 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001348 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001349 /// The following allows us to typecheck messages to "Class".
1350 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001351 }
1352 }
1353 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001354 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001355 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001356 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001357 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001358 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001359 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001360 // Categories are used to extend the class by declaring new methods.
1361 // By the same token, they are also used to add new properties. No
1362 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001363
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001364 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001365 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001366 if (C->getIdentifier() == 0)
1367 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001368 }
Steve Naroff09c47192009-01-09 15:36:25 +00001369 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1370 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1371 // user-defined setter/getter. It also synthesizes setter/getter methods
1372 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001373 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1374 E = CDecl->prop_end(Context);
1375 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001376 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001377 CDecl->setAtEndLoc(AtEndLoc);
1378 }
1379 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001380 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001381 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001382 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001383 } else if (ObjCCategoryImplDecl* CatImplClass =
1384 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001385 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001386
Chris Lattner4d391482007-12-12 07:09:47 +00001387 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001388 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001389 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001391 Categories; Categories = Categories->getNextClassCategory()) {
1392 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001393 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001394 break;
1395 }
1396 }
1397 }
1398 }
Chris Lattner682bf922009-03-29 16:50:03 +00001399 if (isInterfaceDeclKind) {
1400 // Reject invalid vardecls.
1401 for (unsigned i = 0; i != tuvNum; i++) {
1402 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1403 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1404 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001405 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001406 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001407 }
Chris Lattner682bf922009-03-29 16:50:03 +00001408 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001409 }
Chris Lattner4d391482007-12-12 07:09:47 +00001410}
1411
1412
1413/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1414/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415static Decl::ObjCDeclQualifier
1416CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1417 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1418 if (PQTVal & ObjCDeclSpec::DQ_In)
1419 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1420 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1421 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1422 if (PQTVal & ObjCDeclSpec::DQ_Out)
1423 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1424 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1425 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1426 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1427 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1428 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1429 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001430
1431 return ret;
1432}
1433
Chris Lattnerb28317a2009-03-28 19:18:32 +00001434Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001435 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001436 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001438 Selector Sel,
1439 // optional arguments. The number of types/arguments is obtained
1440 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001441 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001442 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001443 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1444 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001445 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001446
1447 // Make sure we can establish a context for the method.
1448 if (!ClassDecl) {
1449 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001450 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001451 }
Chris Lattner4d391482007-12-12 07:09:47 +00001452 QualType resultDeclType;
1453
Steve Naroffccef3712009-02-20 22:59:16 +00001454 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001455 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001456
1457 // Methods cannot return interface types. All ObjC objects are
1458 // passed by reference.
1459 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001460 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1461 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001462 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001463 }
1464 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001466
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001467 ObjCMethodDecl* ObjCMethod =
1468 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001469 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001470 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001471 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001472 MethodDeclKind == tok::objc_optional ?
1473 ObjCMethodDecl::Optional :
1474 ObjCMethodDecl::Required);
1475
Chris Lattner0ed844b2008-04-04 06:12:32 +00001476 llvm::SmallVector<ParmVarDecl*, 16> Params;
1477
Chris Lattner7db638d2009-04-11 19:42:43 +00001478 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001479 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001480
Chris Lattnere294d3f2009-04-11 18:57:04 +00001481 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001482 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001483 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001484 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001485 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001486 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001487 }
1488
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001489 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001490 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001491 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001492 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001493 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001494 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001495 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001496 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001497 ArgInfo[i].Name, ArgType,
1498 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001499 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001500
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001501 if (ArgType->isObjCInterfaceType()) {
1502 Diag(ArgInfo[i].NameLoc,
1503 diag::err_object_cannot_be_passed_returned_by_value)
1504 << 1 << ArgType;
1505 Param->setInvalidDecl();
1506 }
1507
Chris Lattner0ed844b2008-04-04 06:12:32 +00001508 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001509 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001510
1511 // Apply the attributes to the parameter.
1512 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1513
Chris Lattner0ed844b2008-04-04 06:12:32 +00001514 Params.push_back(Param);
1515 }
1516
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001517 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518 ObjCMethod->setObjCDeclQualifier(
1519 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1520 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001521
1522 if (AttrList)
1523 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001524
1525 // For implementations (which can be very "coarse grain"), we add the
1526 // method now. This allows the AST to implement lookup methods that work
1527 // incrementally (without waiting until we parse the @end). It also allows
1528 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001529 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001530 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001531 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001532 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1533 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001534 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001535 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1536 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001537 }
1538 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001539 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001540 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001541 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001542 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1543 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001544 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001545 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1546 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001547 }
1548 }
1549 if (PrevMethod) {
1550 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001551 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001552 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001554 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001555 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001556}
1557
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001558void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1559 SourceLocation Loc,
1560 unsigned &Attributes) {
1561 // FIXME: Improve the reported location.
1562
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001563 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001564 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001565 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1566 ObjCDeclSpec::DQ_PR_assign |
1567 ObjCDeclSpec::DQ_PR_copy |
1568 ObjCDeclSpec::DQ_PR_retain))) {
1569 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1570 "readwrite" :
1571 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1572 "assign" :
1573 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1574 "copy" : "retain";
1575
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001576 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001577 diag::err_objc_property_attr_mutually_exclusive :
1578 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001579 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001580 }
1581
1582 // Check for copy or retain on non-object types.
1583 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1584 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001585 Diag(Loc, diag::err_objc_property_requires_object)
1586 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001587 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1588 }
1589
1590 // Check for more than one of { assign, copy, retain }.
1591 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1592 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001593 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1594 << "assign" << "copy";
1595 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001596 }
1597 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001598 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1599 << "assign" << "retain";
1600 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001601 }
1602 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1603 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001604 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1605 << "copy" << "retain";
1606 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001607 }
1608 }
1609
1610 // Warn if user supplied no assignment attribute, property is
1611 // readwrite, and this is an object type.
1612 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1613 ObjCDeclSpec::DQ_PR_retain)) &&
1614 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1615 Context.isObjCObjectPointerType(PropertyTy)) {
1616 // Skip this warning in gc-only mode.
1617 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1618 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1619
1620 // If non-gc code warn that this is likely inappropriate.
1621 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1622 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1623
1624 // FIXME: Implement warning dependent on NSCopying being
1625 // implemented. See also:
1626 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1627 // (please trim this list while you are at it).
1628 }
1629}
1630
Chris Lattnerb28317a2009-03-28 19:18:32 +00001631Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1632 FieldDeclarator &FD,
1633 ObjCDeclSpec &ODS,
1634 Selector GetterSel,
1635 Selector SetterSel,
1636 DeclPtrTy ClassCategory,
1637 bool *isOverridingProperty,
1638 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001639 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001640 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1641 // default is readwrite!
1642 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1643 // property is defaulted to 'assign' if it is readwrite and is
1644 // not retain or copy
1645 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1646 (isReadWrite &&
1647 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1648 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1649 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001650 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001651 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001652 // May modify Attributes.
1653 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001654 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1655 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001656 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001657 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001658 if ((CCPrimary = CDecl->getClassInterface())) {
1659 // Find the property in continuation class's primary class only.
1660 ObjCPropertyDecl *PIDecl = 0;
1661 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001662 for (ObjCInterfaceDecl::prop_iterator
1663 I = CCPrimary->prop_begin(Context),
1664 E = CCPrimary->prop_end(Context);
1665 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001666 if ((*I)->getIdentifier() == PropertyId) {
1667 PIDecl = *I;
1668 break;
1669 }
1670
1671 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001672 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001673 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001674 unsigned PIkind = PIDecl->getPropertyAttributes();
1675 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001676 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001677 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1678 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001679 PIDecl->makeitReadWriteAttribute();
1680 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1681 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1682 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1683 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1684 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001685 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001686 else
1687 Diag(AtLoc, diag::err_use_continuation_class)
1688 << CCPrimary->getDeclName();
1689 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001690 // Make sure setter decl is synthesized, and added to primary
1691 // class's list.
1692 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001693 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001694 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001695 // No matching property found in the primary class. Just fall thru
1696 // and add property to continuation class's primary class.
1697 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001698 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001699 Diag(CDecl->getLocation(), diag::err_continuation_class);
1700 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001701 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001702 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001703 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001704
Steve Naroff93983f82009-01-11 12:47:58 +00001705 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1706 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001707 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1708 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001709 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001710 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001711
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001712 if (T->isArrayType() || T->isFunctionType()) {
1713 Diag(AtLoc, diag::err_property_type) << T;
1714 PDecl->setInvalidDecl();
1715 }
1716
Chris Lattner97a58872009-02-16 18:32:47 +00001717 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001718
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001719 // Regardless of setter/getter attribute, we save the default getter/setter
1720 // selector names in anticipation of declaration of setter/getter methods.
1721 PDecl->setGetterName(GetterSel);
1722 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001723
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001724 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001725 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001726
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001727 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001728 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001729
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001730 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001731 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001732
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001733 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001735
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001736 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001738
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001739 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001740 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001741
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001742 if (isAssign)
1743 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1744
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001745 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001746 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001747
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001748 if (MethodImplKind == tok::objc_required)
1749 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1750 else if (MethodImplKind == tok::objc_optional)
1751 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001752 // A case of continuation class adding a new property in the class. This
1753 // is not what it was meant for. However, gcc supports it and so should we.
1754 // Make sure setter/getters are declared here.
1755 if (CCPrimary)
1756 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001757
Chris Lattnerb28317a2009-03-28 19:18:32 +00001758 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001759}
1760
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001761/// ActOnPropertyImplDecl - This routine performs semantic checks and
1762/// builds the AST node for a property implementation declaration; declared
1763/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001764///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001765Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1766 SourceLocation PropertyLoc,
1767 bool Synthesize,
1768 DeclPtrTy ClassCatImpDecl,
1769 IdentifierInfo *PropertyId,
1770 IdentifierInfo *PropertyIvar) {
1771 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001772 // Make sure we have a context for the property implementation declaration.
1773 if (!ClassImpDecl) {
1774 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001775 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001776 }
1777 ObjCPropertyDecl *property = 0;
1778 ObjCInterfaceDecl* IDecl = 0;
1779 // Find the class or category class where this property must have
1780 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001781 ObjCImplementationDecl *IC = 0;
1782 ObjCCategoryImplDecl* CatImplClass = 0;
1783 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001784 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001785 // We always synthesize an interface for an implementation
1786 // without an interface decl. So, IDecl is always non-zero.
1787 assert(IDecl &&
1788 "ActOnPropertyImplDecl - @implementation without @interface");
1789
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001790 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001791 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001792 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001793 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001794 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001795 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001796 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001797 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001798 if (Synthesize) {
1799 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001800 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001801 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001802 IDecl = CatImplClass->getClassInterface();
1803 if (!IDecl) {
1804 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001805 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001806 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001807 ObjCCategoryDecl *Category =
1808 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1809
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001810 // If category for this implementation not found, it is an error which
1811 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001812 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001813 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001814 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001815 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001816 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001817 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001818 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001819 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001820 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001821 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001822 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001823 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001824 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001825 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001826 // Check that we have a valid, previously declared ivar for @synthesize
1827 if (Synthesize) {
1828 // @synthesize
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001829 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001830 if (!PropertyIvar)
1831 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001832 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001833 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001834 ObjCInterfaceDecl *ClassDeclared;
1835 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001836 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001837 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1838 PropertyIvar, PropType,
1839 ObjCIvarDecl::Public,
1840 (Expr *)0);
1841 property->setPropertyIvarDecl(Ivar);
1842 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001843 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001844 // Note! I deliberately want it to fall thru so, we have a
1845 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001846 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001847 else if (getLangOptions().ObjCNonFragileABI &&
1848 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1849 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1850 << property->getDeclName() << Ivar->getDeclName()
1851 << ClassDeclared->getDeclName();
1852 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1853 << Ivar << Ivar->getNameAsCString();
1854 // Note! I deliberately want it to fall thru so more errors are caught.
1855 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001856 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1857
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001858 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001859 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001860 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001861 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001862 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001863 // Note! I deliberately want it to fall thru so, we have a
1864 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001865 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001866
1867 // FIXME! Rules for properties are somewhat different that those
1868 // for assignments. Use a new routine to consolidate all cases;
1869 // specifically for property redeclarations as well as for ivars.
1870 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1871 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1872 if (lhsType != rhsType &&
1873 lhsType->isArithmeticType()) {
1874 Diag(PropertyLoc, diag::error_property_ivar_type)
1875 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001876 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001877 }
1878 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00001879 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1880 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001881 Diag(PropertyLoc, diag::error_weak_property)
1882 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001883 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001884 }
1885 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00001886 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1887 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001888 Diag(PropertyLoc, diag::error_strong_property)
1889 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001890 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001891 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001892 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001893 } else if (PropertyIvar)
1894 // @dynamic
1895 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001896 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001897 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001898 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1899 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001900 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001901 ObjCPropertyImplDecl::Synthesize
1902 : ObjCPropertyImplDecl::Dynamic),
1903 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001904 if (IC) {
1905 if (Synthesize)
1906 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001907 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001908 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1909 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1910 << PropertyIvar;
1911 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1912 }
1913
Douglas Gregor653f1b12009-04-23 01:02:12 +00001914 if (ObjCPropertyImplDecl *PPIDecl
1915 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001916 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1917 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001918 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001919 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001920 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001921 }
1922 else {
1923 if (Synthesize)
1924 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001925 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001926 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1927 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1928 << PropertyIvar;
1929 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1930 }
1931
1932 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001933 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001934 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1935 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001936 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001937 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001938 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001939 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001940
Chris Lattnerb28317a2009-03-28 19:18:32 +00001941 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001942}
Anders Carlsson15281452008-11-04 16:57:32 +00001943
Chris Lattnercc98eac2008-12-17 07:13:27 +00001944bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001945 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001946 return false;
1947
1948 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1949 D->setInvalidDecl();
1950
1951 return true;
1952}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001953
1954/// Collect the instance variables declared in an Objective-C object. Used in
1955/// the creation of structures from objects using the @defs directive.
1956/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1957/// part of the AST generation logic of @defs.
1958static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1959 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001960 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001961 if (Class->getSuperClass())
1962 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1963
1964 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001965 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1966 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001967 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001968 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1969 ID->getIdentifier(), ID->getType(),
1970 ID->getBitWidth());
1971 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00001972 }
1973}
1974
1975/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1976/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001977void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001978 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001979 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001980 // Check that ClassName is a valid class
1981 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1982 if (!Class) {
1983 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1984 return;
1985 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001986 if (LangOpts.ObjCNonFragileABI) {
1987 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1988 return;
1989 }
1990
Chris Lattnercc98eac2008-12-17 07:13:27 +00001991 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00001992 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001993
1994 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001995 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001996 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001997 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001998 if (getLangOptions().CPlusPlus)
1999 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002000 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00002001 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002002 }
2003}
2004