blob: cc867862552379e870c7622b886cba993a1b152d [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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018using namespace clang;
19
Ted Kremeneka526c5c2008-01-07 19:49:32 +000020/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000021/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000023 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000024 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
25
26 // If we don't have a valid method decl, simply return.
27 if (!MDecl)
28 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000029
30 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000031 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000032 AddInstanceMethodToGlobalPool(MDecl);
33 else
34 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000035
36 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000037 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000038
Steve Narofff3cf8972009-02-28 16:48:43 +000039 ActiveScope = FnBodyScope;
40
Chris Lattner4d391482007-12-12 07:09:47 +000041 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000046
Daniel Dunbar451318c2008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000049
Chris Lattner8123a952008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000051 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
52 E = MDecl->param_end(); PI != E; ++PI)
53 if ((*PI)->getIdentifier())
54 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000055}
56
Chris Lattner7caeabd2008-07-21 22:17:28 +000057Sema::DeclTy *Sema::
58ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
59 IdentifierInfo *ClassName, SourceLocation ClassLoc,
60 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000061 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000062 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000063 assert(ClassName && "Missing class identifier");
64
65 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000066 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000067 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000068 // Maybe we will complain about the shadowed template parameter.
69 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
70 // Just pretend that we didn't see the previous declaration.
71 PrevDecl = 0;
72 }
73
Ted Kremeneka526c5c2008-01-07 19:49:32 +000074 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000075 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000076 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000077 }
78
Ted Kremeneka526c5c2008-01-07 19:49:32 +000079 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000080 if (IDecl) {
81 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000082 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000083 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000084 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000085 Diag(IDecl->getLocation(), diag::note_previous_definition);
86
Steve Naroffcfe8bf32008-11-18 19:15:30 +000087 // Return the previous class interface.
88 // FIXME: don't leak the objects passed in!
89 return IDecl;
90 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000091 IDecl->setLocation(AtInterfaceLoc);
92 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000093 }
Chris Lattnerb752f282008-07-21 07:06:49 +000094 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000095 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000096 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000097 if (AttrList)
98 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000099
Steve Naroff31102512008-04-02 18:30:49 +0000100 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000101 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000102 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
104 TUScope->AddDecl(IDecl);
105 }
106
107 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
Steve Naroff818cb9e2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000113
114 // Diagnose classes that inherit from deprecated classes.
115 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000116 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000117
Steve Naroff818cb9e2009-02-04 17:14:05 +0000118 if (PrevDecl && SuperClassDecl == 0) {
119 // The previous declaration was not a class decl. Check if we have a
120 // typedef. If we do, get the underlying class type.
121 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
122 QualType T = TDecl->getUnderlyingType();
123 if (T->isObjCInterfaceType()) {
124 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
125 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
126 }
127 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000128
Steve Naroff818cb9e2009-02-04 17:14:05 +0000129 // This handles the following case:
130 //
131 // typedef int SuperClass;
132 // @interface MyClass : SuperClass {} @end
133 //
134 if (!SuperClassDecl) {
135 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
136 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
137 }
138 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000139
Steve Naroff818cb9e2009-02-04 17:14:05 +0000140 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
141 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000146 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000147 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000148 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000149 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000150 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000151 IDecl->setLocEnd(SuperLoc);
152 } else { // we have a root class.
153 IDecl->setLocEnd(ClassLoc);
154 }
155
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000156 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000157 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000158 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
159 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 IDecl->setLocEnd(EndProtoLoc);
161 }
Anders Carlsson15281452008-11-04 16:57:32 +0000162
163 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000164 return IDecl;
165}
166
167/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000168/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000169Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
170 IdentifierInfo *AliasName,
171 SourceLocation AliasLocation,
172 IdentifierInfo *ClassName,
173 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000174 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000175 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000176 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000178 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000179 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000180 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000181 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000182 return 0;
183 }
184 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000185 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000186 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
187 QualType T = TDecl->getUnderlyingType();
188 if (T->isObjCInterfaceType()) {
189 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
190 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000191 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000192 }
193 }
194 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000195 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
196 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000197 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000198 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000199 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000200 return 0;
201 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000202
203 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000205 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000206
207 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000208
209 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000210 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000211 if (!CheckObjCDeclScope(AliasDecl))
212 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000213
Chris Lattner4d391482007-12-12 07:09:47 +0000214 return AliasDecl;
215}
216
Chris Lattnere13b9592008-07-26 04:03:38 +0000217Sema::DeclTy *
218Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
219 IdentifierInfo *ProtocolName,
220 SourceLocation ProtocolLoc,
221 DeclTy * const *ProtoRefs,
222 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000223 SourceLocation EndProtoLoc,
224 AttributeList *AttrList) {
225 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000226 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000228 if (PDecl) {
229 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000230 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000231 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000232 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000233 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000234 // Just return the protocol we already had.
235 // FIXME: don't leak the objects passed in!
236 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000237 }
Steve Narofff11b5082008-08-13 16:39:22 +0000238 // Make sure the cached decl gets a valid start location.
239 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000240 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000241 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000242 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
243 AtProtoInterfaceLoc,ProtocolName);
244 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000245 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000246 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000248 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000249 if (AttrList)
250 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000251 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000252 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000253 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000254 PDecl->setLocEnd(EndProtoLoc);
255 }
Anders Carlsson15281452008-11-04 16:57:32 +0000256
257 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000258 return PDecl;
259}
260
261/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000262/// issues an error if they are not declared. It returns list of
263/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000264void
Chris Lattnere13b9592008-07-26 04:03:38 +0000265Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000266 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000267 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000268 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000269 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000270 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
271 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000272 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000273 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000274 continue;
275 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000276
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000277 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000278
279 // If this is a forward declaration and we are supposed to warn in this
280 // case, do it.
281 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000282 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000283 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000284 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000285 }
286}
287
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000288/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000289/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000290///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000291void
292Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
293 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000294 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000295 ObjCPropertyDecl::PropertyAttributeKind CAttr =
296 Property->getPropertyAttributes();
297 ObjCPropertyDecl::PropertyAttributeKind SAttr =
298 SuperProperty->getPropertyAttributes();
299 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
300 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000301 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000302 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
304 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000305 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000306 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000307 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
308 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000309 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000311
312 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
313 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000317 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000320 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000321 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000322
Chris Lattner717250a2008-07-26 20:50:02 +0000323 if (Context.getCanonicalType(Property->getType()) !=
324 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000325 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327
328}
329
330/// ComparePropertiesInBaseAndSuper - This routine compares property
331/// declarations in base and its super class, if any, and issues
332/// diagnostics in a variety of inconsistant situations.
333///
Chris Lattner70f19542009-02-16 21:26:43 +0000334void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
336 if (!SDecl)
337 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000338 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000339 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
340 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000341 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000342 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000343 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
344 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000345 ObjCPropertyDecl *PDecl = (*I);
346 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000347 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000348 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000349 }
350 }
351}
352
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000353/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
354/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000355/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000356void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000357Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000358 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000359 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
360 if (!IDecl) {
361 // Category
362 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
363 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000364 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
365 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000367 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000369 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000370 CP != CE; ++CP)
371 if ((*CP)->getIdentifier() == Pr->getIdentifier())
372 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000373 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000374 // Property protocol already exist in class. Diagnose any mismatch.
375 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
376 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000377 return;
378 }
Steve Naroff09c47192009-01-09 15:36:25 +0000379 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
380 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000382 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000384 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385 CP != CE; ++CP)
386 if ((*CP)->getIdentifier() == Pr->getIdentifier())
387 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000388 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000390 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000391 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000392}
393
394/// MergeProtocolPropertiesIntoClass - This routine merges properties
395/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000396/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000397///
Chris Lattner70f19542009-02-16 21:26:43 +0000398void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
399 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000400 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000401 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
402
403 if (!IDecl) {
404 // Category
405 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
406 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
407 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
408 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
409 E = MDecl->protocol_end(); P != E; ++P)
410 // Merge properties of category (*P) into IDECL's
411 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
412
413 // Go thru the list of protocols for this category and recursively merge
414 // their properties into this class as well.
415 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
416 E = CatDecl->protocol_end(); P != E; ++P)
417 MergeProtocolPropertiesIntoClass(CatDecl, *P);
418 } else {
419 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
420 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
421 E = MD->protocol_end(); P != E; ++P)
422 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
423 }
424 return;
425 }
426
Chris Lattnerb752f282008-07-21 07:06:49 +0000427 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
429 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000431 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
432
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000433 // Go thru the list of protocols for this class and recursively merge
434 // their properties into this class as well.
435 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
436 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000437 MergeProtocolPropertiesIntoClass(IDecl, *P);
438 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000439 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
440 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
441 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000443 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000444}
445
Chris Lattner4d391482007-12-12 07:09:47 +0000446/// ActOnForwardProtocolDeclaration -
447Action::DeclTy *
448Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000449 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000450 unsigned NumElts,
451 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000452 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000453
454 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000455 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000456 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000457 if (PDecl == 0) { // Not already seen?
458 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
459 IdentList[i].second, Ident);
460 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000461 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000462 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000463 if (attrList)
464 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000465 Protocols.push_back(PDecl);
466 }
Anders Carlsson15281452008-11-04 16:57:32 +0000467
468 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000469 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000470 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000471 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000472 CheckObjCDeclScope(PDecl);
473 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000474}
475
Chris Lattner7caeabd2008-07-21 22:17:28 +0000476Sema::DeclTy *Sema::
477ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
478 IdentifierInfo *ClassName, SourceLocation ClassLoc,
479 IdentifierInfo *CategoryName,
480 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000481 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000482 unsigned NumProtoRefs,
483 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000484 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000485 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
486 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000487 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000488
489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000490 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000491 if (!IDecl || IDecl->isForwardDecl()) {
492 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000494 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000495 }
Chris Lattner4d391482007-12-12 07:09:47 +0000496
Chris Lattner70f19542009-02-16 21:26:43 +0000497 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000498
499 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000500 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000501
502 /// Check for duplicate interface declaration for this category
503 ObjCCategoryDecl *CDeclChain;
504 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
505 CDeclChain = CDeclChain->getNextClassCategory()) {
506 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
507 Diag(CategoryLoc, diag::warn_dup_category_def)
508 << ClassName << CategoryName;
509 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
510 break;
511 }
512 }
513 if (!CDeclChain)
514 CDecl->insertNextClassCategory();
515
Chris Lattner4d391482007-12-12 07:09:47 +0000516 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000517 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000518 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000519 }
Anders Carlsson15281452008-11-04 16:57:32 +0000520
521 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000522 return CDecl;
523}
524
525/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000526/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000527/// object.
528Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
529 SourceLocation AtCatImplLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
531 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000532 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000533 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000534 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
535 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000536 /// Check that class of this category is already completely declared.
537 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000539
Douglas Gregord0434102009-01-09 00:49:46 +0000540 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000541 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000542
Chris Lattner4d391482007-12-12 07:09:47 +0000543 /// TODO: Check that CatName, category name, is not used in another
544 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000545 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000546
547 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000548 return CDecl;
549}
550
551Sema::DeclTy *Sema::ActOnStartClassImplementation(
552 SourceLocation AtClassImplLoc,
553 IdentifierInfo *ClassName, SourceLocation ClassLoc,
554 IdentifierInfo *SuperClassname,
555 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000556 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000557 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000558 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000559 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000560 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000561 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000562 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000563 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000565 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000566 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000567 }
568
569 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000571 if (SuperClassname) {
572 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000573 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000575 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
576 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000577 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000578 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 Diag(SuperClassLoc, diag::err_undef_superclass)
582 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000583 else if (IDecl && IDecl->getSuperClass() != SDecl) {
584 // This implementation and its interface do not have the same
585 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000586 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000587 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000588 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000589 }
590 }
591 }
592
593 if (!IDecl) {
594 // Legacy case of @implementation with no corresponding @interface.
595 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000596
597 // FIXME: Do we support attributes on the @implementation? If so
598 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000599 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
600 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000601 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000602 IDecl->setSuperClass(SDecl);
603 IDecl->setLocEnd(ClassLoc);
604
Douglas Gregord0434102009-01-09 00:49:46 +0000605 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000606 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 // Remember that this needs to be removed when the scope is popped.
608 TUScope->AddDecl(IDecl);
609 }
610
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000612 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000613 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000614
Douglas Gregord0434102009-01-09 00:49:46 +0000615 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000616 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000617
Anders Carlsson15281452008-11-04 16:57:32 +0000618 if (CheckObjCDeclScope(IMPDecl))
619 return IMPDecl;
620
Chris Lattner4d391482007-12-12 07:09:47 +0000621 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000623 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000625 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000627 return IMPDecl;
628}
629
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
631 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000632 SourceLocation RBrace) {
633 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000634 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000635 if (!IDecl)
636 return;
637 /// Check case of non-existing @interface decl.
638 /// (legacy objective-c @implementation decl without an @interface decl).
639 /// Add implementations's ivar to the synthesize class's ivar list.
640 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000641 IDecl->setIVarList(ivars, numIvars, Context);
642 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000643 return;
644 }
645 // If implementation has empty ivar list, just return.
646 if (numIvars == 0)
647 return;
648
649 assert(ivars && "missing @implementation ivars");
650
651 // Check interface's Ivar list against those in the implementation.
652 // names and types must match.
653 //
Chris Lattner4d391482007-12-12 07:09:47 +0000654 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000656 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
657 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658 ObjCIvarDecl* ImplIvar = ivars[j++];
659 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000660 assert (ImplIvar && "missing implementation ivar");
661 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000662 if (Context.getCanonicalType(ImplIvar->getType()) !=
663 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000664 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000665 << ImplIvar->getIdentifier()
666 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000667 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000668 }
669 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
670 // as error.
671 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000672 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000673 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000674 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000675 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000676 }
677 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000678 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000679
680 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000681 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000682 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000683 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000684}
685
Steve Naroff3c2eb662008-02-10 21:38:56 +0000686void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
687 bool &IncompleteImpl) {
688 if (!IncompleteImpl) {
689 Diag(ImpLoc, diag::warn_incomplete_impl);
690 IncompleteImpl = true;
691 }
Chris Lattner08631c52008-11-23 21:45:46 +0000692 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000693}
694
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000695void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
696 ObjCMethodDecl *IntfMethodDecl) {
697 bool err = false;
698 QualType ImpMethodQType =
699 Context.getCanonicalType(ImpMethodDecl->getResultType());
700 QualType IntfMethodQType =
701 Context.getCanonicalType(IntfMethodDecl->getResultType());
702 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
703 err = true;
704 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
705 IF=IntfMethodDecl->param_begin(),
706 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
707 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
708 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
709 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
710 err = true;
711 break;
712 }
713 }
714 if (err) {
715 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
716 << ImpMethodDecl->getDeclName();
717 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
718 }
719}
720
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000721/// isPropertyReadonly - Return true if property is readonly, by searching
722/// for the property in the class and in its categories and implementations
723///
724bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000725 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000726 // by far the most common case.
727 if (!PDecl->isReadOnly())
728 return false;
729 // Even if property is ready only, if interface has a user defined setter,
730 // it is not considered read only.
731 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
732 return false;
733
734 // Main class has the property as 'readonly'. Must search
735 // through the category list to see if the property's
736 // attribute has been over-ridden to 'readwrite'.
737 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
738 Category; Category = Category->getNextClassCategory()) {
739 // Even if property is ready only, if a category has a user defined setter,
740 // it is not considered read only.
741 if (Category->getInstanceMethod(PDecl->getSetterName()))
742 return false;
743 ObjCPropertyDecl *P =
744 Category->FindPropertyDeclaration(PDecl->getIdentifier());
745 if (P && !P->isReadOnly())
746 return false;
747 }
748
749 // Also, check for definition of a setter method in the implementation if
750 // all else failed.
751 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
752 if (ObjCImplementationDecl *IMD =
753 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
754 if (IMD->getInstanceMethod(PDecl->getSetterName()))
755 return false;
756 }
757 else if (ObjCCategoryImplDecl *CIMD =
758 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
759 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
760 return false;
761 }
762 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000763 // Lastly, look through the implementation (if one is in scope).
764 if (ObjCImplementationDecl *ImpDecl =
765 ObjCImplementations[IDecl->getIdentifier()])
766 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
767 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000768 return true;
769}
770
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000771/// FIXME: Type hierarchies in Objective-C can be deep. We could most
772/// likely improve the efficiency of selector lookups and type
773/// checking by associating with each protocol / interface / category
774/// the flattened instance tables. If we used an immutable set to keep
775/// the table then it wouldn't add significant memory cost and it
776/// would be handy for lookups.
777
Steve Naroffefe7f362008-02-08 22:06:17 +0000778/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000779/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000780void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
781 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000782 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000783 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000784 const llvm::DenseSet<Selector> &ClsMap,
785 ObjCInterfaceDecl *IDecl) {
786 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
787
788 // If a method lookup fails locally we still need to look and see if
789 // the method was implemented by a base class or an inherited
790 // protocol. This lookup is slow, but occurs rarely in correct code
791 // and otherwise would terminate in a warning.
792
Chris Lattner4d391482007-12-12 07:09:47 +0000793 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000795 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000796 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000797 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000798 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000799 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000800 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000801 }
802 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000804 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000806 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
807 !ClsMap.count(method->getSelector()) &&
808 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000809 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000810 }
Chris Lattner780f3292008-07-21 21:32:27 +0000811 // Check on this protocols's referenced protocols, recursively.
812 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
813 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000814 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000815}
816
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
818 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000819 llvm::DenseSet<Selector> InsMap;
820 // Check and see if instance methods in class interface have been
821 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000823 E = IMPDecl->instmeth_end(); I != E; ++I)
824 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000825
826 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000828 E = IDecl->instmeth_end(); I != E; ++I) {
829 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000830 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000831 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000832 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000833
834 ObjCMethodDecl *ImpMethodDecl =
835 IMPDecl->getInstanceMethod((*I)->getSelector());
836 ObjCMethodDecl *IntfMethodDecl =
837 IDecl->getInstanceMethod((*I)->getSelector());
838 assert(IntfMethodDecl &&
839 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
840 // ImpMethodDecl may be null as in a @dynamic property.
841 if (ImpMethodDecl)
842 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
843 }
Chris Lattner4c525092007-12-12 17:58:05 +0000844
Chris Lattner4d391482007-12-12 07:09:47 +0000845 llvm::DenseSet<Selector> ClsMap;
846 // Check and see if class methods in class interface have been
847 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000849 E = IMPDecl->classmeth_end(); I != E; ++I)
850 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000851
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000852 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000853 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000854 if (!ClsMap.count((*I)->getSelector()))
855 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000856 else {
857 ObjCMethodDecl *ImpMethodDecl =
858 IMPDecl->getClassMethod((*I)->getSelector());
859 ObjCMethodDecl *IntfMethodDecl =
860 IDecl->getClassMethod((*I)->getSelector());
861 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
862 }
863
Chris Lattner4d391482007-12-12 07:09:47 +0000864
865 // Check the protocol list for unimplemented methods in the @implementation
866 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000867 const ObjCList<ObjCProtocolDecl> &Protocols =
868 IDecl->getReferencedProtocols();
869 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
870 E = Protocols.end(); I != E; ++I)
871 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000872 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000873}
874
875/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000876/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
878 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000879 llvm::DenseSet<Selector> InsMap;
880 // Check and see if instance methods in category interface have been
881 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000883 E = CatImplDecl->instmeth_end(); I != E; ++I)
884 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000885
886 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000887 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000888 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000889 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000890 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000891 else {
892 ObjCMethodDecl *ImpMethodDecl =
893 CatImplDecl->getInstanceMethod((*I)->getSelector());
894 ObjCMethodDecl *IntfMethodDecl =
895 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000896 assert(IntfMethodDecl &&
897 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
898 // ImpMethodDecl may be null as in a @dynamic property.
899 if (ImpMethodDecl)
900 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000901 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000902
Chris Lattner4d391482007-12-12 07:09:47 +0000903 llvm::DenseSet<Selector> ClsMap;
904 // Check and see if class methods in category interface have been
905 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000907 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
908 I != E; ++I)
909 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000910
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000911 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000912 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000913 if (!ClsMap.count((*I)->getSelector()))
914 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000915 else {
916 ObjCMethodDecl *ImpMethodDecl =
917 CatImplDecl->getClassMethod((*I)->getSelector());
918 ObjCMethodDecl *IntfMethodDecl =
919 CatClassDecl->getClassMethod((*I)->getSelector());
920 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
921 }
Chris Lattner4d391482007-12-12 07:09:47 +0000922 // Check the protocol list for unimplemented methods in the @implementation
923 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000924 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
925 E = CatClassDecl->protocol_end(); PI != E; ++PI)
926 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000927 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000928}
929
930/// ActOnForwardClassDeclaration -
931Action::DeclTy *
932Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000933 IdentifierInfo **IdentList,
934 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000935 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000936
937 for (unsigned i = 0; i != NumElts; ++i) {
938 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000939 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000940 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000941 // Maybe we will complain about the shadowed template parameter.
942 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
943 // Just pretend that we didn't see the previous declaration.
944 PrevDecl = 0;
945 }
946
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000947 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000948 // GCC apparently allows the following idiom:
949 //
950 // typedef NSObject < XCElementTogglerP > XCElementToggler;
951 // @class XCElementToggler;
952 //
953 // FIXME: Make an extension?
954 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
955 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000957 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000958 }
Chris Lattner4d391482007-12-12 07:09:47 +0000959 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000961 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000962 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
963 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000964 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000965
Douglas Gregord0434102009-01-09 00:49:46 +0000966 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000967 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // Remember that this needs to be removed when the scope is popped.
969 TUScope->AddDecl(IDecl);
970 }
971
972 Interfaces.push_back(IDecl);
973 }
974
Douglas Gregord0434102009-01-09 00:49:46 +0000975 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000976 &Interfaces[0],
977 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000978 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000979 CheckObjCDeclScope(CDecl);
980 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000981}
982
983
984/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
985/// returns true, or false, accordingly.
986/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000987bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000988 const ObjCMethodDecl *PrevMethod,
989 bool matchBasedOnSizeAndAlignment) {
990 QualType T1 = Context.getCanonicalType(Method->getResultType());
991 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
992
993 if (T1 != T2) {
994 // The result types are different.
995 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000996 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000997 // Incomplete types don't have a size and alignment.
998 if (T1->isIncompleteType() || T2->isIncompleteType())
999 return false;
1000 // Check is based on size and alignment.
1001 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1002 return false;
1003 }
Chris Lattner89951a82009-02-20 18:43:26 +00001004
1005 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1006 E = Method->param_end();
1007 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1008
1009 for (; ParamI != E; ++ParamI, ++PrevI) {
1010 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1011 T1 = Context.getCanonicalType((*ParamI)->getType());
1012 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001013 if (T1 != T2) {
1014 // The result types are different.
1015 if (!matchBasedOnSizeAndAlignment)
1016 return false;
1017 // Incomplete types don't have a size and alignment.
1018 if (T1->isIncompleteType() || T2->isIncompleteType())
1019 return false;
1020 // Check is based on size and alignment.
1021 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1022 return false;
1023 }
Chris Lattner4d391482007-12-12 07:09:47 +00001024 }
1025 return true;
1026}
1027
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1029 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001030 if (!FirstMethod.Method) {
1031 // Haven't seen a method with this selector name yet - add it.
1032 FirstMethod.Method = Method;
1033 FirstMethod.Next = 0;
1034 } else {
1035 // We've seen a method with this name, now check the type signature(s).
1036 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1037
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001038 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001039 Next = Next->Next)
1040 match = MatchTwoMethodDeclarations(Method, Next->Method);
1041
1042 if (!match) {
1043 // We have a new signature for an existing method - add it.
1044 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001045 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001046 }
1047 }
1048}
1049
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001050// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001051ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1052 SourceRange R) {
1053 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001054 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001055
1056 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001057 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1058 // This checks if the methods differ by size & alignment.
1059 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1060 issueWarning = true;
1061 }
1062 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001063 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001064 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001065 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001066 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001067 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001068 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001069 }
1070 return MethList.Method;
1071}
1072
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001073void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1074 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001075 if (!FirstMethod.Method) {
1076 // Haven't seen a method with this selector name yet - add it.
1077 FirstMethod.Method = Method;
1078 FirstMethod.Next = 0;
1079 } else {
1080 // We've seen a method with this name, now check the type signature(s).
1081 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1082
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001083 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001084 Next = Next->Next)
1085 match = MatchTwoMethodDeclarations(Method, Next->Method);
1086
1087 if (!match) {
1088 // We have a new signature for an existing method - add it.
1089 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001090 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001091 FirstMethod.Next = OMI;
1092 }
1093 }
1094}
1095
Steve Naroff0701bbb2009-01-08 17:28:14 +00001096/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1097/// have the property type and issue diagnostics if they don't.
1098/// Also synthesize a getter/setter method if none exist (and update the
1099/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1100/// methods is the "right" thing to do.
1101void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1102 ObjCContainerDecl *CD) {
1103 ObjCMethodDecl *GetterMethod, *SetterMethod;
1104
1105 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1106 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1107
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001108 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001109 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001110 Diag(property->getLocation(),
1111 diag::err_accessor_property_type_mismatch)
1112 << property->getDeclName()
1113 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001114 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1115 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001116
1117 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001118 if (Context.getCanonicalType(SetterMethod->getResultType())
1119 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001120 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001121 if (SetterMethod->param_size() != 1 ||
1122 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001123 Diag(property->getLocation(),
1124 diag::err_accessor_property_type_mismatch)
1125 << property->getDeclName()
1126 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001127 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1128 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001129 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001130
1131 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001132 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001133 // FIXME: The synthesized property we set here is misleading. We
1134 // almost always synthesize these methods unless the user explicitly
1135 // provided prototypes (which is odd, but allowed). Sema should be
1136 // typechecking that the declarations jive in that situation (which
1137 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001138 if (!GetterMethod) {
1139 // No instance method of same name as property getter name was found.
1140 // Declare a getter method and add it to the list of methods
1141 // for this class.
1142 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1143 property->getLocation(), property->getGetterName(),
1144 property->getType(), CD, true, false, true,
1145 (property->getPropertyImplementation() ==
1146 ObjCPropertyDecl::Optional) ?
1147 ObjCMethodDecl::Optional :
1148 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001149 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001150 } else
1151 // A user declared getter will be synthesize when @synthesize of
1152 // the property with the same name is seen in the @implementation
1153 GetterMethod->setIsSynthesized();
1154 property->setGetterMethodDecl(GetterMethod);
1155
1156 // Skip setter if property is read-only.
1157 if (!property->isReadOnly()) {
1158 // Find the default setter and if one not found, add one.
1159 if (!SetterMethod) {
1160 // No instance method of same name as property setter name was found.
1161 // Declare a setter method and add it to the list of methods
1162 // for this class.
1163 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1164 property->getLocation(),
1165 property->getSetterName(),
1166 Context.VoidTy, CD, true, false, true,
1167 (property->getPropertyImplementation() ==
1168 ObjCPropertyDecl::Optional) ?
1169 ObjCMethodDecl::Optional :
1170 ObjCMethodDecl::Required);
1171 // Invent the arguments for the setter. We don't bother making a
1172 // nice name for the argument.
1173 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1174 SourceLocation(),
1175 property->getIdentifier(),
1176 property->getType(),
1177 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001178 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001179 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001180 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001181 } else
1182 // A user declared setter will be synthesize when @synthesize of
1183 // the property with the same name is seen in the @implementation
1184 SetterMethod->setIsSynthesized();
1185 property->setSetterMethodDecl(SetterMethod);
1186 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001187 // Add any synthesized methods to the global pool. This allows us to
1188 // handle the following, which is supported by GCC (and part of the design).
1189 //
1190 // @interface Foo
1191 // @property double bar;
1192 // @end
1193 //
1194 // void thisIsUnfortunate() {
1195 // id foo;
1196 // double bar = [foo bar];
1197 // }
1198 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001199 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001200 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001201 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001202 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001203}
1204
Steve Naroffa56f6162007-12-18 01:30:32 +00001205// Note: For class/category implemenations, allMethods/allProperties is
1206// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001207void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1208 DeclTy **allMethods, unsigned allNum,
1209 DeclTy **allProperties, unsigned pNum) {
1210 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1211
Steve Naroffa56f6162007-12-18 01:30:32 +00001212 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1213 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001214 // should be true.
1215 if (!ClassDecl)
1216 return;
1217
Chris Lattner4d391482007-12-12 07:09:47 +00001218 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001219 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1220 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001222
Steve Naroff0701bbb2009-01-08 17:28:14 +00001223 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001224
1225 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1226 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1227 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1228
Chris Lattner4d391482007-12-12 07:09:47 +00001229 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001230 ObjCMethodDecl *Method =
1231 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001232
1233 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001234 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001235 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001236 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001237 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1238 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001239 if ((isInterfaceDeclKind && PrevMethod && !match)
1240 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001241 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001242 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001243 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001244 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001245 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001246 InsMap[Method->getSelector()] = Method;
1247 /// The following allows us to typecheck messages to "id".
1248 AddInstanceMethodToGlobalPool(Method);
1249 }
1250 }
1251 else {
1252 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001254 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1255 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001256 if ((isInterfaceDeclKind && PrevMethod && !match)
1257 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001258 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001259 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001260 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001261 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001262 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001263 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001264 /// The following allows us to typecheck messages to "Class".
1265 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001266 }
1267 }
1268 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001269 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001270 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001271 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001272 ComparePropertiesInBaseAndSuper(I);
1273 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001274 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001275 // Categories are used to extend the class by declaring new methods.
1276 // By the same token, they are also used to add new properties. No
1277 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001278
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001279 // Merge protocol properties into category
1280 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001281 }
Steve Naroff09c47192009-01-09 15:36:25 +00001282 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1283 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1284 // user-defined setter/getter. It also synthesizes setter/getter methods
1285 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001286 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1287 E = CDecl->prop_end(); I != E; ++I)
1288 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001289 CDecl->setAtEndLoc(AtEndLoc);
1290 }
1291 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001292 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001293 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001294 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001295 } else if (ObjCCategoryImplDecl* CatImplClass =
1296 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001297 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001298
Chris Lattner4d391482007-12-12 07:09:47 +00001299 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001300 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001301 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001303 Categories; Categories = Categories->getNextClassCategory()) {
1304 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1305 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1306 break;
1307 }
1308 }
1309 }
1310 }
1311}
1312
1313
1314/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1315/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001316static Decl::ObjCDeclQualifier
1317CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1318 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1319 if (PQTVal & ObjCDeclSpec::DQ_In)
1320 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1321 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1322 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1323 if (PQTVal & ObjCDeclSpec::DQ_Out)
1324 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1325 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1326 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1327 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1328 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1329 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1330 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001331
1332 return ret;
1333}
1334
1335Sema::DeclTy *Sema::ActOnMethodDeclaration(
1336 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001337 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001338 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001339 Selector Sel,
1340 // optional arguments. The number of types/arguments is obtained
1341 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001342 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001343 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001344 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1345 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001346 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001347
1348 // Make sure we can establish a context for the method.
1349 if (!ClassDecl) {
1350 Diag(MethodLoc, diag::error_missing_method_context);
1351 return 0;
1352 }
Chris Lattner4d391482007-12-12 07:09:47 +00001353 QualType resultDeclType;
1354
Steve Naroffccef3712009-02-20 22:59:16 +00001355 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001356 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001357
1358 // Methods cannot return interface types. All ObjC objects are
1359 // passed by reference.
1360 if (resultDeclType->isObjCInterfaceType()) {
1361 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1362 << "returned";
1363 return 0;
1364 }
1365 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001367
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001368 ObjCMethodDecl* ObjCMethod =
1369 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001370 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001371 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001372 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001373 MethodDeclKind == tok::objc_optional ?
1374 ObjCMethodDecl::Optional :
1375 ObjCMethodDecl::Required);
1376
Chris Lattner0ed844b2008-04-04 06:12:32 +00001377 llvm::SmallVector<ParmVarDecl*, 16> Params;
1378
1379 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1380 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001381 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001382
Steve Naroff6082c622008-12-09 19:36:17 +00001383 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001384 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001385 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001386 if (argType->isArrayType()) { // (char *[]) -> (char **)
1387 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001388 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001389 }
Steve Naroff6082c622008-12-09 19:36:17 +00001390 else if (argType->isFunctionType())
1391 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001392 else if (argType->isObjCInterfaceType()) {
1393 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001394 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1395 << "passed";
1396 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001397 return 0;
1398 }
Steve Naroff6082c622008-12-09 19:36:17 +00001399 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001400 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001401 ParmVarDecl* Param;
1402 if (originalArgType.isNull())
1403 Param = ParmVarDecl::Create(Context, ObjCMethod,
1404 SourceLocation(/*FIXME*/),
1405 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001406 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001407 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001408 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1409 SourceLocation(/*FIXME*/),
1410 ArgNames[i], argType, originalArgType,
1411 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001412
Chris Lattner0ed844b2008-04-04 06:12:32 +00001413 Param->setObjCDeclQualifier(
1414 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1415 Params.push_back(Param);
1416 }
1417
Chris Lattner38af2de2009-02-20 21:35:13 +00001418 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001419 ObjCMethod->setObjCDeclQualifier(
1420 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1421 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001422
1423 if (AttrList)
1424 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001425
1426 // For implementations (which can be very "coarse grain"), we add the
1427 // method now. This allows the AST to implement lookup methods that work
1428 // incrementally (without waiting until we parse the @end). It also allows
1429 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001431 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001432 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001433 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001435 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001436 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001438 }
1439 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001440 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001441 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001442 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001443 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001445 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001446 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001447 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001448 }
1449 }
1450 if (PrevMethod) {
1451 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001452 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001453 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001454 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001455 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001456 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001457}
1458
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001459void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1460 SourceLocation Loc,
1461 unsigned &Attributes) {
1462 // FIXME: Improve the reported location.
1463
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001464 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001465 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001466 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1467 ObjCDeclSpec::DQ_PR_assign |
1468 ObjCDeclSpec::DQ_PR_copy |
1469 ObjCDeclSpec::DQ_PR_retain))) {
1470 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1471 "readwrite" :
1472 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1473 "assign" :
1474 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1475 "copy" : "retain";
1476
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001477 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001478 diag::err_objc_property_attr_mutually_exclusive :
1479 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001480 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001481 }
1482
1483 // Check for copy or retain on non-object types.
1484 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1485 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001486 Diag(Loc, diag::err_objc_property_requires_object)
1487 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001488 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1489 }
1490
1491 // Check for more than one of { assign, copy, retain }.
1492 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1493 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001494 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1495 << "assign" << "copy";
1496 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001497 }
1498 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001499 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1500 << "assign" << "retain";
1501 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001502 }
1503 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1504 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001505 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1506 << "copy" << "retain";
1507 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001508 }
1509 }
1510
1511 // Warn if user supplied no assignment attribute, property is
1512 // readwrite, and this is an object type.
1513 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1514 ObjCDeclSpec::DQ_PR_retain)) &&
1515 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1516 Context.isObjCObjectPointerType(PropertyTy)) {
1517 // Skip this warning in gc-only mode.
1518 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1519 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1520
1521 // If non-gc code warn that this is likely inappropriate.
1522 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1523 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1524
1525 // FIXME: Implement warning dependent on NSCopying being
1526 // implemented. See also:
1527 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1528 // (please trim this list while you are at it).
1529 }
1530}
1531
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001532Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1533 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001534 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001535 Selector GetterSel,
1536 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001537 DeclTy *ClassCategory,
1538 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001539 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001540 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001541 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1542 // default is readwrite!
1543 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1544 // property is defaulted to 'assign' if it is readwrite and is
1545 // not retain or copy
1546 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1547 (isReadWrite &&
1548 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1549 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1550 QualType T = GetTypeForDeclarator(FD.D, S);
1551 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001552
1553 // May modify Attributes.
1554 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001555
1556 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1557 if (!CDecl->getIdentifier()) {
1558 // This is an anonymous category. property requires special
1559 // handling.
1560 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1561 if (ObjCPropertyDecl *PIDecl =
1562 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1563 // property 'PIDecl's readonly attribute will be over-ridden
1564 // with anonymous category's readwrite property attribute!
1565 unsigned PIkind = PIDecl->getPropertyAttributes();
1566 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001567 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001568 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1569 Diag(AtLoc, diag::warn_property_attr_mismatch);
1570 PIDecl->makeitReadWriteAttribute();
1571 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1572 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1573 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1574 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1575 PIDecl->setSetterName(SetterSel);
1576 // FIXME: use a common routine with addPropertyMethods.
1577 ObjCMethodDecl *SetterDecl =
1578 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1579 Context.VoidTy,
1580 ICDecl,
1581 true, false, true,
1582 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001583 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001584 SourceLocation(),
1585 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001586 T, VarDecl::None, 0);
1587 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001588 PIDecl->setSetterMethodDecl(SetterDecl);
1589 }
1590 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001591 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001592 *isOverridingProperty = true;
1593 return 0;
1594 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001595 // No matching property found in the main class. Just fall thru
1596 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001597 // it works as is. This category becomes just like a category
1598 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001599 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001600 Diag(CDecl->getLocation(), diag::err_continuation_class);
1601 *isOverridingProperty = true;
1602 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001603 }
1604 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001605
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001606 Type *t = T.getTypePtr();
1607 if (t->isArrayType() || t->isFunctionType())
1608 Diag(AtLoc, diag::err_property_type) << T;
1609
Steve Naroff93983f82009-01-11 12:47:58 +00001610 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1611 assert(DC && "ClassDecl is not a DeclContext");
1612 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001613 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001614 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001615
1616 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001617
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001618 // Regardless of setter/getter attribute, we save the default getter/setter
1619 // selector names in anticipation of declaration of setter/getter methods.
1620 PDecl->setGetterName(GetterSel);
1621 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001622
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001623 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001625
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001626 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001628
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001629 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001631
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001632 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001634
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001635 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001636 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001637
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001638 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001640
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001641 if (isAssign)
1642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1643
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001644 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001646
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001647 if (MethodImplKind == tok::objc_required)
1648 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1649 else if (MethodImplKind == tok::objc_optional)
1650 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1651
Chris Lattner4d391482007-12-12 07:09:47 +00001652 return PDecl;
1653}
1654
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001655/// ActOnPropertyImplDecl - This routine performs semantic checks and
1656/// builds the AST node for a property implementation declaration; declared
1657/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001658///
1659Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1660 SourceLocation PropertyLoc,
1661 bool Synthesize,
1662 DeclTy *ClassCatImpDecl,
1663 IdentifierInfo *PropertyId,
1664 IdentifierInfo *PropertyIvar) {
1665 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1666 // Make sure we have a context for the property implementation declaration.
1667 if (!ClassImpDecl) {
1668 Diag(AtLoc, diag::error_missing_property_context);
1669 return 0;
1670 }
1671 ObjCPropertyDecl *property = 0;
1672 ObjCInterfaceDecl* IDecl = 0;
1673 // Find the class or category class where this property must have
1674 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001675 ObjCImplementationDecl *IC = 0;
1676 ObjCCategoryImplDecl* CatImplClass = 0;
1677 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001678 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001679 // We always synthesize an interface for an implementation
1680 // without an interface decl. So, IDecl is always non-zero.
1681 assert(IDecl &&
1682 "ActOnPropertyImplDecl - @implementation without @interface");
1683
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001684 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001685 property = IDecl->FindPropertyDeclaration(PropertyId);
1686 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001687 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001688 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001689 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001690 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001691 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001692 if (Synthesize) {
1693 Diag(AtLoc, diag::error_synthesize_category_decl);
1694 return 0;
1695 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001696 IDecl = CatImplClass->getClassInterface();
1697 if (!IDecl) {
1698 Diag(AtLoc, diag::error_missing_property_interface);
1699 return 0;
1700 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001701 ObjCCategoryDecl *Category =
1702 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1703
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001704 // If category for this implementation not found, it is an error which
1705 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001706 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001707 return 0;
1708 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001709 property = Category->FindPropertyDeclaration(PropertyId);
1710 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001711 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001712 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001713 return 0;
1714 }
1715 }
1716 else {
1717 Diag(AtLoc, diag::error_bad_property_context);
1718 return 0;
1719 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001720 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001721 // Check that we have a valid, previously declared ivar for @synthesize
1722 if (Synthesize) {
1723 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001724 if (!PropertyIvar)
1725 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001726 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001727 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001728 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001729 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001730 return 0;
1731 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001732 QualType PropType = Context.getCanonicalType(property->getType());
1733 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1734
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001735 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001736 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001737 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001738 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001739 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001740 return 0;
1741 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001742 else {
1743 // FIXME! Rules for properties are somewhat different that those
1744 // for assignments. Use a new routine to consolidate all cases;
1745 // specifically for property redeclarations as well as for ivars.
1746 QualType lhsType =
1747 Context.getCanonicalType(PropType).getUnqualifiedType();
1748 QualType rhsType =
1749 Context.getCanonicalType(IvarType).getUnqualifiedType();
1750 if (lhsType != rhsType &&
1751 lhsType->isArithmeticType()) {
1752 Diag(PropertyLoc, diag::error_property_ivar_type)
1753 << property->getDeclName() << Ivar->getDeclName();
1754 return 0;
1755 }
Fariborz Jahanian9bc77b22009-02-27 22:38:11 +00001756 // __weak is explicit. So it works on Canonical type.
1757 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1758 Diag(PropertyLoc, diag::error_weak_property)
1759 << property->getDeclName() << Ivar->getDeclName();
1760 return 0;
1761 }
1762 if ((Context.isObjCObjectPointerType(property->getType()) ||
1763 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1764 Diag(PropertyLoc, diag::error_strong_property)
1765 << property->getDeclName() << Ivar->getDeclName();
1766 return 0;
1767 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001768 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001769 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001770 } else if (PropertyIvar) {
1771 // @dynamic
1772 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1773 return 0;
1774 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001775 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001776 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001777 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1778 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001779 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001780 ObjCPropertyImplDecl::Synthesize
1781 : ObjCPropertyImplDecl::Dynamic),
1782 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001783 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001784 if (IC) {
1785 if (Synthesize)
1786 if (ObjCPropertyImplDecl *PPIDecl =
1787 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1788 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1789 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1790 << PropertyIvar;
1791 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1792 }
1793
1794 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1795 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1796 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1797 return 0;
1798 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001799 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001800 }
1801 else {
1802 if (Synthesize)
1803 if (ObjCPropertyImplDecl *PPIDecl =
1804 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1805 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1806 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1807 << PropertyIvar;
1808 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1809 }
1810
1811 if (ObjCPropertyImplDecl *PPIDecl =
1812 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1813 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1814 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1815 return 0;
1816 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001817 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001818 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001819
1820 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001821}
Anders Carlsson15281452008-11-04 16:57:32 +00001822
Chris Lattnercc98eac2008-12-17 07:13:27 +00001823bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001824 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001825 return false;
1826
1827 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1828 D->setInvalidDecl();
1829
1830 return true;
1831}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001832
1833/// Collect the instance variables declared in an Objective-C object. Used in
1834/// the creation of structures from objects using the @defs directive.
1835/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1836/// part of the AST generation logic of @defs.
1837static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1838 ASTContext& Ctx,
1839 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1840 if (Class->getSuperClass())
1841 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1842
1843 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1844 for (ObjCInterfaceDecl::ivar_iterator
1845 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1846
1847 ObjCIvarDecl* ID = *I;
1848 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1849 ID->getLocation(),
1850 ID->getIdentifier(),
1851 ID->getType(),
1852 ID->getBitWidth()));
1853 }
1854}
1855
1856/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1857/// instance variables of ClassName into Decls.
1858void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1859 IdentifierInfo *ClassName,
1860 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1861 // Check that ClassName is a valid class
1862 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1863 if (!Class) {
1864 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1865 return;
1866 }
1867 // Collect the instance variables
1868 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1869
1870 // Introduce all of these fields into the appropriate scope.
1871 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1872 D != Decls.end(); ++D) {
1873 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1874 if (getLangOptions().CPlusPlus)
1875 PushOnScopeChains(cast<FieldDecl>(FD), S);
1876 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001877 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001878 }
1879}
1880