blob: c3789508e146cf75e666055cf268797e23c189b4 [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);
111 if (PrevDecl && SuperClassDecl == 0) {
112 // The previous declaration was not a class decl. Check if we have a
113 // typedef. If we do, get the underlying class type.
114 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
115 QualType T = TDecl->getUnderlyingType();
116 if (T->isObjCInterfaceType()) {
117 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
118 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
119 }
120 }
121 // This handles the following case:
122 //
123 // typedef int SuperClass;
124 // @interface MyClass : SuperClass {} @end
125 //
126 if (!SuperClassDecl) {
127 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
128 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
129 }
130 }
131 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
132 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000133 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000134 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000135 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000136 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000137 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000138 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000139 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000140 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000141 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000142 IDecl->setLocEnd(SuperLoc);
143 } else { // we have a root class.
144 IDecl->setLocEnd(ClassLoc);
145 }
146
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000147 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000148 if (NumProtoRefs) {
149 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000150 IDecl->setLocEnd(EndProtoLoc);
151 }
Anders Carlsson15281452008-11-04 16:57:32 +0000152
153 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000154 return IDecl;
155}
156
157/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000158/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000159Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
160 IdentifierInfo *AliasName,
161 SourceLocation AliasLocation,
162 IdentifierInfo *ClassName,
163 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000164 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000165 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000166 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000167 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000168 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000169 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000170 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000171 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000172 return 0;
173 }
174 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000175 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000176 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
177 QualType T = TDecl->getUnderlyingType();
178 if (T->isObjCInterfaceType()) {
179 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
180 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000181 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000182 }
183 }
184 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000185 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
186 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000187 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000188 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000189 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000190 return 0;
191 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000192
193 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000195 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000196
197 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000198
199 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000200 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000201 if (!CheckObjCDeclScope(AliasDecl))
202 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000203
Chris Lattner4d391482007-12-12 07:09:47 +0000204 return AliasDecl;
205}
206
Chris Lattnere13b9592008-07-26 04:03:38 +0000207Sema::DeclTy *
208Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
209 IdentifierInfo *ProtocolName,
210 SourceLocation ProtocolLoc,
211 DeclTy * const *ProtoRefs,
212 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000213 SourceLocation EndProtoLoc,
214 AttributeList *AttrList) {
215 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000216 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000218 if (PDecl) {
219 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000220 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000221 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000222 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000223 // Just return the protocol we already had.
224 // FIXME: don't leak the objects passed in!
225 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
Steve Narofff11b5082008-08-13 16:39:22 +0000227 // Make sure the cached decl gets a valid start location.
228 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000229 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000230 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000231 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
232 AtProtoInterfaceLoc,ProtocolName);
233 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000234 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000235 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000237 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000238 if (AttrList)
239 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000240 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000241 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000242 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000243 PDecl->setLocEnd(EndProtoLoc);
244 }
Anders Carlsson15281452008-11-04 16:57:32 +0000245
246 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000247 return PDecl;
248}
249
250/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000251/// issues an error if they are not declared. It returns list of
252/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000253void
Chris Lattnere13b9592008-07-26 04:03:38 +0000254Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000255 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000256 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000257 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000258 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000259 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
260 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000261 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000262 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000263 continue;
264 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000265
Chris Lattner16b34b42009-02-16 21:30:01 +0000266 DiagnoseUseOfDeprecatedDeclImpl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000267
268 // If this is a forward declaration and we are supposed to warn in this
269 // case, do it.
270 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000271 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000272 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000273 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000274 }
275}
276
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000277/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000278/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000279///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000280void
281Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
282 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000283 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284 ObjCPropertyDecl::PropertyAttributeKind CAttr =
285 Property->getPropertyAttributes();
286 ObjCPropertyDecl::PropertyAttributeKind SAttr =
287 SuperProperty->getPropertyAttributes();
288 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
289 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000290 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000291 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000292 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
293 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000294 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000295 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000296 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
297 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000298 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000299 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000300
301 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
302 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000303 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000304 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000306 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000307 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000308 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000309 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000311
Chris Lattner717250a2008-07-26 20:50:02 +0000312 if (Context.getCanonicalType(Property->getType()) !=
313 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316
317}
318
319/// ComparePropertiesInBaseAndSuper - This routine compares property
320/// declarations in base and its super class, if any, and issues
321/// diagnostics in a variety of inconsistant situations.
322///
Chris Lattner70f19542009-02-16 21:26:43 +0000323void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000324 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
325 if (!SDecl)
326 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000327 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000328 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
329 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000330 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000332 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
333 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000334 ObjCPropertyDecl *PDecl = (*I);
335 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000336 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000337 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 }
339 }
340}
341
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000342/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
343/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000344/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000345void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000346Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000347 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000348 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
349 if (!IDecl) {
350 // Category
351 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
352 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000353 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
354 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000355 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000356 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000357 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000358 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000359 CP != CE; ++CP)
360 if ((*CP)->getIdentifier() == Pr->getIdentifier())
361 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000362 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000363 // Property protocol already exist in class. Diagnose any mismatch.
364 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
365 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366 return;
367 }
Steve Naroff09c47192009-01-09 15:36:25 +0000368 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
369 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000370 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000371 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000372 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000373 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000374 CP != CE; ++CP)
375 if ((*CP)->getIdentifier() == Pr->getIdentifier())
376 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000377 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000378 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000379 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381}
382
383/// MergeProtocolPropertiesIntoClass - This routine merges properties
384/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000385/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386///
Chris Lattner70f19542009-02-16 21:26:43 +0000387void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
388 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000390 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
391
392 if (!IDecl) {
393 // Category
394 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
395 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
396 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
397 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
398 E = MDecl->protocol_end(); P != E; ++P)
399 // Merge properties of category (*P) into IDECL's
400 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
401
402 // Go thru the list of protocols for this category and recursively merge
403 // their properties into this class as well.
404 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
405 E = CatDecl->protocol_end(); P != E; ++P)
406 MergeProtocolPropertiesIntoClass(CatDecl, *P);
407 } else {
408 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
409 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
410 E = MD->protocol_end(); P != E; ++P)
411 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
412 }
413 return;
414 }
415
Chris Lattnerb752f282008-07-21 07:06:49 +0000416 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000417 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
418 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000419 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000420 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
421
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000422 // Go thru the list of protocols for this class and recursively merge
423 // their properties into this class as well.
424 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
425 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000426 MergeProtocolPropertiesIntoClass(IDecl, *P);
427 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000428 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
429 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
430 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000432 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000433}
434
Chris Lattner4d391482007-12-12 07:09:47 +0000435/// ActOnForwardProtocolDeclaration -
436Action::DeclTy *
437Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000438 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000439 unsigned NumElts,
440 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000441 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000442
443 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000444 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000445 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000446 if (PDecl == 0) { // Not already seen?
447 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
448 IdentList[i].second, Ident);
449 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000450 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000451 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000452 if (attrList)
453 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000454 Protocols.push_back(PDecl);
455 }
Anders Carlsson15281452008-11-04 16:57:32 +0000456
457 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000458 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000459 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000460 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000461 CheckObjCDeclScope(PDecl);
462 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000463}
464
Chris Lattner7caeabd2008-07-21 22:17:28 +0000465Sema::DeclTy *Sema::
466ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
467 IdentifierInfo *ClassName, SourceLocation ClassLoc,
468 IdentifierInfo *CategoryName,
469 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000470 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000471 unsigned NumProtoRefs,
472 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000473 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000474 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
475 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000476 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000477
478 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000479 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000480 if (!IDecl || IDecl->isForwardDecl()) {
481 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000482 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000483 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000484 }
Chris Lattner4d391482007-12-12 07:09:47 +0000485
Chris Lattner70f19542009-02-16 21:26:43 +0000486 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000487
488 // If the interface is deprecated, warn about it.
489 DiagnoseUseOfDeprecatedDeclImpl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000490
491 /// Check for duplicate interface declaration for this category
492 ObjCCategoryDecl *CDeclChain;
493 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
494 CDeclChain = CDeclChain->getNextClassCategory()) {
495 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
496 Diag(CategoryLoc, diag::warn_dup_category_def)
497 << ClassName << CategoryName;
498 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
499 break;
500 }
501 }
502 if (!CDeclChain)
503 CDecl->insertNextClassCategory();
504
Chris Lattner4d391482007-12-12 07:09:47 +0000505 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000506 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
507 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000508 }
Anders Carlsson15281452008-11-04 16:57:32 +0000509
510 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000511 return CDecl;
512}
513
514/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000515/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000516/// object.
517Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
518 SourceLocation AtCatImplLoc,
519 IdentifierInfo *ClassName, SourceLocation ClassLoc,
520 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000521 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000522 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000523 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
524 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000525 /// Check that class of this category is already completely declared.
526 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000527 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000528
Douglas Gregord0434102009-01-09 00:49:46 +0000529 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000530 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000531
Chris Lattner4d391482007-12-12 07:09:47 +0000532 /// TODO: Check that CatName, category name, is not used in another
533 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000534 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000535
536 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000537 return CDecl;
538}
539
540Sema::DeclTy *Sema::ActOnStartClassImplementation(
541 SourceLocation AtClassImplLoc,
542 IdentifierInfo *ClassName, SourceLocation ClassLoc,
543 IdentifierInfo *SuperClassname,
544 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000545 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000546 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000547 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000548 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000549 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000550 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000551 }
552 else {
553 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000555 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000557 }
558
559 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000560 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000561 if (SuperClassname) {
562 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000563 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000565 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
566 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000567 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000568 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000570 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000571 Diag(SuperClassLoc, diag::err_undef_superclass)
572 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000573 else if (IDecl && IDecl->getSuperClass() != SDecl) {
574 // This implementation and its interface do not have the same
575 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000577 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000578 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000579 }
580 }
581 }
582
583 if (!IDecl) {
584 // Legacy case of @implementation with no corresponding @interface.
585 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000586
587 // FIXME: Do we support attributes on the @implementation? If so
588 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000589 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
590 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000591 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000592 IDecl->setSuperClass(SDecl);
593 IDecl->setLocEnd(ClassLoc);
594
Douglas Gregord0434102009-01-09 00:49:46 +0000595 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000596 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 // Remember that this needs to be removed when the scope is popped.
598 TUScope->AddDecl(IDecl);
599 }
600
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000602 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000603 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000604
Douglas Gregord0434102009-01-09 00:49:46 +0000605 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000606 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000607
Anders Carlsson15281452008-11-04 16:57:32 +0000608 if (CheckObjCDeclScope(IMPDecl))
609 return IMPDecl;
610
Chris Lattner4d391482007-12-12 07:09:47 +0000611 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000613 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000614 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000615 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000617 return IMPDecl;
618}
619
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
621 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000622 SourceLocation RBrace) {
623 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000624 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000625 if (!IDecl)
626 return;
627 /// Check case of non-existing @interface decl.
628 /// (legacy objective-c @implementation decl without an @interface decl).
629 /// Add implementations's ivar to the synthesize class's ivar list.
630 if (IDecl->ImplicitInterfaceDecl()) {
631 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
632 return;
633 }
634 // If implementation has empty ivar list, just return.
635 if (numIvars == 0)
636 return;
637
638 assert(ivars && "missing @implementation ivars");
639
640 // Check interface's Ivar list against those in the implementation.
641 // names and types must match.
642 //
Chris Lattner4d391482007-12-12 07:09:47 +0000643 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000645 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
646 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 ObjCIvarDecl* ImplIvar = ivars[j++];
648 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000649 assert (ImplIvar && "missing implementation ivar");
650 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000651 if (Context.getCanonicalType(ImplIvar->getType()) !=
652 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000653 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000654 << ImplIvar->getIdentifier()
655 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000656 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000657 }
658 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
659 // as error.
660 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000661 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000662 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000663 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000664 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000665 }
666 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000667 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000668
669 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000670 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000671 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000672 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000673}
674
Steve Naroff3c2eb662008-02-10 21:38:56 +0000675void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
676 bool &IncompleteImpl) {
677 if (!IncompleteImpl) {
678 Diag(ImpLoc, diag::warn_incomplete_impl);
679 IncompleteImpl = true;
680 }
Chris Lattner08631c52008-11-23 21:45:46 +0000681 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000682}
683
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000684void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
685 ObjCMethodDecl *IntfMethodDecl) {
686 bool err = false;
687 QualType ImpMethodQType =
688 Context.getCanonicalType(ImpMethodDecl->getResultType());
689 QualType IntfMethodQType =
690 Context.getCanonicalType(IntfMethodDecl->getResultType());
691 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
692 err = true;
693 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
694 IF=IntfMethodDecl->param_begin(),
695 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
696 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
697 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
698 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
699 err = true;
700 break;
701 }
702 }
703 if (err) {
704 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
705 << ImpMethodDecl->getDeclName();
706 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
707 }
708}
709
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000710/// isPropertyReadonly - Return true if property is readonly, by searching
711/// for the property in the class and in its categories and implementations
712///
713bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
714 ObjCInterfaceDecl *IDecl) const {
715 // by far the most common case.
716 if (!PDecl->isReadOnly())
717 return false;
718 // Even if property is ready only, if interface has a user defined setter,
719 // it is not considered read only.
720 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
721 return false;
722
723 // Main class has the property as 'readonly'. Must search
724 // through the category list to see if the property's
725 // attribute has been over-ridden to 'readwrite'.
726 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
727 Category; Category = Category->getNextClassCategory()) {
728 // Even if property is ready only, if a category has a user defined setter,
729 // it is not considered read only.
730 if (Category->getInstanceMethod(PDecl->getSetterName()))
731 return false;
732 ObjCPropertyDecl *P =
733 Category->FindPropertyDeclaration(PDecl->getIdentifier());
734 if (P && !P->isReadOnly())
735 return false;
736 }
737
738 // Also, check for definition of a setter method in the implementation if
739 // all else failed.
740 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
741 if (ObjCImplementationDecl *IMD =
742 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
743 if (IMD->getInstanceMethod(PDecl->getSetterName()))
744 return false;
745 }
746 else if (ObjCCategoryImplDecl *CIMD =
747 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
748 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
749 return false;
750 }
751 }
752 return true;
753}
754
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000755/// FIXME: Type hierarchies in Objective-C can be deep. We could most
756/// likely improve the efficiency of selector lookups and type
757/// checking by associating with each protocol / interface / category
758/// the flattened instance tables. If we used an immutable set to keep
759/// the table then it wouldn't add significant memory cost and it
760/// would be handy for lookups.
761
Steve Naroffefe7f362008-02-08 22:06:17 +0000762/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000763/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000764void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
765 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000766 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000767 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000768 const llvm::DenseSet<Selector> &ClsMap,
769 ObjCInterfaceDecl *IDecl) {
770 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
771
772 // If a method lookup fails locally we still need to look and see if
773 // the method was implemented by a base class or an inherited
774 // protocol. This lookup is slow, but occurs rarely in correct code
775 // and otherwise would terminate in a warning.
776
Chris Lattner4d391482007-12-12 07:09:47 +0000777 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000779 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000781 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000782 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000783 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000784 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000785 }
786 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000788 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000789 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000790 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
791 !ClsMap.count(method->getSelector()) &&
792 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000793 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000794 }
Chris Lattner780f3292008-07-21 21:32:27 +0000795 // Check on this protocols's referenced protocols, recursively.
796 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
797 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000798 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000799}
800
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
802 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000803 llvm::DenseSet<Selector> InsMap;
804 // Check and see if instance methods in class interface have been
805 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000807 E = IMPDecl->instmeth_end(); I != E; ++I)
808 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000809
810 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000812 E = IDecl->instmeth_end(); I != E; ++I) {
813 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000814 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000815 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000816 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000817
818 ObjCMethodDecl *ImpMethodDecl =
819 IMPDecl->getInstanceMethod((*I)->getSelector());
820 ObjCMethodDecl *IntfMethodDecl =
821 IDecl->getInstanceMethod((*I)->getSelector());
822 assert(IntfMethodDecl &&
823 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
824 // ImpMethodDecl may be null as in a @dynamic property.
825 if (ImpMethodDecl)
826 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
827 }
Chris Lattner4c525092007-12-12 17:58:05 +0000828
Chris Lattner4d391482007-12-12 07:09:47 +0000829 llvm::DenseSet<Selector> ClsMap;
830 // Check and see if class methods in class interface have been
831 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000832 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000833 E = IMPDecl->classmeth_end(); I != E; ++I)
834 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000835
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000837 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000838 if (!ClsMap.count((*I)->getSelector()))
839 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000840 else {
841 ObjCMethodDecl *ImpMethodDecl =
842 IMPDecl->getClassMethod((*I)->getSelector());
843 ObjCMethodDecl *IntfMethodDecl =
844 IDecl->getClassMethod((*I)->getSelector());
845 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
846 }
847
Chris Lattner4d391482007-12-12 07:09:47 +0000848
849 // Check the protocol list for unimplemented methods in the @implementation
850 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000851 const ObjCList<ObjCProtocolDecl> &Protocols =
852 IDecl->getReferencedProtocols();
853 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
854 E = Protocols.end(); I != E; ++I)
855 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000856 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000857}
858
859/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000860/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
862 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000863 llvm::DenseSet<Selector> InsMap;
864 // Check and see if instance methods in category interface have been
865 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000867 E = CatImplDecl->instmeth_end(); I != E; ++I)
868 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000869
870 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000872 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000873 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000874 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000875 else {
876 ObjCMethodDecl *ImpMethodDecl =
877 CatImplDecl->getInstanceMethod((*I)->getSelector());
878 ObjCMethodDecl *IntfMethodDecl =
879 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000880 assert(IntfMethodDecl &&
881 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
882 // ImpMethodDecl may be null as in a @dynamic property.
883 if (ImpMethodDecl)
884 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000885 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000886
Chris Lattner4d391482007-12-12 07:09:47 +0000887 llvm::DenseSet<Selector> ClsMap;
888 // Check and see if class methods in category interface have been
889 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000890 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000891 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
892 I != E; ++I)
893 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000894
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000896 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000897 if (!ClsMap.count((*I)->getSelector()))
898 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000899 else {
900 ObjCMethodDecl *ImpMethodDecl =
901 CatImplDecl->getClassMethod((*I)->getSelector());
902 ObjCMethodDecl *IntfMethodDecl =
903 CatClassDecl->getClassMethod((*I)->getSelector());
904 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
905 }
Chris Lattner4d391482007-12-12 07:09:47 +0000906 // Check the protocol list for unimplemented methods in the @implementation
907 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000908 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
909 E = CatClassDecl->protocol_end(); PI != E; ++PI)
910 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000911 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000912}
913
914/// ActOnForwardClassDeclaration -
915Action::DeclTy *
916Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000917 IdentifierInfo **IdentList,
918 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000920
921 for (unsigned i = 0; i != NumElts; ++i) {
922 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000923 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000924 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000925 // Maybe we will complain about the shadowed template parameter.
926 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
927 // Just pretend that we didn't see the previous declaration.
928 PrevDecl = 0;
929 }
930
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000931 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000932 // GCC apparently allows the following idiom:
933 //
934 // typedef NSObject < XCElementTogglerP > XCElementToggler;
935 // @class XCElementToggler;
936 //
937 // FIXME: Make an extension?
938 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
939 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000941 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000942 }
Chris Lattner4d391482007-12-12 07:09:47 +0000943 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000944 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000945 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000946 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
947 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000948 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000949
Douglas Gregord0434102009-01-09 00:49:46 +0000950 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000951 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000952 // Remember that this needs to be removed when the scope is popped.
953 TUScope->AddDecl(IDecl);
954 }
955
956 Interfaces.push_back(IDecl);
957 }
958
Douglas Gregord0434102009-01-09 00:49:46 +0000959 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000960 &Interfaces[0],
961 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000962 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000963 CheckObjCDeclScope(CDecl);
964 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000965}
966
967
968/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
969/// returns true, or false, accordingly.
970/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000972 const ObjCMethodDecl *PrevMethod,
973 bool matchBasedOnSizeAndAlignment) {
974 QualType T1 = Context.getCanonicalType(Method->getResultType());
975 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
976
977 if (T1 != T2) {
978 // The result types are different.
979 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000980 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000981 // Incomplete types don't have a size and alignment.
982 if (T1->isIncompleteType() || T2->isIncompleteType())
983 return false;
984 // Check is based on size and alignment.
985 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
986 return false;
987 }
988 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
989 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
990 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
991 if (T1 != T2) {
992 // The result types are different.
993 if (!matchBasedOnSizeAndAlignment)
994 return false;
995 // Incomplete types don't have a size and alignment.
996 if (T1->isIncompleteType() || T2->isIncompleteType())
997 return false;
998 // Check is based on size and alignment.
999 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1000 return false;
1001 }
Chris Lattner4d391482007-12-12 07:09:47 +00001002 }
1003 return true;
1004}
1005
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1007 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001008 if (!FirstMethod.Method) {
1009 // Haven't seen a method with this selector name yet - add it.
1010 FirstMethod.Method = Method;
1011 FirstMethod.Next = 0;
1012 } else {
1013 // We've seen a method with this name, now check the type signature(s).
1014 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1015
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001017 Next = Next->Next)
1018 match = MatchTwoMethodDeclarations(Method, Next->Method);
1019
1020 if (!match) {
1021 // We have a new signature for an existing method - add it.
1022 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001023 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001024 }
1025 }
1026}
1027
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001028// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001029ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1030 SourceRange R) {
1031 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001032 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001033
1034 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001035 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1036 // This checks if the methods differ by size & alignment.
1037 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1038 issueWarning = true;
1039 }
1040 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001041 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001042 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001043 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001044 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001045 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001046 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001047 }
1048 return MethList.Method;
1049}
1050
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1052 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001053 if (!FirstMethod.Method) {
1054 // Haven't seen a method with this selector name yet - add it.
1055 FirstMethod.Method = Method;
1056 FirstMethod.Next = 0;
1057 } else {
1058 // We've seen a method with this name, now check the type signature(s).
1059 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1060
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001061 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001062 Next = Next->Next)
1063 match = MatchTwoMethodDeclarations(Method, Next->Method);
1064
1065 if (!match) {
1066 // We have a new signature for an existing method - add it.
1067 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001068 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001069 FirstMethod.Next = OMI;
1070 }
1071 }
1072}
1073
Steve Naroff0701bbb2009-01-08 17:28:14 +00001074/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1075/// have the property type and issue diagnostics if they don't.
1076/// Also synthesize a getter/setter method if none exist (and update the
1077/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1078/// methods is the "right" thing to do.
1079void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1080 ObjCContainerDecl *CD) {
1081 ObjCMethodDecl *GetterMethod, *SetterMethod;
1082
1083 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1084 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1085
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001086 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001087 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001088 Diag(property->getLocation(),
1089 diag::err_accessor_property_type_mismatch)
1090 << property->getDeclName()
1091 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001092 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1093 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001094
1095 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001096 if (Context.getCanonicalType(SetterMethod->getResultType())
1097 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001098 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1099 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001100 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001101 Diag(property->getLocation(),
1102 diag::err_accessor_property_type_mismatch)
1103 << property->getDeclName()
1104 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001105 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1106 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001107 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001108
1109 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001110 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001111 // FIXME: The synthesized property we set here is misleading. We
1112 // almost always synthesize these methods unless the user explicitly
1113 // provided prototypes (which is odd, but allowed). Sema should be
1114 // typechecking that the declarations jive in that situation (which
1115 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001116 if (!GetterMethod) {
1117 // No instance method of same name as property getter name was found.
1118 // Declare a getter method and add it to the list of methods
1119 // for this class.
1120 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1121 property->getLocation(), property->getGetterName(),
1122 property->getType(), CD, true, false, true,
1123 (property->getPropertyImplementation() ==
1124 ObjCPropertyDecl::Optional) ?
1125 ObjCMethodDecl::Optional :
1126 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001127 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001128 } else
1129 // A user declared getter will be synthesize when @synthesize of
1130 // the property with the same name is seen in the @implementation
1131 GetterMethod->setIsSynthesized();
1132 property->setGetterMethodDecl(GetterMethod);
1133
1134 // Skip setter if property is read-only.
1135 if (!property->isReadOnly()) {
1136 // Find the default setter and if one not found, add one.
1137 if (!SetterMethod) {
1138 // No instance method of same name as property setter name was found.
1139 // Declare a setter method and add it to the list of methods
1140 // for this class.
1141 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1142 property->getLocation(),
1143 property->getSetterName(),
1144 Context.VoidTy, CD, true, false, true,
1145 (property->getPropertyImplementation() ==
1146 ObjCPropertyDecl::Optional) ?
1147 ObjCMethodDecl::Optional :
1148 ObjCMethodDecl::Required);
1149 // Invent the arguments for the setter. We don't bother making a
1150 // nice name for the argument.
1151 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1152 SourceLocation(),
1153 property->getIdentifier(),
1154 property->getType(),
1155 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001156 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001157 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001158 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001159 } else
1160 // A user declared setter will be synthesize when @synthesize of
1161 // the property with the same name is seen in the @implementation
1162 SetterMethod->setIsSynthesized();
1163 property->setSetterMethodDecl(SetterMethod);
1164 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001165 // Add any synthesized methods to the global pool. This allows us to
1166 // handle the following, which is supported by GCC (and part of the design).
1167 //
1168 // @interface Foo
1169 // @property double bar;
1170 // @end
1171 //
1172 // void thisIsUnfortunate() {
1173 // id foo;
1174 // double bar = [foo bar];
1175 // }
1176 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001177 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001178 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001179 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001180 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001181}
1182
Steve Naroffa56f6162007-12-18 01:30:32 +00001183// Note: For class/category implemenations, allMethods/allProperties is
1184// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001185void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1186 DeclTy **allMethods, unsigned allNum,
1187 DeclTy **allProperties, unsigned pNum) {
1188 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1189
Steve Naroffa56f6162007-12-18 01:30:32 +00001190 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1191 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001192 // should be true.
1193 if (!ClassDecl)
1194 return;
1195
Chris Lattner4d391482007-12-12 07:09:47 +00001196 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001197 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1198 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001199 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001200
Steve Naroff0701bbb2009-01-08 17:28:14 +00001201 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001202
1203 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1204 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1205 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1206
Chris Lattner4d391482007-12-12 07:09:47 +00001207 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001208 ObjCMethodDecl *Method =
1209 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001210
1211 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001212 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001213 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001214 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001215 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1216 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001217 if ((isInterfaceDeclKind && PrevMethod && !match)
1218 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001219 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001220 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001221 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001222 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001223 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001224 InsMap[Method->getSelector()] = Method;
1225 /// The following allows us to typecheck messages to "id".
1226 AddInstanceMethodToGlobalPool(Method);
1227 }
1228 }
1229 else {
1230 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001231 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001232 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1233 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001234 if ((isInterfaceDeclKind && PrevMethod && !match)
1235 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001236 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001237 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001238 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001239 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001240 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001241 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001242 /// The following allows us to typecheck messages to "Class".
1243 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001244 }
1245 }
1246 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001247 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001248 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001249 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001250 ComparePropertiesInBaseAndSuper(I);
1251 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001252 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001253 // Categories are used to extend the class by declaring new methods.
1254 // By the same token, they are also used to add new properties. No
1255 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001256
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001257 // Merge protocol properties into category
1258 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001259 }
Steve Naroff09c47192009-01-09 15:36:25 +00001260 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1261 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1262 // user-defined setter/getter. It also synthesizes setter/getter methods
1263 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001264 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1265 E = CDecl->prop_end(); I != E; ++I)
1266 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001267 CDecl->setAtEndLoc(AtEndLoc);
1268 }
1269 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001270 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001271 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001272 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001273 } else if (ObjCCategoryImplDecl* CatImplClass =
1274 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001275 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001276
Chris Lattner4d391482007-12-12 07:09:47 +00001277 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001278 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001279 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001280 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001281 Categories; Categories = Categories->getNextClassCategory()) {
1282 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1283 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1284 break;
1285 }
1286 }
1287 }
1288 }
1289}
1290
1291
1292/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1293/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294static Decl::ObjCDeclQualifier
1295CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1296 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1297 if (PQTVal & ObjCDeclSpec::DQ_In)
1298 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1299 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1300 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1301 if (PQTVal & ObjCDeclSpec::DQ_Out)
1302 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1303 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1304 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1305 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1306 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1307 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1308 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001309
1310 return ret;
1311}
1312
1313Sema::DeclTy *Sema::ActOnMethodDeclaration(
1314 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001315 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001316 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001317 Selector Sel,
1318 // optional arguments. The number of types/arguments is obtained
1319 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001320 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001321 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001322 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1323 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001324 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001325
1326 // Make sure we can establish a context for the method.
1327 if (!ClassDecl) {
1328 Diag(MethodLoc, diag::error_missing_method_context);
1329 return 0;
1330 }
Chris Lattner4d391482007-12-12 07:09:47 +00001331 QualType resultDeclType;
1332
1333 if (ReturnType)
1334 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1335 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001336 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001337
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001338 ObjCMethodDecl* ObjCMethod =
1339 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001340 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001341 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001342 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001343 MethodDeclKind == tok::objc_optional ?
1344 ObjCMethodDecl::Optional :
1345 ObjCMethodDecl::Required);
1346
Chris Lattner0ed844b2008-04-04 06:12:32 +00001347 llvm::SmallVector<ParmVarDecl*, 16> Params;
1348
1349 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1350 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001351 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001352
Steve Naroff6082c622008-12-09 19:36:17 +00001353 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001354 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001355 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001356 if (argType->isArrayType()) { // (char *[]) -> (char **)
1357 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001358 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001359 }
Steve Naroff6082c622008-12-09 19:36:17 +00001360 else if (argType->isFunctionType())
1361 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001362 else if (argType->isObjCInterfaceType()) {
1363 // FIXME! provide more precise location for the parameter
1364 Diag(MethodLoc, diag::err_object_as_method_param);
1365 return 0;
1366 }
Steve Naroff6082c622008-12-09 19:36:17 +00001367 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001368 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001369 ParmVarDecl* Param;
1370 if (originalArgType.isNull())
1371 Param = ParmVarDecl::Create(Context, ObjCMethod,
1372 SourceLocation(/*FIXME*/),
1373 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001374 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001375 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001376 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1377 SourceLocation(/*FIXME*/),
1378 ArgNames[i], argType, originalArgType,
1379 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001380
Chris Lattner0ed844b2008-04-04 06:12:32 +00001381 Param->setObjCDeclQualifier(
1382 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1383 Params.push_back(Param);
1384 }
1385
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001386 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1387 ObjCMethod->setObjCDeclQualifier(
1388 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1389 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001390
1391 if (AttrList)
1392 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001393
1394 // For implementations (which can be very "coarse grain"), we add the
1395 // method now. This allows the AST to implement lookup methods that work
1396 // incrementally (without waiting until we parse the @end). It also allows
1397 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001398 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001399 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001400 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001401 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001403 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001404 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001406 }
1407 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001409 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001410 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001411 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001413 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001414 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001416 }
1417 }
1418 if (PrevMethod) {
1419 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001420 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001421 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001422 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001423 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001424 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001425}
1426
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001427void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1428 SourceLocation Loc,
1429 unsigned &Attributes) {
1430 // FIXME: Improve the reported location.
1431
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001432 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001433 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001434 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1435 ObjCDeclSpec::DQ_PR_assign |
1436 ObjCDeclSpec::DQ_PR_copy |
1437 ObjCDeclSpec::DQ_PR_retain))) {
1438 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1439 "readwrite" :
1440 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1441 "assign" :
1442 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1443 "copy" : "retain";
1444
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001445 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001446 diag::err_objc_property_attr_mutually_exclusive :
1447 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001448 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001449 }
1450
1451 // Check for copy or retain on non-object types.
1452 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1453 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001454 Diag(Loc, diag::err_objc_property_requires_object)
1455 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001456 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1457 }
1458
1459 // Check for more than one of { assign, copy, retain }.
1460 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1461 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001462 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1463 << "assign" << "copy";
1464 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001465 }
1466 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001467 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1468 << "assign" << "retain";
1469 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001470 }
1471 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1472 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001473 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1474 << "copy" << "retain";
1475 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001476 }
1477 }
1478
1479 // Warn if user supplied no assignment attribute, property is
1480 // readwrite, and this is an object type.
1481 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1482 ObjCDeclSpec::DQ_PR_retain)) &&
1483 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1484 Context.isObjCObjectPointerType(PropertyTy)) {
1485 // Skip this warning in gc-only mode.
1486 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1487 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1488
1489 // If non-gc code warn that this is likely inappropriate.
1490 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1491 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1492
1493 // FIXME: Implement warning dependent on NSCopying being
1494 // implemented. See also:
1495 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1496 // (please trim this list while you are at it).
1497 }
1498}
1499
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001500Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1501 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001502 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001503 Selector GetterSel,
1504 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001505 DeclTy *ClassCategory,
1506 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001507 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001508 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001509 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1510 // default is readwrite!
1511 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1512 // property is defaulted to 'assign' if it is readwrite and is
1513 // not retain or copy
1514 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1515 (isReadWrite &&
1516 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1517 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1518 QualType T = GetTypeForDeclarator(FD.D, S);
1519 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001520
1521 // May modify Attributes.
1522 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001523
1524 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1525 if (!CDecl->getIdentifier()) {
1526 // This is an anonymous category. property requires special
1527 // handling.
1528 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1529 if (ObjCPropertyDecl *PIDecl =
1530 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1531 // property 'PIDecl's readonly attribute will be over-ridden
1532 // with anonymous category's readwrite property attribute!
1533 unsigned PIkind = PIDecl->getPropertyAttributes();
1534 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001535 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001536 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1537 Diag(AtLoc, diag::warn_property_attr_mismatch);
1538 PIDecl->makeitReadWriteAttribute();
1539 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1540 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1541 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1542 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1543 PIDecl->setSetterName(SetterSel);
1544 // FIXME: use a common routine with addPropertyMethods.
1545 ObjCMethodDecl *SetterDecl =
1546 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1547 Context.VoidTy,
1548 ICDecl,
1549 true, false, true,
1550 ObjCMethodDecl::Required);
1551 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1552 SetterDecl,
1553 SourceLocation(),
1554 FD.D.getIdentifier(),
1555 T,
1556 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001557 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001558 SetterDecl->setMethodParams(&Argument, 1);
1559 PIDecl->setSetterMethodDecl(SetterDecl);
1560 }
1561 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001562 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001563 *isOverridingProperty = true;
1564 return 0;
1565 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001566 // No matching property found in the main class. Just fall thru
1567 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001568 // it works as is. This category becomes just like a category
1569 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001570 } else {
1571 Diag(CDecl->getLocation(), diag::err_continuation_class);
1572 *isOverridingProperty = true;
1573 return 0;
1574 }
1575 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001576
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001577 Type *t = T.getTypePtr();
1578 if (t->isArrayType() || t->isFunctionType())
1579 Diag(AtLoc, diag::err_property_type) << T;
1580
Steve Naroff93983f82009-01-11 12:47:58 +00001581 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1582 assert(DC && "ClassDecl is not a DeclContext");
1583 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001584 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001585 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001586
1587 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001588
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001589 // Regardless of setter/getter attribute, we save the default getter/setter
1590 // selector names in anticipation of declaration of setter/getter methods.
1591 PDecl->setGetterName(GetterSel);
1592 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001593
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001594 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001596
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001597 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001599
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001600 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001602
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001603 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001605
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001606 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001608
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001609 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001611
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001612 if (isAssign)
1613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1614
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001615 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001617
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001618 if (MethodImplKind == tok::objc_required)
1619 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1620 else if (MethodImplKind == tok::objc_optional)
1621 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1622
Chris Lattner4d391482007-12-12 07:09:47 +00001623 return PDecl;
1624}
1625
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001626/// ActOnPropertyImplDecl - This routine performs semantic checks and
1627/// builds the AST node for a property implementation declaration; declared
1628/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001629///
1630Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1631 SourceLocation PropertyLoc,
1632 bool Synthesize,
1633 DeclTy *ClassCatImpDecl,
1634 IdentifierInfo *PropertyId,
1635 IdentifierInfo *PropertyIvar) {
1636 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1637 // Make sure we have a context for the property implementation declaration.
1638 if (!ClassImpDecl) {
1639 Diag(AtLoc, diag::error_missing_property_context);
1640 return 0;
1641 }
1642 ObjCPropertyDecl *property = 0;
1643 ObjCInterfaceDecl* IDecl = 0;
1644 // Find the class or category class where this property must have
1645 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001646 ObjCImplementationDecl *IC = 0;
1647 ObjCCategoryImplDecl* CatImplClass = 0;
1648 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001649 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001650 // We always synthesize an interface for an implementation
1651 // without an interface decl. So, IDecl is always non-zero.
1652 assert(IDecl &&
1653 "ActOnPropertyImplDecl - @implementation without @interface");
1654
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001655 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001656 property = IDecl->FindPropertyDeclaration(PropertyId);
1657 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001658 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001659 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001660 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001661 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001662 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001663 if (Synthesize) {
1664 Diag(AtLoc, diag::error_synthesize_category_decl);
1665 return 0;
1666 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001667 IDecl = CatImplClass->getClassInterface();
1668 if (!IDecl) {
1669 Diag(AtLoc, diag::error_missing_property_interface);
1670 return 0;
1671 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001672 ObjCCategoryDecl *Category =
1673 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1674
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001675 // If category for this implementation not found, it is an error which
1676 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001677 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001678 return 0;
1679 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001680 property = Category->FindPropertyDeclaration(PropertyId);
1681 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001682 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001683 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001684 return 0;
1685 }
1686 }
1687 else {
1688 Diag(AtLoc, diag::error_bad_property_context);
1689 return 0;
1690 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001691 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001692 // Check that we have a valid, previously declared ivar for @synthesize
1693 if (Synthesize) {
1694 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001695 if (!PropertyIvar)
1696 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001697 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001698 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001699 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001700 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001701 return 0;
1702 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001703 QualType PropType = Context.getCanonicalType(property->getType());
1704 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1705
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001706 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001707 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001708 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001709 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001710 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001711 return 0;
1712 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001713 else {
1714 // FIXME! Rules for properties are somewhat different that those
1715 // for assignments. Use a new routine to consolidate all cases;
1716 // specifically for property redeclarations as well as for ivars.
1717 QualType lhsType =
1718 Context.getCanonicalType(PropType).getUnqualifiedType();
1719 QualType rhsType =
1720 Context.getCanonicalType(IvarType).getUnqualifiedType();
1721 if (lhsType != rhsType &&
1722 lhsType->isArithmeticType()) {
1723 Diag(PropertyLoc, diag::error_property_ivar_type)
1724 << property->getDeclName() << Ivar->getDeclName();
1725 return 0;
1726 }
1727 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001728 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001729 } else if (PropertyIvar) {
1730 // @dynamic
1731 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1732 return 0;
1733 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001734 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001735 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001736 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1737 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001738 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001739 ObjCPropertyImplDecl::Synthesize
1740 : ObjCPropertyImplDecl::Dynamic),
1741 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001742 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001743 if (IC) {
1744 if (Synthesize)
1745 if (ObjCPropertyImplDecl *PPIDecl =
1746 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1747 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1748 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1749 << PropertyIvar;
1750 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1751 }
1752
1753 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1754 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1755 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1756 return 0;
1757 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001758 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001759 }
1760 else {
1761 if (Synthesize)
1762 if (ObjCPropertyImplDecl *PPIDecl =
1763 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1764 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1765 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1766 << PropertyIvar;
1767 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1768 }
1769
1770 if (ObjCPropertyImplDecl *PPIDecl =
1771 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1772 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1773 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1774 return 0;
1775 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001776 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001777 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001778
1779 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001780}
Anders Carlsson15281452008-11-04 16:57:32 +00001781
Chris Lattnercc98eac2008-12-17 07:13:27 +00001782bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001783 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001784 return false;
1785
1786 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1787 D->setInvalidDecl();
1788
1789 return true;
1790}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001791
1792/// Collect the instance variables declared in an Objective-C object. Used in
1793/// the creation of structures from objects using the @defs directive.
1794/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1795/// part of the AST generation logic of @defs.
1796static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1797 ASTContext& Ctx,
1798 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1799 if (Class->getSuperClass())
1800 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1801
1802 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1803 for (ObjCInterfaceDecl::ivar_iterator
1804 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1805
1806 ObjCIvarDecl* ID = *I;
1807 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1808 ID->getLocation(),
1809 ID->getIdentifier(),
1810 ID->getType(),
1811 ID->getBitWidth()));
1812 }
1813}
1814
1815/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1816/// instance variables of ClassName into Decls.
1817void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1818 IdentifierInfo *ClassName,
1819 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1820 // Check that ClassName is a valid class
1821 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1822 if (!Class) {
1823 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1824 return;
1825 }
1826 // Collect the instance variables
1827 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1828
1829 // Introduce all of these fields into the appropriate scope.
1830 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1831 D != Decls.end(); ++D) {
1832 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1833 if (getLangOptions().CPlusPlus)
1834 PushOnScopeChains(cast<FieldDecl>(FD), S);
1835 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001836 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001837 }
1838}
1839