blob: fb74519b57de9b667d06c7d784940d56efe0b504 [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,
1236 DeclTy **allProperties, unsigned pNum) {
1237 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1238
Steve Naroffa56f6162007-12-18 01:30:32 +00001239 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1240 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001241 // should be true.
1242 if (!ClassDecl)
1243 return;
1244
Chris Lattner4d391482007-12-12 07:09:47 +00001245 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001246 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1247 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001248 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001249
Steve Naroff0701bbb2009-01-08 17:28:14 +00001250 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001251
1252 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1253 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1254 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1255
Chris Lattner4d391482007-12-12 07:09:47 +00001256 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001257 ObjCMethodDecl *Method =
1258 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001259
1260 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001261 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001262 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001264 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1265 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001266 if ((isInterfaceDeclKind && PrevMethod && !match)
1267 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001268 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001269 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001270 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001271 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001272 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001273 InsMap[Method->getSelector()] = Method;
1274 /// The following allows us to typecheck messages to "id".
1275 AddInstanceMethodToGlobalPool(Method);
1276 }
1277 }
1278 else {
1279 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001280 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001281 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1282 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001283 if ((isInterfaceDeclKind && PrevMethod && !match)
1284 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001285 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001286 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001287 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001288 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001289 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001290 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001291 /// The following allows us to typecheck messages to "Class".
1292 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001293 }
1294 }
1295 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001296 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001297 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001298 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001299 ComparePropertiesInBaseAndSuper(I);
1300 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001301 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001302 // Categories are used to extend the class by declaring new methods.
1303 // By the same token, they are also used to add new properties. No
1304 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001305
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001306 // Merge protocol properties into category
1307 MergeProtocolPropertiesIntoClass(C, C);
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001308 if (C->getIdentifier() == 0)
1309 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001310 }
Steve Naroff09c47192009-01-09 15:36:25 +00001311 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1312 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1313 // user-defined setter/getter. It also synthesizes setter/getter methods
1314 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001315 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1316 E = CDecl->prop_end(); I != E; ++I)
1317 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001318 CDecl->setAtEndLoc(AtEndLoc);
1319 }
1320 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001321 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001322 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001323 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001324 } else if (ObjCCategoryImplDecl* CatImplClass =
1325 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001326 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001327
Chris Lattner4d391482007-12-12 07:09:47 +00001328 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001329 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001330 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001331 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001332 Categories; Categories = Categories->getNextClassCategory()) {
1333 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001334 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001335 break;
1336 }
1337 }
1338 }
1339 }
1340}
1341
1342
1343/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1344/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001345static Decl::ObjCDeclQualifier
1346CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1347 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1348 if (PQTVal & ObjCDeclSpec::DQ_In)
1349 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1350 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1351 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1352 if (PQTVal & ObjCDeclSpec::DQ_Out)
1353 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1354 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1355 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1356 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1357 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1358 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1359 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001360
1361 return ret;
1362}
1363
1364Sema::DeclTy *Sema::ActOnMethodDeclaration(
1365 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001366 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001367 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001368 Selector Sel,
1369 // optional arguments. The number of types/arguments is obtained
1370 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001372 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001373 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1374 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001375 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001376
1377 // Make sure we can establish a context for the method.
1378 if (!ClassDecl) {
1379 Diag(MethodLoc, diag::error_missing_method_context);
1380 return 0;
1381 }
Chris Lattner4d391482007-12-12 07:09:47 +00001382 QualType resultDeclType;
1383
Steve Naroffccef3712009-02-20 22:59:16 +00001384 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001385 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001386
1387 // Methods cannot return interface types. All ObjC objects are
1388 // passed by reference.
1389 if (resultDeclType->isObjCInterfaceType()) {
1390 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1391 << "returned";
1392 return 0;
1393 }
1394 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001395 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001396
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001397 ObjCMethodDecl* ObjCMethod =
1398 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001399 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001400 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001401 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001402 MethodDeclKind == tok::objc_optional ?
1403 ObjCMethodDecl::Optional :
1404 ObjCMethodDecl::Required);
1405
Chris Lattner0ed844b2008-04-04 06:12:32 +00001406 llvm::SmallVector<ParmVarDecl*, 16> Params;
1407
1408 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1409 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001410 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001411
Steve Naroff6082c622008-12-09 19:36:17 +00001412 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001413 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001414 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001415 if (argType->isArrayType()) { // (char *[]) -> (char **)
1416 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001417 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001418 }
Steve Naroff6082c622008-12-09 19:36:17 +00001419 else if (argType->isFunctionType())
1420 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001421 else if (argType->isObjCInterfaceType()) {
1422 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001423 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1424 << "passed";
1425 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001426 return 0;
1427 }
Steve Naroff6082c622008-12-09 19:36:17 +00001428 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001429 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001430 ParmVarDecl* Param;
1431 if (originalArgType.isNull())
1432 Param = ParmVarDecl::Create(Context, ObjCMethod,
1433 SourceLocation(/*FIXME*/),
1434 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001435 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001436 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001437 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1438 SourceLocation(/*FIXME*/),
1439 ArgNames[i], argType, originalArgType,
1440 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001441
Chris Lattner0ed844b2008-04-04 06:12:32 +00001442 Param->setObjCDeclQualifier(
1443 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1444 Params.push_back(Param);
1445 }
1446
Chris Lattner38af2de2009-02-20 21:35:13 +00001447 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448 ObjCMethod->setObjCDeclQualifier(
1449 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1450 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001451
1452 if (AttrList)
1453 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001454
1455 // For implementations (which can be very "coarse grain"), we add the
1456 // method now. This allows the AST to implement lookup methods that work
1457 // incrementally (without waiting until we parse the @end). It also allows
1458 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001459 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001460 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001461 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001462 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001464 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001465 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001466 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001467 }
1468 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001470 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001471 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001472 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001474 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001475 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001477 }
1478 }
1479 if (PrevMethod) {
1480 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001481 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001482 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001483 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001484 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001486}
1487
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001488void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1489 SourceLocation Loc,
1490 unsigned &Attributes) {
1491 // FIXME: Improve the reported location.
1492
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001493 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001494 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001495 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1496 ObjCDeclSpec::DQ_PR_assign |
1497 ObjCDeclSpec::DQ_PR_copy |
1498 ObjCDeclSpec::DQ_PR_retain))) {
1499 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1500 "readwrite" :
1501 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1502 "assign" :
1503 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1504 "copy" : "retain";
1505
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001506 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001507 diag::err_objc_property_attr_mutually_exclusive :
1508 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001509 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001510 }
1511
1512 // Check for copy or retain on non-object types.
1513 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1514 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001515 Diag(Loc, diag::err_objc_property_requires_object)
1516 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001517 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1518 }
1519
1520 // Check for more than one of { assign, copy, retain }.
1521 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1522 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001523 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1524 << "assign" << "copy";
1525 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001526 }
1527 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001528 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1529 << "assign" << "retain";
1530 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001531 }
1532 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1533 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001534 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1535 << "copy" << "retain";
1536 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001537 }
1538 }
1539
1540 // Warn if user supplied no assignment attribute, property is
1541 // readwrite, and this is an object type.
1542 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1543 ObjCDeclSpec::DQ_PR_retain)) &&
1544 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1545 Context.isObjCObjectPointerType(PropertyTy)) {
1546 // Skip this warning in gc-only mode.
1547 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1548 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1549
1550 // If non-gc code warn that this is likely inappropriate.
1551 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1552 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1553
1554 // FIXME: Implement warning dependent on NSCopying being
1555 // implemented. See also:
1556 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1557 // (please trim this list while you are at it).
1558 }
1559}
1560
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001561Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1562 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001563 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001564 Selector GetterSel,
1565 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001566 DeclTy *ClassCategory,
1567 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001568 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001569 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001570 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1571 // default is readwrite!
1572 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1573 // property is defaulted to 'assign' if it is readwrite and is
1574 // not retain or copy
1575 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1576 (isReadWrite &&
1577 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1578 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1579 QualType T = GetTypeForDeclarator(FD.D, S);
1580 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001581
1582 // May modify Attributes.
1583 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001584
1585 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1586 if (!CDecl->getIdentifier()) {
1587 // This is an anonymous category. property requires special
1588 // handling.
1589 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1590 if (ObjCPropertyDecl *PIDecl =
1591 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1592 // property 'PIDecl's readonly attribute will be over-ridden
1593 // with anonymous category's readwrite property attribute!
1594 unsigned PIkind = PIDecl->getPropertyAttributes();
1595 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001596 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001597 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1598 Diag(AtLoc, diag::warn_property_attr_mismatch);
1599 PIDecl->makeitReadWriteAttribute();
1600 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1601 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1602 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1603 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1604 PIDecl->setSetterName(SetterSel);
1605 // FIXME: use a common routine with addPropertyMethods.
1606 ObjCMethodDecl *SetterDecl =
1607 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1608 Context.VoidTy,
1609 ICDecl,
1610 true, false, true,
1611 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001612 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001613 SourceLocation(),
1614 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001615 T, VarDecl::None, 0);
1616 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001617 PIDecl->setSetterMethodDecl(SetterDecl);
1618 }
1619 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001620 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001621 *isOverridingProperty = true;
1622 return 0;
1623 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001624 // No matching property found in the main class. Just fall thru
1625 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001626 // it works as is. This category becomes just like a category
1627 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001628 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001629 Diag(CDecl->getLocation(), diag::err_continuation_class);
1630 *isOverridingProperty = true;
1631 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001632 }
1633 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001634
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001635 Type *t = T.getTypePtr();
1636 if (t->isArrayType() || t->isFunctionType())
1637 Diag(AtLoc, diag::err_property_type) << T;
1638
Steve Naroff93983f82009-01-11 12:47:58 +00001639 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1640 assert(DC && "ClassDecl is not a DeclContext");
1641 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001642 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001643 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001644
1645 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001646
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001647 // Regardless of setter/getter attribute, we save the default getter/setter
1648 // selector names in anticipation of declaration of setter/getter methods.
1649 PDecl->setGetterName(GetterSel);
1650 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001651
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001652 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001654
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001655 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001656 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001657
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001658 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001659 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001660
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001661 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001662 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001663
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001664 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001665 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001666
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001667 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001668 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001669
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001670 if (isAssign)
1671 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1672
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001673 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001674 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001675
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001676 if (MethodImplKind == tok::objc_required)
1677 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1678 else if (MethodImplKind == tok::objc_optional)
1679 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1680
Chris Lattner4d391482007-12-12 07:09:47 +00001681 return PDecl;
1682}
1683
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001684/// ActOnPropertyImplDecl - This routine performs semantic checks and
1685/// builds the AST node for a property implementation declaration; declared
1686/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001687///
1688Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1689 SourceLocation PropertyLoc,
1690 bool Synthesize,
1691 DeclTy *ClassCatImpDecl,
1692 IdentifierInfo *PropertyId,
1693 IdentifierInfo *PropertyIvar) {
1694 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1695 // Make sure we have a context for the property implementation declaration.
1696 if (!ClassImpDecl) {
1697 Diag(AtLoc, diag::error_missing_property_context);
1698 return 0;
1699 }
1700 ObjCPropertyDecl *property = 0;
1701 ObjCInterfaceDecl* IDecl = 0;
1702 // Find the class or category class where this property must have
1703 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001704 ObjCImplementationDecl *IC = 0;
1705 ObjCCategoryImplDecl* CatImplClass = 0;
1706 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001707 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001708 // We always synthesize an interface for an implementation
1709 // without an interface decl. So, IDecl is always non-zero.
1710 assert(IDecl &&
1711 "ActOnPropertyImplDecl - @implementation without @interface");
1712
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001713 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001714 property = IDecl->FindPropertyDeclaration(PropertyId);
1715 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001716 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001717 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001718 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001719 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001720 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001721 if (Synthesize) {
1722 Diag(AtLoc, diag::error_synthesize_category_decl);
1723 return 0;
1724 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001725 IDecl = CatImplClass->getClassInterface();
1726 if (!IDecl) {
1727 Diag(AtLoc, diag::error_missing_property_interface);
1728 return 0;
1729 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001730 ObjCCategoryDecl *Category =
1731 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1732
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001733 // If category for this implementation not found, it is an error which
1734 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001735 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001736 return 0;
1737 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001738 property = Category->FindPropertyDeclaration(PropertyId);
1739 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001740 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001741 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001742 return 0;
1743 }
1744 }
1745 else {
1746 Diag(AtLoc, diag::error_bad_property_context);
1747 return 0;
1748 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001749 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001750 // Check that we have a valid, previously declared ivar for @synthesize
1751 if (Synthesize) {
1752 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001753 if (!PropertyIvar)
1754 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001755 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001756 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001757 if (!Ivar) {
Steve Narofffbffca62009-03-05 15:45:01 +00001758 if (getLangOptions().ObjCNonFragileABI)
1759 Diag(PropertyLoc, diag::error_synthesized_ivar_yet_not_supported)
1760 << PropertyId;
1761 else
Steve Narofff4c00ff2009-03-03 22:09:41 +00001762 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001763 return 0;
1764 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001765 QualType PropType = Context.getCanonicalType(property->getType());
1766 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1767
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001768 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001769 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001770 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001771 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001772 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001773 return 0;
1774 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001775 else {
1776 // FIXME! Rules for properties are somewhat different that those
1777 // for assignments. Use a new routine to consolidate all cases;
1778 // specifically for property redeclarations as well as for ivars.
1779 QualType lhsType =
1780 Context.getCanonicalType(PropType).getUnqualifiedType();
1781 QualType rhsType =
1782 Context.getCanonicalType(IvarType).getUnqualifiedType();
1783 if (lhsType != rhsType &&
1784 lhsType->isArithmeticType()) {
1785 Diag(PropertyLoc, diag::error_property_ivar_type)
1786 << property->getDeclName() << Ivar->getDeclName();
1787 return 0;
1788 }
Fariborz Jahanian9bc77b22009-02-27 22:38:11 +00001789 // __weak is explicit. So it works on Canonical type.
1790 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1791 Diag(PropertyLoc, diag::error_weak_property)
1792 << property->getDeclName() << Ivar->getDeclName();
1793 return 0;
1794 }
1795 if ((Context.isObjCObjectPointerType(property->getType()) ||
1796 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1797 Diag(PropertyLoc, diag::error_strong_property)
1798 << property->getDeclName() << Ivar->getDeclName();
1799 return 0;
1800 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001801 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001802 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001803 } else if (PropertyIvar) {
1804 // @dynamic
1805 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1806 return 0;
1807 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001808 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001809 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001810 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1811 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001812 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001813 ObjCPropertyImplDecl::Synthesize
1814 : ObjCPropertyImplDecl::Dynamic),
1815 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001816 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001817 if (IC) {
1818 if (Synthesize)
1819 if (ObjCPropertyImplDecl *PPIDecl =
1820 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1821 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1822 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1823 << PropertyIvar;
1824 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1825 }
1826
1827 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1828 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1829 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1830 return 0;
1831 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001832 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001833 }
1834 else {
1835 if (Synthesize)
1836 if (ObjCPropertyImplDecl *PPIDecl =
1837 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1838 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1839 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1840 << PropertyIvar;
1841 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1842 }
1843
1844 if (ObjCPropertyImplDecl *PPIDecl =
1845 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1846 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1847 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1848 return 0;
1849 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001850 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001851 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001852
1853 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001854}
Anders Carlsson15281452008-11-04 16:57:32 +00001855
Chris Lattnercc98eac2008-12-17 07:13:27 +00001856bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001857 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001858 return false;
1859
1860 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1861 D->setInvalidDecl();
1862
1863 return true;
1864}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001865
1866/// Collect the instance variables declared in an Objective-C object. Used in
1867/// the creation of structures from objects using the @defs directive.
1868/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1869/// part of the AST generation logic of @defs.
1870static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1871 ASTContext& Ctx,
1872 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1873 if (Class->getSuperClass())
1874 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1875
1876 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1877 for (ObjCInterfaceDecl::ivar_iterator
1878 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1879
1880 ObjCIvarDecl* ID = *I;
1881 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1882 ID->getLocation(),
1883 ID->getIdentifier(),
1884 ID->getType(),
1885 ID->getBitWidth()));
1886 }
1887}
1888
1889/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1890/// instance variables of ClassName into Decls.
1891void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1892 IdentifierInfo *ClassName,
1893 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1894 // Check that ClassName is a valid class
1895 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1896 if (!Class) {
1897 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1898 return;
1899 }
1900 // Collect the instance variables
1901 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1902
1903 // Introduce all of these fields into the appropriate scope.
1904 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1905 D != Decls.end(); ++D) {
1906 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1907 if (getLangOptions().CPlusPlus)
1908 PushOnScopeChains(cast<FieldDecl>(FD), S);
1909 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001910 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001911 }
1912}
1913