blob: 76a5c2c42a367cf07983ed52c2dfa10eed662f40 [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
266 if (PDecl->getAttr<UnavailableAttr>())
267 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
268 PDecl->getDeclName();
269 if (PDecl->getAttr<DeprecatedAttr>())
270 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
271 PDecl->getDeclName();
Chris Lattnereacc3922008-07-26 03:47:43 +0000272
273 // If this is a forward declaration and we are supposed to warn in this
274 // case, do it.
275 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000276 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000277 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000278 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000279 }
280}
281
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000282/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000283/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000285void
286Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
287 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000288 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289 ObjCPropertyDecl::PropertyAttributeKind CAttr =
290 Property->getPropertyAttributes();
291 ObjCPropertyDecl::PropertyAttributeKind SAttr =
292 SuperProperty->getPropertyAttributes();
293 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
294 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000295 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000296 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000297 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
298 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000299 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000300 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
302 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305
306 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
307 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000308 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000309 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000311 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000312 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000313 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316
Chris Lattner717250a2008-07-26 20:50:02 +0000317 if (Context.getCanonicalType(Property->getType()) !=
318 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000319 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000320 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000321
322}
323
324/// ComparePropertiesInBaseAndSuper - This routine compares property
325/// declarations in base and its super class, if any, and issues
326/// diagnostics in a variety of inconsistant situations.
327///
Chris Lattner70f19542009-02-16 21:26:43 +0000328void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
330 if (!SDecl)
331 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000332 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000333 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
334 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000335 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000336 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000337 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
338 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000339 ObjCPropertyDecl *PDecl = (*I);
340 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000341 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 }
344 }
345}
346
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000347/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
348/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000349/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000350void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000351Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000352 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000353 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
354 if (!IDecl) {
355 // Category
356 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
357 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000358 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
359 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000360 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000361 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000362 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000363 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000364 CP != CE; ++CP)
365 if ((*CP)->getIdentifier() == Pr->getIdentifier())
366 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000367 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368 // Property protocol already exist in class. Diagnose any mismatch.
369 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
370 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000371 return;
372 }
Steve Naroff09c47192009-01-09 15:36:25 +0000373 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
374 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000375 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000376 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000377 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000378 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000379 CP != CE; ++CP)
380 if ((*CP)->getIdentifier() == Pr->getIdentifier())
381 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000382 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000384 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386}
387
388/// MergeProtocolPropertiesIntoClass - This routine merges properties
389/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000390/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000391///
Chris Lattner70f19542009-02-16 21:26:43 +0000392void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
393 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000394 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000395 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
396
397 if (!IDecl) {
398 // Category
399 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
400 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
401 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
402 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
403 E = MDecl->protocol_end(); P != E; ++P)
404 // Merge properties of category (*P) into IDECL's
405 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
406
407 // Go thru the list of protocols for this category and recursively merge
408 // their properties into this class as well.
409 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
410 E = CatDecl->protocol_end(); P != E; ++P)
411 MergeProtocolPropertiesIntoClass(CatDecl, *P);
412 } else {
413 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
414 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
415 E = MD->protocol_end(); P != E; ++P)
416 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
417 }
418 return;
419 }
420
Chris Lattnerb752f282008-07-21 07:06:49 +0000421 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000422 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
423 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000425 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
426
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 // Go thru the list of protocols for this class and recursively merge
428 // their properties into this class as well.
429 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
430 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000431 MergeProtocolPropertiesIntoClass(IDecl, *P);
432 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000433 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
434 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
435 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000436 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000437 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438}
439
Chris Lattner4d391482007-12-12 07:09:47 +0000440/// ActOnForwardProtocolDeclaration -
441Action::DeclTy *
442Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000443 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000444 unsigned NumElts,
445 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000446 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000447
448 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000449 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000450 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000451 if (PDecl == 0) { // Not already seen?
452 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
453 IdentList[i].second, Ident);
454 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000455 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000456 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000457 if (attrList)
458 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000459 Protocols.push_back(PDecl);
460 }
Anders Carlsson15281452008-11-04 16:57:32 +0000461
462 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000463 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000464 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000465 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000466 CheckObjCDeclScope(PDecl);
467 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000468}
469
Chris Lattner7caeabd2008-07-21 22:17:28 +0000470Sema::DeclTy *Sema::
471ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
472 IdentifierInfo *ClassName, SourceLocation ClassLoc,
473 IdentifierInfo *CategoryName,
474 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000475 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000476 unsigned NumProtoRefs,
477 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000478 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000479 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
480 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000481 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000482
483 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000484 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000485 if (!IDecl || IDecl->isForwardDecl()) {
486 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000487 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000488 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000489 }
Chris Lattner4d391482007-12-12 07:09:47 +0000490
Chris Lattner70f19542009-02-16 21:26:43 +0000491 CDecl->setClassInterface(IDecl);
492
493 /// Check for duplicate interface declaration for this category
494 ObjCCategoryDecl *CDeclChain;
495 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
496 CDeclChain = CDeclChain->getNextClassCategory()) {
497 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
498 Diag(CategoryLoc, diag::warn_dup_category_def)
499 << ClassName << CategoryName;
500 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
501 break;
502 }
503 }
504 if (!CDeclChain)
505 CDecl->insertNextClassCategory();
506
Chris Lattner4d391482007-12-12 07:09:47 +0000507 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000508 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
509 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000510 }
Anders Carlsson15281452008-11-04 16:57:32 +0000511
512 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000513 return CDecl;
514}
515
516/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000518/// object.
519Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
520 SourceLocation AtCatImplLoc,
521 IdentifierInfo *ClassName, SourceLocation ClassLoc,
522 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000524 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000525 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
526 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000527 /// Check that class of this category is already completely declared.
528 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000529 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000530
Douglas Gregord0434102009-01-09 00:49:46 +0000531 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000532 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000533
Chris Lattner4d391482007-12-12 07:09:47 +0000534 /// TODO: Check that CatName, category name, is not used in another
535 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000536 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000537
538 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000539 return CDecl;
540}
541
542Sema::DeclTy *Sema::ActOnStartClassImplementation(
543 SourceLocation AtClassImplLoc,
544 IdentifierInfo *ClassName, SourceLocation ClassLoc,
545 IdentifierInfo *SuperClassname,
546 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000547 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000548 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000549 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000551 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000552 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000553 }
554 else {
555 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000556 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000557 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000558 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
560
561 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000563 if (SuperClassname) {
564 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000565 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
568 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000569 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000570 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000572 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 Diag(SuperClassLoc, diag::err_undef_superclass)
574 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000575 else if (IDecl && IDecl->getSuperClass() != SDecl) {
576 // This implementation and its interface do not have the same
577 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000578 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000579 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000580 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 }
582 }
583 }
584
585 if (!IDecl) {
586 // Legacy case of @implementation with no corresponding @interface.
587 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000588
589 // FIXME: Do we support attributes on the @implementation? If so
590 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000591 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
592 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000593 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000594 IDecl->setSuperClass(SDecl);
595 IDecl->setLocEnd(ClassLoc);
596
Douglas Gregord0434102009-01-09 00:49:46 +0000597 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000598 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000599 // Remember that this needs to be removed when the scope is popped.
600 TUScope->AddDecl(IDecl);
601 }
602
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000604 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000605 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000606
Douglas Gregord0434102009-01-09 00:49:46 +0000607 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000608 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000609
Anders Carlsson15281452008-11-04 16:57:32 +0000610 if (CheckObjCDeclScope(IMPDecl))
611 return IMPDecl;
612
Chris Lattner4d391482007-12-12 07:09:47 +0000613 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000615 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000616 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000617 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000619 return IMPDecl;
620}
621
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
623 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000624 SourceLocation RBrace) {
625 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000626 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000627 if (!IDecl)
628 return;
629 /// Check case of non-existing @interface decl.
630 /// (legacy objective-c @implementation decl without an @interface decl).
631 /// Add implementations's ivar to the synthesize class's ivar list.
632 if (IDecl->ImplicitInterfaceDecl()) {
633 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
634 return;
635 }
636 // If implementation has empty ivar list, just return.
637 if (numIvars == 0)
638 return;
639
640 assert(ivars && "missing @implementation ivars");
641
642 // Check interface's Ivar list against those in the implementation.
643 // names and types must match.
644 //
Chris Lattner4d391482007-12-12 07:09:47 +0000645 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000646 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000647 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
648 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649 ObjCIvarDecl* ImplIvar = ivars[j++];
650 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000651 assert (ImplIvar && "missing implementation ivar");
652 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000653 if (Context.getCanonicalType(ImplIvar->getType()) !=
654 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000655 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000656 << ImplIvar->getIdentifier()
657 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000658 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000659 }
660 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
661 // as error.
662 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000663 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000664 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000665 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000666 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000667 }
668 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000669 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000670
671 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000672 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000673 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000674 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000675}
676
Steve Naroff3c2eb662008-02-10 21:38:56 +0000677void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
678 bool &IncompleteImpl) {
679 if (!IncompleteImpl) {
680 Diag(ImpLoc, diag::warn_incomplete_impl);
681 IncompleteImpl = true;
682 }
Chris Lattner08631c52008-11-23 21:45:46 +0000683 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000684}
685
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000686void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
687 ObjCMethodDecl *IntfMethodDecl) {
688 bool err = false;
689 QualType ImpMethodQType =
690 Context.getCanonicalType(ImpMethodDecl->getResultType());
691 QualType IntfMethodQType =
692 Context.getCanonicalType(IntfMethodDecl->getResultType());
693 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
694 err = true;
695 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
696 IF=IntfMethodDecl->param_begin(),
697 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
698 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
699 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
700 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
701 err = true;
702 break;
703 }
704 }
705 if (err) {
706 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
707 << ImpMethodDecl->getDeclName();
708 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
709 }
710}
711
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000712/// isPropertyReadonly - Return true if property is readonly, by searching
713/// for the property in the class and in its categories and implementations
714///
715bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
716 ObjCInterfaceDecl *IDecl) const {
717 // by far the most common case.
718 if (!PDecl->isReadOnly())
719 return false;
720 // Even if property is ready only, if interface has a user defined setter,
721 // it is not considered read only.
722 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
723 return false;
724
725 // Main class has the property as 'readonly'. Must search
726 // through the category list to see if the property's
727 // attribute has been over-ridden to 'readwrite'.
728 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
729 Category; Category = Category->getNextClassCategory()) {
730 // Even if property is ready only, if a category has a user defined setter,
731 // it is not considered read only.
732 if (Category->getInstanceMethod(PDecl->getSetterName()))
733 return false;
734 ObjCPropertyDecl *P =
735 Category->FindPropertyDeclaration(PDecl->getIdentifier());
736 if (P && !P->isReadOnly())
737 return false;
738 }
739
740 // Also, check for definition of a setter method in the implementation if
741 // all else failed.
742 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
743 if (ObjCImplementationDecl *IMD =
744 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
745 if (IMD->getInstanceMethod(PDecl->getSetterName()))
746 return false;
747 }
748 else if (ObjCCategoryImplDecl *CIMD =
749 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
750 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
751 return false;
752 }
753 }
754 return true;
755}
756
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000757/// FIXME: Type hierarchies in Objective-C can be deep. We could most
758/// likely improve the efficiency of selector lookups and type
759/// checking by associating with each protocol / interface / category
760/// the flattened instance tables. If we used an immutable set to keep
761/// the table then it wouldn't add significant memory cost and it
762/// would be handy for lookups.
763
Steve Naroffefe7f362008-02-08 22:06:17 +0000764/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000765/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000766void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
767 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000768 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000769 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000770 const llvm::DenseSet<Selector> &ClsMap,
771 ObjCInterfaceDecl *IDecl) {
772 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
773
774 // If a method lookup fails locally we still need to look and see if
775 // the method was implemented by a base class or an inherited
776 // protocol. This lookup is slow, but occurs rarely in correct code
777 // and otherwise would terminate in a warning.
778
Chris Lattner4d391482007-12-12 07:09:47 +0000779 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000781 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000783 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000784 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000785 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000786 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000787 }
788 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000789 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000790 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000791 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000792 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
793 !ClsMap.count(method->getSelector()) &&
794 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000795 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000796 }
Chris Lattner780f3292008-07-21 21:32:27 +0000797 // Check on this protocols's referenced protocols, recursively.
798 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
799 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000800 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000801}
802
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
804 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000805 llvm::DenseSet<Selector> InsMap;
806 // Check and see if instance methods in class interface have been
807 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000809 E = IMPDecl->instmeth_end(); I != E; ++I)
810 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000811
812 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000814 E = IDecl->instmeth_end(); I != E; ++I) {
815 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000816 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000817 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000818 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000819
820 ObjCMethodDecl *ImpMethodDecl =
821 IMPDecl->getInstanceMethod((*I)->getSelector());
822 ObjCMethodDecl *IntfMethodDecl =
823 IDecl->getInstanceMethod((*I)->getSelector());
824 assert(IntfMethodDecl &&
825 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
826 // ImpMethodDecl may be null as in a @dynamic property.
827 if (ImpMethodDecl)
828 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
829 }
Chris Lattner4c525092007-12-12 17:58:05 +0000830
Chris Lattner4d391482007-12-12 07:09:47 +0000831 llvm::DenseSet<Selector> ClsMap;
832 // Check and see if class methods in class interface have been
833 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000834 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000835 E = IMPDecl->classmeth_end(); I != E; ++I)
836 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000837
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000838 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000839 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000840 if (!ClsMap.count((*I)->getSelector()))
841 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000842 else {
843 ObjCMethodDecl *ImpMethodDecl =
844 IMPDecl->getClassMethod((*I)->getSelector());
845 ObjCMethodDecl *IntfMethodDecl =
846 IDecl->getClassMethod((*I)->getSelector());
847 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
848 }
849
Chris Lattner4d391482007-12-12 07:09:47 +0000850
851 // Check the protocol list for unimplemented methods in the @implementation
852 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000853 const ObjCList<ObjCProtocolDecl> &Protocols =
854 IDecl->getReferencedProtocols();
855 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
856 E = Protocols.end(); I != E; ++I)
857 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000858 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000859}
860
861/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000862/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000863void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
864 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000865 llvm::DenseSet<Selector> InsMap;
866 // Check and see if instance methods in category interface have been
867 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000869 E = CatImplDecl->instmeth_end(); I != E; ++I)
870 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000871
872 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000873 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000874 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000875 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000876 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000877 else {
878 ObjCMethodDecl *ImpMethodDecl =
879 CatImplDecl->getInstanceMethod((*I)->getSelector());
880 ObjCMethodDecl *IntfMethodDecl =
881 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000882 assert(IntfMethodDecl &&
883 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
884 // ImpMethodDecl may be null as in a @dynamic property.
885 if (ImpMethodDecl)
886 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000887 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000888
Chris Lattner4d391482007-12-12 07:09:47 +0000889 llvm::DenseSet<Selector> ClsMap;
890 // Check and see if class methods in category interface have been
891 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000892 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000893 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
894 I != E; ++I)
895 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000896
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000897 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000898 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000899 if (!ClsMap.count((*I)->getSelector()))
900 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000901 else {
902 ObjCMethodDecl *ImpMethodDecl =
903 CatImplDecl->getClassMethod((*I)->getSelector());
904 ObjCMethodDecl *IntfMethodDecl =
905 CatClassDecl->getClassMethod((*I)->getSelector());
906 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
907 }
Chris Lattner4d391482007-12-12 07:09:47 +0000908 // Check the protocol list for unimplemented methods in the @implementation
909 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000910 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
911 E = CatClassDecl->protocol_end(); PI != E; ++PI)
912 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000913 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000914}
915
916/// ActOnForwardClassDeclaration -
917Action::DeclTy *
918Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000919 IdentifierInfo **IdentList,
920 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000922
923 for (unsigned i = 0; i != NumElts; ++i) {
924 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000925 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000926 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000927 // Maybe we will complain about the shadowed template parameter.
928 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
929 // Just pretend that we didn't see the previous declaration.
930 PrevDecl = 0;
931 }
932
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000934 // GCC apparently allows the following idiom:
935 //
936 // typedef NSObject < XCElementTogglerP > XCElementToggler;
937 // @class XCElementToggler;
938 //
939 // FIXME: Make an extension?
940 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
941 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000942 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000943 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000944 }
Chris Lattner4d391482007-12-12 07:09:47 +0000945 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000947 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000948 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
949 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000950 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000951
Douglas Gregord0434102009-01-09 00:49:46 +0000952 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000953 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000954 // Remember that this needs to be removed when the scope is popped.
955 TUScope->AddDecl(IDecl);
956 }
957
958 Interfaces.push_back(IDecl);
959 }
960
Douglas Gregord0434102009-01-09 00:49:46 +0000961 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000962 &Interfaces[0],
963 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000964 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000965 CheckObjCDeclScope(CDecl);
966 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000967}
968
969
970/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
971/// returns true, or false, accordingly.
972/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000974 const ObjCMethodDecl *PrevMethod,
975 bool matchBasedOnSizeAndAlignment) {
976 QualType T1 = Context.getCanonicalType(Method->getResultType());
977 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
978
979 if (T1 != T2) {
980 // The result types are different.
981 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000982 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000983 // Incomplete types don't have a size and alignment.
984 if (T1->isIncompleteType() || T2->isIncompleteType())
985 return false;
986 // Check is based on size and alignment.
987 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
988 return false;
989 }
990 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
991 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
992 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
993 if (T1 != T2) {
994 // The result types are different.
995 if (!matchBasedOnSizeAndAlignment)
996 return false;
997 // Incomplete types don't have a size and alignment.
998 if (T1->isIncompleteType() || T2->isIncompleteType())
999 return false;
1000 // Check is based on size and alignment.
1001 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1002 return false;
1003 }
Chris Lattner4d391482007-12-12 07:09:47 +00001004 }
1005 return true;
1006}
1007
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1009 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001010 if (!FirstMethod.Method) {
1011 // Haven't seen a method with this selector name yet - add it.
1012 FirstMethod.Method = Method;
1013 FirstMethod.Next = 0;
1014 } else {
1015 // We've seen a method with this name, now check the type signature(s).
1016 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1017
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001019 Next = Next->Next)
1020 match = MatchTwoMethodDeclarations(Method, Next->Method);
1021
1022 if (!match) {
1023 // We have a new signature for an existing method - add it.
1024 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001025 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001026 }
1027 }
1028}
1029
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001030// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001031ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1032 SourceRange R) {
1033 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001034 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001035
1036 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001037 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1038 // This checks if the methods differ by size & alignment.
1039 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1040 issueWarning = true;
1041 }
1042 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001043 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001044 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001045 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001046 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001047 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001048 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001049 }
1050 return MethList.Method;
1051}
1052
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001053void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1054 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001055 if (!FirstMethod.Method) {
1056 // Haven't seen a method with this selector name yet - add it.
1057 FirstMethod.Method = Method;
1058 FirstMethod.Next = 0;
1059 } else {
1060 // We've seen a method with this name, now check the type signature(s).
1061 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1062
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001063 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001064 Next = Next->Next)
1065 match = MatchTwoMethodDeclarations(Method, Next->Method);
1066
1067 if (!match) {
1068 // We have a new signature for an existing method - add it.
1069 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001071 FirstMethod.Next = OMI;
1072 }
1073 }
1074}
1075
Steve Naroff0701bbb2009-01-08 17:28:14 +00001076/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1077/// have the property type and issue diagnostics if they don't.
1078/// Also synthesize a getter/setter method if none exist (and update the
1079/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1080/// methods is the "right" thing to do.
1081void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1082 ObjCContainerDecl *CD) {
1083 ObjCMethodDecl *GetterMethod, *SetterMethod;
1084
1085 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1086 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1087
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001088 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001089 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001090 Diag(property->getLocation(),
1091 diag::err_accessor_property_type_mismatch)
1092 << property->getDeclName()
1093 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001094 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1095 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001096
1097 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001098 if (Context.getCanonicalType(SetterMethod->getResultType())
1099 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001100 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1101 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001102 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001103 Diag(property->getLocation(),
1104 diag::err_accessor_property_type_mismatch)
1105 << property->getDeclName()
1106 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001107 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1108 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001109 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001110
1111 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001112 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001113 // FIXME: The synthesized property we set here is misleading. We
1114 // almost always synthesize these methods unless the user explicitly
1115 // provided prototypes (which is odd, but allowed). Sema should be
1116 // typechecking that the declarations jive in that situation (which
1117 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001118 if (!GetterMethod) {
1119 // No instance method of same name as property getter name was found.
1120 // Declare a getter method and add it to the list of methods
1121 // for this class.
1122 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1123 property->getLocation(), property->getGetterName(),
1124 property->getType(), CD, true, false, true,
1125 (property->getPropertyImplementation() ==
1126 ObjCPropertyDecl::Optional) ?
1127 ObjCMethodDecl::Optional :
1128 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001129 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001130 } else
1131 // A user declared getter will be synthesize when @synthesize of
1132 // the property with the same name is seen in the @implementation
1133 GetterMethod->setIsSynthesized();
1134 property->setGetterMethodDecl(GetterMethod);
1135
1136 // Skip setter if property is read-only.
1137 if (!property->isReadOnly()) {
1138 // Find the default setter and if one not found, add one.
1139 if (!SetterMethod) {
1140 // No instance method of same name as property setter name was found.
1141 // Declare a setter method and add it to the list of methods
1142 // for this class.
1143 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1144 property->getLocation(),
1145 property->getSetterName(),
1146 Context.VoidTy, CD, true, false, true,
1147 (property->getPropertyImplementation() ==
1148 ObjCPropertyDecl::Optional) ?
1149 ObjCMethodDecl::Optional :
1150 ObjCMethodDecl::Required);
1151 // Invent the arguments for the setter. We don't bother making a
1152 // nice name for the argument.
1153 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1154 SourceLocation(),
1155 property->getIdentifier(),
1156 property->getType(),
1157 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001158 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001159 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001160 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001161 } else
1162 // A user declared setter will be synthesize when @synthesize of
1163 // the property with the same name is seen in the @implementation
1164 SetterMethod->setIsSynthesized();
1165 property->setSetterMethodDecl(SetterMethod);
1166 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001167 // Add any synthesized methods to the global pool. This allows us to
1168 // handle the following, which is supported by GCC (and part of the design).
1169 //
1170 // @interface Foo
1171 // @property double bar;
1172 // @end
1173 //
1174 // void thisIsUnfortunate() {
1175 // id foo;
1176 // double bar = [foo bar];
1177 // }
1178 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001179 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001180 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001181 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001182 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001183}
1184
Steve Naroffa56f6162007-12-18 01:30:32 +00001185// Note: For class/category implemenations, allMethods/allProperties is
1186// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001187void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1188 DeclTy **allMethods, unsigned allNum,
1189 DeclTy **allProperties, unsigned pNum) {
1190 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1191
Steve Naroffa56f6162007-12-18 01:30:32 +00001192 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1193 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001194 // should be true.
1195 if (!ClassDecl)
1196 return;
1197
Chris Lattner4d391482007-12-12 07:09:47 +00001198 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001199 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1200 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001201 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001202
Steve Naroff0701bbb2009-01-08 17:28:14 +00001203 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001204
1205 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1206 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1207 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1208
Chris Lattner4d391482007-12-12 07:09:47 +00001209 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001210 ObjCMethodDecl *Method =
1211 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001212
1213 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001214 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001215 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001217 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1218 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001219 if ((isInterfaceDeclKind && PrevMethod && !match)
1220 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001221 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001222 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001223 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001224 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001225 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001226 InsMap[Method->getSelector()] = Method;
1227 /// The following allows us to typecheck messages to "id".
1228 AddInstanceMethodToGlobalPool(Method);
1229 }
1230 }
1231 else {
1232 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001234 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1235 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001236 if ((isInterfaceDeclKind && PrevMethod && !match)
1237 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001238 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001239 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001240 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001241 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001242 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001243 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001244 /// The following allows us to typecheck messages to "Class".
1245 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001246 }
1247 }
1248 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001249 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001250 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001251 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001252 ComparePropertiesInBaseAndSuper(I);
1253 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001254 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001255 // Categories are used to extend the class by declaring new methods.
1256 // By the same token, they are also used to add new properties. No
1257 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001258
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001259 // Merge protocol properties into category
1260 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001261 }
Steve Naroff09c47192009-01-09 15:36:25 +00001262 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1263 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1264 // user-defined setter/getter. It also synthesizes setter/getter methods
1265 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001266 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1267 E = CDecl->prop_end(); I != E; ++I)
1268 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001269 CDecl->setAtEndLoc(AtEndLoc);
1270 }
1271 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001272 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001273 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001274 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001275 } else if (ObjCCategoryImplDecl* CatImplClass =
1276 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001277 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001278
Chris Lattner4d391482007-12-12 07:09:47 +00001279 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001280 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001281 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001283 Categories; Categories = Categories->getNextClassCategory()) {
1284 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1285 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1286 break;
1287 }
1288 }
1289 }
1290 }
1291}
1292
1293
1294/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1295/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001296static Decl::ObjCDeclQualifier
1297CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1298 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1299 if (PQTVal & ObjCDeclSpec::DQ_In)
1300 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1301 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1302 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1303 if (PQTVal & ObjCDeclSpec::DQ_Out)
1304 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1305 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1306 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1307 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1308 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1309 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1310 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001311
1312 return ret;
1313}
1314
1315Sema::DeclTy *Sema::ActOnMethodDeclaration(
1316 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001317 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001318 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001319 Selector Sel,
1320 // optional arguments. The number of types/arguments is obtained
1321 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001323 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001324 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1325 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001326 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001327
1328 // Make sure we can establish a context for the method.
1329 if (!ClassDecl) {
1330 Diag(MethodLoc, diag::error_missing_method_context);
1331 return 0;
1332 }
Chris Lattner4d391482007-12-12 07:09:47 +00001333 QualType resultDeclType;
1334
1335 if (ReturnType)
1336 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1337 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001338 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001339
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001340 ObjCMethodDecl* ObjCMethod =
1341 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001342 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001343 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001344 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001345 MethodDeclKind == tok::objc_optional ?
1346 ObjCMethodDecl::Optional :
1347 ObjCMethodDecl::Required);
1348
Chris Lattner0ed844b2008-04-04 06:12:32 +00001349 llvm::SmallVector<ParmVarDecl*, 16> Params;
1350
1351 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1352 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001353 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001354
Steve Naroff6082c622008-12-09 19:36:17 +00001355 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001356 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001357 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001358 if (argType->isArrayType()) { // (char *[]) -> (char **)
1359 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001360 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001361 }
Steve Naroff6082c622008-12-09 19:36:17 +00001362 else if (argType->isFunctionType())
1363 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001364 else if (argType->isObjCInterfaceType()) {
1365 // FIXME! provide more precise location for the parameter
1366 Diag(MethodLoc, diag::err_object_as_method_param);
1367 return 0;
1368 }
Steve Naroff6082c622008-12-09 19:36:17 +00001369 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001370 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001371 ParmVarDecl* Param;
1372 if (originalArgType.isNull())
1373 Param = ParmVarDecl::Create(Context, ObjCMethod,
1374 SourceLocation(/*FIXME*/),
1375 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001376 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001377 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001378 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1379 SourceLocation(/*FIXME*/),
1380 ArgNames[i], argType, originalArgType,
1381 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001382
Chris Lattner0ed844b2008-04-04 06:12:32 +00001383 Param->setObjCDeclQualifier(
1384 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1385 Params.push_back(Param);
1386 }
1387
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1389 ObjCMethod->setObjCDeclQualifier(
1390 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1391 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001392
1393 if (AttrList)
1394 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001395
1396 // For implementations (which can be very "coarse grain"), we add the
1397 // method now. This allows the AST to implement lookup methods that work
1398 // incrementally (without waiting until we parse the @end). It also allows
1399 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001401 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001402 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001403 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001404 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001405 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001406 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001407 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001408 }
1409 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001410 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001411 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001412 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001413 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001415 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001416 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001417 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001418 }
1419 }
1420 if (PrevMethod) {
1421 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001422 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001423 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001424 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001425 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001426 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001427}
1428
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001429void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1430 SourceLocation Loc,
1431 unsigned &Attributes) {
1432 // FIXME: Improve the reported location.
1433
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001434 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001435 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001436 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1437 ObjCDeclSpec::DQ_PR_assign |
1438 ObjCDeclSpec::DQ_PR_copy |
1439 ObjCDeclSpec::DQ_PR_retain))) {
1440 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1441 "readwrite" :
1442 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1443 "assign" :
1444 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1445 "copy" : "retain";
1446
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001447 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001448 diag::err_objc_property_attr_mutually_exclusive :
1449 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001450 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001451 }
1452
1453 // Check for copy or retain on non-object types.
1454 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1455 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001456 Diag(Loc, diag::err_objc_property_requires_object)
1457 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001458 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1459 }
1460
1461 // Check for more than one of { assign, copy, retain }.
1462 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1463 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001464 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1465 << "assign" << "copy";
1466 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001467 }
1468 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001469 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1470 << "assign" << "retain";
1471 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001472 }
1473 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1474 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001475 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1476 << "copy" << "retain";
1477 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001478 }
1479 }
1480
1481 // Warn if user supplied no assignment attribute, property is
1482 // readwrite, and this is an object type.
1483 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1484 ObjCDeclSpec::DQ_PR_retain)) &&
1485 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1486 Context.isObjCObjectPointerType(PropertyTy)) {
1487 // Skip this warning in gc-only mode.
1488 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1489 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1490
1491 // If non-gc code warn that this is likely inappropriate.
1492 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1493 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1494
1495 // FIXME: Implement warning dependent on NSCopying being
1496 // implemented. See also:
1497 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1498 // (please trim this list while you are at it).
1499 }
1500}
1501
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001502Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1503 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001504 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001505 Selector GetterSel,
1506 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001507 DeclTy *ClassCategory,
1508 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001509 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001510 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001511 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1512 // default is readwrite!
1513 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1514 // property is defaulted to 'assign' if it is readwrite and is
1515 // not retain or copy
1516 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1517 (isReadWrite &&
1518 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1519 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1520 QualType T = GetTypeForDeclarator(FD.D, S);
1521 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001522
1523 // May modify Attributes.
1524 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001525
1526 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1527 if (!CDecl->getIdentifier()) {
1528 // This is an anonymous category. property requires special
1529 // handling.
1530 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1531 if (ObjCPropertyDecl *PIDecl =
1532 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1533 // property 'PIDecl's readonly attribute will be over-ridden
1534 // with anonymous category's readwrite property attribute!
1535 unsigned PIkind = PIDecl->getPropertyAttributes();
1536 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001537 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001538 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1539 Diag(AtLoc, diag::warn_property_attr_mismatch);
1540 PIDecl->makeitReadWriteAttribute();
1541 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1542 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1543 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1544 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1545 PIDecl->setSetterName(SetterSel);
1546 // FIXME: use a common routine with addPropertyMethods.
1547 ObjCMethodDecl *SetterDecl =
1548 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1549 Context.VoidTy,
1550 ICDecl,
1551 true, false, true,
1552 ObjCMethodDecl::Required);
1553 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1554 SetterDecl,
1555 SourceLocation(),
1556 FD.D.getIdentifier(),
1557 T,
1558 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001559 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001560 SetterDecl->setMethodParams(&Argument, 1);
1561 PIDecl->setSetterMethodDecl(SetterDecl);
1562 }
1563 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001564 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001565 *isOverridingProperty = true;
1566 return 0;
1567 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001568 // No matching property found in the main class. Just fall thru
1569 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001570 // it works as is. This category becomes just like a category
1571 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001572 } else {
1573 Diag(CDecl->getLocation(), diag::err_continuation_class);
1574 *isOverridingProperty = true;
1575 return 0;
1576 }
1577 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001578
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001579 Type *t = T.getTypePtr();
1580 if (t->isArrayType() || t->isFunctionType())
1581 Diag(AtLoc, diag::err_property_type) << T;
1582
Steve Naroff93983f82009-01-11 12:47:58 +00001583 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1584 assert(DC && "ClassDecl is not a DeclContext");
1585 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001586 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001587 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001588
1589 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001590
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001591 // Regardless of setter/getter attribute, we save the default getter/setter
1592 // selector names in anticipation of declaration of setter/getter methods.
1593 PDecl->setGetterName(GetterSel);
1594 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001595
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001596 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001597 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001598
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001599 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001601
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001602 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001604
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001605 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001607
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001608 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001610
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001611 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001613
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001614 if (isAssign)
1615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1616
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001617 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001619
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001620 if (MethodImplKind == tok::objc_required)
1621 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1622 else if (MethodImplKind == tok::objc_optional)
1623 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1624
Chris Lattner4d391482007-12-12 07:09:47 +00001625 return PDecl;
1626}
1627
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001628/// ActOnPropertyImplDecl - This routine performs semantic checks and
1629/// builds the AST node for a property implementation declaration; declared
1630/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001631///
1632Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1633 SourceLocation PropertyLoc,
1634 bool Synthesize,
1635 DeclTy *ClassCatImpDecl,
1636 IdentifierInfo *PropertyId,
1637 IdentifierInfo *PropertyIvar) {
1638 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1639 // Make sure we have a context for the property implementation declaration.
1640 if (!ClassImpDecl) {
1641 Diag(AtLoc, diag::error_missing_property_context);
1642 return 0;
1643 }
1644 ObjCPropertyDecl *property = 0;
1645 ObjCInterfaceDecl* IDecl = 0;
1646 // Find the class or category class where this property must have
1647 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001648 ObjCImplementationDecl *IC = 0;
1649 ObjCCategoryImplDecl* CatImplClass = 0;
1650 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001651 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001652 // We always synthesize an interface for an implementation
1653 // without an interface decl. So, IDecl is always non-zero.
1654 assert(IDecl &&
1655 "ActOnPropertyImplDecl - @implementation without @interface");
1656
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001657 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001658 property = IDecl->FindPropertyDeclaration(PropertyId);
1659 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001660 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001661 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001662 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001663 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001664 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001665 if (Synthesize) {
1666 Diag(AtLoc, diag::error_synthesize_category_decl);
1667 return 0;
1668 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001669 IDecl = CatImplClass->getClassInterface();
1670 if (!IDecl) {
1671 Diag(AtLoc, diag::error_missing_property_interface);
1672 return 0;
1673 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001674 ObjCCategoryDecl *Category =
1675 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1676
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001677 // If category for this implementation not found, it is an error which
1678 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001679 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001680 return 0;
1681 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001682 property = Category->FindPropertyDeclaration(PropertyId);
1683 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001684 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001685 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001686 return 0;
1687 }
1688 }
1689 else {
1690 Diag(AtLoc, diag::error_bad_property_context);
1691 return 0;
1692 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001693 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001694 // Check that we have a valid, previously declared ivar for @synthesize
1695 if (Synthesize) {
1696 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001697 if (!PropertyIvar)
1698 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001699 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001700 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001701 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001702 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001703 return 0;
1704 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001705 QualType PropType = Context.getCanonicalType(property->getType());
1706 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1707
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001708 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001709 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001710 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001711 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001712 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001713 return 0;
1714 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001715 else {
1716 // FIXME! Rules for properties are somewhat different that those
1717 // for assignments. Use a new routine to consolidate all cases;
1718 // specifically for property redeclarations as well as for ivars.
1719 QualType lhsType =
1720 Context.getCanonicalType(PropType).getUnqualifiedType();
1721 QualType rhsType =
1722 Context.getCanonicalType(IvarType).getUnqualifiedType();
1723 if (lhsType != rhsType &&
1724 lhsType->isArithmeticType()) {
1725 Diag(PropertyLoc, diag::error_property_ivar_type)
1726 << property->getDeclName() << Ivar->getDeclName();
1727 return 0;
1728 }
1729 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001730 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001731 } else if (PropertyIvar) {
1732 // @dynamic
1733 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1734 return 0;
1735 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001736 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001737 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001738 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1739 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001740 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001741 ObjCPropertyImplDecl::Synthesize
1742 : ObjCPropertyImplDecl::Dynamic),
1743 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001744 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001745 if (IC) {
1746 if (Synthesize)
1747 if (ObjCPropertyImplDecl *PPIDecl =
1748 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1749 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1750 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1751 << PropertyIvar;
1752 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1753 }
1754
1755 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1756 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1757 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1758 return 0;
1759 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001760 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001761 }
1762 else {
1763 if (Synthesize)
1764 if (ObjCPropertyImplDecl *PPIDecl =
1765 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1766 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1767 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1768 << PropertyIvar;
1769 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1770 }
1771
1772 if (ObjCPropertyImplDecl *PPIDecl =
1773 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1774 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1775 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1776 return 0;
1777 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001778 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001779 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001780
1781 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001782}
Anders Carlsson15281452008-11-04 16:57:32 +00001783
Chris Lattnercc98eac2008-12-17 07:13:27 +00001784bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001785 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001786 return false;
1787
1788 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1789 D->setInvalidDecl();
1790
1791 return true;
1792}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001793
1794/// Collect the instance variables declared in an Objective-C object. Used in
1795/// the creation of structures from objects using the @defs directive.
1796/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1797/// part of the AST generation logic of @defs.
1798static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1799 ASTContext& Ctx,
1800 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1801 if (Class->getSuperClass())
1802 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1803
1804 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1805 for (ObjCInterfaceDecl::ivar_iterator
1806 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1807
1808 ObjCIvarDecl* ID = *I;
1809 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1810 ID->getLocation(),
1811 ID->getIdentifier(),
1812 ID->getType(),
1813 ID->getBitWidth()));
1814 }
1815}
1816
1817/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1818/// instance variables of ClassName into Decls.
1819void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1820 IdentifierInfo *ClassName,
1821 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1822 // Check that ClassName is a valid class
1823 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1824 if (!Class) {
1825 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1826 return;
1827 }
1828 // Collect the instance variables
1829 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1830
1831 // Introduce all of these fields into the appropriate scope.
1832 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1833 D != Decls.end(); ++D) {
1834 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1835 if (getLangOptions().CPlusPlus)
1836 PushOnScopeChains(cast<FieldDecl>(FD), S);
1837 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001838 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001839 }
1840}
1841