blob: 1e87a6f8195b7e06868a711b54eecd4fff5e6a49 [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 Gregor47b9a1c2009-02-04 17:27:36 +000067 NamedDecl *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) {
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
Steve Naroff818cb9e2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
113 if (PrevDecl && SuperClassDecl == 0) {
114 // The previous declaration was not a class decl. Check if we have a
115 // typedef. If we do, get the underlying class type.
116 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
117 QualType T = TDecl->getUnderlyingType();
118 if (T->isObjCInterfaceType()) {
119 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
120 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
121 }
122 }
123 // This handles the following case:
124 //
125 // typedef int SuperClass;
126 // @interface MyClass : SuperClass {} @end
127 //
128 if (!SuperClassDecl) {
129 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
130 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
131 }
132 }
133 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
134 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000135 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000136 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000137 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000138 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000139 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000140 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000141 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000142 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000143 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000144 IDecl->setLocEnd(SuperLoc);
145 } else { // we have a root class.
146 IDecl->setLocEnd(ClassLoc);
147 }
148
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000149 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000150 if (NumProtoRefs) {
151 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 IDecl->setLocEnd(EndProtoLoc);
153 }
Anders Carlsson15281452008-11-04 16:57:32 +0000154
155 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000156 return IDecl;
157}
158
159/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000160/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000161Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
162 IdentifierInfo *AliasName,
163 SourceLocation AliasLocation,
164 IdentifierInfo *ClassName,
165 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000166 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000167 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000168 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000169 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000170 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000171 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000172 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000173 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000174 return 0;
175 }
176 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000177 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000178 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
179 QualType T = TDecl->getUnderlyingType();
180 if (T->isObjCInterfaceType()) {
181 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
182 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000183 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000184 }
185 }
186 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000187 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
188 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000189 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000190 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000191 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000192 return 0;
193 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000194
195 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000197 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000198
199 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000200
201 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000202 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000203 if (!CheckObjCDeclScope(AliasDecl))
204 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000205
Chris Lattner4d391482007-12-12 07:09:47 +0000206 return AliasDecl;
207}
208
Chris Lattnere13b9592008-07-26 04:03:38 +0000209Sema::DeclTy *
210Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
211 IdentifierInfo *ProtocolName,
212 SourceLocation ProtocolLoc,
213 DeclTy * const *ProtoRefs,
214 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000215 SourceLocation EndProtoLoc,
216 AttributeList *AttrList) {
217 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000218 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000220 if (PDecl) {
221 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000222 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000223 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000224 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000225 // Just return the protocol we already had.
226 // FIXME: don't leak the objects passed in!
227 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000228 }
Steve Narofff11b5082008-08-13 16:39:22 +0000229 // Make sure the cached decl gets a valid start location.
230 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000231 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000232 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000233 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
234 AtProtoInterfaceLoc,ProtocolName);
235 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000236 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000237 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000238 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000239 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000240 if (AttrList)
241 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000242 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000243 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000244 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000245 PDecl->setLocEnd(EndProtoLoc);
246 }
Anders Carlsson15281452008-11-04 16:57:32 +0000247
248 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 return PDecl;
250}
251
252/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000253/// issues an error if they are not declared. It returns list of
254/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000255void
Chris Lattnere13b9592008-07-26 04:03:38 +0000256Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000257 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000258 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000259 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000260 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000261 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
262 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000264 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000265 continue;
266 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000267 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000268 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000269 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
270 PDecl->getDeclName();
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000271 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000272 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
273 PDecl->getDeclName();
274 }
Chris Lattnereacc3922008-07-26 03:47:43 +0000275
276 // If this is a forward declaration and we are supposed to warn in this
277 // case, do it.
278 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000279 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000280 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000281 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000282 }
283}
284
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000285/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000286/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000287///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000288void
289Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
290 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000291 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000292 ObjCPropertyDecl::PropertyAttributeKind CAttr =
293 Property->getPropertyAttributes();
294 ObjCPropertyDecl::PropertyAttributeKind SAttr =
295 SuperProperty->getPropertyAttributes();
296 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
297 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000298 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000299 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000300 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
301 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
305 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000306 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000307 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000308
309 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
310 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000311 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000312 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000313 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000317 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319
Chris Lattner717250a2008-07-26 20:50:02 +0000320 if (Context.getCanonicalType(Property->getType()) !=
321 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000322 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000323 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000324
325}
326
327/// ComparePropertiesInBaseAndSuper - This routine compares property
328/// declarations in base and its super class, if any, and issues
329/// diagnostics in a variety of inconsistant situations.
330///
331void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000332Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000333 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
334 if (!SDecl)
335 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000336 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000337 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
338 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000339 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000341 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
342 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 ObjCPropertyDecl *PDecl = (*I);
344 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000345 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000346 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000347 }
348 }
349}
350
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000351/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
352/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000353/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000354void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000355Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000356 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000357 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
358 if (!IDecl) {
359 // Category
360 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
361 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
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 Jahanian1ac2bc42008-12-06 23:03:39 +0000364 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000365 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000367 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368 CP != CE; ++CP)
369 if ((*CP)->getIdentifier() == Pr->getIdentifier())
370 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000371 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000372 // Property protocol already exist in class. Diagnose any mismatch.
373 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
374 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000375 return;
376 }
Steve Naroff09c47192009-01-09 15:36:25 +0000377 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
378 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000379 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000380 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000382 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383 CP != CE; ++CP)
384 if ((*CP)->getIdentifier() == Pr->getIdentifier())
385 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000386 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000388 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000390}
391
392/// MergeProtocolPropertiesIntoClass - This routine merges properties
393/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000394/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000395///
396
397void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000399 DeclTy *MergeItsProtocols) {
400 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000401 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
402
403 if (!IDecl) {
404 // Category
405 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
406 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
407 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
408 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
409 E = MDecl->protocol_end(); P != E; ++P)
410 // Merge properties of category (*P) into IDECL's
411 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
412
413 // Go thru the list of protocols for this category and recursively merge
414 // their properties into this class as well.
415 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
416 E = CatDecl->protocol_end(); P != E; ++P)
417 MergeProtocolPropertiesIntoClass(CatDecl, *P);
418 } else {
419 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
420 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
421 E = MD->protocol_end(); P != E; ++P)
422 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
423 }
424 return;
425 }
426
Chris Lattnerb752f282008-07-21 07:06:49 +0000427 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
429 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000431 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
432
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000433 // Go thru the list of protocols for this class and recursively merge
434 // their properties into this class as well.
435 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
436 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000437 MergeProtocolPropertiesIntoClass(IDecl, *P);
438 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000439 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
440 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
441 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000443 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000444}
445
Chris Lattner4d391482007-12-12 07:09:47 +0000446/// ActOnForwardProtocolDeclaration -
447Action::DeclTy *
448Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000449 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000450 unsigned NumElts,
451 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000452 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000453
454 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000455 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000456 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000457 if (PDecl == 0) { // Not already seen?
458 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
459 IdentList[i].second, Ident);
460 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000461 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000462 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000463 if (attrList)
464 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000465 Protocols.push_back(PDecl);
466 }
Anders Carlsson15281452008-11-04 16:57:32 +0000467
468 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000469 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000470 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000471 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000472 CheckObjCDeclScope(PDecl);
473 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000474}
475
Chris Lattner7caeabd2008-07-21 22:17:28 +0000476Sema::DeclTy *Sema::
477ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
478 IdentifierInfo *ClassName, SourceLocation ClassLoc,
479 IdentifierInfo *CategoryName,
480 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000481 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000482 unsigned NumProtoRefs,
483 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000484 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000485
Chris Lattner61f9d412008-03-16 20:34:23 +0000486 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000487 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
488 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000489 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000490 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000491
492 /// Check that class of this category is already completely declared.
493 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000494 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000495 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000496 /// Check for duplicate interface declaration for this category
497 ObjCCategoryDecl *CDeclChain;
498 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
499 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000500 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000501 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000502 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000503 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000504 break;
505 }
Chris Lattner4d391482007-12-12 07:09:47 +0000506 }
Steve Naroffd100c802008-06-05 15:03:27 +0000507 if (!CDeclChain)
508 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000509 }
Chris Lattner4d391482007-12-12 07:09:47 +0000510
511 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000512 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
513 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000514 }
Anders Carlsson15281452008-11-04 16:57:32 +0000515
516 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 return CDecl;
518}
519
520/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000521/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000522/// object.
523Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
524 SourceLocation AtCatImplLoc,
525 IdentifierInfo *ClassName, SourceLocation ClassLoc,
526 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000528 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000529 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
530 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000531 /// Check that class of this category is already completely declared.
532 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000533 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000534
Douglas Gregord0434102009-01-09 00:49:46 +0000535 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000536 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000537
Chris Lattner4d391482007-12-12 07:09:47 +0000538 /// TODO: Check that CatName, category name, is not used in another
539 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000540 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000541
542 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000543 return CDecl;
544}
545
546Sema::DeclTy *Sema::ActOnStartClassImplementation(
547 SourceLocation AtClassImplLoc,
548 IdentifierInfo *ClassName, SourceLocation ClassLoc,
549 IdentifierInfo *SuperClassname,
550 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000551 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000552 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000553 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000556 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000557 }
558 else {
559 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000560 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000561 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000563 }
564
565 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000567 if (SuperClassname) {
568 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000569 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000571 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
572 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000573 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000574 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000576 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000577 Diag(SuperClassLoc, diag::err_undef_superclass)
578 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000579 else if (IDecl && IDecl->getSuperClass() != SDecl) {
580 // This implementation and its interface do not have the same
581 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000582 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000583 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000584 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000585 }
586 }
587 }
588
589 if (!IDecl) {
590 // Legacy case of @implementation with no corresponding @interface.
591 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000592
593 // FIXME: Do we support attributes on the @implementation? If so
594 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000595 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
596 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000597 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000598 IDecl->setSuperClass(SDecl);
599 IDecl->setLocEnd(ClassLoc);
600
Douglas Gregord0434102009-01-09 00:49:46 +0000601 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000602 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000603 // Remember that this needs to be removed when the scope is popped.
604 TUScope->AddDecl(IDecl);
605 }
606
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000608 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000609 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000610
Douglas Gregord0434102009-01-09 00:49:46 +0000611 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000612 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000613
Anders Carlsson15281452008-11-04 16:57:32 +0000614 if (CheckObjCDeclScope(IMPDecl))
615 return IMPDecl;
616
Chris Lattner4d391482007-12-12 07:09:47 +0000617 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000619 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000620 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000621 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000623 return IMPDecl;
624}
625
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
627 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000628 SourceLocation RBrace) {
629 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000630 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000631 if (!IDecl)
632 return;
633 /// Check case of non-existing @interface decl.
634 /// (legacy objective-c @implementation decl without an @interface decl).
635 /// Add implementations's ivar to the synthesize class's ivar list.
636 if (IDecl->ImplicitInterfaceDecl()) {
637 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
638 return;
639 }
640 // If implementation has empty ivar list, just return.
641 if (numIvars == 0)
642 return;
643
644 assert(ivars && "missing @implementation ivars");
645
646 // Check interface's Ivar list against those in the implementation.
647 // names and types must match.
648 //
Chris Lattner4d391482007-12-12 07:09:47 +0000649 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000651 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
652 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000653 ObjCIvarDecl* ImplIvar = ivars[j++];
654 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000655 assert (ImplIvar && "missing implementation ivar");
656 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000657 if (Context.getCanonicalType(ImplIvar->getType()) !=
658 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000659 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000660 << ImplIvar->getIdentifier()
661 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000662 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000663 }
664 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
665 // as error.
666 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000667 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000668 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000669 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000670 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000671 }
672 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000673 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000674
675 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000676 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000677 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000678 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000679}
680
Steve Naroff3c2eb662008-02-10 21:38:56 +0000681void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
682 bool &IncompleteImpl) {
683 if (!IncompleteImpl) {
684 Diag(ImpLoc, diag::warn_incomplete_impl);
685 IncompleteImpl = true;
686 }
Chris Lattner08631c52008-11-23 21:45:46 +0000687 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000688}
689
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000690void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
691 ObjCMethodDecl *IntfMethodDecl) {
692 bool err = false;
693 QualType ImpMethodQType =
694 Context.getCanonicalType(ImpMethodDecl->getResultType());
695 QualType IntfMethodQType =
696 Context.getCanonicalType(IntfMethodDecl->getResultType());
697 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
698 err = true;
699 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
700 IF=IntfMethodDecl->param_begin(),
701 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
702 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
703 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
704 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
705 err = true;
706 break;
707 }
708 }
709 if (err) {
710 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
711 << ImpMethodDecl->getDeclName();
712 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
713 }
714}
715
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000716/// isPropertyReadonly - Return true if property is readonly, by searching
717/// for the property in the class and in its categories and implementations
718///
719bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
720 ObjCInterfaceDecl *IDecl) const {
721 // by far the most common case.
722 if (!PDecl->isReadOnly())
723 return false;
724 // Even if property is ready only, if interface has a user defined setter,
725 // it is not considered read only.
726 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
727 return false;
728
729 // Main class has the property as 'readonly'. Must search
730 // through the category list to see if the property's
731 // attribute has been over-ridden to 'readwrite'.
732 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
733 Category; Category = Category->getNextClassCategory()) {
734 // Even if property is ready only, if a category has a user defined setter,
735 // it is not considered read only.
736 if (Category->getInstanceMethod(PDecl->getSetterName()))
737 return false;
738 ObjCPropertyDecl *P =
739 Category->FindPropertyDeclaration(PDecl->getIdentifier());
740 if (P && !P->isReadOnly())
741 return false;
742 }
743
744 // Also, check for definition of a setter method in the implementation if
745 // all else failed.
746 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
747 if (ObjCImplementationDecl *IMD =
748 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
749 if (IMD->getInstanceMethod(PDecl->getSetterName()))
750 return false;
751 }
752 else if (ObjCCategoryImplDecl *CIMD =
753 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
754 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
755 return false;
756 }
757 }
758 return true;
759}
760
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000761/// FIXME: Type hierarchies in Objective-C can be deep. We could most
762/// likely improve the efficiency of selector lookups and type
763/// checking by associating with each protocol / interface / category
764/// the flattened instance tables. If we used an immutable set to keep
765/// the table then it wouldn't add significant memory cost and it
766/// would be handy for lookups.
767
Steve Naroffefe7f362008-02-08 22:06:17 +0000768/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000769/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000770void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
771 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000772 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000773 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000774 const llvm::DenseSet<Selector> &ClsMap,
775 ObjCInterfaceDecl *IDecl) {
776 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
777
778 // If a method lookup fails locally we still need to look and see if
779 // the method was implemented by a base class or an inherited
780 // protocol. This lookup is slow, but occurs rarely in correct code
781 // and otherwise would terminate in a warning.
782
Chris Lattner4d391482007-12-12 07:09:47 +0000783 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000784 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000785 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000786 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000787 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000788 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000789 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000790 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000791 }
792 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000794 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000795 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000796 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
797 !ClsMap.count(method->getSelector()) &&
798 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000799 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000800 }
Chris Lattner780f3292008-07-21 21:32:27 +0000801 // Check on this protocols's referenced protocols, recursively.
802 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
803 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000804 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000805}
806
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
808 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000809 llvm::DenseSet<Selector> InsMap;
810 // Check and see if instance methods in class interface have been
811 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000813 E = IMPDecl->instmeth_end(); I != E; ++I)
814 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000815
816 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000818 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000819 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000820 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000821 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000822 ObjCMethodDecl *ImpMethodDecl =
823 IMPDecl->getInstanceMethod((*I)->getSelector());
824 ObjCMethodDecl *IntfMethodDecl =
825 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000826 assert(IntfMethodDecl &&
827 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
828 // ImpMethodDecl may be null as in a @dynamic property.
829 if (ImpMethodDecl)
830 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000831 }
Chris Lattner4c525092007-12-12 17:58:05 +0000832
Chris Lattner4d391482007-12-12 07:09:47 +0000833 llvm::DenseSet<Selector> ClsMap;
834 // Check and see if class methods in class interface have been
835 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000837 E = IMPDecl->classmeth_end(); I != E; ++I)
838 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000839
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000840 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000841 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000842 if (!ClsMap.count((*I)->getSelector()))
843 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000844 else {
845 ObjCMethodDecl *ImpMethodDecl =
846 IMPDecl->getClassMethod((*I)->getSelector());
847 ObjCMethodDecl *IntfMethodDecl =
848 IDecl->getClassMethod((*I)->getSelector());
849 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
850 }
851
Chris Lattner4d391482007-12-12 07:09:47 +0000852
853 // Check the protocol list for unimplemented methods in the @implementation
854 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000855 const ObjCList<ObjCProtocolDecl> &Protocols =
856 IDecl->getReferencedProtocols();
857 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
858 E = Protocols.end(); I != E; ++I)
859 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000860 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000861}
862
863/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000864/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
866 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000867 llvm::DenseSet<Selector> InsMap;
868 // Check and see if instance methods in category interface have been
869 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000871 E = CatImplDecl->instmeth_end(); I != E; ++I)
872 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000873
874 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000876 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000877 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000878 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000879 else {
880 ObjCMethodDecl *ImpMethodDecl =
881 CatImplDecl->getInstanceMethod((*I)->getSelector());
882 ObjCMethodDecl *IntfMethodDecl =
883 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000884 assert(IntfMethodDecl &&
885 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
886 // ImpMethodDecl may be null as in a @dynamic property.
887 if (ImpMethodDecl)
888 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000889 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000890
Chris Lattner4d391482007-12-12 07:09:47 +0000891 llvm::DenseSet<Selector> ClsMap;
892 // Check and see if class methods in category interface have been
893 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000894 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000895 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
896 I != E; ++I)
897 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000898
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000899 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000900 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000901 if (!ClsMap.count((*I)->getSelector()))
902 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000903 else {
904 ObjCMethodDecl *ImpMethodDecl =
905 CatImplDecl->getClassMethod((*I)->getSelector());
906 ObjCMethodDecl *IntfMethodDecl =
907 CatClassDecl->getClassMethod((*I)->getSelector());
908 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
909 }
Chris Lattner4d391482007-12-12 07:09:47 +0000910 // Check the protocol list for unimplemented methods in the @implementation
911 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000912 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
913 E = CatClassDecl->protocol_end(); PI != E; ++PI)
914 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000915 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000916}
917
918/// ActOnForwardClassDeclaration -
919Action::DeclTy *
920Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
921 IdentifierInfo **IdentList, unsigned NumElts)
922{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000924
925 for (unsigned i = 0; i != NumElts; ++i) {
926 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000927 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000928 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000929 // Maybe we will complain about the shadowed template parameter.
930 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
931 // Just pretend that we didn't see the previous declaration.
932 PrevDecl = 0;
933 }
934
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000935 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000936 // GCC apparently allows the following idiom:
937 //
938 // typedef NSObject < XCElementTogglerP > XCElementToggler;
939 // @class XCElementToggler;
940 //
941 // FIXME: Make an extension?
942 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
943 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000944 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000945 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000946 }
Chris Lattner4d391482007-12-12 07:09:47 +0000947 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000949 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000950 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
951 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000952 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000953
Douglas Gregord0434102009-01-09 00:49:46 +0000954 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000955 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000956 // Remember that this needs to be removed when the scope is popped.
957 TUScope->AddDecl(IDecl);
958 }
959
960 Interfaces.push_back(IDecl);
961 }
962
Douglas Gregord0434102009-01-09 00:49:46 +0000963 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000964 &Interfaces[0],
965 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000966 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000967 CheckObjCDeclScope(CDecl);
968 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000969}
970
971
972/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
973/// returns true, or false, accordingly.
974/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000976 const ObjCMethodDecl *PrevMethod,
977 bool matchBasedOnSizeAndAlignment) {
978 QualType T1 = Context.getCanonicalType(Method->getResultType());
979 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
980
981 if (T1 != T2) {
982 // The result types are different.
983 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000984 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000985 // Incomplete types don't have a size and alignment.
986 if (T1->isIncompleteType() || T2->isIncompleteType())
987 return false;
988 // Check is based on size and alignment.
989 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
990 return false;
991 }
992 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
993 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
994 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
995 if (T1 != T2) {
996 // The result types are different.
997 if (!matchBasedOnSizeAndAlignment)
998 return false;
999 // Incomplete types don't have a size and alignment.
1000 if (T1->isIncompleteType() || T2->isIncompleteType())
1001 return false;
1002 // Check is based on size and alignment.
1003 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1004 return false;
1005 }
Chris Lattner4d391482007-12-12 07:09:47 +00001006 }
1007 return true;
1008}
1009
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1011 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001012 if (!FirstMethod.Method) {
1013 // Haven't seen a method with this selector name yet - add it.
1014 FirstMethod.Method = Method;
1015 FirstMethod.Next = 0;
1016 } else {
1017 // We've seen a method with this name, now check the type signature(s).
1018 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1019
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001020 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001021 Next = Next->Next)
1022 match = MatchTwoMethodDeclarations(Method, Next->Method);
1023
1024 if (!match) {
1025 // We have a new signature for an existing method - add it.
1026 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001027 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001028 }
1029 }
1030}
1031
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001032// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001033ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1034 SourceRange R) {
1035 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001036 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001037
1038 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001039 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1040 // This checks if the methods differ by size & alignment.
1041 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1042 issueWarning = true;
1043 }
1044 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001045 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001046 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001047 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001048 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001049 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001050 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001051 }
1052 return MethList.Method;
1053}
1054
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001055void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1056 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001057 if (!FirstMethod.Method) {
1058 // Haven't seen a method with this selector name yet - add it.
1059 FirstMethod.Method = Method;
1060 FirstMethod.Next = 0;
1061 } else {
1062 // We've seen a method with this name, now check the type signature(s).
1063 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1064
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001065 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001066 Next = Next->Next)
1067 match = MatchTwoMethodDeclarations(Method, Next->Method);
1068
1069 if (!match) {
1070 // We have a new signature for an existing method - add it.
1071 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001072 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001073 FirstMethod.Next = OMI;
1074 }
1075 }
1076}
1077
Steve Naroff0701bbb2009-01-08 17:28:14 +00001078/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1079/// have the property type and issue diagnostics if they don't.
1080/// Also synthesize a getter/setter method if none exist (and update the
1081/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1082/// methods is the "right" thing to do.
1083void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1084 ObjCContainerDecl *CD) {
1085 ObjCMethodDecl *GetterMethod, *SetterMethod;
1086
1087 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1088 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1089
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001090 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001091 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001092 Diag(property->getLocation(),
1093 diag::err_accessor_property_type_mismatch)
1094 << property->getDeclName()
1095 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001096 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1097 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001098
1099 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001100 if (Context.getCanonicalType(SetterMethod->getResultType())
1101 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001102 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1103 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001104 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001105 Diag(property->getLocation(),
1106 diag::err_accessor_property_type_mismatch)
1107 << property->getDeclName()
1108 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001109 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1110 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001111 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001112
1113 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001114 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001115 // FIXME: The synthesized property we set here is misleading. We
1116 // almost always synthesize these methods unless the user explicitly
1117 // provided prototypes (which is odd, but allowed). Sema should be
1118 // typechecking that the declarations jive in that situation (which
1119 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001120 if (!GetterMethod) {
1121 // No instance method of same name as property getter name was found.
1122 // Declare a getter method and add it to the list of methods
1123 // for this class.
1124 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1125 property->getLocation(), property->getGetterName(),
1126 property->getType(), CD, true, false, true,
1127 (property->getPropertyImplementation() ==
1128 ObjCPropertyDecl::Optional) ?
1129 ObjCMethodDecl::Optional :
1130 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001131 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001132 } else
1133 // A user declared getter will be synthesize when @synthesize of
1134 // the property with the same name is seen in the @implementation
1135 GetterMethod->setIsSynthesized();
1136 property->setGetterMethodDecl(GetterMethod);
1137
1138 // Skip setter if property is read-only.
1139 if (!property->isReadOnly()) {
1140 // Find the default setter and if one not found, add one.
1141 if (!SetterMethod) {
1142 // No instance method of same name as property setter name was found.
1143 // Declare a setter method and add it to the list of methods
1144 // for this class.
1145 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1146 property->getLocation(),
1147 property->getSetterName(),
1148 Context.VoidTy, CD, true, false, true,
1149 (property->getPropertyImplementation() ==
1150 ObjCPropertyDecl::Optional) ?
1151 ObjCMethodDecl::Optional :
1152 ObjCMethodDecl::Required);
1153 // Invent the arguments for the setter. We don't bother making a
1154 // nice name for the argument.
1155 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1156 SourceLocation(),
1157 property->getIdentifier(),
1158 property->getType(),
1159 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001160 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001161 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001162 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001163 } else
1164 // A user declared setter will be synthesize when @synthesize of
1165 // the property with the same name is seen in the @implementation
1166 SetterMethod->setIsSynthesized();
1167 property->setSetterMethodDecl(SetterMethod);
1168 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001169 // Add any synthesized methods to the global pool. This allows us to
1170 // handle the following, which is supported by GCC (and part of the design).
1171 //
1172 // @interface Foo
1173 // @property double bar;
1174 // @end
1175 //
1176 // void thisIsUnfortunate() {
1177 // id foo;
1178 // double bar = [foo bar];
1179 // }
1180 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001181 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001182 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001183 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001184 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001185}
1186
Steve Naroffa56f6162007-12-18 01:30:32 +00001187// Note: For class/category implemenations, allMethods/allProperties is
1188// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001189void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1190 DeclTy **allMethods, unsigned allNum,
1191 DeclTy **allProperties, unsigned pNum) {
1192 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1193
Steve Naroffa56f6162007-12-18 01:30:32 +00001194 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1195 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001196 // should be true.
1197 if (!ClassDecl)
1198 return;
1199
Chris Lattner4d391482007-12-12 07:09:47 +00001200 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001201 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1202 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001203 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001204
Steve Naroff0701bbb2009-01-08 17:28:14 +00001205 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001206
1207 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1208 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1209 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1210
Chris Lattner4d391482007-12-12 07:09:47 +00001211 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 ObjCMethodDecl *Method =
1213 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001214
1215 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001216 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001217 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001218 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001219 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1220 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001221 if ((isInterfaceDeclKind && PrevMethod && !match)
1222 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001223 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001224 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001225 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001226 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001227 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001228 InsMap[Method->getSelector()] = Method;
1229 /// The following allows us to typecheck messages to "id".
1230 AddInstanceMethodToGlobalPool(Method);
1231 }
1232 }
1233 else {
1234 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001236 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1237 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001238 if ((isInterfaceDeclKind && PrevMethod && !match)
1239 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001240 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001241 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001242 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001243 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001244 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001245 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001246 /// The following allows us to typecheck messages to "Class".
1247 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001248 }
1249 }
1250 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001251 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001252 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001253 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001254 ComparePropertiesInBaseAndSuper(I);
1255 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001256 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001257 // Categories are used to extend the class by declaring new methods.
1258 // By the same token, they are also used to add new properties. No
1259 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001260
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001261 // Merge protocol properties into category
1262 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001263 }
Steve Naroff09c47192009-01-09 15:36:25 +00001264 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1265 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1266 // user-defined setter/getter. It also synthesizes setter/getter methods
1267 // and adds them to the DeclContext and global method pools.
1268 for (ObjCContainerDecl::prop_iterator i = CDecl->prop_begin(),
1269 e = CDecl->prop_end(); i != e; ++i)
1270 ProcessPropertyDecl((*i), CDecl);
1271 CDecl->setAtEndLoc(AtEndLoc);
1272 }
1273 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001274 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001275 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001276 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001277 } else if (ObjCCategoryImplDecl* CatImplClass =
1278 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001279 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001280 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001281 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001282 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001283 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001284 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001285 Categories; Categories = Categories->getNextClassCategory()) {
1286 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1287 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1288 break;
1289 }
1290 }
1291 }
1292 }
1293}
1294
1295
1296/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1297/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298static Decl::ObjCDeclQualifier
1299CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1300 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1301 if (PQTVal & ObjCDeclSpec::DQ_In)
1302 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1303 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1304 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1305 if (PQTVal & ObjCDeclSpec::DQ_Out)
1306 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1307 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1308 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1309 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1310 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1311 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1312 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001313
1314 return ret;
1315}
1316
1317Sema::DeclTy *Sema::ActOnMethodDeclaration(
1318 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001319 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001320 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001321 Selector Sel,
1322 // optional arguments. The number of types/arguments is obtained
1323 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001324 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001325 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001326 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1327 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001328 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001329
1330 // Make sure we can establish a context for the method.
1331 if (!ClassDecl) {
1332 Diag(MethodLoc, diag::error_missing_method_context);
1333 return 0;
1334 }
Chris Lattner4d391482007-12-12 07:09:47 +00001335 QualType resultDeclType;
1336
1337 if (ReturnType)
1338 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1339 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001341
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001342 ObjCMethodDecl* ObjCMethod =
1343 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001344 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001345 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001346 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001347 MethodDeclKind == tok::objc_optional ?
1348 ObjCMethodDecl::Optional :
1349 ObjCMethodDecl::Required);
1350
Chris Lattner0ed844b2008-04-04 06:12:32 +00001351 llvm::SmallVector<ParmVarDecl*, 16> Params;
1352
1353 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1354 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001355 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001356
Steve Naroff6082c622008-12-09 19:36:17 +00001357 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001358 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001359 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001360 if (argType->isArrayType()) { // (char *[]) -> (char **)
1361 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001362 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001363 }
Steve Naroff6082c622008-12-09 19:36:17 +00001364 else if (argType->isFunctionType())
1365 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001366 else if (argType->isObjCInterfaceType()) {
1367 // FIXME! provide more precise location for the parameter
1368 Diag(MethodLoc, diag::err_object_as_method_param);
1369 return 0;
1370 }
Steve Naroff6082c622008-12-09 19:36:17 +00001371 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001372 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001373 ParmVarDecl* Param;
1374 if (originalArgType.isNull())
1375 Param = ParmVarDecl::Create(Context, ObjCMethod,
1376 SourceLocation(/*FIXME*/),
1377 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001378 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001379 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001380 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1381 SourceLocation(/*FIXME*/),
1382 ArgNames[i], argType, originalArgType,
1383 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001384
Chris Lattner0ed844b2008-04-04 06:12:32 +00001385 Param->setObjCDeclQualifier(
1386 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1387 Params.push_back(Param);
1388 }
1389
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1391 ObjCMethod->setObjCDeclQualifier(
1392 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1393 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001394
1395 if (AttrList)
1396 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001397
1398 // For implementations (which can be very "coarse grain"), we add the
1399 // method now. This allows the AST to implement lookup methods that work
1400 // incrementally (without waiting until we parse the @end). It also allows
1401 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001403 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001404 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001405 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001406 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001407 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001408 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001410 }
1411 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001413 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001414 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001415 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001416 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001417 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001418 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001419 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001420 }
1421 }
1422 if (PrevMethod) {
1423 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001424 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001425 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001426 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001427 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001428 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001429}
1430
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001431void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1432 SourceLocation Loc,
1433 unsigned &Attributes) {
1434 // FIXME: Improve the reported location.
1435
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001436 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001437 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001438 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1439 ObjCDeclSpec::DQ_PR_assign |
1440 ObjCDeclSpec::DQ_PR_copy |
1441 ObjCDeclSpec::DQ_PR_retain))) {
1442 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1443 "readwrite" :
1444 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1445 "assign" :
1446 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1447 "copy" : "retain";
1448
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001449 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001450 diag::err_objc_property_attr_mutually_exclusive :
1451 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001452 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001453 }
1454
1455 // Check for copy or retain on non-object types.
1456 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1457 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001458 Diag(Loc, diag::err_objc_property_requires_object)
1459 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001460 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1461 }
1462
1463 // Check for more than one of { assign, copy, retain }.
1464 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1465 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001466 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1467 << "assign" << "copy";
1468 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001469 }
1470 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001471 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1472 << "assign" << "retain";
1473 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001474 }
1475 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1476 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001477 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1478 << "copy" << "retain";
1479 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001480 }
1481 }
1482
1483 // Warn if user supplied no assignment attribute, property is
1484 // readwrite, and this is an object type.
1485 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1486 ObjCDeclSpec::DQ_PR_retain)) &&
1487 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1488 Context.isObjCObjectPointerType(PropertyTy)) {
1489 // Skip this warning in gc-only mode.
1490 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1491 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1492
1493 // If non-gc code warn that this is likely inappropriate.
1494 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1495 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1496
1497 // FIXME: Implement warning dependent on NSCopying being
1498 // implemented. See also:
1499 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1500 // (please trim this list while you are at it).
1501 }
1502}
1503
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001504Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1505 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001506 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001507 Selector GetterSel,
1508 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001509 DeclTy *ClassCategory,
1510 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001511 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001512 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001513 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1514 // default is readwrite!
1515 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1516 // property is defaulted to 'assign' if it is readwrite and is
1517 // not retain or copy
1518 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1519 (isReadWrite &&
1520 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1521 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1522 QualType T = GetTypeForDeclarator(FD.D, S);
1523 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001524
1525 // May modify Attributes.
1526 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001527
1528 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1529 if (!CDecl->getIdentifier()) {
1530 // This is an anonymous category. property requires special
1531 // handling.
1532 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1533 if (ObjCPropertyDecl *PIDecl =
1534 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1535 // property 'PIDecl's readonly attribute will be over-ridden
1536 // with anonymous category's readwrite property attribute!
1537 unsigned PIkind = PIDecl->getPropertyAttributes();
1538 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001539 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001540 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1541 Diag(AtLoc, diag::warn_property_attr_mismatch);
1542 PIDecl->makeitReadWriteAttribute();
1543 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1544 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1545 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1546 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1547 PIDecl->setSetterName(SetterSel);
1548 // FIXME: use a common routine with addPropertyMethods.
1549 ObjCMethodDecl *SetterDecl =
1550 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1551 Context.VoidTy,
1552 ICDecl,
1553 true, false, true,
1554 ObjCMethodDecl::Required);
1555 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1556 SetterDecl,
1557 SourceLocation(),
1558 FD.D.getIdentifier(),
1559 T,
1560 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001561 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001562 SetterDecl->setMethodParams(&Argument, 1);
1563 PIDecl->setSetterMethodDecl(SetterDecl);
1564 }
1565 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001566 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001567 *isOverridingProperty = true;
1568 return 0;
1569 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001570 // No matching property found in the main class. Just fall thru
1571 // and add property to the anonymous category. It looks like
1572 // it works as is. This category becomes just like a category
1573 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001574 } else {
1575 Diag(CDecl->getLocation(), diag::err_continuation_class);
1576 *isOverridingProperty = true;
1577 return 0;
1578 }
1579 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001580
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001581 Type *t = T.getTypePtr();
1582 if (t->isArrayType() || t->isFunctionType())
1583 Diag(AtLoc, diag::err_property_type) << T;
1584
Steve Naroff93983f82009-01-11 12:47:58 +00001585 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1586 assert(DC && "ClassDecl is not a DeclContext");
1587 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001588 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001589 DC->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +00001590
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001591 // Regardless of setter/getter attribute, we save the default getter/setter
1592 // selector names in anticipation of declaration of setter/getter methods.
1593 PDecl->setGetterName(GetterSel);
1594 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001595
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001596 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001597 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001598
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001599 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001601
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001602 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001604
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001605 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001607
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001608 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001610
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001611 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001613
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001614 if (isAssign)
1615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1616
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001617 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001619
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001620 if (MethodImplKind == tok::objc_required)
1621 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1622 else if (MethodImplKind == tok::objc_optional)
1623 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1624
Chris Lattner4d391482007-12-12 07:09:47 +00001625 return PDecl;
1626}
1627
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001628/// ActOnPropertyImplDecl - This routine performs semantic checks and
1629/// builds the AST node for a property implementation declaration; declared
1630/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001631///
1632Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1633 SourceLocation PropertyLoc,
1634 bool Synthesize,
1635 DeclTy *ClassCatImpDecl,
1636 IdentifierInfo *PropertyId,
1637 IdentifierInfo *PropertyIvar) {
1638 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1639 // Make sure we have a context for the property implementation declaration.
1640 if (!ClassImpDecl) {
1641 Diag(AtLoc, diag::error_missing_property_context);
1642 return 0;
1643 }
1644 ObjCPropertyDecl *property = 0;
1645 ObjCInterfaceDecl* IDecl = 0;
1646 // Find the class or category class where this property must have
1647 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001648 ObjCImplementationDecl *IC = 0;
1649 ObjCCategoryImplDecl* CatImplClass = 0;
1650 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001651 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001652 // We always synthesize an interface for an implementation
1653 // without an interface decl. So, IDecl is always non-zero.
1654 assert(IDecl &&
1655 "ActOnPropertyImplDecl - @implementation without @interface");
1656
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001657 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001658 property = IDecl->FindPropertyDeclaration(PropertyId);
1659 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001660 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001661 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001662 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001663 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001664 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001665 if (Synthesize) {
1666 Diag(AtLoc, diag::error_synthesize_category_decl);
1667 return 0;
1668 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001669 IDecl = CatImplClass->getClassInterface();
1670 if (!IDecl) {
1671 Diag(AtLoc, diag::error_missing_property_interface);
1672 return 0;
1673 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001674 ObjCCategoryDecl *Category =
1675 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1676
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001677 // If category for this implementation not found, it is an error which
1678 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001679 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001680 return 0;
1681 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001682 property = Category->FindPropertyDeclaration(PropertyId);
1683 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001684 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001685 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001686 return 0;
1687 }
1688 }
1689 else {
1690 Diag(AtLoc, diag::error_bad_property_context);
1691 return 0;
1692 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001693 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001694 // Check that we have a valid, previously declared ivar for @synthesize
1695 if (Synthesize) {
1696 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001697 if (!PropertyIvar)
1698 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001699 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001700 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001701 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001702 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001703 return 0;
1704 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001705 QualType PropType = Context.getCanonicalType(property->getType());
1706 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1707
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001708 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001709 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001710 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001711 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001712 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001713 return 0;
1714 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001715 else {
1716 // FIXME! Rules for properties are somewhat different that those
1717 // for assignments. Use a new routine to consolidate all cases;
1718 // specifically for property redeclarations as well as for ivars.
1719 QualType lhsType =
1720 Context.getCanonicalType(PropType).getUnqualifiedType();
1721 QualType rhsType =
1722 Context.getCanonicalType(IvarType).getUnqualifiedType();
1723 if (lhsType != rhsType &&
1724 lhsType->isArithmeticType()) {
1725 Diag(PropertyLoc, diag::error_property_ivar_type)
1726 << property->getDeclName() << Ivar->getDeclName();
1727 return 0;
1728 }
1729 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001730 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001731 } else if (PropertyIvar) {
1732 // @dynamic
1733 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1734 return 0;
1735 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001736 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001737 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001738 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1739 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001740 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001741 ObjCPropertyImplDecl::Synthesize
1742 : ObjCPropertyImplDecl::Dynamic),
1743 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001744 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001745 if (IC) {
1746 if (Synthesize)
1747 if (ObjCPropertyImplDecl *PPIDecl =
1748 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1749 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1750 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1751 << PropertyIvar;
1752 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1753 }
1754
1755 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1756 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1757 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1758 return 0;
1759 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001760 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001761 }
1762 else {
1763 if (Synthesize)
1764 if (ObjCPropertyImplDecl *PPIDecl =
1765 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1766 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1767 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1768 << PropertyIvar;
1769 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1770 }
1771
1772 if (ObjCPropertyImplDecl *PPIDecl =
1773 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1774 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1775 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1776 return 0;
1777 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001778 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001779 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001780
1781 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001782}
Anders Carlsson15281452008-11-04 16:57:32 +00001783
Chris Lattnercc98eac2008-12-17 07:13:27 +00001784bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001785 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001786 return false;
1787
1788 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1789 D->setInvalidDecl();
1790
1791 return true;
1792}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001793
1794/// Collect the instance variables declared in an Objective-C object. Used in
1795/// the creation of structures from objects using the @defs directive.
1796/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1797/// part of the AST generation logic of @defs.
1798static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1799 ASTContext& Ctx,
1800 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1801 if (Class->getSuperClass())
1802 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1803
1804 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1805 for (ObjCInterfaceDecl::ivar_iterator
1806 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1807
1808 ObjCIvarDecl* ID = *I;
1809 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1810 ID->getLocation(),
1811 ID->getIdentifier(),
1812 ID->getType(),
1813 ID->getBitWidth()));
1814 }
1815}
1816
1817/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1818/// instance variables of ClassName into Decls.
1819void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1820 IdentifierInfo *ClassName,
1821 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1822 // Check that ClassName is a valid class
1823 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1824 if (!Class) {
1825 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1826 return;
1827 }
1828 // Collect the instance variables
1829 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1830
1831 // Introduce all of these fields into the appropriate scope.
1832 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1833 D != Decls.end(); ++D) {
1834 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1835 if (getLangOptions().CPlusPlus)
1836 PushOnScopeChains(cast<FieldDecl>(FD), S);
1837 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001838 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001839 }
1840}
1841