blob: ba35333fe52c0c4ef5eba425e8e51158ac0f00a1 [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.
Steve Naroffebf64432009-02-28 16:59:13 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
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
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000045
Daniel Dunbar451318c2008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000048
Chris Lattner8123a952008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000050 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
51 E = MDecl->param_end(); PI != E; ++PI)
52 if ((*PI)->getIdentifier())
53 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000054}
55
Chris Lattner7caeabd2008-07-21 22:17:28 +000056Sema::DeclTy *Sema::
57ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000060 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000076 }
77
Ted Kremeneka526c5c2008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000082 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000083 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000084 Diag(IDecl->getLocation(), diag::note_previous_definition);
85
Steve Naroffcfe8bf32008-11-18 19:15:30 +000086 // Return the previous class interface.
87 // FIXME: don't leak the objects passed in!
88 return IDecl;
89 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000090 IDecl->setLocation(AtInterfaceLoc);
91 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000092 }
Chris Lattnerb752f282008-07-21 07:06:49 +000093 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000094 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000095 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000096 if (AttrList)
97 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000098
Steve Naroff31102512008-04-02 18:30:49 +000099 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000100 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000101 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000107 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000108 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000109
Steve Naroff818cb9e2009-02-04 17:14:05 +0000110 ObjCInterfaceDecl *SuperClassDecl =
111 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000112
113 // Diagnose classes that inherit from deprecated classes.
114 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000115 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000116
Steve Naroff818cb9e2009-02-04 17:14:05 +0000117 if (PrevDecl && SuperClassDecl == 0) {
118 // The previous declaration was not a class decl. Check if we have a
119 // typedef. If we do, get the underlying class type.
120 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
121 QualType T = TDecl->getUnderlyingType();
122 if (T->isObjCInterfaceType()) {
123 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
124 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
125 }
126 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000127
Steve Naroff818cb9e2009-02-04 17:14:05 +0000128 // This handles the following case:
129 //
130 // typedef int SuperClass;
131 // @interface MyClass : SuperClass {} @end
132 //
133 if (!SuperClassDecl) {
134 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
135 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
136 }
137 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000138
Steve Naroff818cb9e2009-02-04 17:14:05 +0000139 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
140 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000141 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000142 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000143 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000145 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000146 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000147 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000148 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000149 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000150 IDecl->setLocEnd(SuperLoc);
151 } else { // we have a root class.
152 IDecl->setLocEnd(ClassLoc);
153 }
154
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000155 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000156 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000157 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
158 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000159 IDecl->setLocEnd(EndProtoLoc);
160 }
Anders Carlsson15281452008-11-04 16:57:32 +0000161
162 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000163 return IDecl;
164}
165
166/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000167/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000168Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
169 IdentifierInfo *AliasName,
170 SourceLocation AliasLocation,
171 IdentifierInfo *ClassName,
172 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000173 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000174 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000175 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000176 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000177 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000179 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000180 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000181 return 0;
182 }
183 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000184 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000185 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
186 QualType T = TDecl->getUnderlyingType();
187 if (T->isObjCInterfaceType()) {
188 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
189 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000190 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000191 }
192 }
193 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000194 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
195 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000196 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000197 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000198 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000199 return 0;
200 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000201
202 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000203 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000204 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000205
206 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000207
208 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000209 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000210 if (!CheckObjCDeclScope(AliasDecl))
211 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000212
Chris Lattner4d391482007-12-12 07:09:47 +0000213 return AliasDecl;
214}
215
Steve Naroff61d68522009-03-05 15:22:01 +0000216void Sema::CheckForwardProtocolDeclarationForCircularDependency(
217 IdentifierInfo *PName,
218 SourceLocation &Ploc, SourceLocation PrevLoc,
219 const ObjCList<ObjCProtocolDecl> &PList)
220{
221 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
222 E = PList.end(); I != E; ++I) {
223
224 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
225 if (PDecl->getIdentifier() == PName) {
226 Diag(Ploc, diag::err_protocol_has_circular_dependency);
227 Diag(PrevLoc, diag::note_previous_definition);
228 }
229 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
230 PDecl->getLocation(), PDecl->getReferencedProtocols());
231 }
232 }
233}
234
Chris Lattnere13b9592008-07-26 04:03:38 +0000235Sema::DeclTy *
236Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
237 IdentifierInfo *ProtocolName,
238 SourceLocation ProtocolLoc,
239 DeclTy * const *ProtoRefs,
240 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000241 SourceLocation EndProtoLoc,
242 AttributeList *AttrList) {
243 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000244 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000246 if (PDecl) {
247 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000248 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000249 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000250 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000251 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000252 // Just return the protocol we already had.
253 // FIXME: don't leak the objects passed in!
254 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000255 }
Steve Naroff61d68522009-03-05 15:22:01 +0000256 ObjCList<ObjCProtocolDecl> PList;
257 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
258 CheckForwardProtocolDeclarationForCircularDependency(
259 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
260 PList.Destroy(Context);
261
Steve Narofff11b5082008-08-13 16:39:22 +0000262 // Make sure the cached decl gets a valid start location.
263 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000264 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000266 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
267 AtProtoInterfaceLoc,ProtocolName);
268 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000269 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000270 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000271 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000272 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000273 if (AttrList)
274 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000275 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000276 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000277 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 PDecl->setLocEnd(EndProtoLoc);
279 }
Anders Carlsson15281452008-11-04 16:57:32 +0000280
281 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000282 return PDecl;
283}
284
285/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000286/// issues an error if they are not declared. It returns list of
287/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000288void
Chris Lattnere13b9592008-07-26 04:03:38 +0000289Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000290 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000291 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000292 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000293 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000294 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
295 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000296 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000297 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000298 continue;
299 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000300
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000301 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000302
303 // If this is a forward declaration and we are supposed to warn in this
304 // case, do it.
305 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000306 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000307 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000308 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000309 }
310}
311
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000313/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000314///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000315void
316Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
317 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 ObjCPropertyDecl::PropertyAttributeKind CAttr =
320 Property->getPropertyAttributes();
321 ObjCPropertyDecl::PropertyAttributeKind SAttr =
322 SuperProperty->getPropertyAttributes();
323 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
324 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
328 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000329 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000330 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
332 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335
336 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
337 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000338 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000339 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000341 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000344 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000345 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000346
347 QualType LHSType =
348 Context.getCanonicalType(SuperProperty->getType());
349 QualType RHSType =
350 Context.getCanonicalType(Property->getType());
351
352 if (!Context.typesAreCompatible(LHSType, RHSType)) {
353 // FIXME: Incorporate this test with typesAreCompatible.
354 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
355 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
356 return;
357 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
358 << Property->getType() << SuperProperty->getType() << inheritedName;
359 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360}
361
362/// ComparePropertiesInBaseAndSuper - This routine compares property
363/// declarations in base and its super class, if any, and issues
364/// diagnostics in a variety of inconsistant situations.
365///
Chris Lattner70f19542009-02-16 21:26:43 +0000366void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
368 if (!SDecl)
369 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000370 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000371 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
372 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000373 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000374 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000375 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
376 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377 ObjCPropertyDecl *PDecl = (*I);
378 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000379 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000381 }
382 }
383}
384
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
386/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000387/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000388void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000390 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
392 if (!IDecl) {
393 // Category
394 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
395 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000396 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
397 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000399 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000401 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402 CP != CE; ++CP)
403 if ((*CP)->getIdentifier() == Pr->getIdentifier())
404 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000405 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000406 // Property protocol already exist in class. Diagnose any mismatch.
407 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
408 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000409 return;
410 }
Steve Naroff09c47192009-01-09 15:36:25 +0000411 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
412 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000414 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000416 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000417 CP != CE; ++CP)
418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424}
425
426/// MergeProtocolPropertiesIntoClass - This routine merges properties
427/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000428/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429///
Chris Lattner70f19542009-02-16 21:26:43 +0000430void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
431 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000433 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
434
435 if (!IDecl) {
436 // Category
437 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
438 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
439 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
440 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
441 E = MDecl->protocol_end(); P != E; ++P)
442 // Merge properties of category (*P) into IDECL's
443 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
444
445 // Go thru the list of protocols for this category and recursively merge
446 // their properties into this class as well.
447 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
448 E = CatDecl->protocol_end(); P != E; ++P)
449 MergeProtocolPropertiesIntoClass(CatDecl, *P);
450 } else {
451 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
452 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
453 E = MD->protocol_end(); P != E; ++P)
454 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
455 }
456 return;
457 }
458
Chris Lattnerb752f282008-07-21 07:06:49 +0000459 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000460 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
461 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000463 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
464
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 // Go thru the list of protocols for this class and recursively merge
466 // their properties into this class as well.
467 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
468 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000469 MergeProtocolPropertiesIntoClass(IDecl, *P);
470 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000471 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
472 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
473 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000475 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476}
477
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000478/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000479/// a class method in its extension.
480///
481void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
482 ObjCInterfaceDecl *ID) {
483 if (!ID)
484 return; // Possibly due to previous error
485
486 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
487 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
488 e = ID->meth_end(); i != e; ++i) {
489 ObjCMethodDecl *MD = *i;
490 MethodMap[MD->getSelector()] = MD;
491 }
492
493 if (MethodMap.empty())
494 return;
495 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
496 e = CAT->meth_end(); i != e; ++i) {
497 ObjCMethodDecl *Method = *i;
498 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
499 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
500 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
501 << Method->getDeclName();
502 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
503 }
504 }
505}
506
Chris Lattner4d391482007-12-12 07:09:47 +0000507/// ActOnForwardProtocolDeclaration -
508Action::DeclTy *
509Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000510 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000511 unsigned NumElts,
512 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000514
515 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000516 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000517 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000518 if (PDecl == 0) { // Not already seen?
519 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
520 IdentList[i].second, Ident);
521 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000522 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000523 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524 if (attrList)
525 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 Protocols.push_back(PDecl);
527 }
Anders Carlsson15281452008-11-04 16:57:32 +0000528
529 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000530 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000531 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000532 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000533 CheckObjCDeclScope(PDecl);
534 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Chris Lattner7caeabd2008-07-21 22:17:28 +0000537Sema::DeclTy *Sema::
538ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
539 IdentifierInfo *ClassName, SourceLocation ClassLoc,
540 IdentifierInfo *CategoryName,
541 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000542 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000543 unsigned NumProtoRefs,
544 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000545 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000546 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
547 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000548 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000549
550 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000551 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000552 if (!IDecl || IDecl->isForwardDecl()) {
553 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000555 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000556 }
Chris Lattner4d391482007-12-12 07:09:47 +0000557
Chris Lattner70f19542009-02-16 21:26:43 +0000558 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000559
560 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000561 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000562
563 /// Check for duplicate interface declaration for this category
564 ObjCCategoryDecl *CDeclChain;
565 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
566 CDeclChain = CDeclChain->getNextClassCategory()) {
567 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
568 Diag(CategoryLoc, diag::warn_dup_category_def)
569 << ClassName << CategoryName;
570 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
571 break;
572 }
573 }
574 if (!CDeclChain)
575 CDecl->insertNextClassCategory();
576
Chris Lattner4d391482007-12-12 07:09:47 +0000577 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000578 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000579 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Anders Carlsson15281452008-11-04 16:57:32 +0000581
582 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000583 return CDecl;
584}
585
586/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000588/// object.
589Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
590 SourceLocation AtCatImplLoc,
591 IdentifierInfo *ClassName, SourceLocation ClassLoc,
592 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000594 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000595 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
596 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 /// Check that class of this category is already completely declared.
598 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000600
Douglas Gregord0434102009-01-09 00:49:46 +0000601 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000602 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000603
Chris Lattner4d391482007-12-12 07:09:47 +0000604 /// TODO: Check that CatName, category name, is not used in another
605 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000606 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000607
608 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000609 return CDecl;
610}
611
612Sema::DeclTy *Sema::ActOnStartClassImplementation(
613 SourceLocation AtClassImplLoc,
614 IdentifierInfo *ClassName, SourceLocation ClassLoc,
615 IdentifierInfo *SuperClassname,
616 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000618 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000619 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000622 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000623 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000624 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000626 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000627 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000628 }
629
630 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000631 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000632 if (SuperClassname) {
633 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000634 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
637 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000638 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000641 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000642 Diag(SuperClassLoc, diag::err_undef_superclass)
643 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000644 else if (IDecl && IDecl->getSuperClass() != SDecl) {
645 // This implementation and its interface do not have the same
646 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000647 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000648 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000649 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000650 }
651 }
652 }
653
654 if (!IDecl) {
655 // Legacy case of @implementation with no corresponding @interface.
656 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000657
658 // FIXME: Do we support attributes on the @implementation? If so
659 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000660 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
661 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000662 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000663 IDecl->setSuperClass(SDecl);
664 IDecl->setLocEnd(ClassLoc);
665
Douglas Gregord0434102009-01-09 00:49:46 +0000666 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000667 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668 // Remember that this needs to be removed when the scope is popped.
669 TUScope->AddDecl(IDecl);
670 }
671
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000673 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000674 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000675
Douglas Gregord0434102009-01-09 00:49:46 +0000676 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000677 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000678
Anders Carlsson15281452008-11-04 16:57:32 +0000679 if (CheckObjCDeclScope(IMPDecl))
680 return IMPDecl;
681
Chris Lattner4d391482007-12-12 07:09:47 +0000682 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000684 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000685 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000686 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000688 return IMPDecl;
689}
690
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
692 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000693 SourceLocation RBrace) {
694 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000695 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000696 if (!IDecl)
697 return;
698 /// Check case of non-existing @interface decl.
699 /// (legacy objective-c @implementation decl without an @interface decl).
700 /// Add implementations's ivar to the synthesize class's ivar list.
701 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000702 IDecl->setIVarList(ivars, numIvars, Context);
703 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000704 return;
705 }
706 // If implementation has empty ivar list, just return.
707 if (numIvars == 0)
708 return;
709
710 assert(ivars && "missing @implementation ivars");
711
712 // Check interface's Ivar list against those in the implementation.
713 // names and types must match.
714 //
Chris Lattner4d391482007-12-12 07:09:47 +0000715 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000716 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000717 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
718 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000719 ObjCIvarDecl* ImplIvar = ivars[j++];
720 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000721 assert (ImplIvar && "missing implementation ivar");
722 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000723
724 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000725 if (Context.getCanonicalType(ImplIvar->getType()) !=
726 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000727 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000728 << ImplIvar->getIdentifier()
729 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000730 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000731 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
732 Expr *ImplBitWidth = ImplIvar->getBitWidth();
733 Expr *ClsBitWidth = ClsIvar->getBitWidth();
734 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
735 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
736 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
737 << ImplIvar->getIdentifier();
738 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
739 }
740 }
741 // Make sure the names are identical.
742 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000743 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000744 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000745 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000746 }
747 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000749
750 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000751 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000752 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000753 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000754}
755
Steve Naroff3c2eb662008-02-10 21:38:56 +0000756void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
757 bool &IncompleteImpl) {
758 if (!IncompleteImpl) {
759 Diag(ImpLoc, diag::warn_incomplete_impl);
760 IncompleteImpl = true;
761 }
Chris Lattner08631c52008-11-23 21:45:46 +0000762 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000763}
764
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000765void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
766 ObjCMethodDecl *IntfMethodDecl) {
767 bool err = false;
768 QualType ImpMethodQType =
769 Context.getCanonicalType(ImpMethodDecl->getResultType());
770 QualType IntfMethodQType =
771 Context.getCanonicalType(IntfMethodDecl->getResultType());
772 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
773 err = true;
774 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
775 IF=IntfMethodDecl->param_begin(),
776 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
777 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
778 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
779 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
780 err = true;
781 break;
782 }
783 }
784 if (err) {
785 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
786 << ImpMethodDecl->getDeclName();
787 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
788 }
789}
790
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000791/// isPropertyReadonly - Return true if property is readonly, by searching
792/// for the property in the class and in its categories and implementations
793///
794bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000795 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000796 // by far the most common case.
797 if (!PDecl->isReadOnly())
798 return false;
799 // Even if property is ready only, if interface has a user defined setter,
800 // it is not considered read only.
801 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
802 return false;
803
804 // Main class has the property as 'readonly'. Must search
805 // through the category list to see if the property's
806 // attribute has been over-ridden to 'readwrite'.
807 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
808 Category; Category = Category->getNextClassCategory()) {
809 // Even if property is ready only, if a category has a user defined setter,
810 // it is not considered read only.
811 if (Category->getInstanceMethod(PDecl->getSetterName()))
812 return false;
813 ObjCPropertyDecl *P =
814 Category->FindPropertyDeclaration(PDecl->getIdentifier());
815 if (P && !P->isReadOnly())
816 return false;
817 }
818
819 // Also, check for definition of a setter method in the implementation if
820 // all else failed.
821 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
822 if (ObjCImplementationDecl *IMD =
823 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
824 if (IMD->getInstanceMethod(PDecl->getSetterName()))
825 return false;
826 }
827 else if (ObjCCategoryImplDecl *CIMD =
828 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
829 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
830 return false;
831 }
832 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000833 // Lastly, look through the implementation (if one is in scope).
834 if (ObjCImplementationDecl *ImpDecl =
835 ObjCImplementations[IDecl->getIdentifier()])
836 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
837 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000838 return true;
839}
840
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000841/// FIXME: Type hierarchies in Objective-C can be deep. We could most
842/// likely improve the efficiency of selector lookups and type
843/// checking by associating with each protocol / interface / category
844/// the flattened instance tables. If we used an immutable set to keep
845/// the table then it wouldn't add significant memory cost and it
846/// would be handy for lookups.
847
Steve Naroffefe7f362008-02-08 22:06:17 +0000848/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000849/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000850void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
851 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000852 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000853 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000854 const llvm::DenseSet<Selector> &ClsMap,
855 ObjCInterfaceDecl *IDecl) {
856 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
857
858 // If a method lookup fails locally we still need to look and see if
859 // the method was implemented by a base class or an inherited
860 // protocol. This lookup is slow, but occurs rarely in correct code
861 // and otherwise would terminate in a warning.
862
Chris Lattner4d391482007-12-12 07:09:47 +0000863 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000865 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000867 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000868 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000869 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000870 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000871 }
872 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000873 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000874 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000876 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
877 !ClsMap.count(method->getSelector()) &&
878 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000879 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000880 }
Chris Lattner780f3292008-07-21 21:32:27 +0000881 // Check on this protocols's referenced protocols, recursively.
882 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
883 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000884 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000885}
886
Chris Lattnercddc8882009-03-01 00:56:52 +0000887void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
888 ObjCContainerDecl* CDecl,
889 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000890 llvm::DenseSet<Selector> InsMap;
891 // Check and see if instance methods in class interface have been
892 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000893 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000894 E = IMPDecl->instmeth_end(); I != E; ++I)
895 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000896
Chris Lattnercddc8882009-03-01 00:56:52 +0000897 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
898 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000899 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000900 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000901 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000902 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000903
904 ObjCMethodDecl *ImpMethodDecl =
905 IMPDecl->getInstanceMethod((*I)->getSelector());
906 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000907 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000908 assert(IntfMethodDecl &&
909 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
910 // ImpMethodDecl may be null as in a @dynamic property.
911 if (ImpMethodDecl)
912 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
913 }
Chris Lattner4c525092007-12-12 17:58:05 +0000914
Chris Lattner4d391482007-12-12 07:09:47 +0000915 llvm::DenseSet<Selector> ClsMap;
916 // Check and see if class methods in class interface have been
917 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000919 E = IMPDecl->classmeth_end(); I != E; ++I)
920 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000921
Chris Lattnercddc8882009-03-01 00:56:52 +0000922 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
923 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000924 if (!ClsMap.count((*I)->getSelector()))
925 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000926 else {
927 ObjCMethodDecl *ImpMethodDecl =
928 IMPDecl->getClassMethod((*I)->getSelector());
929 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000930 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000931 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
932 }
933
Chris Lattner4d391482007-12-12 07:09:47 +0000934
935 // Check the protocol list for unimplemented methods in the @implementation
936 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000937 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
938 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
939 E = I->protocol_end(); PI != E; ++PI)
940 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
941 InsMap, ClsMap, I);
942 // Check class extensions (unnamed categories)
943 for (ObjCCategoryDecl *Categories = I->getCategoryList();
944 Categories; Categories = Categories->getNextClassCategory()) {
945 if (!Categories->getIdentifier()) {
946 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
947 break;
948 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000949 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000950 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
951 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
952 E = C->protocol_end(); PI != E; ++PI)
953 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
954 InsMap, ClsMap, C->getClassInterface());
955 } else
956 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000957}
958
959/// ActOnForwardClassDeclaration -
960Action::DeclTy *
961Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000962 IdentifierInfo **IdentList,
963 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000964 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000965
966 for (unsigned i = 0; i != NumElts; ++i) {
967 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000968 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000969 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000970 // Maybe we will complain about the shadowed template parameter.
971 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
972 // Just pretend that we didn't see the previous declaration.
973 PrevDecl = 0;
974 }
975
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000976 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000977 // GCC apparently allows the following idiom:
978 //
979 // typedef NSObject < XCElementTogglerP > XCElementToggler;
980 // @class XCElementToggler;
981 //
982 // FIXME: Make an extension?
983 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
984 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000985 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000986 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000987 }
Chris Lattner4d391482007-12-12 07:09:47 +0000988 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000990 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000991 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
992 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000993 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000994
Douglas Gregord0434102009-01-09 00:49:46 +0000995 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000996 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000997 // Remember that this needs to be removed when the scope is popped.
998 TUScope->AddDecl(IDecl);
999 }
1000
1001 Interfaces.push_back(IDecl);
1002 }
1003
Douglas Gregord0434102009-01-09 00:49:46 +00001004 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001005 &Interfaces[0],
1006 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +00001007 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001008 CheckObjCDeclScope(CDecl);
1009 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001010}
1011
1012
1013/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1014/// returns true, or false, accordingly.
1015/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001017 const ObjCMethodDecl *PrevMethod,
1018 bool matchBasedOnSizeAndAlignment) {
1019 QualType T1 = Context.getCanonicalType(Method->getResultType());
1020 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1021
1022 if (T1 != T2) {
1023 // The result types are different.
1024 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001025 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001026 // Incomplete types don't have a size and alignment.
1027 if (T1->isIncompleteType() || T2->isIncompleteType())
1028 return false;
1029 // Check is based on size and alignment.
1030 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1031 return false;
1032 }
Chris Lattner89951a82009-02-20 18:43:26 +00001033
1034 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1035 E = Method->param_end();
1036 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1037
1038 for (; ParamI != E; ++ParamI, ++PrevI) {
1039 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1040 T1 = Context.getCanonicalType((*ParamI)->getType());
1041 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001042 if (T1 != T2) {
1043 // The result types are different.
1044 if (!matchBasedOnSizeAndAlignment)
1045 return false;
1046 // Incomplete types don't have a size and alignment.
1047 if (T1->isIncompleteType() || T2->isIncompleteType())
1048 return false;
1049 // Check is based on size and alignment.
1050 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1051 return false;
1052 }
Chris Lattner4d391482007-12-12 07:09:47 +00001053 }
1054 return true;
1055}
1056
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001058 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1059 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001060 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001061 Entry.Method = Method;
1062 Entry.Next = 0;
1063 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001064 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001065
1066 // We've seen a method with this name, see if we have already seen this type
1067 // signature.
1068 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1069 if (MatchTwoMethodDeclarations(Method, List->Method))
1070 return;
1071
1072 // We have a new signature for an existing method - add it.
1073 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1074 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001075}
1076
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001077// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001078ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1079 SourceRange R) {
1080 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001081 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001082
1083 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001084 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1085 // This checks if the methods differ by size & alignment.
1086 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1087 issueWarning = true;
1088 }
1089 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001090 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001091 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001092 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001093 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001094 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001095 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001096 }
1097 return MethList.Method;
1098}
1099
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1101 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001102 if (!FirstMethod.Method) {
1103 // Haven't seen a method with this selector name yet - add it.
1104 FirstMethod.Method = Method;
1105 FirstMethod.Next = 0;
1106 } else {
1107 // We've seen a method with this name, now check the type signature(s).
1108 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1109
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001110 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001111 Next = Next->Next)
1112 match = MatchTwoMethodDeclarations(Method, Next->Method);
1113
1114 if (!match) {
1115 // We have a new signature for an existing method - add it.
1116 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001118 FirstMethod.Next = OMI;
1119 }
1120 }
1121}
1122
Steve Naroff0701bbb2009-01-08 17:28:14 +00001123/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1124/// have the property type and issue diagnostics if they don't.
1125/// Also synthesize a getter/setter method if none exist (and update the
1126/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1127/// methods is the "right" thing to do.
1128void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1129 ObjCContainerDecl *CD) {
1130 ObjCMethodDecl *GetterMethod, *SetterMethod;
1131
1132 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1133 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1134
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001135 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001136 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001137 Diag(property->getLocation(),
1138 diag::err_accessor_property_type_mismatch)
1139 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001140 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001141 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1142 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001143
1144 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001145 if (Context.getCanonicalType(SetterMethod->getResultType())
1146 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001147 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001148 if (SetterMethod->param_size() != 1 ||
1149 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001150 Diag(property->getLocation(),
1151 diag::err_accessor_property_type_mismatch)
1152 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001153 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001154 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1155 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001156 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001157
1158 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001159 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001160 // FIXME: The synthesized property we set here is misleading. We
1161 // almost always synthesize these methods unless the user explicitly
1162 // provided prototypes (which is odd, but allowed). Sema should be
1163 // typechecking that the declarations jive in that situation (which
1164 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001165 if (!GetterMethod) {
1166 // No instance method of same name as property getter name was found.
1167 // Declare a getter method and add it to the list of methods
1168 // for this class.
1169 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1170 property->getLocation(), property->getGetterName(),
1171 property->getType(), CD, true, false, true,
1172 (property->getPropertyImplementation() ==
1173 ObjCPropertyDecl::Optional) ?
1174 ObjCMethodDecl::Optional :
1175 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001176 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001177 } else
1178 // A user declared getter will be synthesize when @synthesize of
1179 // the property with the same name is seen in the @implementation
1180 GetterMethod->setIsSynthesized();
1181 property->setGetterMethodDecl(GetterMethod);
1182
1183 // Skip setter if property is read-only.
1184 if (!property->isReadOnly()) {
1185 // Find the default setter and if one not found, add one.
1186 if (!SetterMethod) {
1187 // No instance method of same name as property setter name was found.
1188 // Declare a setter method and add it to the list of methods
1189 // for this class.
1190 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1191 property->getLocation(),
1192 property->getSetterName(),
1193 Context.VoidTy, CD, true, false, true,
1194 (property->getPropertyImplementation() ==
1195 ObjCPropertyDecl::Optional) ?
1196 ObjCMethodDecl::Optional :
1197 ObjCMethodDecl::Required);
1198 // Invent the arguments for the setter. We don't bother making a
1199 // nice name for the argument.
1200 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1201 SourceLocation(),
1202 property->getIdentifier(),
1203 property->getType(),
1204 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001205 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001206 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001207 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001208 } else
1209 // A user declared setter will be synthesize when @synthesize of
1210 // the property with the same name is seen in the @implementation
1211 SetterMethod->setIsSynthesized();
1212 property->setSetterMethodDecl(SetterMethod);
1213 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001214 // Add any synthesized methods to the global pool. This allows us to
1215 // handle the following, which is supported by GCC (and part of the design).
1216 //
1217 // @interface Foo
1218 // @property double bar;
1219 // @end
1220 //
1221 // void thisIsUnfortunate() {
1222 // id foo;
1223 // double bar = [foo bar];
1224 // }
1225 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001226 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001227 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001228 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001229 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001230}
1231
Steve Naroffa56f6162007-12-18 01:30:32 +00001232// Note: For class/category implemenations, allMethods/allProperties is
1233// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001234void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1235 DeclTy **allMethods, unsigned allNum,
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001236 DeclTy **allProperties, unsigned pNum,
1237 DeclTy **allTUVars,
1238 unsigned tuvNum) {
Chris Lattner4d391482007-12-12 07:09:47 +00001239 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1240
Steve Naroffa56f6162007-12-18 01:30:32 +00001241 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1242 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001243 // should be true.
1244 if (!ClassDecl)
1245 return;
1246
Chris Lattner4d391482007-12-12 07:09:47 +00001247 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001248 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1249 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001250 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001251
Steve Naroff0701bbb2009-01-08 17:28:14 +00001252 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001253
1254 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1255 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1256 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1257
Chris Lattner4d391482007-12-12 07:09:47 +00001258 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259 ObjCMethodDecl *Method =
1260 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001261
1262 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001263 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001264 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001266 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1267 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001268 if ((isInterfaceDeclKind && PrevMethod && !match)
1269 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001270 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001271 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001272 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001273 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001274 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001275 InsMap[Method->getSelector()] = Method;
1276 /// The following allows us to typecheck messages to "id".
1277 AddInstanceMethodToGlobalPool(Method);
1278 }
1279 }
1280 else {
1281 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001283 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1284 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001285 if ((isInterfaceDeclKind && PrevMethod && !match)
1286 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001287 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001288 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001289 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001290 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001291 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001292 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001293 /// The following allows us to typecheck messages to "Class".
1294 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001295 }
1296 }
1297 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001299 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001300 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001301 ComparePropertiesInBaseAndSuper(I);
1302 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001303 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001304 // Categories are used to extend the class by declaring new methods.
1305 // By the same token, they are also used to add new properties. No
1306 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001307
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001308 // Merge protocol properties into category
1309 MergeProtocolPropertiesIntoClass(C, C);
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001310 if (C->getIdentifier() == 0)
1311 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001312 }
Steve Naroff09c47192009-01-09 15:36:25 +00001313 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1314 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1315 // user-defined setter/getter. It also synthesizes setter/getter methods
1316 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001317 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1318 E = CDecl->prop_end(); I != E; ++I)
1319 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001320 CDecl->setAtEndLoc(AtEndLoc);
1321 }
1322 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001323 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001324 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001325 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001326 } else if (ObjCCategoryImplDecl* CatImplClass =
1327 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001328 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001329
Chris Lattner4d391482007-12-12 07:09:47 +00001330 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001331 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001332 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001333 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001334 Categories; Categories = Categories->getNextClassCategory()) {
1335 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001336 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001337 break;
1338 }
1339 }
1340 }
1341 }
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001342 if (isInterfaceDeclKind)
1343 for (unsigned i = 0; i < tuvNum; i++) {
1344 if (VarDecl *VDecl = dyn_cast<VarDecl>((Decl*)allTUVars[i])) {
1345 if (VDecl->getStorageClass() != VarDecl::Extern &&
1346 VDecl->getStorageClass() != VarDecl::PrivateExtern) {
1347 NamedDecl *ClassNameDecl = dyn_cast<NamedDecl>(ClassDecl);
1348 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass)
1349 << ClassNameDecl->getIdentifier();
1350 }
1351 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001352 }
Chris Lattner4d391482007-12-12 07:09:47 +00001353}
1354
1355
1356/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1357/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358static Decl::ObjCDeclQualifier
1359CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1360 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1361 if (PQTVal & ObjCDeclSpec::DQ_In)
1362 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1363 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1364 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1365 if (PQTVal & ObjCDeclSpec::DQ_Out)
1366 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1367 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1368 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1369 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1370 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1371 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1372 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001373
1374 return ret;
1375}
1376
1377Sema::DeclTy *Sema::ActOnMethodDeclaration(
1378 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001379 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001380 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001381 Selector Sel,
1382 // optional arguments. The number of types/arguments is obtained
1383 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001385 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001386 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1387 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001388 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001389
1390 // Make sure we can establish a context for the method.
1391 if (!ClassDecl) {
1392 Diag(MethodLoc, diag::error_missing_method_context);
1393 return 0;
1394 }
Chris Lattner4d391482007-12-12 07:09:47 +00001395 QualType resultDeclType;
1396
Steve Naroffccef3712009-02-20 22:59:16 +00001397 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001398 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001399
1400 // Methods cannot return interface types. All ObjC objects are
1401 // passed by reference.
1402 if (resultDeclType->isObjCInterfaceType()) {
1403 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1404 << "returned";
1405 return 0;
1406 }
1407 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001409
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001410 ObjCMethodDecl* ObjCMethod =
1411 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001412 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001413 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001414 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001415 MethodDeclKind == tok::objc_optional ?
1416 ObjCMethodDecl::Optional :
1417 ObjCMethodDecl::Required);
1418
Chris Lattner0ed844b2008-04-04 06:12:32 +00001419 llvm::SmallVector<ParmVarDecl*, 16> Params;
1420
1421 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1422 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001423 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001424
Steve Naroff6082c622008-12-09 19:36:17 +00001425 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001426 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001427 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001428 if (argType->isArrayType()) { // (char *[]) -> (char **)
1429 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001430 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001431 }
Steve Naroff6082c622008-12-09 19:36:17 +00001432 else if (argType->isFunctionType())
1433 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001434 else if (argType->isObjCInterfaceType()) {
1435 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001436 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1437 << "passed";
1438 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001439 return 0;
1440 }
Steve Naroff6082c622008-12-09 19:36:17 +00001441 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001442 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001443 ParmVarDecl* Param;
1444 if (originalArgType.isNull())
1445 Param = ParmVarDecl::Create(Context, ObjCMethod,
1446 SourceLocation(/*FIXME*/),
1447 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001448 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001449 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001450 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1451 SourceLocation(/*FIXME*/),
1452 ArgNames[i], argType, originalArgType,
1453 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001454
Chris Lattner0ed844b2008-04-04 06:12:32 +00001455 Param->setObjCDeclQualifier(
1456 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1457 Params.push_back(Param);
1458 }
1459
Chris Lattner38af2de2009-02-20 21:35:13 +00001460 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461 ObjCMethod->setObjCDeclQualifier(
1462 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1463 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001464
1465 if (AttrList)
1466 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001467
1468 // For implementations (which can be very "coarse grain"), we add the
1469 // method now. This allows the AST to implement lookup methods that work
1470 // incrementally (without waiting until we parse the @end). It also allows
1471 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001472 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001473 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001474 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001475 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001477 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001478 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001479 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001480 }
1481 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001482 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001483 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001484 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001485 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001486 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001487 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001488 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001489 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001490 }
1491 }
1492 if (PrevMethod) {
1493 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001494 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001495 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001496 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001497 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001498 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001499}
1500
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001501void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1502 SourceLocation Loc,
1503 unsigned &Attributes) {
1504 // FIXME: Improve the reported location.
1505
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001506 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001507 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001508 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1509 ObjCDeclSpec::DQ_PR_assign |
1510 ObjCDeclSpec::DQ_PR_copy |
1511 ObjCDeclSpec::DQ_PR_retain))) {
1512 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1513 "readwrite" :
1514 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1515 "assign" :
1516 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1517 "copy" : "retain";
1518
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001519 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001520 diag::err_objc_property_attr_mutually_exclusive :
1521 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001522 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001523 }
1524
1525 // Check for copy or retain on non-object types.
1526 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1527 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001528 Diag(Loc, diag::err_objc_property_requires_object)
1529 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001530 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1531 }
1532
1533 // Check for more than one of { assign, copy, retain }.
1534 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1535 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001536 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1537 << "assign" << "copy";
1538 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001539 }
1540 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001541 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1542 << "assign" << "retain";
1543 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001544 }
1545 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1546 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001547 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1548 << "copy" << "retain";
1549 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001550 }
1551 }
1552
1553 // Warn if user supplied no assignment attribute, property is
1554 // readwrite, and this is an object type.
1555 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1556 ObjCDeclSpec::DQ_PR_retain)) &&
1557 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1558 Context.isObjCObjectPointerType(PropertyTy)) {
1559 // Skip this warning in gc-only mode.
1560 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1561 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1562
1563 // If non-gc code warn that this is likely inappropriate.
1564 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1565 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1566
1567 // FIXME: Implement warning dependent on NSCopying being
1568 // implemented. See also:
1569 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1570 // (please trim this list while you are at it).
1571 }
1572}
1573
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001574Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1575 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001576 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001577 Selector GetterSel,
1578 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001579 DeclTy *ClassCategory,
1580 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001581 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001582 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001583 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1584 // default is readwrite!
1585 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1586 // property is defaulted to 'assign' if it is readwrite and is
1587 // not retain or copy
1588 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1589 (isReadWrite &&
1590 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1591 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1592 QualType T = GetTypeForDeclarator(FD.D, S);
1593 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001594
1595 // May modify Attributes.
1596 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001597
1598 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1599 if (!CDecl->getIdentifier()) {
1600 // This is an anonymous category. property requires special
1601 // handling.
1602 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1603 if (ObjCPropertyDecl *PIDecl =
1604 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1605 // property 'PIDecl's readonly attribute will be over-ridden
1606 // with anonymous category's readwrite property attribute!
1607 unsigned PIkind = PIDecl->getPropertyAttributes();
1608 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001609 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001610 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1611 Diag(AtLoc, diag::warn_property_attr_mismatch);
1612 PIDecl->makeitReadWriteAttribute();
1613 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1614 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1615 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1616 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1617 PIDecl->setSetterName(SetterSel);
1618 // FIXME: use a common routine with addPropertyMethods.
1619 ObjCMethodDecl *SetterDecl =
1620 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1621 Context.VoidTy,
1622 ICDecl,
1623 true, false, true,
1624 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001625 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001626 SourceLocation(),
1627 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001628 T, VarDecl::None, 0);
1629 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001630 PIDecl->setSetterMethodDecl(SetterDecl);
1631 }
1632 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001633 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001634 *isOverridingProperty = true;
1635 return 0;
1636 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001637 // No matching property found in the main class. Just fall thru
1638 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001639 // it works as is. This category becomes just like a category
1640 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001641 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001642 Diag(CDecl->getLocation(), diag::err_continuation_class);
1643 *isOverridingProperty = true;
1644 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001645 }
1646 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001647
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001648 Type *t = T.getTypePtr();
1649 if (t->isArrayType() || t->isFunctionType())
1650 Diag(AtLoc, diag::err_property_type) << T;
1651
Steve Naroff93983f82009-01-11 12:47:58 +00001652 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1653 assert(DC && "ClassDecl is not a DeclContext");
1654 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001655 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001656 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001657
1658 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001659
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001660 // Regardless of setter/getter attribute, we save the default getter/setter
1661 // selector names in anticipation of declaration of setter/getter methods.
1662 PDecl->setGetterName(GetterSel);
1663 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001664
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001665 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001666 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001667
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001668 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001669 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001670
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001671 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001672 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001673
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001674 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001676
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001677 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001678 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001679
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001680 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001682
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001683 if (isAssign)
1684 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1685
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001686 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001687 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001688
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001689 if (MethodImplKind == tok::objc_required)
1690 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1691 else if (MethodImplKind == tok::objc_optional)
1692 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1693
Chris Lattner4d391482007-12-12 07:09:47 +00001694 return PDecl;
1695}
1696
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001697/// ActOnPropertyImplDecl - This routine performs semantic checks and
1698/// builds the AST node for a property implementation declaration; declared
1699/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001700///
1701Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1702 SourceLocation PropertyLoc,
1703 bool Synthesize,
1704 DeclTy *ClassCatImpDecl,
1705 IdentifierInfo *PropertyId,
1706 IdentifierInfo *PropertyIvar) {
1707 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1708 // Make sure we have a context for the property implementation declaration.
1709 if (!ClassImpDecl) {
1710 Diag(AtLoc, diag::error_missing_property_context);
1711 return 0;
1712 }
1713 ObjCPropertyDecl *property = 0;
1714 ObjCInterfaceDecl* IDecl = 0;
1715 // Find the class or category class where this property must have
1716 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001717 ObjCImplementationDecl *IC = 0;
1718 ObjCCategoryImplDecl* CatImplClass = 0;
1719 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001720 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001721 // We always synthesize an interface for an implementation
1722 // without an interface decl. So, IDecl is always non-zero.
1723 assert(IDecl &&
1724 "ActOnPropertyImplDecl - @implementation without @interface");
1725
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001726 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001727 property = IDecl->FindPropertyDeclaration(PropertyId);
1728 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001729 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001730 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001731 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001732 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001733 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001734 if (Synthesize) {
1735 Diag(AtLoc, diag::error_synthesize_category_decl);
1736 return 0;
1737 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001738 IDecl = CatImplClass->getClassInterface();
1739 if (!IDecl) {
1740 Diag(AtLoc, diag::error_missing_property_interface);
1741 return 0;
1742 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001743 ObjCCategoryDecl *Category =
1744 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1745
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001746 // If category for this implementation not found, it is an error which
1747 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001748 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001749 return 0;
1750 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001751 property = Category->FindPropertyDeclaration(PropertyId);
1752 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001753 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001754 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001755 return 0;
1756 }
1757 }
1758 else {
1759 Diag(AtLoc, diag::error_bad_property_context);
1760 return 0;
1761 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001762 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001763 // Check that we have a valid, previously declared ivar for @synthesize
1764 if (Synthesize) {
1765 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001766 if (!PropertyIvar)
1767 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001768 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001769 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001770 if (!Ivar) {
Steve Narofffbffca62009-03-05 15:45:01 +00001771 if (getLangOptions().ObjCNonFragileABI)
1772 Diag(PropertyLoc, diag::error_synthesized_ivar_yet_not_supported)
1773 << PropertyId;
1774 else
Steve Narofff4c00ff2009-03-03 22:09:41 +00001775 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001776 return 0;
1777 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001778 QualType PropType = Context.getCanonicalType(property->getType());
1779 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1780
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001781 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001782 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001783 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001784 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001785 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001786 return 0;
1787 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001788 else {
1789 // FIXME! Rules for properties are somewhat different that those
1790 // for assignments. Use a new routine to consolidate all cases;
1791 // specifically for property redeclarations as well as for ivars.
1792 QualType lhsType =
1793 Context.getCanonicalType(PropType).getUnqualifiedType();
1794 QualType rhsType =
1795 Context.getCanonicalType(IvarType).getUnqualifiedType();
1796 if (lhsType != rhsType &&
1797 lhsType->isArithmeticType()) {
1798 Diag(PropertyLoc, diag::error_property_ivar_type)
1799 << property->getDeclName() << Ivar->getDeclName();
1800 return 0;
1801 }
Fariborz Jahanian9bc77b22009-02-27 22:38:11 +00001802 // __weak is explicit. So it works on Canonical type.
1803 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1804 Diag(PropertyLoc, diag::error_weak_property)
1805 << property->getDeclName() << Ivar->getDeclName();
1806 return 0;
1807 }
1808 if ((Context.isObjCObjectPointerType(property->getType()) ||
1809 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1810 Diag(PropertyLoc, diag::error_strong_property)
1811 << property->getDeclName() << Ivar->getDeclName();
1812 return 0;
1813 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001814 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001815 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001816 } else if (PropertyIvar) {
1817 // @dynamic
1818 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1819 return 0;
1820 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001821 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001822 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001823 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1824 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001825 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001826 ObjCPropertyImplDecl::Synthesize
1827 : ObjCPropertyImplDecl::Dynamic),
1828 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001829 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001830 if (IC) {
1831 if (Synthesize)
1832 if (ObjCPropertyImplDecl *PPIDecl =
1833 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1834 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1835 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1836 << PropertyIvar;
1837 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1838 }
1839
1840 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1841 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1842 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1843 return 0;
1844 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001845 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001846 }
1847 else {
1848 if (Synthesize)
1849 if (ObjCPropertyImplDecl *PPIDecl =
1850 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1851 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1852 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1853 << PropertyIvar;
1854 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1855 }
1856
1857 if (ObjCPropertyImplDecl *PPIDecl =
1858 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1859 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1860 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1861 return 0;
1862 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001863 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001864 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001865
1866 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001867}
Anders Carlsson15281452008-11-04 16:57:32 +00001868
Chris Lattnercc98eac2008-12-17 07:13:27 +00001869bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001870 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001871 return false;
1872
1873 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1874 D->setInvalidDecl();
1875
1876 return true;
1877}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001878
1879/// Collect the instance variables declared in an Objective-C object. Used in
1880/// the creation of structures from objects using the @defs directive.
1881/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1882/// part of the AST generation logic of @defs.
1883static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1884 ASTContext& Ctx,
1885 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1886 if (Class->getSuperClass())
1887 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1888
1889 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1890 for (ObjCInterfaceDecl::ivar_iterator
1891 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1892
1893 ObjCIvarDecl* ID = *I;
1894 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1895 ID->getLocation(),
1896 ID->getIdentifier(),
1897 ID->getType(),
1898 ID->getBitWidth()));
1899 }
1900}
1901
1902/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1903/// instance variables of ClassName into Decls.
1904void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1905 IdentifierInfo *ClassName,
1906 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1907 // Check that ClassName is a valid class
1908 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1909 if (!Class) {
1910 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1911 return;
1912 }
1913 // Collect the instance variables
1914 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1915
1916 // Introduce all of these fields into the appropriate scope.
1917 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1918 D != Decls.end(); ++D) {
1919 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1920 if (getLangOptions().CPlusPlus)
1921 PushOnScopeChains(cast<FieldDecl>(FD), S);
1922 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001923 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001924 }
1925}
1926