blob: b8906377671ab0d0fae2f2917cc50c503940bdc8 [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
39 // Create Decl objects for each parameter, entrring them in the scope for
40 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000041
42 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000043 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000044
Daniel Dunbar451318c2008-08-26 06:07:48 +000045 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
46 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000047
Chris Lattner8123a952008-04-10 02:22:51 +000048 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000049 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000050 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Fariborz Jahanian4247c072009-02-16 19:40:26 +000051 if (PDecl->getIdentifier())
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000052 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000053 }
54}
55
Chris Lattner7caeabd2008-07-21 22:17:28 +000056Sema::DeclTy *Sema::
57ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000060 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000076 }
77
Ted Kremeneka526c5c2008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000082 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000083 Diag(IDecl->getLocation(), diag::note_previous_definition);
84
Steve Naroffcfe8bf32008-11-18 19:15:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
87 return IDecl;
88 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000089 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000091 }
Chris Lattnerb752f282008-07-21 07:06:49 +000092 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000093 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000094 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000095 if (AttrList)
96 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000097
Steve Naroff31102512008-04-02 18:30:49 +000098 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +000099 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000100 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 // Remember that this needs to be removed when the scope is popped.
102 TUScope->AddDecl(IDecl);
103 }
104
105 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000106 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000107 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000108
Steve Naroff818cb9e2009-02-04 17:14:05 +0000109 ObjCInterfaceDecl *SuperClassDecl =
110 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000111
112 // Diagnose classes that inherit from deprecated classes.
113 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000114 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000115
Steve Naroff818cb9e2009-02-04 17:14:05 +0000116 if (PrevDecl && SuperClassDecl == 0) {
117 // The previous declaration was not a class decl. Check if we have a
118 // typedef. If we do, get the underlying class type.
119 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
120 QualType T = TDecl->getUnderlyingType();
121 if (T->isObjCInterfaceType()) {
122 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
123 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
124 }
125 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000126
Steve Naroff818cb9e2009-02-04 17:14:05 +0000127 // This handles the following case:
128 //
129 // typedef int SuperClass;
130 // @interface MyClass : SuperClass {} @end
131 //
132 if (!SuperClassDecl) {
133 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
134 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
135 }
136 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000137
Steve Naroff818cb9e2009-02-04 17:14:05 +0000138 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000140 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000141 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000142 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000146 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000148 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 IDecl->setLocEnd(SuperLoc);
150 } else { // we have a root class.
151 IDecl->setLocEnd(ClassLoc);
152 }
153
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000154 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000155 if (NumProtoRefs) {
156 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000157 IDecl->setLocEnd(EndProtoLoc);
158 }
Anders Carlsson15281452008-11-04 16:57:32 +0000159
160 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000161 return IDecl;
162}
163
164/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000165/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000166Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
167 IdentifierInfo *AliasName,
168 SourceLocation AliasLocation,
169 IdentifierInfo *ClassName,
170 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000171 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000172 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000173 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000174 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000175 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000176 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000177 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000179 return 0;
180 }
181 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000182 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000183 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
184 QualType T = TDecl->getUnderlyingType();
185 if (T->isObjCInterfaceType()) {
186 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
187 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000188 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000189 }
190 }
191 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000192 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
193 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000194 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000195 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000196 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000197 return 0;
198 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000199
200 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000202 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000203
204 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000205
206 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000207 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000208 if (!CheckObjCDeclScope(AliasDecl))
209 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000210
Chris Lattner4d391482007-12-12 07:09:47 +0000211 return AliasDecl;
212}
213
Chris Lattnere13b9592008-07-26 04:03:38 +0000214Sema::DeclTy *
215Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
216 IdentifierInfo *ProtocolName,
217 SourceLocation ProtocolLoc,
218 DeclTy * const *ProtoRefs,
219 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000220 SourceLocation EndProtoLoc,
221 AttributeList *AttrList) {
222 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000223 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000225 if (PDecl) {
226 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000227 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000228 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000229 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000230 // Just return the protocol we already had.
231 // FIXME: don't leak the objects passed in!
232 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000233 }
Steve Narofff11b5082008-08-13 16:39:22 +0000234 // Make sure the cached decl gets a valid start location.
235 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000236 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000237 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000238 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
239 AtProtoInterfaceLoc,ProtocolName);
240 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000241 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000242 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000243 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000244 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000245 if (AttrList)
246 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000247 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000248 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000249 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000250 PDecl->setLocEnd(EndProtoLoc);
251 }
Anders Carlsson15281452008-11-04 16:57:32 +0000252
253 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000254 return PDecl;
255}
256
257/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000258/// issues an error if they are not declared. It returns list of
259/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000260void
Chris Lattnere13b9592008-07-26 04:03:38 +0000261Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000262 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000263 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000264 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000265 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000266 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
267 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000268 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000269 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000270 continue;
271 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000272
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000273 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000274
275 // If this is a forward declaration and we are supposed to warn in this
276 // case, do it.
277 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000278 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000279 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000280 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000281 }
282}
283
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000285/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000287void
288Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
289 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000290 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000291 ObjCPropertyDecl::PropertyAttributeKind CAttr =
292 Property->getPropertyAttributes();
293 ObjCPropertyDecl::PropertyAttributeKind SAttr =
294 SuperProperty->getPropertyAttributes();
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
296 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000297 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000298 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000299 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
300 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000301 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000302 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
304 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000307
308 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
309 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000310 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000311 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000313 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000314 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000315 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000316 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000317 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000318
Chris Lattner717250a2008-07-26 20:50:02 +0000319 if (Context.getCanonicalType(Property->getType()) !=
320 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000323
324}
325
326/// ComparePropertiesInBaseAndSuper - This routine compares property
327/// declarations in base and its super class, if any, and issues
328/// diagnostics in a variety of inconsistant situations.
329///
Chris Lattner70f19542009-02-16 21:26:43 +0000330void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
332 if (!SDecl)
333 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000334 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000335 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
336 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000337 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000339 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
340 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000341 ObjCPropertyDecl *PDecl = (*I);
342 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000343 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000344 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000345 }
346 }
347}
348
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000349/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
350/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000351/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000352void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000353Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000354 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000355 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
356 if (!IDecl) {
357 // Category
358 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
359 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000360 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
361 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000362 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000363 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000364 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000365 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366 CP != CE; ++CP)
367 if ((*CP)->getIdentifier() == Pr->getIdentifier())
368 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000369 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000370 // Property protocol already exist in class. Diagnose any mismatch.
371 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
372 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000373 return;
374 }
Steve Naroff09c47192009-01-09 15:36:25 +0000375 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
376 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000377 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000378 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000379 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000380 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381 CP != CE; ++CP)
382 if ((*CP)->getIdentifier() == Pr->getIdentifier())
383 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000384 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000386 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000388}
389
390/// MergeProtocolPropertiesIntoClass - This routine merges properties
391/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000392/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000393///
Chris Lattner70f19542009-02-16 21:26:43 +0000394void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
395 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000396 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000397 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
398
399 if (!IDecl) {
400 // Category
401 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
402 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
403 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
404 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
405 E = MDecl->protocol_end(); P != E; ++P)
406 // Merge properties of category (*P) into IDECL's
407 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
408
409 // Go thru the list of protocols for this category and recursively merge
410 // their properties into this class as well.
411 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
412 E = CatDecl->protocol_end(); P != E; ++P)
413 MergeProtocolPropertiesIntoClass(CatDecl, *P);
414 } else {
415 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
416 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
417 E = MD->protocol_end(); P != E; ++P)
418 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
419 }
420 return;
421 }
422
Chris Lattnerb752f282008-07-21 07:06:49 +0000423 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
425 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000426 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000427 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
428
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429 // Go thru the list of protocols for this class and recursively merge
430 // their properties into this class as well.
431 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
432 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000433 MergeProtocolPropertiesIntoClass(IDecl, *P);
434 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000435 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
436 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
437 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000439 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440}
441
Chris Lattner4d391482007-12-12 07:09:47 +0000442/// ActOnForwardProtocolDeclaration -
443Action::DeclTy *
444Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000445 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000446 unsigned NumElts,
447 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000449
450 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000451 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000452 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000453 if (PDecl == 0) { // Not already seen?
454 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
455 IdentList[i].second, Ident);
456 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000457 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000458 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000459 if (attrList)
460 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000461 Protocols.push_back(PDecl);
462 }
Anders Carlsson15281452008-11-04 16:57:32 +0000463
464 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000465 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000466 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000467 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000468 CheckObjCDeclScope(PDecl);
469 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000470}
471
Chris Lattner7caeabd2008-07-21 22:17:28 +0000472Sema::DeclTy *Sema::
473ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
474 IdentifierInfo *ClassName, SourceLocation ClassLoc,
475 IdentifierInfo *CategoryName,
476 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000477 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000478 unsigned NumProtoRefs,
479 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000480 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000481 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
482 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000483 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000484
485 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000486 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000487 if (!IDecl || IDecl->isForwardDecl()) {
488 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000489 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000490 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000491 }
Chris Lattner4d391482007-12-12 07:09:47 +0000492
Chris Lattner70f19542009-02-16 21:26:43 +0000493 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000494
495 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000496 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000497
498 /// Check for duplicate interface declaration for this category
499 ObjCCategoryDecl *CDeclChain;
500 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
501 CDeclChain = CDeclChain->getNextClassCategory()) {
502 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
503 Diag(CategoryLoc, diag::warn_dup_category_def)
504 << ClassName << CategoryName;
505 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
506 break;
507 }
508 }
509 if (!CDeclChain)
510 CDecl->insertNextClassCategory();
511
Chris Lattner4d391482007-12-12 07:09:47 +0000512 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000513 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
514 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000515 }
Anders Carlsson15281452008-11-04 16:57:32 +0000516
517 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000518 return CDecl;
519}
520
521/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000523/// object.
524Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
525 SourceLocation AtCatImplLoc,
526 IdentifierInfo *ClassName, SourceLocation ClassLoc,
527 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000529 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000530 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
531 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000532 /// Check that class of this category is already completely declared.
533 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000534 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000535
Douglas Gregord0434102009-01-09 00:49:46 +0000536 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000537 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000538
Chris Lattner4d391482007-12-12 07:09:47 +0000539 /// TODO: Check that CatName, category name, is not used in another
540 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000541 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000542
543 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000544 return CDecl;
545}
546
547Sema::DeclTy *Sema::ActOnStartClassImplementation(
548 SourceLocation AtClassImplLoc,
549 IdentifierInfo *ClassName, SourceLocation ClassLoc,
550 IdentifierInfo *SuperClassname,
551 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000552 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000553 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000554 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000557 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000558 }
559 else {
560 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000561 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000562 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000563 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000564 }
565
566 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000567 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000568 if (SuperClassname) {
569 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000570 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000572 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
573 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000574 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000575 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000576 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000577 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000578 Diag(SuperClassLoc, diag::err_undef_superclass)
579 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000580 else if (IDecl && IDecl->getSuperClass() != SDecl) {
581 // This implementation and its interface do not have the same
582 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000583 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000584 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000585 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000586 }
587 }
588 }
589
590 if (!IDecl) {
591 // Legacy case of @implementation with no corresponding @interface.
592 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000593
594 // FIXME: Do we support attributes on the @implementation? If so
595 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000596 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
597 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000598 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000599 IDecl->setSuperClass(SDecl);
600 IDecl->setLocEnd(ClassLoc);
601
Douglas Gregord0434102009-01-09 00:49:46 +0000602 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000603 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000604 // Remember that this needs to be removed when the scope is popped.
605 TUScope->AddDecl(IDecl);
606 }
607
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000609 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000610 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000611
Douglas Gregord0434102009-01-09 00:49:46 +0000612 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000613 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000614
Anders Carlsson15281452008-11-04 16:57:32 +0000615 if (CheckObjCDeclScope(IMPDecl))
616 return IMPDecl;
617
Chris Lattner4d391482007-12-12 07:09:47 +0000618 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000620 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000622 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000624 return IMPDecl;
625}
626
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
628 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000629 SourceLocation RBrace) {
630 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000631 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000632 if (!IDecl)
633 return;
634 /// Check case of non-existing @interface decl.
635 /// (legacy objective-c @implementation decl without an @interface decl).
636 /// Add implementations's ivar to the synthesize class's ivar list.
637 if (IDecl->ImplicitInterfaceDecl()) {
638 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
639 return;
640 }
641 // If implementation has empty ivar list, just return.
642 if (numIvars == 0)
643 return;
644
645 assert(ivars && "missing @implementation ivars");
646
647 // Check interface's Ivar list against those in the implementation.
648 // names and types must match.
649 //
Chris Lattner4d391482007-12-12 07:09:47 +0000650 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000652 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
653 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654 ObjCIvarDecl* ImplIvar = ivars[j++];
655 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000656 assert (ImplIvar && "missing implementation ivar");
657 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000658 if (Context.getCanonicalType(ImplIvar->getType()) !=
659 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000660 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000661 << ImplIvar->getIdentifier()
662 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000663 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
666 // as error.
667 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000668 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000669 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000670 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000671 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000672 }
673 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000674 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000675
676 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000677 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000678 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000679 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000680}
681
Steve Naroff3c2eb662008-02-10 21:38:56 +0000682void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
683 bool &IncompleteImpl) {
684 if (!IncompleteImpl) {
685 Diag(ImpLoc, diag::warn_incomplete_impl);
686 IncompleteImpl = true;
687 }
Chris Lattner08631c52008-11-23 21:45:46 +0000688 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000689}
690
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000691void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
692 ObjCMethodDecl *IntfMethodDecl) {
693 bool err = false;
694 QualType ImpMethodQType =
695 Context.getCanonicalType(ImpMethodDecl->getResultType());
696 QualType IntfMethodQType =
697 Context.getCanonicalType(IntfMethodDecl->getResultType());
698 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
699 err = true;
700 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
701 IF=IntfMethodDecl->param_begin(),
702 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
703 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
704 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
705 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
706 err = true;
707 break;
708 }
709 }
710 if (err) {
711 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
712 << ImpMethodDecl->getDeclName();
713 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
714 }
715}
716
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000717/// isPropertyReadonly - Return true if property is readonly, by searching
718/// for the property in the class and in its categories and implementations
719///
720bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
721 ObjCInterfaceDecl *IDecl) const {
722 // by far the most common case.
723 if (!PDecl->isReadOnly())
724 return false;
725 // Even if property is ready only, if interface has a user defined setter,
726 // it is not considered read only.
727 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
728 return false;
729
730 // Main class has the property as 'readonly'. Must search
731 // through the category list to see if the property's
732 // attribute has been over-ridden to 'readwrite'.
733 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
734 Category; Category = Category->getNextClassCategory()) {
735 // Even if property is ready only, if a category has a user defined setter,
736 // it is not considered read only.
737 if (Category->getInstanceMethod(PDecl->getSetterName()))
738 return false;
739 ObjCPropertyDecl *P =
740 Category->FindPropertyDeclaration(PDecl->getIdentifier());
741 if (P && !P->isReadOnly())
742 return false;
743 }
744
745 // Also, check for definition of a setter method in the implementation if
746 // all else failed.
747 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
748 if (ObjCImplementationDecl *IMD =
749 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
750 if (IMD->getInstanceMethod(PDecl->getSetterName()))
751 return false;
752 }
753 else if (ObjCCategoryImplDecl *CIMD =
754 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
755 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
756 return false;
757 }
758 }
759 return true;
760}
761
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000762/// FIXME: Type hierarchies in Objective-C can be deep. We could most
763/// likely improve the efficiency of selector lookups and type
764/// checking by associating with each protocol / interface / category
765/// the flattened instance tables. If we used an immutable set to keep
766/// the table then it wouldn't add significant memory cost and it
767/// would be handy for lookups.
768
Steve Naroffefe7f362008-02-08 22:06:17 +0000769/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000770/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000771void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
772 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000773 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000774 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000775 const llvm::DenseSet<Selector> &ClsMap,
776 ObjCInterfaceDecl *IDecl) {
777 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
778
779 // If a method lookup fails locally we still need to look and see if
780 // the method was implemented by a base class or an inherited
781 // protocol. This lookup is slow, but occurs rarely in correct code
782 // and otherwise would terminate in a warning.
783
Chris Lattner4d391482007-12-12 07:09:47 +0000784 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000785 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000786 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000788 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000789 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000790 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000791 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000792 }
793 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000795 E = PDecl->classmeth_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 &&
798 !ClsMap.count(method->getSelector()) &&
799 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000800 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000801 }
Chris Lattner780f3292008-07-21 21:32:27 +0000802 // Check on this protocols's referenced protocols, recursively.
803 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
804 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000805 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000806}
807
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
809 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000810 llvm::DenseSet<Selector> InsMap;
811 // Check and see if instance methods in class interface have been
812 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000814 E = IMPDecl->instmeth_end(); I != E; ++I)
815 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000816
817 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000819 E = IDecl->instmeth_end(); I != E; ++I) {
820 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000821 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000822 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000823 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000824
825 ObjCMethodDecl *ImpMethodDecl =
826 IMPDecl->getInstanceMethod((*I)->getSelector());
827 ObjCMethodDecl *IntfMethodDecl =
828 IDecl->getInstanceMethod((*I)->getSelector());
829 assert(IntfMethodDecl &&
830 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
831 // ImpMethodDecl may be null as in a @dynamic property.
832 if (ImpMethodDecl)
833 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
834 }
Chris Lattner4c525092007-12-12 17:58:05 +0000835
Chris Lattner4d391482007-12-12 07:09:47 +0000836 llvm::DenseSet<Selector> ClsMap;
837 // Check and see if class methods in class interface have been
838 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000840 E = IMPDecl->classmeth_end(); I != E; ++I)
841 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000842
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000843 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000844 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000845 if (!ClsMap.count((*I)->getSelector()))
846 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000847 else {
848 ObjCMethodDecl *ImpMethodDecl =
849 IMPDecl->getClassMethod((*I)->getSelector());
850 ObjCMethodDecl *IntfMethodDecl =
851 IDecl->getClassMethod((*I)->getSelector());
852 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
853 }
854
Chris Lattner4d391482007-12-12 07:09:47 +0000855
856 // Check the protocol list for unimplemented methods in the @implementation
857 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000858 const ObjCList<ObjCProtocolDecl> &Protocols =
859 IDecl->getReferencedProtocols();
860 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
861 E = Protocols.end(); I != E; ++I)
862 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000863 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000864}
865
866/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000867/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
869 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000870 llvm::DenseSet<Selector> InsMap;
871 // Check and see if instance methods in category interface have been
872 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000873 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000874 E = CatImplDecl->instmeth_end(); I != E; ++I)
875 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000876
877 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000878 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000879 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000880 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000881 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000882 else {
883 ObjCMethodDecl *ImpMethodDecl =
884 CatImplDecl->getInstanceMethod((*I)->getSelector());
885 ObjCMethodDecl *IntfMethodDecl =
886 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000887 assert(IntfMethodDecl &&
888 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
889 // ImpMethodDecl may be null as in a @dynamic property.
890 if (ImpMethodDecl)
891 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000892 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000893
Chris Lattner4d391482007-12-12 07:09:47 +0000894 llvm::DenseSet<Selector> ClsMap;
895 // Check and see if class methods in category interface have been
896 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000897 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000898 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
899 I != E; ++I)
900 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000901
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000903 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000904 if (!ClsMap.count((*I)->getSelector()))
905 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000906 else {
907 ObjCMethodDecl *ImpMethodDecl =
908 CatImplDecl->getClassMethod((*I)->getSelector());
909 ObjCMethodDecl *IntfMethodDecl =
910 CatClassDecl->getClassMethod((*I)->getSelector());
911 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
912 }
Chris Lattner4d391482007-12-12 07:09:47 +0000913 // Check the protocol list for unimplemented methods in the @implementation
914 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000915 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
916 E = CatClassDecl->protocol_end(); PI != E; ++PI)
917 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000918 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000919}
920
921/// ActOnForwardClassDeclaration -
922Action::DeclTy *
923Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000924 IdentifierInfo **IdentList,
925 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000927
928 for (unsigned i = 0; i != NumElts; ++i) {
929 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000930 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000931 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000932 // Maybe we will complain about the shadowed template parameter.
933 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
934 // Just pretend that we didn't see the previous declaration.
935 PrevDecl = 0;
936 }
937
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000939 // GCC apparently allows the following idiom:
940 //
941 // typedef NSObject < XCElementTogglerP > XCElementToggler;
942 // @class XCElementToggler;
943 //
944 // FIXME: Make an extension?
945 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
946 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000947 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000948 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000949 }
Chris Lattner4d391482007-12-12 07:09:47 +0000950 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000951 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000952 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000953 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
954 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000955 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000956
Douglas Gregord0434102009-01-09 00:49:46 +0000957 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000958 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000959 // Remember that this needs to be removed when the scope is popped.
960 TUScope->AddDecl(IDecl);
961 }
962
963 Interfaces.push_back(IDecl);
964 }
965
Douglas Gregord0434102009-01-09 00:49:46 +0000966 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000967 &Interfaces[0],
968 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000969 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000970 CheckObjCDeclScope(CDecl);
971 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000972}
973
974
975/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
976/// returns true, or false, accordingly.
977/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000979 const ObjCMethodDecl *PrevMethod,
980 bool matchBasedOnSizeAndAlignment) {
981 QualType T1 = Context.getCanonicalType(Method->getResultType());
982 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
983
984 if (T1 != T2) {
985 // The result types are different.
986 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000987 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000988 // Incomplete types don't have a size and alignment.
989 if (T1->isIncompleteType() || T2->isIncompleteType())
990 return false;
991 // Check is based on size and alignment.
992 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
993 return false;
994 }
995 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
996 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
997 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
998 if (T1 != T2) {
999 // The result types are different.
1000 if (!matchBasedOnSizeAndAlignment)
1001 return false;
1002 // Incomplete types don't have a size and alignment.
1003 if (T1->isIncompleteType() || T2->isIncompleteType())
1004 return false;
1005 // Check is based on size and alignment.
1006 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1007 return false;
1008 }
Chris Lattner4d391482007-12-12 07:09:47 +00001009 }
1010 return true;
1011}
1012
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1014 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001015 if (!FirstMethod.Method) {
1016 // Haven't seen a method with this selector name yet - add it.
1017 FirstMethod.Method = Method;
1018 FirstMethod.Next = 0;
1019 } else {
1020 // We've seen a method with this name, now check the type signature(s).
1021 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1022
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001024 Next = Next->Next)
1025 match = MatchTwoMethodDeclarations(Method, Next->Method);
1026
1027 if (!match) {
1028 // We have a new signature for an existing method - add it.
1029 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001030 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001031 }
1032 }
1033}
1034
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001035// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001036ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1037 SourceRange R) {
1038 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001039 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001040
1041 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001042 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1043 // This checks if the methods differ by size & alignment.
1044 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1045 issueWarning = true;
1046 }
1047 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001048 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001049 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001050 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001051 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001052 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001053 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001054 }
1055 return MethList.Method;
1056}
1057
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001058void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1059 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001060 if (!FirstMethod.Method) {
1061 // Haven't seen a method with this selector name yet - add it.
1062 FirstMethod.Method = Method;
1063 FirstMethod.Next = 0;
1064 } else {
1065 // We've seen a method with this name, now check the type signature(s).
1066 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1067
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001068 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001069 Next = Next->Next)
1070 match = MatchTwoMethodDeclarations(Method, Next->Method);
1071
1072 if (!match) {
1073 // We have a new signature for an existing method - add it.
1074 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001075 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001076 FirstMethod.Next = OMI;
1077 }
1078 }
1079}
1080
Steve Naroff0701bbb2009-01-08 17:28:14 +00001081/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1082/// have the property type and issue diagnostics if they don't.
1083/// Also synthesize a getter/setter method if none exist (and update the
1084/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1085/// methods is the "right" thing to do.
1086void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1087 ObjCContainerDecl *CD) {
1088 ObjCMethodDecl *GetterMethod, *SetterMethod;
1089
1090 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1091 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1092
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001093 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001094 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001095 Diag(property->getLocation(),
1096 diag::err_accessor_property_type_mismatch)
1097 << property->getDeclName()
1098 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001099 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1100 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001101
1102 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001103 if (Context.getCanonicalType(SetterMethod->getResultType())
1104 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001105 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1106 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001107 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001108 Diag(property->getLocation(),
1109 diag::err_accessor_property_type_mismatch)
1110 << property->getDeclName()
1111 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001112 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1113 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001114 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001115
1116 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001117 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001118 // FIXME: The synthesized property we set here is misleading. We
1119 // almost always synthesize these methods unless the user explicitly
1120 // provided prototypes (which is odd, but allowed). Sema should be
1121 // typechecking that the declarations jive in that situation (which
1122 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001123 if (!GetterMethod) {
1124 // No instance method of same name as property getter name was found.
1125 // Declare a getter method and add it to the list of methods
1126 // for this class.
1127 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1128 property->getLocation(), property->getGetterName(),
1129 property->getType(), CD, true, false, true,
1130 (property->getPropertyImplementation() ==
1131 ObjCPropertyDecl::Optional) ?
1132 ObjCMethodDecl::Optional :
1133 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001134 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001135 } else
1136 // A user declared getter will be synthesize when @synthesize of
1137 // the property with the same name is seen in the @implementation
1138 GetterMethod->setIsSynthesized();
1139 property->setGetterMethodDecl(GetterMethod);
1140
1141 // Skip setter if property is read-only.
1142 if (!property->isReadOnly()) {
1143 // Find the default setter and if one not found, add one.
1144 if (!SetterMethod) {
1145 // No instance method of same name as property setter name was found.
1146 // Declare a setter method and add it to the list of methods
1147 // for this class.
1148 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1149 property->getLocation(),
1150 property->getSetterName(),
1151 Context.VoidTy, CD, true, false, true,
1152 (property->getPropertyImplementation() ==
1153 ObjCPropertyDecl::Optional) ?
1154 ObjCMethodDecl::Optional :
1155 ObjCMethodDecl::Required);
1156 // Invent the arguments for the setter. We don't bother making a
1157 // nice name for the argument.
1158 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1159 SourceLocation(),
1160 property->getIdentifier(),
1161 property->getType(),
1162 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001163 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001164 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001165 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001166 } else
1167 // A user declared setter will be synthesize when @synthesize of
1168 // the property with the same name is seen in the @implementation
1169 SetterMethod->setIsSynthesized();
1170 property->setSetterMethodDecl(SetterMethod);
1171 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001172 // Add any synthesized methods to the global pool. This allows us to
1173 // handle the following, which is supported by GCC (and part of the design).
1174 //
1175 // @interface Foo
1176 // @property double bar;
1177 // @end
1178 //
1179 // void thisIsUnfortunate() {
1180 // id foo;
1181 // double bar = [foo bar];
1182 // }
1183 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001184 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001185 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001186 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001187 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001188}
1189
Steve Naroffa56f6162007-12-18 01:30:32 +00001190// Note: For class/category implemenations, allMethods/allProperties is
1191// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001192void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1193 DeclTy **allMethods, unsigned allNum,
1194 DeclTy **allProperties, unsigned pNum) {
1195 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1196
Steve Naroffa56f6162007-12-18 01:30:32 +00001197 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1198 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001199 // should be true.
1200 if (!ClassDecl)
1201 return;
1202
Chris Lattner4d391482007-12-12 07:09:47 +00001203 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001204 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1205 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001206 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001207
Steve Naroff0701bbb2009-01-08 17:28:14 +00001208 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001209
1210 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1211 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1212 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1213
Chris Lattner4d391482007-12-12 07:09:47 +00001214 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215 ObjCMethodDecl *Method =
1216 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001217
1218 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001219 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001220 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001222 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1223 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001224 if ((isInterfaceDeclKind && PrevMethod && !match)
1225 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001226 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001227 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001228 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001229 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001230 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001231 InsMap[Method->getSelector()] = Method;
1232 /// The following allows us to typecheck messages to "id".
1233 AddInstanceMethodToGlobalPool(Method);
1234 }
1235 }
1236 else {
1237 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001238 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001239 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1240 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001241 if ((isInterfaceDeclKind && PrevMethod && !match)
1242 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001243 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001244 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001245 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001246 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001247 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001248 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001249 /// The following allows us to typecheck messages to "Class".
1250 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001251 }
1252 }
1253 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001254 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001255 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001256 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001257 ComparePropertiesInBaseAndSuper(I);
1258 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001259 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001260 // Categories are used to extend the class by declaring new methods.
1261 // By the same token, they are also used to add new properties. No
1262 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001263
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001264 // Merge protocol properties into category
1265 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001266 }
Steve Naroff09c47192009-01-09 15:36:25 +00001267 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1268 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1269 // user-defined setter/getter. It also synthesizes setter/getter methods
1270 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001271 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1272 E = CDecl->prop_end(); I != E; ++I)
1273 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001274 CDecl->setAtEndLoc(AtEndLoc);
1275 }
1276 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001277 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001278 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001279 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001280 } else if (ObjCCategoryImplDecl* CatImplClass =
1281 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001282 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001283
Chris Lattner4d391482007-12-12 07:09:47 +00001284 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001285 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001286 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001287 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001288 Categories; Categories = Categories->getNextClassCategory()) {
1289 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1290 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1291 break;
1292 }
1293 }
1294 }
1295 }
1296}
1297
1298
1299/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1300/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001301static Decl::ObjCDeclQualifier
1302CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1303 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1304 if (PQTVal & ObjCDeclSpec::DQ_In)
1305 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1306 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1307 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1308 if (PQTVal & ObjCDeclSpec::DQ_Out)
1309 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1310 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1311 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1312 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1313 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1314 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1315 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001316
1317 return ret;
1318}
1319
1320Sema::DeclTy *Sema::ActOnMethodDeclaration(
1321 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001322 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001324 Selector Sel,
1325 // optional arguments. The number of types/arguments is obtained
1326 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001327 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001328 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001329 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1330 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001331 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001332
1333 // Make sure we can establish a context for the method.
1334 if (!ClassDecl) {
1335 Diag(MethodLoc, diag::error_missing_method_context);
1336 return 0;
1337 }
Chris Lattner4d391482007-12-12 07:09:47 +00001338 QualType resultDeclType;
1339
1340 if (ReturnType)
1341 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1342 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001343 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001344
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001345 ObjCMethodDecl* ObjCMethod =
1346 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001347 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001348 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001349 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001350 MethodDeclKind == tok::objc_optional ?
1351 ObjCMethodDecl::Optional :
1352 ObjCMethodDecl::Required);
1353
Chris Lattner0ed844b2008-04-04 06:12:32 +00001354 llvm::SmallVector<ParmVarDecl*, 16> Params;
1355
1356 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1357 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001358 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001359
Steve Naroff6082c622008-12-09 19:36:17 +00001360 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001361 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001362 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001363 if (argType->isArrayType()) { // (char *[]) -> (char **)
1364 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001365 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001366 }
Steve Naroff6082c622008-12-09 19:36:17 +00001367 else if (argType->isFunctionType())
1368 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001369 else if (argType->isObjCInterfaceType()) {
1370 // FIXME! provide more precise location for the parameter
1371 Diag(MethodLoc, diag::err_object_as_method_param);
1372 return 0;
1373 }
Steve Naroff6082c622008-12-09 19:36:17 +00001374 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001375 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001376 ParmVarDecl* Param;
1377 if (originalArgType.isNull())
1378 Param = ParmVarDecl::Create(Context, ObjCMethod,
1379 SourceLocation(/*FIXME*/),
1380 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001381 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001382 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001383 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1384 SourceLocation(/*FIXME*/),
1385 ArgNames[i], argType, originalArgType,
1386 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001387
Chris Lattner0ed844b2008-04-04 06:12:32 +00001388 Param->setObjCDeclQualifier(
1389 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1390 Params.push_back(Param);
1391 }
1392
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001393 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1394 ObjCMethod->setObjCDeclQualifier(
1395 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1396 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001397
1398 if (AttrList)
1399 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001400
1401 // For implementations (which can be very "coarse grain"), we add the
1402 // method now. This allows the AST to implement lookup methods that work
1403 // incrementally (without waiting until we parse the @end). It also allows
1404 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001406 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001407 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001408 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001410 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001411 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001413 }
1414 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001416 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001417 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001418 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001419 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001420 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001421 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001422 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001423 }
1424 }
1425 if (PrevMethod) {
1426 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001427 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001428 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001429 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001430 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001432}
1433
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001434void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1435 SourceLocation Loc,
1436 unsigned &Attributes) {
1437 // FIXME: Improve the reported location.
1438
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001439 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001440 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001441 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1442 ObjCDeclSpec::DQ_PR_assign |
1443 ObjCDeclSpec::DQ_PR_copy |
1444 ObjCDeclSpec::DQ_PR_retain))) {
1445 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1446 "readwrite" :
1447 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1448 "assign" :
1449 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1450 "copy" : "retain";
1451
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001452 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001453 diag::err_objc_property_attr_mutually_exclusive :
1454 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001455 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001456 }
1457
1458 // Check for copy or retain on non-object types.
1459 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1460 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001461 Diag(Loc, diag::err_objc_property_requires_object)
1462 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001463 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1464 }
1465
1466 // Check for more than one of { assign, copy, retain }.
1467 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1468 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001469 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1470 << "assign" << "copy";
1471 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001472 }
1473 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001474 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1475 << "assign" << "retain";
1476 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001477 }
1478 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1479 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001480 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1481 << "copy" << "retain";
1482 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001483 }
1484 }
1485
1486 // Warn if user supplied no assignment attribute, property is
1487 // readwrite, and this is an object type.
1488 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1489 ObjCDeclSpec::DQ_PR_retain)) &&
1490 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1491 Context.isObjCObjectPointerType(PropertyTy)) {
1492 // Skip this warning in gc-only mode.
1493 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1494 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1495
1496 // If non-gc code warn that this is likely inappropriate.
1497 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1498 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1499
1500 // FIXME: Implement warning dependent on NSCopying being
1501 // implemented. See also:
1502 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1503 // (please trim this list while you are at it).
1504 }
1505}
1506
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001507Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1508 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001509 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001510 Selector GetterSel,
1511 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001512 DeclTy *ClassCategory,
1513 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001514 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001515 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001516 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1517 // default is readwrite!
1518 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1519 // property is defaulted to 'assign' if it is readwrite and is
1520 // not retain or copy
1521 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1522 (isReadWrite &&
1523 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1524 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1525 QualType T = GetTypeForDeclarator(FD.D, S);
1526 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001527
1528 // May modify Attributes.
1529 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001530
1531 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1532 if (!CDecl->getIdentifier()) {
1533 // This is an anonymous category. property requires special
1534 // handling.
1535 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1536 if (ObjCPropertyDecl *PIDecl =
1537 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1538 // property 'PIDecl's readonly attribute will be over-ridden
1539 // with anonymous category's readwrite property attribute!
1540 unsigned PIkind = PIDecl->getPropertyAttributes();
1541 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001542 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001543 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1544 Diag(AtLoc, diag::warn_property_attr_mismatch);
1545 PIDecl->makeitReadWriteAttribute();
1546 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1547 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1548 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1549 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1550 PIDecl->setSetterName(SetterSel);
1551 // FIXME: use a common routine with addPropertyMethods.
1552 ObjCMethodDecl *SetterDecl =
1553 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1554 Context.VoidTy,
1555 ICDecl,
1556 true, false, true,
1557 ObjCMethodDecl::Required);
1558 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1559 SetterDecl,
1560 SourceLocation(),
1561 FD.D.getIdentifier(),
1562 T,
1563 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001564 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001565 SetterDecl->setMethodParams(&Argument, 1);
1566 PIDecl->setSetterMethodDecl(SetterDecl);
1567 }
1568 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001569 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001570 *isOverridingProperty = true;
1571 return 0;
1572 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001573 // No matching property found in the main class. Just fall thru
1574 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001575 // it works as is. This category becomes just like a category
1576 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001577 } else {
1578 Diag(CDecl->getLocation(), diag::err_continuation_class);
1579 *isOverridingProperty = true;
1580 return 0;
1581 }
1582 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001583
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001584 Type *t = T.getTypePtr();
1585 if (t->isArrayType() || t->isFunctionType())
1586 Diag(AtLoc, diag::err_property_type) << T;
1587
Steve Naroff93983f82009-01-11 12:47:58 +00001588 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1589 assert(DC && "ClassDecl is not a DeclContext");
1590 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001591 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001592 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001593
1594 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001595
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001596 // Regardless of setter/getter attribute, we save the default getter/setter
1597 // selector names in anticipation of declaration of setter/getter methods.
1598 PDecl->setGetterName(GetterSel);
1599 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001600
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001601 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001603
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001604 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001606
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001607 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001609
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001610 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001612
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001613 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001615
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001616 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001618
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001619 if (isAssign)
1620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1621
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001622 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001624
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001625 if (MethodImplKind == tok::objc_required)
1626 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1627 else if (MethodImplKind == tok::objc_optional)
1628 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1629
Chris Lattner4d391482007-12-12 07:09:47 +00001630 return PDecl;
1631}
1632
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001633/// ActOnPropertyImplDecl - This routine performs semantic checks and
1634/// builds the AST node for a property implementation declaration; declared
1635/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001636///
1637Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1638 SourceLocation PropertyLoc,
1639 bool Synthesize,
1640 DeclTy *ClassCatImpDecl,
1641 IdentifierInfo *PropertyId,
1642 IdentifierInfo *PropertyIvar) {
1643 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1644 // Make sure we have a context for the property implementation declaration.
1645 if (!ClassImpDecl) {
1646 Diag(AtLoc, diag::error_missing_property_context);
1647 return 0;
1648 }
1649 ObjCPropertyDecl *property = 0;
1650 ObjCInterfaceDecl* IDecl = 0;
1651 // Find the class or category class where this property must have
1652 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001653 ObjCImplementationDecl *IC = 0;
1654 ObjCCategoryImplDecl* CatImplClass = 0;
1655 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001656 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001657 // We always synthesize an interface for an implementation
1658 // without an interface decl. So, IDecl is always non-zero.
1659 assert(IDecl &&
1660 "ActOnPropertyImplDecl - @implementation without @interface");
1661
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001662 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001663 property = IDecl->FindPropertyDeclaration(PropertyId);
1664 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001665 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001666 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001667 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001668 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001669 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001670 if (Synthesize) {
1671 Diag(AtLoc, diag::error_synthesize_category_decl);
1672 return 0;
1673 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001674 IDecl = CatImplClass->getClassInterface();
1675 if (!IDecl) {
1676 Diag(AtLoc, diag::error_missing_property_interface);
1677 return 0;
1678 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001679 ObjCCategoryDecl *Category =
1680 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1681
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001682 // If category for this implementation not found, it is an error which
1683 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001684 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001685 return 0;
1686 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001687 property = Category->FindPropertyDeclaration(PropertyId);
1688 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001689 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001690 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001691 return 0;
1692 }
1693 }
1694 else {
1695 Diag(AtLoc, diag::error_bad_property_context);
1696 return 0;
1697 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001698 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001699 // Check that we have a valid, previously declared ivar for @synthesize
1700 if (Synthesize) {
1701 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001702 if (!PropertyIvar)
1703 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001704 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001705 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001706 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001707 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001708 return 0;
1709 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001710 QualType PropType = Context.getCanonicalType(property->getType());
1711 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1712
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001713 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001714 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001715 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001716 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001717 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001718 return 0;
1719 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001720 else {
1721 // FIXME! Rules for properties are somewhat different that those
1722 // for assignments. Use a new routine to consolidate all cases;
1723 // specifically for property redeclarations as well as for ivars.
1724 QualType lhsType =
1725 Context.getCanonicalType(PropType).getUnqualifiedType();
1726 QualType rhsType =
1727 Context.getCanonicalType(IvarType).getUnqualifiedType();
1728 if (lhsType != rhsType &&
1729 lhsType->isArithmeticType()) {
1730 Diag(PropertyLoc, diag::error_property_ivar_type)
1731 << property->getDeclName() << Ivar->getDeclName();
1732 return 0;
1733 }
1734 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001735 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001736 } else if (PropertyIvar) {
1737 // @dynamic
1738 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1739 return 0;
1740 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001741 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001742 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001743 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1744 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001745 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001746 ObjCPropertyImplDecl::Synthesize
1747 : ObjCPropertyImplDecl::Dynamic),
1748 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001749 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001750 if (IC) {
1751 if (Synthesize)
1752 if (ObjCPropertyImplDecl *PPIDecl =
1753 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1754 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1755 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1756 << PropertyIvar;
1757 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1758 }
1759
1760 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1761 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1762 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1763 return 0;
1764 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001765 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001766 }
1767 else {
1768 if (Synthesize)
1769 if (ObjCPropertyImplDecl *PPIDecl =
1770 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1771 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1772 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1773 << PropertyIvar;
1774 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1775 }
1776
1777 if (ObjCPropertyImplDecl *PPIDecl =
1778 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1779 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1780 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1781 return 0;
1782 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001783 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001784 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001785
1786 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001787}
Anders Carlsson15281452008-11-04 16:57:32 +00001788
Chris Lattnercc98eac2008-12-17 07:13:27 +00001789bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001790 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001791 return false;
1792
1793 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1794 D->setInvalidDecl();
1795
1796 return true;
1797}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001798
1799/// Collect the instance variables declared in an Objective-C object. Used in
1800/// the creation of structures from objects using the @defs directive.
1801/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1802/// part of the AST generation logic of @defs.
1803static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1804 ASTContext& Ctx,
1805 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1806 if (Class->getSuperClass())
1807 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1808
1809 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1810 for (ObjCInterfaceDecl::ivar_iterator
1811 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1812
1813 ObjCIvarDecl* ID = *I;
1814 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1815 ID->getLocation(),
1816 ID->getIdentifier(),
1817 ID->getType(),
1818 ID->getBitWidth()));
1819 }
1820}
1821
1822/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1823/// instance variables of ClassName into Decls.
1824void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1825 IdentifierInfo *ClassName,
1826 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1827 // Check that ClassName is a valid class
1828 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1829 if (!Class) {
1830 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1831 return;
1832 }
1833 // Collect the instance variables
1834 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1835
1836 // Introduce all of these fields into the appropriate scope.
1837 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1838 D != Decls.end(); ++D) {
1839 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1840 if (getLangOptions().CPlusPlus)
1841 PushOnScopeChains(cast<FieldDecl>(FD), S);
1842 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001843 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001844 }
1845}
1846