blob: c3519f8f010e3b4c90c1be68f636d644775f69e4 [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 +000018
19using namespace clang;
20
Ted Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000045
Daniel Dunbar451318c2008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000048
Chris Lattner8123a952008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000050 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000051 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000052 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000053 if (II)
54 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000055 }
56}
57
Chris Lattner7caeabd2008-07-21 22:17:28 +000058Sema::DeclTy *Sema::
59ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000062 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +000067 Decl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000069 // Maybe we will complain about the shadowed template parameter.
70 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
71 // Just pretend that we didn't see the previous declaration.
72 PrevDecl = 0;
73 }
74
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000078 }
79
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000084 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000085 Diag(IDecl->getLocation(), diag::note_previous_definition);
86
Steve Naroffcfe8bf32008-11-18 19:15:30 +000087 // Return the previous class interface.
88 // FIXME: don't leak the objects passed in!
89 return IDecl;
90 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000091 IDecl->setLocation(AtInterfaceLoc);
92 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000093 }
Chris Lattnerb752f282008-07-21 07:06:49 +000094 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000095 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000096 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000097 if (AttrList)
98 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000099
Steve Naroff31102512008-04-02 18:30:49 +0000100 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000101 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000102 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
104 TUScope->AddDecl(IDecl);
105 }
106
107 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000109 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000110 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000112 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000113 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000114 }
115 else {
116 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000117 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000118
119 if (!SuperClassEntry)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000120 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000121 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
122 else if (SuperClassEntry->isForwardDecl())
123 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner08631c52008-11-23 21:45:46 +0000124 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000125 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000126 }
127 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000128 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000129 IDecl->setLocEnd(SuperLoc);
130 } else { // we have a root class.
131 IDecl->setLocEnd(ClassLoc);
132 }
133
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000134 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000135 if (NumProtoRefs) {
136 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000137 IDecl->setLocEnd(EndProtoLoc);
138 }
Anders Carlsson15281452008-11-04 16:57:32 +0000139
140 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000141 return IDecl;
142}
143
144/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000145/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000146Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
147 IdentifierInfo *AliasName,
148 SourceLocation AliasLocation,
149 IdentifierInfo *ClassName,
150 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000151 // Look for previous declaration of alias name
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000152 Decl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000153 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000154 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000155 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000156 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000157 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000158 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000159 return 0;
160 }
161 // Check for class declaration
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000162 Decl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000163 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
164 QualType T = TDecl->getUnderlyingType();
165 if (T->isObjCInterfaceType()) {
166 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
167 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000168 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000169 }
170 }
171 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000172 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
173 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000174 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000175 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000176 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000177 return 0;
178 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000179
180 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000182 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000183
184 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000185
186 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000187 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000188 if (!CheckObjCDeclScope(AliasDecl))
189 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000190
Chris Lattner4d391482007-12-12 07:09:47 +0000191 return AliasDecl;
192}
193
Chris Lattnere13b9592008-07-26 04:03:38 +0000194Sema::DeclTy *
195Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
196 IdentifierInfo *ProtocolName,
197 SourceLocation ProtocolLoc,
198 DeclTy * const *ProtoRefs,
199 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000200 SourceLocation EndProtoLoc,
201 AttributeList *AttrList) {
202 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000203 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000205 if (PDecl) {
206 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000207 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000208 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000209 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000210 // Just return the protocol we already had.
211 // FIXME: don't leak the objects passed in!
212 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000213 }
Steve Narofff11b5082008-08-13 16:39:22 +0000214 // Make sure the cached decl gets a valid start location.
215 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000216 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000217 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000218 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
219 AtProtoInterfaceLoc,ProtocolName);
220 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000221 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000222 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000223 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000224 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000225 if (AttrList)
226 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000227 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000228 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000229 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000230 PDecl->setLocEnd(EndProtoLoc);
231 }
Anders Carlsson15281452008-11-04 16:57:32 +0000232
233 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000234 return PDecl;
235}
236
237/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000238/// issues an error if they are not declared. It returns list of
239/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000240void
Chris Lattnere13b9592008-07-26 04:03:38 +0000241Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000242 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000243 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000244 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000245 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000246 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
247 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000248 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000249 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000250 continue;
251 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000252 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000253 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000254 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
255 PDecl->getDeclName();
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000256 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000257 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
258 PDecl->getDeclName();
259 }
Chris Lattnereacc3922008-07-26 03:47:43 +0000260
261 // If this is a forward declaration and we are supposed to warn in this
262 // case, do it.
263 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000264 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000265 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000266 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000267 }
268}
269
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000270/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000271/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000272///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000273void
274Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
275 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000276 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000277 ObjCPropertyDecl::PropertyAttributeKind CAttr =
278 Property->getPropertyAttributes();
279 ObjCPropertyDecl::PropertyAttributeKind SAttr =
280 SuperProperty->getPropertyAttributes();
281 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
282 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000283 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000284 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000285 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
286 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000287 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000288 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
290 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000291 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000292 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000293
294 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
295 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000296 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000297 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000298 if (Property->getSetterName() != SuperProperty->getSetterName())
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() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000302 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000303 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304
Chris Lattner717250a2008-07-26 20:50:02 +0000305 if (Context.getCanonicalType(Property->getType()) !=
306 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000307 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000308 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000309
310}
311
312/// ComparePropertiesInBaseAndSuper - This routine compares property
313/// declarations in base and its super class, if any, and issues
314/// diagnostics in a variety of inconsistant situations.
315///
316void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000317Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000318 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
319 if (!SDecl)
320 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000321 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000322 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
323 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000324 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000325 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000326 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
327 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000328 ObjCPropertyDecl *PDecl = (*I);
329 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000330 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000331 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332 }
333 }
334}
335
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000336/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
337/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000338/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000339void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000340Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000341 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000342 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
343 if (!IDecl) {
344 // Category
345 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
346 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000347 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
348 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000349 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000350 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000351 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000352 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000353 CP != CE; ++CP)
354 if ((*CP)->getIdentifier() == Pr->getIdentifier())
355 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000356 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000357 // Property protocol already exist in class. Diagnose any mismatch.
358 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
359 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000360 return;
361 }
Steve Naroff09c47192009-01-09 15:36:25 +0000362 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
363 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000364 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000365 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000366 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000367 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000368 CP != CE; ++CP)
369 if ((*CP)->getIdentifier() == Pr->getIdentifier())
370 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000371 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000372 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000373 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000374 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000375}
376
377/// MergeProtocolPropertiesIntoClass - This routine merges properties
378/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000379/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380///
381
382void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000383Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000384 DeclTy *MergeItsProtocols) {
385 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000386 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
387
388 if (!IDecl) {
389 // Category
390 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
391 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
392 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
393 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
394 E = MDecl->protocol_end(); P != E; ++P)
395 // Merge properties of category (*P) into IDECL's
396 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
397
398 // Go thru the list of protocols for this category and recursively merge
399 // their properties into this class as well.
400 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
401 E = CatDecl->protocol_end(); P != E; ++P)
402 MergeProtocolPropertiesIntoClass(CatDecl, *P);
403 } else {
404 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
405 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
406 E = MD->protocol_end(); P != E; ++P)
407 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
408 }
409 return;
410 }
411
Chris Lattnerb752f282008-07-21 07:06:49 +0000412 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
414 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000416 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
417
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000418 // Go thru the list of protocols for this class and recursively merge
419 // their properties into this class as well.
420 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
421 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000422 MergeProtocolPropertiesIntoClass(IDecl, *P);
423 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000424 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
425 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
426 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000428 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429}
430
Chris Lattner4d391482007-12-12 07:09:47 +0000431/// ActOnForwardProtocolDeclaration -
432Action::DeclTy *
433Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000434 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000435 unsigned NumElts,
436 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000438
439 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000440 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000441 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000442 if (PDecl == 0) { // Not already seen?
443 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
444 IdentList[i].second, Ident);
445 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000446 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000447 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000448 if (attrList)
449 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000450 Protocols.push_back(PDecl);
451 }
Anders Carlsson15281452008-11-04 16:57:32 +0000452
453 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000454 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000455 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000456 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000457 CheckObjCDeclScope(PDecl);
458 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000459}
460
Chris Lattner7caeabd2008-07-21 22:17:28 +0000461Sema::DeclTy *Sema::
462ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
463 IdentifierInfo *ClassName, SourceLocation ClassLoc,
464 IdentifierInfo *CategoryName,
465 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000466 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000467 unsigned NumProtoRefs,
468 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000470
Chris Lattner61f9d412008-03-16 20:34:23 +0000471 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000472 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
473 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000474 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000475 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000476
477 /// Check that class of this category is already completely declared.
478 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000479 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000480 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000481 /// Check for duplicate interface declaration for this category
482 ObjCCategoryDecl *CDeclChain;
483 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
484 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000485 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000486 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000487 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000488 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000489 break;
490 }
Chris Lattner4d391482007-12-12 07:09:47 +0000491 }
Steve Naroffd100c802008-06-05 15:03:27 +0000492 if (!CDeclChain)
493 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000494 }
Chris Lattner4d391482007-12-12 07:09:47 +0000495
496 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000497 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
498 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000499 }
Anders Carlsson15281452008-11-04 16:57:32 +0000500
501 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000502 return CDecl;
503}
504
505/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000507/// object.
508Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
509 SourceLocation AtCatImplLoc,
510 IdentifierInfo *ClassName, SourceLocation ClassLoc,
511 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000512 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000513 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000514 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
515 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 /// Check that class of this category is already completely declared.
517 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000518 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000519
Douglas Gregord0434102009-01-09 00:49:46 +0000520 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000521 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000522
Chris Lattner4d391482007-12-12 07:09:47 +0000523 /// TODO: Check that CatName, category name, is not used in another
524 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000525 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000526
527 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000528 return CDecl;
529}
530
531Sema::DeclTy *Sema::ActOnStartClassImplementation(
532 SourceLocation AtClassImplLoc,
533 IdentifierInfo *ClassName, SourceLocation ClassLoc,
534 IdentifierInfo *SuperClassname,
535 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000536 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000537 // Check for another declaration kind with the same name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000538 Decl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000541 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000542 }
543 else {
544 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000545 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000546 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000547 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000548 }
549
550 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000551 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000552 if (SuperClassname) {
553 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000554 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
557 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000558 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000559 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000560 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000561 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 Diag(SuperClassLoc, diag::err_undef_superclass)
563 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000564 else if (IDecl && IDecl->getSuperClass() != SDecl) {
565 // This implementation and its interface do not have the same
566 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000568 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000569 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000570 }
571 }
572 }
573
574 if (!IDecl) {
575 // Legacy case of @implementation with no corresponding @interface.
576 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000577
578 // FIXME: Do we support attributes on the @implementation? If so
579 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000580 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
581 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000582 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000583 IDecl->setSuperClass(SDecl);
584 IDecl->setLocEnd(ClassLoc);
585
Douglas Gregord0434102009-01-09 00:49:46 +0000586 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000587 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 // Remember that this needs to be removed when the scope is popped.
589 TUScope->AddDecl(IDecl);
590 }
591
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000593 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000594 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000595
Douglas Gregord0434102009-01-09 00:49:46 +0000596 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000597 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000598
Anders Carlsson15281452008-11-04 16:57:32 +0000599 if (CheckObjCDeclScope(IMPDecl))
600 return IMPDecl;
601
Chris Lattner4d391482007-12-12 07:09:47 +0000602 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000604 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000605 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000606 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000608 return IMPDecl;
609}
610
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
612 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000613 SourceLocation RBrace) {
614 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000615 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000616 if (!IDecl)
617 return;
618 /// Check case of non-existing @interface decl.
619 /// (legacy objective-c @implementation decl without an @interface decl).
620 /// Add implementations's ivar to the synthesize class's ivar list.
621 if (IDecl->ImplicitInterfaceDecl()) {
622 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
623 return;
624 }
625 // If implementation has empty ivar list, just return.
626 if (numIvars == 0)
627 return;
628
629 assert(ivars && "missing @implementation ivars");
630
631 // Check interface's Ivar list against those in the implementation.
632 // names and types must match.
633 //
Chris Lattner4d391482007-12-12 07:09:47 +0000634 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000636 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
637 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638 ObjCIvarDecl* ImplIvar = ivars[j++];
639 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000640 assert (ImplIvar && "missing implementation ivar");
641 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000642 if (Context.getCanonicalType(ImplIvar->getType()) !=
643 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000644 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000645 << ImplIvar->getIdentifier()
646 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000647 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000648 }
649 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
650 // as error.
651 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000652 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000653 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000654 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000655 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000656 }
657 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000658 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000659
660 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000661 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000662 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000663 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000664}
665
Steve Naroff3c2eb662008-02-10 21:38:56 +0000666void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
667 bool &IncompleteImpl) {
668 if (!IncompleteImpl) {
669 Diag(ImpLoc, diag::warn_incomplete_impl);
670 IncompleteImpl = true;
671 }
Chris Lattner08631c52008-11-23 21:45:46 +0000672 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000673}
674
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000675void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
676 ObjCMethodDecl *IntfMethodDecl) {
677 bool err = false;
678 QualType ImpMethodQType =
679 Context.getCanonicalType(ImpMethodDecl->getResultType());
680 QualType IntfMethodQType =
681 Context.getCanonicalType(IntfMethodDecl->getResultType());
682 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
683 err = true;
684 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
685 IF=IntfMethodDecl->param_begin(),
686 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
687 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
688 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
689 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
690 err = true;
691 break;
692 }
693 }
694 if (err) {
695 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
696 << ImpMethodDecl->getDeclName();
697 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
698 }
699}
700
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000701/// isPropertyReadonly - Return true if property is readonly, by searching
702/// for the property in the class and in its categories and implementations
703///
704bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
705 ObjCInterfaceDecl *IDecl) const {
706 // by far the most common case.
707 if (!PDecl->isReadOnly())
708 return false;
709 // Even if property is ready only, if interface has a user defined setter,
710 // it is not considered read only.
711 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
712 return false;
713
714 // Main class has the property as 'readonly'. Must search
715 // through the category list to see if the property's
716 // attribute has been over-ridden to 'readwrite'.
717 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
718 Category; Category = Category->getNextClassCategory()) {
719 // Even if property is ready only, if a category has a user defined setter,
720 // it is not considered read only.
721 if (Category->getInstanceMethod(PDecl->getSetterName()))
722 return false;
723 ObjCPropertyDecl *P =
724 Category->FindPropertyDeclaration(PDecl->getIdentifier());
725 if (P && !P->isReadOnly())
726 return false;
727 }
728
729 // Also, check for definition of a setter method in the implementation if
730 // all else failed.
731 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
732 if (ObjCImplementationDecl *IMD =
733 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
734 if (IMD->getInstanceMethod(PDecl->getSetterName()))
735 return false;
736 }
737 else if (ObjCCategoryImplDecl *CIMD =
738 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
739 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
740 return false;
741 }
742 }
743 return true;
744}
745
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000746/// FIXME: Type hierarchies in Objective-C can be deep. We could most
747/// likely improve the efficiency of selector lookups and type
748/// checking by associating with each protocol / interface / category
749/// the flattened instance tables. If we used an immutable set to keep
750/// the table then it wouldn't add significant memory cost and it
751/// would be handy for lookups.
752
Steve Naroffefe7f362008-02-08 22:06:17 +0000753/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000754/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000755void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
756 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000757 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000758 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000759 const llvm::DenseSet<Selector> &ClsMap,
760 ObjCInterfaceDecl *IDecl) {
761 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
762
763 // If a method lookup fails locally we still need to look and see if
764 // the method was implemented by a base class or an inherited
765 // protocol. This lookup is slow, but occurs rarely in correct code
766 // and otherwise would terminate in a warning.
767
Chris Lattner4d391482007-12-12 07:09:47 +0000768 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000769 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000770 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000771 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000772 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000773 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000774 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000775 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000776 }
777 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000779 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000781 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
782 !ClsMap.count(method->getSelector()) &&
783 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000784 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000785 }
Chris Lattner780f3292008-07-21 21:32:27 +0000786 // Check on this protocols's referenced protocols, recursively.
787 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
788 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000789 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000790}
791
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
793 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000794 llvm::DenseSet<Selector> InsMap;
795 // Check and see if instance methods in class interface have been
796 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000797 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000798 E = IMPDecl->instmeth_end(); I != E; ++I)
799 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000800
801 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000803 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000804 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000805 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000806 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000807 ObjCMethodDecl *ImpMethodDecl =
808 IMPDecl->getInstanceMethod((*I)->getSelector());
809 ObjCMethodDecl *IntfMethodDecl =
810 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000811 assert(IntfMethodDecl &&
812 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
813 // ImpMethodDecl may be null as in a @dynamic property.
814 if (ImpMethodDecl)
815 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000816 }
Chris Lattner4c525092007-12-12 17:58:05 +0000817
Chris Lattner4d391482007-12-12 07:09:47 +0000818 llvm::DenseSet<Selector> ClsMap;
819 // Check and see if class methods in class interface have been
820 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000821 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000822 E = IMPDecl->classmeth_end(); I != E; ++I)
823 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000824
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000826 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000827 if (!ClsMap.count((*I)->getSelector()))
828 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000829 else {
830 ObjCMethodDecl *ImpMethodDecl =
831 IMPDecl->getClassMethod((*I)->getSelector());
832 ObjCMethodDecl *IntfMethodDecl =
833 IDecl->getClassMethod((*I)->getSelector());
834 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
835 }
836
Chris Lattner4d391482007-12-12 07:09:47 +0000837
838 // Check the protocol list for unimplemented methods in the @implementation
839 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000840 const ObjCList<ObjCProtocolDecl> &Protocols =
841 IDecl->getReferencedProtocols();
842 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
843 E = Protocols.end(); I != E; ++I)
844 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000845 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000846}
847
848/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000849/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
851 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000852 llvm::DenseSet<Selector> InsMap;
853 // Check and see if instance methods in category interface have been
854 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000855 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000856 E = CatImplDecl->instmeth_end(); I != E; ++I)
857 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000858
859 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000861 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000862 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000863 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000864 else {
865 ObjCMethodDecl *ImpMethodDecl =
866 CatImplDecl->getInstanceMethod((*I)->getSelector());
867 ObjCMethodDecl *IntfMethodDecl =
868 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000869 assert(IntfMethodDecl &&
870 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
871 // ImpMethodDecl may be null as in a @dynamic property.
872 if (ImpMethodDecl)
873 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000874 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000875
Chris Lattner4d391482007-12-12 07:09:47 +0000876 llvm::DenseSet<Selector> ClsMap;
877 // Check and see if class methods in category interface have been
878 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000880 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
881 I != E; ++I)
882 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000883
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000884 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000885 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000886 if (!ClsMap.count((*I)->getSelector()))
887 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000888 else {
889 ObjCMethodDecl *ImpMethodDecl =
890 CatImplDecl->getClassMethod((*I)->getSelector());
891 ObjCMethodDecl *IntfMethodDecl =
892 CatClassDecl->getClassMethod((*I)->getSelector());
893 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
894 }
Chris Lattner4d391482007-12-12 07:09:47 +0000895 // Check the protocol list for unimplemented methods in the @implementation
896 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000897 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
898 E = CatClassDecl->protocol_end(); PI != E; ++PI)
899 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000900 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000901}
902
903/// ActOnForwardClassDeclaration -
904Action::DeclTy *
905Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
906 IdentifierInfo **IdentList, unsigned NumElts)
907{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000909
910 for (unsigned i = 0; i != NumElts; ++i) {
911 // Check for another declaration kind with the same name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000912 Decl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000913 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000914 // Maybe we will complain about the shadowed template parameter.
915 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
916 // Just pretend that we didn't see the previous declaration.
917 PrevDecl = 0;
918 }
919
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000921 // GCC apparently allows the following idiom:
922 //
923 // typedef NSObject < XCElementTogglerP > XCElementToggler;
924 // @class XCElementToggler;
925 //
926 // FIXME: Make an extension?
927 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
928 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000929 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000930 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000931 }
Chris Lattner4d391482007-12-12 07:09:47 +0000932 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000934 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000935 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
936 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000937 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000938
Douglas Gregord0434102009-01-09 00:49:46 +0000939 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000940 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000941 // Remember that this needs to be removed when the scope is popped.
942 TUScope->AddDecl(IDecl);
943 }
944
945 Interfaces.push_back(IDecl);
946 }
947
Douglas Gregord0434102009-01-09 00:49:46 +0000948 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000949 &Interfaces[0],
950 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000951 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000952 CheckObjCDeclScope(CDecl);
953 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000954}
955
956
957/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
958/// returns true, or false, accordingly.
959/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000961 const ObjCMethodDecl *PrevMethod,
962 bool matchBasedOnSizeAndAlignment) {
963 QualType T1 = Context.getCanonicalType(Method->getResultType());
964 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
965
966 if (T1 != T2) {
967 // The result types are different.
968 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000969 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000970 // Incomplete types don't have a size and alignment.
971 if (T1->isIncompleteType() || T2->isIncompleteType())
972 return false;
973 // Check is based on size and alignment.
974 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
975 return false;
976 }
977 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
978 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
979 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
980 if (T1 != T2) {
981 // The result types are different.
982 if (!matchBasedOnSizeAndAlignment)
983 return false;
984 // Incomplete types don't have a size and alignment.
985 if (T1->isIncompleteType() || T2->isIncompleteType())
986 return false;
987 // Check is based on size and alignment.
988 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
989 return false;
990 }
Chris Lattner4d391482007-12-12 07:09:47 +0000991 }
992 return true;
993}
994
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000995void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
996 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000997 if (!FirstMethod.Method) {
998 // Haven't seen a method with this selector name yet - add it.
999 FirstMethod.Method = Method;
1000 FirstMethod.Next = 0;
1001 } else {
1002 // We've seen a method with this name, now check the type signature(s).
1003 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1004
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001006 Next = Next->Next)
1007 match = MatchTwoMethodDeclarations(Method, Next->Method);
1008
1009 if (!match) {
1010 // We have a new signature for an existing method - add it.
1011 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001012 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001013 }
1014 }
1015}
1016
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001017// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001018ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1019 SourceRange R) {
1020 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001021 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001022
1023 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001024 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1025 // This checks if the methods differ by size & alignment.
1026 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1027 issueWarning = true;
1028 }
1029 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001030 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001031 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001032 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001033 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001034 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001035 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001036 }
1037 return MethList.Method;
1038}
1039
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1041 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001042 if (!FirstMethod.Method) {
1043 // Haven't seen a method with this selector name yet - add it.
1044 FirstMethod.Method = Method;
1045 FirstMethod.Next = 0;
1046 } else {
1047 // We've seen a method with this name, now check the type signature(s).
1048 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1049
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001051 Next = Next->Next)
1052 match = MatchTwoMethodDeclarations(Method, Next->Method);
1053
1054 if (!match) {
1055 // We have a new signature for an existing method - add it.
1056 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001058 FirstMethod.Next = OMI;
1059 }
1060 }
1061}
1062
Steve Naroff0701bbb2009-01-08 17:28:14 +00001063/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1064/// have the property type and issue diagnostics if they don't.
1065/// Also synthesize a getter/setter method if none exist (and update the
1066/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1067/// methods is the "right" thing to do.
1068void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1069 ObjCContainerDecl *CD) {
1070 ObjCMethodDecl *GetterMethod, *SetterMethod;
1071
1072 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1073 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1074
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001075 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001076 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001077 Diag(property->getLocation(),
1078 diag::err_accessor_property_type_mismatch)
1079 << property->getDeclName()
1080 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001081 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1082 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001083
1084 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001085 if (Context.getCanonicalType(SetterMethod->getResultType())
1086 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001087 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1088 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001089 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001090 Diag(property->getLocation(),
1091 diag::err_accessor_property_type_mismatch)
1092 << property->getDeclName()
1093 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001094 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1095 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001096 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001097
1098 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001099 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001100 // FIXME: The synthesized property we set here is misleading. We
1101 // almost always synthesize these methods unless the user explicitly
1102 // provided prototypes (which is odd, but allowed). Sema should be
1103 // typechecking that the declarations jive in that situation (which
1104 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001105 if (!GetterMethod) {
1106 // No instance method of same name as property getter name was found.
1107 // Declare a getter method and add it to the list of methods
1108 // for this class.
1109 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1110 property->getLocation(), property->getGetterName(),
1111 property->getType(), CD, true, false, true,
1112 (property->getPropertyImplementation() ==
1113 ObjCPropertyDecl::Optional) ?
1114 ObjCMethodDecl::Optional :
1115 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001116 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001117 } else
1118 // A user declared getter will be synthesize when @synthesize of
1119 // the property with the same name is seen in the @implementation
1120 GetterMethod->setIsSynthesized();
1121 property->setGetterMethodDecl(GetterMethod);
1122
1123 // Skip setter if property is read-only.
1124 if (!property->isReadOnly()) {
1125 // Find the default setter and if one not found, add one.
1126 if (!SetterMethod) {
1127 // No instance method of same name as property setter name was found.
1128 // Declare a setter method and add it to the list of methods
1129 // for this class.
1130 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1131 property->getLocation(),
1132 property->getSetterName(),
1133 Context.VoidTy, CD, true, false, true,
1134 (property->getPropertyImplementation() ==
1135 ObjCPropertyDecl::Optional) ?
1136 ObjCMethodDecl::Optional :
1137 ObjCMethodDecl::Required);
1138 // Invent the arguments for the setter. We don't bother making a
1139 // nice name for the argument.
1140 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1141 SourceLocation(),
1142 property->getIdentifier(),
1143 property->getType(),
1144 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001145 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001146 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001147 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001148 } else
1149 // A user declared setter will be synthesize when @synthesize of
1150 // the property with the same name is seen in the @implementation
1151 SetterMethod->setIsSynthesized();
1152 property->setSetterMethodDecl(SetterMethod);
1153 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001154 // Add any synthesized methods to the global pool. This allows us to
1155 // handle the following, which is supported by GCC (and part of the design).
1156 //
1157 // @interface Foo
1158 // @property double bar;
1159 // @end
1160 //
1161 // void thisIsUnfortunate() {
1162 // id foo;
1163 // double bar = [foo bar];
1164 // }
1165 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001166 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001167 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001168 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001169 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001170}
1171
Steve Naroffa56f6162007-12-18 01:30:32 +00001172// Note: For class/category implemenations, allMethods/allProperties is
1173// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001174void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1175 DeclTy **allMethods, unsigned allNum,
1176 DeclTy **allProperties, unsigned pNum) {
1177 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1178
Steve Naroffa56f6162007-12-18 01:30:32 +00001179 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1180 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001181 // should be true.
1182 if (!ClassDecl)
1183 return;
1184
Chris Lattner4d391482007-12-12 07:09:47 +00001185 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001186 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1187 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001188 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001189
Steve Naroff0701bbb2009-01-08 17:28:14 +00001190 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001191
1192 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1193 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1194 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1195
Chris Lattner4d391482007-12-12 07:09:47 +00001196 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001197 ObjCMethodDecl *Method =
1198 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001199
1200 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001201 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001202 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001203 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001204 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1205 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001206 if ((isInterfaceDeclKind && PrevMethod && !match)
1207 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001208 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001209 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001210 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001211 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001212 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001213 InsMap[Method->getSelector()] = Method;
1214 /// The following allows us to typecheck messages to "id".
1215 AddInstanceMethodToGlobalPool(Method);
1216 }
1217 }
1218 else {
1219 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001220 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001221 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1222 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001223 if ((isInterfaceDeclKind && PrevMethod && !match)
1224 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001225 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001226 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001227 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001228 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001229 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001230 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001231 /// The following allows us to typecheck messages to "Class".
1232 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001233 }
1234 }
1235 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001236 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001237 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001238 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001239 ComparePropertiesInBaseAndSuper(I);
1240 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001241 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001242 // Categories are used to extend the class by declaring new methods.
1243 // By the same token, they are also used to add new properties. No
1244 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001245
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001246 // Merge protocol properties into category
1247 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001248 }
Steve Naroff09c47192009-01-09 15:36:25 +00001249 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1250 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1251 // user-defined setter/getter. It also synthesizes setter/getter methods
1252 // and adds them to the DeclContext and global method pools.
1253 for (ObjCContainerDecl::prop_iterator i = CDecl->prop_begin(),
1254 e = CDecl->prop_end(); i != e; ++i)
1255 ProcessPropertyDecl((*i), CDecl);
1256 CDecl->setAtEndLoc(AtEndLoc);
1257 }
1258 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001259 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001260 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001261 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001262 } else if (ObjCCategoryImplDecl* CatImplClass =
1263 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001264 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001266 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001267 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001268 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001269 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001270 Categories; Categories = Categories->getNextClassCategory()) {
1271 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1272 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1273 break;
1274 }
1275 }
1276 }
1277 }
1278}
1279
1280
1281/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1282/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283static Decl::ObjCDeclQualifier
1284CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1285 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1286 if (PQTVal & ObjCDeclSpec::DQ_In)
1287 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1288 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1289 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1290 if (PQTVal & ObjCDeclSpec::DQ_Out)
1291 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1292 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1293 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1294 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1295 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1296 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1297 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001298
1299 return ret;
1300}
1301
1302Sema::DeclTy *Sema::ActOnMethodDeclaration(
1303 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001304 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001305 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001306 Selector Sel,
1307 // optional arguments. The number of types/arguments is obtained
1308 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001309 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001310 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001311 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1312 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001313 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001314
1315 // Make sure we can establish a context for the method.
1316 if (!ClassDecl) {
1317 Diag(MethodLoc, diag::error_missing_method_context);
1318 return 0;
1319 }
Chris Lattner4d391482007-12-12 07:09:47 +00001320 QualType resultDeclType;
1321
1322 if (ReturnType)
1323 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1324 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001325 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001326
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001327 ObjCMethodDecl* ObjCMethod =
1328 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001329 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001330 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001331 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001332 MethodDeclKind == tok::objc_optional ?
1333 ObjCMethodDecl::Optional :
1334 ObjCMethodDecl::Required);
1335
Chris Lattner0ed844b2008-04-04 06:12:32 +00001336 llvm::SmallVector<ParmVarDecl*, 16> Params;
1337
1338 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1339 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001340 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001341
Steve Naroff6082c622008-12-09 19:36:17 +00001342 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001343 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001344 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001345 if (argType->isArrayType()) { // (char *[]) -> (char **)
1346 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001347 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001348 }
Steve Naroff6082c622008-12-09 19:36:17 +00001349 else if (argType->isFunctionType())
1350 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001351 else if (argType->isObjCInterfaceType()) {
1352 // FIXME! provide more precise location for the parameter
1353 Diag(MethodLoc, diag::err_object_as_method_param);
1354 return 0;
1355 }
Steve Naroff6082c622008-12-09 19:36:17 +00001356 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001357 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001358 ParmVarDecl* Param;
1359 if (originalArgType.isNull())
1360 Param = ParmVarDecl::Create(Context, ObjCMethod,
1361 SourceLocation(/*FIXME*/),
1362 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001363 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001364 else
1365 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1366 SourceLocation(/*FIXME*/),
1367 ArgNames[i], argType, originalArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001368 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001369
Chris Lattner0ed844b2008-04-04 06:12:32 +00001370 Param->setObjCDeclQualifier(
1371 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1372 Params.push_back(Param);
1373 }
1374
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1376 ObjCMethod->setObjCDeclQualifier(
1377 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1378 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001379
1380 if (AttrList)
1381 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001382
1383 // For implementations (which can be very "coarse grain"), we add the
1384 // method now. This allows the AST to implement lookup methods that work
1385 // incrementally (without waiting until we parse the @end). It also allows
1386 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001388 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001389 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001390 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001391 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001392 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001393 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001394 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001395 }
1396 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001398 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001399 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001400 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001402 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001403 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001404 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001405 }
1406 }
1407 if (PrevMethod) {
1408 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001409 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001410 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001411 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001412 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001413 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001414}
1415
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001416void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1417 SourceLocation Loc,
1418 unsigned &Attributes) {
1419 // FIXME: Improve the reported location.
1420
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001421 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001422 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001423 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1424 ObjCDeclSpec::DQ_PR_assign |
1425 ObjCDeclSpec::DQ_PR_copy |
1426 ObjCDeclSpec::DQ_PR_retain))) {
1427 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1428 "readwrite" :
1429 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1430 "assign" :
1431 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1432 "copy" : "retain";
1433
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001434 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001435 diag::err_objc_property_attr_mutually_exclusive :
1436 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001437 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001438 }
1439
1440 // Check for copy or retain on non-object types.
1441 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1442 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001443 Diag(Loc, diag::err_objc_property_requires_object)
1444 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001445 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1446 }
1447
1448 // Check for more than one of { assign, copy, retain }.
1449 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1450 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001451 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1452 << "assign" << "copy";
1453 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001454 }
1455 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001456 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1457 << "assign" << "retain";
1458 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001459 }
1460 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1461 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001462 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1463 << "copy" << "retain";
1464 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001465 }
1466 }
1467
1468 // Warn if user supplied no assignment attribute, property is
1469 // readwrite, and this is an object type.
1470 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1471 ObjCDeclSpec::DQ_PR_retain)) &&
1472 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1473 Context.isObjCObjectPointerType(PropertyTy)) {
1474 // Skip this warning in gc-only mode.
1475 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1476 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1477
1478 // If non-gc code warn that this is likely inappropriate.
1479 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1480 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1481
1482 // FIXME: Implement warning dependent on NSCopying being
1483 // implemented. See also:
1484 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1485 // (please trim this list while you are at it).
1486 }
1487}
1488
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001489Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1490 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001491 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001492 Selector GetterSel,
1493 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001494 DeclTy *ClassCategory,
1495 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001496 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001497 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001498 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1499 // default is readwrite!
1500 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1501 // property is defaulted to 'assign' if it is readwrite and is
1502 // not retain or copy
1503 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1504 (isReadWrite &&
1505 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1506 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1507 QualType T = GetTypeForDeclarator(FD.D, S);
1508 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001509
1510 // May modify Attributes.
1511 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001512
1513 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1514 if (!CDecl->getIdentifier()) {
1515 // This is an anonymous category. property requires special
1516 // handling.
1517 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1518 if (ObjCPropertyDecl *PIDecl =
1519 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1520 // property 'PIDecl's readonly attribute will be over-ridden
1521 // with anonymous category's readwrite property attribute!
1522 unsigned PIkind = PIDecl->getPropertyAttributes();
1523 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001524 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001525 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1526 Diag(AtLoc, diag::warn_property_attr_mismatch);
1527 PIDecl->makeitReadWriteAttribute();
1528 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1529 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1530 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1531 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1532 PIDecl->setSetterName(SetterSel);
1533 // FIXME: use a common routine with addPropertyMethods.
1534 ObjCMethodDecl *SetterDecl =
1535 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1536 Context.VoidTy,
1537 ICDecl,
1538 true, false, true,
1539 ObjCMethodDecl::Required);
1540 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1541 SetterDecl,
1542 SourceLocation(),
1543 FD.D.getIdentifier(),
1544 T,
1545 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001546 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001547 SetterDecl->setMethodParams(&Argument, 1);
1548 PIDecl->setSetterMethodDecl(SetterDecl);
1549 }
1550 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001551 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001552 *isOverridingProperty = true;
1553 return 0;
1554 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001555 // No matching property found in the main class. Just fall thru
1556 // and add property to the anonymous category. It looks like
1557 // it works as is. This category becomes just like a category
1558 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001559 } else {
1560 Diag(CDecl->getLocation(), diag::err_continuation_class);
1561 *isOverridingProperty = true;
1562 return 0;
1563 }
1564 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001565
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001566 Type *t = T.getTypePtr();
1567 if (t->isArrayType() || t->isFunctionType())
1568 Diag(AtLoc, diag::err_property_type) << T;
1569
Steve Naroff93983f82009-01-11 12:47:58 +00001570 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1571 assert(DC && "ClassDecl is not a DeclContext");
1572 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001573 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001574 DC->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +00001575
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001576 // Regardless of setter/getter attribute, we save the default getter/setter
1577 // selector names in anticipation of declaration of setter/getter methods.
1578 PDecl->setGetterName(GetterSel);
1579 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001580
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001581 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001582 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001583
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001584 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001585 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001586
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001587 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001588 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001589
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001590 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001591 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001592
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001593 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001595
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001596 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001597 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001598
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001599 if (isAssign)
1600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1601
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001602 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001604
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001605 if (MethodImplKind == tok::objc_required)
1606 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1607 else if (MethodImplKind == tok::objc_optional)
1608 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1609
Chris Lattner4d391482007-12-12 07:09:47 +00001610 return PDecl;
1611}
1612
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001613/// ActOnPropertyImplDecl - This routine performs semantic checks and
1614/// builds the AST node for a property implementation declaration; declared
1615/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001616///
1617Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1618 SourceLocation PropertyLoc,
1619 bool Synthesize,
1620 DeclTy *ClassCatImpDecl,
1621 IdentifierInfo *PropertyId,
1622 IdentifierInfo *PropertyIvar) {
1623 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1624 // Make sure we have a context for the property implementation declaration.
1625 if (!ClassImpDecl) {
1626 Diag(AtLoc, diag::error_missing_property_context);
1627 return 0;
1628 }
1629 ObjCPropertyDecl *property = 0;
1630 ObjCInterfaceDecl* IDecl = 0;
1631 // Find the class or category class where this property must have
1632 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001633 ObjCImplementationDecl *IC = 0;
1634 ObjCCategoryImplDecl* CatImplClass = 0;
1635 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001636 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001637 // We always synthesize an interface for an implementation
1638 // without an interface decl. So, IDecl is always non-zero.
1639 assert(IDecl &&
1640 "ActOnPropertyImplDecl - @implementation without @interface");
1641
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001642 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001643 property = IDecl->FindPropertyDeclaration(PropertyId);
1644 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001645 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001646 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001647 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001648 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001649 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001650 if (Synthesize) {
1651 Diag(AtLoc, diag::error_synthesize_category_decl);
1652 return 0;
1653 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001654 IDecl = CatImplClass->getClassInterface();
1655 if (!IDecl) {
1656 Diag(AtLoc, diag::error_missing_property_interface);
1657 return 0;
1658 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001659 ObjCCategoryDecl *Category =
1660 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1661
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001662 // If category for this implementation not found, it is an error which
1663 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001664 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001665 return 0;
1666 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001667 property = Category->FindPropertyDeclaration(PropertyId);
1668 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001669 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001670 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001671 return 0;
1672 }
1673 }
1674 else {
1675 Diag(AtLoc, diag::error_bad_property_context);
1676 return 0;
1677 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001678 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001679 // Check that we have a valid, previously declared ivar for @synthesize
1680 if (Synthesize) {
1681 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001682 if (!PropertyIvar)
1683 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001684 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001685 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001686 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001687 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001688 return 0;
1689 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001690 QualType PropType = Context.getCanonicalType(property->getType());
1691 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1692
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001693 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001694 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001695 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001696 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001697 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001698 return 0;
1699 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001700 else {
1701 // FIXME! Rules for properties are somewhat different that those
1702 // for assignments. Use a new routine to consolidate all cases;
1703 // specifically for property redeclarations as well as for ivars.
1704 QualType lhsType =
1705 Context.getCanonicalType(PropType).getUnqualifiedType();
1706 QualType rhsType =
1707 Context.getCanonicalType(IvarType).getUnqualifiedType();
1708 if (lhsType != rhsType &&
1709 lhsType->isArithmeticType()) {
1710 Diag(PropertyLoc, diag::error_property_ivar_type)
1711 << property->getDeclName() << Ivar->getDeclName();
1712 return 0;
1713 }
1714 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001715 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001716 } else if (PropertyIvar) {
1717 // @dynamic
1718 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1719 return 0;
1720 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001721 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001722 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001723 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1724 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001725 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001726 ObjCPropertyImplDecl::Synthesize
1727 : ObjCPropertyImplDecl::Dynamic),
1728 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001729 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001730 if (IC) {
1731 if (Synthesize)
1732 if (ObjCPropertyImplDecl *PPIDecl =
1733 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1734 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1735 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1736 << PropertyIvar;
1737 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1738 }
1739
1740 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1741 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1742 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1743 return 0;
1744 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001745 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001746 }
1747 else {
1748 if (Synthesize)
1749 if (ObjCPropertyImplDecl *PPIDecl =
1750 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1751 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1752 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1753 << PropertyIvar;
1754 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1755 }
1756
1757 if (ObjCPropertyImplDecl *PPIDecl =
1758 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1759 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1760 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1761 return 0;
1762 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001763 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001764 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001765
1766 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001767}
Anders Carlsson15281452008-11-04 16:57:32 +00001768
Chris Lattnercc98eac2008-12-17 07:13:27 +00001769bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001770 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001771 return false;
1772
1773 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1774 D->setInvalidDecl();
1775
1776 return true;
1777}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001778
1779/// Collect the instance variables declared in an Objective-C object. Used in
1780/// the creation of structures from objects using the @defs directive.
1781/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1782/// part of the AST generation logic of @defs.
1783static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1784 ASTContext& Ctx,
1785 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1786 if (Class->getSuperClass())
1787 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1788
1789 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1790 for (ObjCInterfaceDecl::ivar_iterator
1791 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1792
1793 ObjCIvarDecl* ID = *I;
1794 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1795 ID->getLocation(),
1796 ID->getIdentifier(),
1797 ID->getType(),
1798 ID->getBitWidth()));
1799 }
1800}
1801
1802/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1803/// instance variables of ClassName into Decls.
1804void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1805 IdentifierInfo *ClassName,
1806 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1807 // Check that ClassName is a valid class
1808 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1809 if (!Class) {
1810 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1811 return;
1812 }
1813 // Collect the instance variables
1814 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1815
1816 // Introduce all of these fields into the appropriate scope.
1817 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1818 D != Decls.end(); ++D) {
1819 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1820 if (getLangOptions().CPlusPlus)
1821 PushOnScopeChains(cast<FieldDecl>(FD), S);
1822 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001823 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001824 }
1825}
1826