blob: 55b0d16e8c48590ea2dda75493d80935ef363d05 [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"
Steve Naroffca331292009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroffebf64432009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Steve Naroffebf64432009-02-28 16:59:13 +000023void Sema::ActOnStartOfObjCMethodDef(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
Steve Narofff3cf8972009-02-28 16:48:43 +000040 ActiveScope = FnBodyScope;
41
Chris Lattner4d391482007-12-12 07:09:47 +000042 // Create Decl objects for each parameter, entrring them in the scope for
43 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000047
Daniel Dunbar451318c2008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000050
Chris Lattner8123a952008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000052 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
53 E = MDecl->param_end(); PI != E; ++PI)
54 if ((*PI)->getIdentifier())
55 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056}
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 Lattner1829a6d2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000103 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000109 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000110 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000111
Steve Naroff818cb9e2009-02-04 17:14:05 +0000112 ObjCInterfaceDecl *SuperClassDecl =
113 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000114
115 // Diagnose classes that inherit from deprecated classes.
116 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000117 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000118
Steve Naroff818cb9e2009-02-04 17:14:05 +0000119 if (PrevDecl && SuperClassDecl == 0) {
120 // The previous declaration was not a class decl. Check if we have a
121 // typedef. If we do, get the underlying class type.
122 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
123 QualType T = TDecl->getUnderlyingType();
124 if (T->isObjCInterfaceType()) {
125 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
126 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
127 }
128 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000129
Steve Naroff818cb9e2009-02-04 17:14:05 +0000130 // This handles the following case:
131 //
132 // typedef int SuperClass;
133 // @interface MyClass : SuperClass {} @end
134 //
135 if (!SuperClassDecl) {
136 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
137 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
138 }
139 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000140
Steve Naroff818cb9e2009-02-04 17:14:05 +0000141 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
142 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000145 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000146 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000148 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000150 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000151 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 IDecl->setLocEnd(SuperLoc);
153 } else { // we have a root class.
154 IDecl->setLocEnd(ClassLoc);
155 }
156
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000157 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000158 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000159 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
160 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000161 IDecl->setLocEnd(EndProtoLoc);
162 }
Anders Carlsson15281452008-11-04 16:57:32 +0000163
164 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000165 return IDecl;
166}
167
168/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000169/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000170Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
171 IdentifierInfo *AliasName,
172 SourceLocation AliasLocation,
173 IdentifierInfo *ClassName,
174 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000175 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000176 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000177 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000179 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000180 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000181 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000182 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000183 return 0;
184 }
185 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000186 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000187 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
188 QualType T = TDecl->getUnderlyingType();
189 if (T->isObjCInterfaceType()) {
190 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
191 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000192 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000193 }
194 }
195 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000196 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
197 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000198 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000199 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000200 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000201 return 0;
202 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000203
204 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000205 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000206 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000207
208 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000209
210 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000211 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000212 if (!CheckObjCDeclScope(AliasDecl))
213 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000214
Chris Lattner4d391482007-12-12 07:09:47 +0000215 return AliasDecl;
216}
217
Chris Lattnere13b9592008-07-26 04:03:38 +0000218Sema::DeclTy *
219Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
220 IdentifierInfo *ProtocolName,
221 SourceLocation ProtocolLoc,
222 DeclTy * const *ProtoRefs,
223 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000224 SourceLocation EndProtoLoc,
225 AttributeList *AttrList) {
226 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000227 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000228 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000229 if (PDecl) {
230 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000231 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000232 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000233 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000234 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000235 // Just return the protocol we already had.
236 // FIXME: don't leak the objects passed in!
237 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000238 }
Steve Narofff11b5082008-08-13 16:39:22 +0000239 // Make sure the cached decl gets a valid start location.
240 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000241 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000242 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000243 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
244 AtProtoInterfaceLoc,ProtocolName);
245 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000246 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000247 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000248 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000249 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000250 if (AttrList)
251 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000252 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000253 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000254 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000255 PDecl->setLocEnd(EndProtoLoc);
256 }
Anders Carlsson15281452008-11-04 16:57:32 +0000257
258 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000259 return PDecl;
260}
261
262/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000263/// issues an error if they are not declared. It returns list of
264/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000265void
Chris Lattnere13b9592008-07-26 04:03:38 +0000266Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000267 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000268 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000269 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000270 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000271 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
272 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000274 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000275 continue;
276 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000277
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000278 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000279
280 // If this is a forward declaration and we are supposed to warn in this
281 // case, do it.
282 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000283 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000284 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000285 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000286 }
287}
288
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000290/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000291///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000292void
293Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
294 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000295 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000296 ObjCPropertyDecl::PropertyAttributeKind CAttr =
297 Property->getPropertyAttributes();
298 ObjCPropertyDecl::PropertyAttributeKind SAttr =
299 SuperProperty->getPropertyAttributes();
300 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
301 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000302 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000303 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
305 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000308 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
309 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000310 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000311 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312
313 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
314 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000315 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000316 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000317 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000318 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000319 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000320 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000323
324 QualType LHSType =
325 Context.getCanonicalType(SuperProperty->getType());
326 QualType RHSType =
327 Context.getCanonicalType(Property->getType());
328
329 if (!Context.typesAreCompatible(LHSType, RHSType)) {
330 // FIXME: Incorporate this test with typesAreCompatible.
331 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
332 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
333 return;
334 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
335 << Property->getType() << SuperProperty->getType() << inheritedName;
336 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000337}
338
339/// ComparePropertiesInBaseAndSuper - This routine compares property
340/// declarations in base and its super class, if any, and issues
341/// diagnostics in a variety of inconsistant situations.
342///
Chris Lattner70f19542009-02-16 21:26:43 +0000343void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000344 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
345 if (!SDecl)
346 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000347 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000348 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
349 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000350 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000352 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
353 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000354 ObjCPropertyDecl *PDecl = (*I);
355 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000356 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000357 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358 }
359 }
360}
361
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000362/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
363/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000364/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000365void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000367 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
369 if (!IDecl) {
370 // Category
371 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
372 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000373 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
374 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000375 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000376 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000377 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000378 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000379 CP != CE; ++CP)
380 if ((*CP)->getIdentifier() == Pr->getIdentifier())
381 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000382 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000383 // Property protocol already exist in class. Diagnose any mismatch.
384 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
385 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000386 return;
387 }
Steve Naroff09c47192009-01-09 15:36:25 +0000388 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
389 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000390 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000391 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000392 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000393 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000394 CP != CE; ++CP)
395 if ((*CP)->getIdentifier() == Pr->getIdentifier())
396 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000397 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000398 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000399 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000400 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000401}
402
403/// MergeProtocolPropertiesIntoClass - This routine merges properties
404/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000405/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000406///
Chris Lattner70f19542009-02-16 21:26:43 +0000407void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
408 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000409 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000410 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
411
412 if (!IDecl) {
413 // Category
414 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
415 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
416 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
417 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
418 E = MDecl->protocol_end(); P != E; ++P)
419 // Merge properties of category (*P) into IDECL's
420 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
421
422 // Go thru the list of protocols for this category and recursively merge
423 // their properties into this class as well.
424 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
425 E = CatDecl->protocol_end(); P != E; ++P)
426 MergeProtocolPropertiesIntoClass(CatDecl, *P);
427 } else {
428 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
429 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
430 E = MD->protocol_end(); P != E; ++P)
431 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
432 }
433 return;
434 }
435
Chris Lattnerb752f282008-07-21 07:06:49 +0000436 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000437 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
438 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000439 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000440 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
441
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442 // Go thru the list of protocols for this class and recursively merge
443 // their properties into this class as well.
444 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
445 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000446 MergeProtocolPropertiesIntoClass(IDecl, *P);
447 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000448 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
449 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
450 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000451 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000452 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000453}
454
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000455/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000456/// a class method in its extension.
457///
458void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
459 ObjCInterfaceDecl *ID) {
460 if (!ID)
461 return; // Possibly due to previous error
462
463 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
464 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
465 e = ID->meth_end(); i != e; ++i) {
466 ObjCMethodDecl *MD = *i;
467 MethodMap[MD->getSelector()] = MD;
468 }
469
470 if (MethodMap.empty())
471 return;
472 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
473 e = CAT->meth_end(); i != e; ++i) {
474 ObjCMethodDecl *Method = *i;
475 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
476 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
477 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
478 << Method->getDeclName();
479 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
480 }
481 }
482}
483
Chris Lattner4d391482007-12-12 07:09:47 +0000484/// ActOnForwardProtocolDeclaration -
485Action::DeclTy *
486Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000487 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000488 unsigned NumElts,
489 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000491
492 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000493 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000494 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000495 if (PDecl == 0) { // Not already seen?
496 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
497 IdentList[i].second, Ident);
498 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000499 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000500 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000501 if (attrList)
502 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000503 Protocols.push_back(PDecl);
504 }
Anders Carlsson15281452008-11-04 16:57:32 +0000505
506 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000507 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000508 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000509 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000510 CheckObjCDeclScope(PDecl);
511 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000512}
513
Chris Lattner7caeabd2008-07-21 22:17:28 +0000514Sema::DeclTy *Sema::
515ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
516 IdentifierInfo *ClassName, SourceLocation ClassLoc,
517 IdentifierInfo *CategoryName,
518 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000519 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000520 unsigned NumProtoRefs,
521 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000522 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000523 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
524 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000525 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000526
527 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000528 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000529 if (!IDecl || IDecl->isForwardDecl()) {
530 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000531 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000532 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000533 }
Chris Lattner4d391482007-12-12 07:09:47 +0000534
Chris Lattner70f19542009-02-16 21:26:43 +0000535 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000536
537 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000538 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000539
540 /// Check for duplicate interface declaration for this category
541 ObjCCategoryDecl *CDeclChain;
542 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
543 CDeclChain = CDeclChain->getNextClassCategory()) {
544 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
545 Diag(CategoryLoc, diag::warn_dup_category_def)
546 << ClassName << CategoryName;
547 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
548 break;
549 }
550 }
551 if (!CDeclChain)
552 CDecl->insertNextClassCategory();
553
Chris Lattner4d391482007-12-12 07:09:47 +0000554 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000555 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000556 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000557 }
Anders Carlsson15281452008-11-04 16:57:32 +0000558
559 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000560 return CDecl;
561}
562
563/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000565/// object.
566Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
567 SourceLocation AtCatImplLoc,
568 IdentifierInfo *ClassName, SourceLocation ClassLoc,
569 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000571 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000572 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
573 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000574 /// Check that class of this category is already completely declared.
575 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000577
Douglas Gregord0434102009-01-09 00:49:46 +0000578 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000579 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000580
Chris Lattner4d391482007-12-12 07:09:47 +0000581 /// TODO: Check that CatName, category name, is not used in another
582 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000583 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000584
585 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000586 return CDecl;
587}
588
589Sema::DeclTy *Sema::ActOnStartClassImplementation(
590 SourceLocation AtClassImplLoc,
591 IdentifierInfo *ClassName, SourceLocation ClassLoc,
592 IdentifierInfo *SuperClassname,
593 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000595 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000596 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000598 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000599 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000600 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000601 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000603 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000604 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000605 }
606
607 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 if (SuperClassname) {
610 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000611 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000613 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
614 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000615 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000616 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000618 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 Diag(SuperClassLoc, diag::err_undef_superclass)
620 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000621 else if (IDecl && IDecl->getSuperClass() != SDecl) {
622 // This implementation and its interface do not have the same
623 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000625 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000626 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000627 }
628 }
629 }
630
631 if (!IDecl) {
632 // Legacy case of @implementation with no corresponding @interface.
633 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000634
635 // FIXME: Do we support attributes on the @implementation? If so
636 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000637 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
638 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000639 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000640 IDecl->setSuperClass(SDecl);
641 IDecl->setLocEnd(ClassLoc);
642
Douglas Gregord0434102009-01-09 00:49:46 +0000643 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000644 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000645 // Remember that this needs to be removed when the scope is popped.
646 TUScope->AddDecl(IDecl);
647 }
648
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000650 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000651 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000652
Douglas Gregord0434102009-01-09 00:49:46 +0000653 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000654 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000655
Anders Carlsson15281452008-11-04 16:57:32 +0000656 if (CheckObjCDeclScope(IMPDecl))
657 return IMPDecl;
658
Chris Lattner4d391482007-12-12 07:09:47 +0000659 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000660 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000661 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000662 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000663 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000664 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000665 return IMPDecl;
666}
667
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
669 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000670 SourceLocation RBrace) {
671 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000672 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000673 if (!IDecl)
674 return;
675 /// Check case of non-existing @interface decl.
676 /// (legacy objective-c @implementation decl without an @interface decl).
677 /// Add implementations's ivar to the synthesize class's ivar list.
678 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000679 IDecl->setIVarList(ivars, numIvars, Context);
680 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000681 return;
682 }
683 // If implementation has empty ivar list, just return.
684 if (numIvars == 0)
685 return;
686
687 assert(ivars && "missing @implementation ivars");
688
689 // Check interface's Ivar list against those in the implementation.
690 // names and types must match.
691 //
Chris Lattner4d391482007-12-12 07:09:47 +0000692 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000694 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
695 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 ObjCIvarDecl* ImplIvar = ivars[j++];
697 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000698 assert (ImplIvar && "missing implementation ivar");
699 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000700
701 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000702 if (Context.getCanonicalType(ImplIvar->getType()) !=
703 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000704 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000705 << ImplIvar->getIdentifier()
706 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000707 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000708 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
709 Expr *ImplBitWidth = ImplIvar->getBitWidth();
710 Expr *ClsBitWidth = ClsIvar->getBitWidth();
711 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
712 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
713 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
714 << ImplIvar->getIdentifier();
715 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
716 }
717 }
718 // Make sure the names are identical.
719 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000720 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000721 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000722 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000723 }
724 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000725 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000726
727 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000728 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000729 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000730 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000731}
732
Steve Naroff3c2eb662008-02-10 21:38:56 +0000733void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
734 bool &IncompleteImpl) {
735 if (!IncompleteImpl) {
736 Diag(ImpLoc, diag::warn_incomplete_impl);
737 IncompleteImpl = true;
738 }
Chris Lattner08631c52008-11-23 21:45:46 +0000739 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000740}
741
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000742void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
743 ObjCMethodDecl *IntfMethodDecl) {
744 bool err = false;
745 QualType ImpMethodQType =
746 Context.getCanonicalType(ImpMethodDecl->getResultType());
747 QualType IntfMethodQType =
748 Context.getCanonicalType(IntfMethodDecl->getResultType());
749 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
750 err = true;
751 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
752 IF=IntfMethodDecl->param_begin(),
753 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
754 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
755 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
756 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
757 err = true;
758 break;
759 }
760 }
761 if (err) {
762 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
763 << ImpMethodDecl->getDeclName();
764 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
765 }
766}
767
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000768/// isPropertyReadonly - Return true if property is readonly, by searching
769/// for the property in the class and in its categories and implementations
770///
771bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000772 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000773 // by far the most common case.
774 if (!PDecl->isReadOnly())
775 return false;
776 // Even if property is ready only, if interface has a user defined setter,
777 // it is not considered read only.
778 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
779 return false;
780
781 // Main class has the property as 'readonly'. Must search
782 // through the category list to see if the property's
783 // attribute has been over-ridden to 'readwrite'.
784 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
785 Category; Category = Category->getNextClassCategory()) {
786 // Even if property is ready only, if a category has a user defined setter,
787 // it is not considered read only.
788 if (Category->getInstanceMethod(PDecl->getSetterName()))
789 return false;
790 ObjCPropertyDecl *P =
791 Category->FindPropertyDeclaration(PDecl->getIdentifier());
792 if (P && !P->isReadOnly())
793 return false;
794 }
795
796 // Also, check for definition of a setter method in the implementation if
797 // all else failed.
798 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
799 if (ObjCImplementationDecl *IMD =
800 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
801 if (IMD->getInstanceMethod(PDecl->getSetterName()))
802 return false;
803 }
804 else if (ObjCCategoryImplDecl *CIMD =
805 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
806 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
807 return false;
808 }
809 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000810 // Lastly, look through the implementation (if one is in scope).
811 if (ObjCImplementationDecl *ImpDecl =
812 ObjCImplementations[IDecl->getIdentifier()])
813 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
814 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000815 return true;
816}
817
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000818/// FIXME: Type hierarchies in Objective-C can be deep. We could most
819/// likely improve the efficiency of selector lookups and type
820/// checking by associating with each protocol / interface / category
821/// the flattened instance tables. If we used an immutable set to keep
822/// the table then it wouldn't add significant memory cost and it
823/// would be handy for lookups.
824
Steve Naroffefe7f362008-02-08 22:06:17 +0000825/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000826/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000827void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
828 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000829 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000830 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000831 const llvm::DenseSet<Selector> &ClsMap,
832 ObjCInterfaceDecl *IDecl) {
833 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
834
835 // If a method lookup fails locally we still need to look and see if
836 // the method was implemented by a base class or an inherited
837 // protocol. This lookup is slow, but occurs rarely in correct code
838 // and otherwise would terminate in a warning.
839
Chris Lattner4d391482007-12-12 07:09:47 +0000840 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000841 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000842 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000843 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000844 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000845 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000846 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000847 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000848 }
849 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000851 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000852 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000853 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
854 !ClsMap.count(method->getSelector()) &&
855 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000856 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000857 }
Chris Lattner780f3292008-07-21 21:32:27 +0000858 // Check on this protocols's referenced protocols, recursively.
859 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
860 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000861 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000862}
863
Chris Lattnercddc8882009-03-01 00:56:52 +0000864void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
865 ObjCContainerDecl* CDecl,
866 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000867 llvm::DenseSet<Selector> InsMap;
868 // Check and see if instance methods in class interface have been
869 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000871 E = IMPDecl->instmeth_end(); I != E; ++I)
872 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000873
Chris Lattnercddc8882009-03-01 00:56:52 +0000874 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
875 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000876 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000877 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000878 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000879 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000880
881 ObjCMethodDecl *ImpMethodDecl =
882 IMPDecl->getInstanceMethod((*I)->getSelector());
883 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000884 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000885 assert(IntfMethodDecl &&
886 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
887 // ImpMethodDecl may be null as in a @dynamic property.
888 if (ImpMethodDecl)
889 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
890 }
Chris Lattner4c525092007-12-12 17:58:05 +0000891
Chris Lattner4d391482007-12-12 07:09:47 +0000892 llvm::DenseSet<Selector> ClsMap;
893 // Check and see if class methods in class interface have been
894 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000896 E = IMPDecl->classmeth_end(); I != E; ++I)
897 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000898
Chris Lattnercddc8882009-03-01 00:56:52 +0000899 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
900 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000901 if (!ClsMap.count((*I)->getSelector()))
902 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000903 else {
904 ObjCMethodDecl *ImpMethodDecl =
905 IMPDecl->getClassMethod((*I)->getSelector());
906 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000907 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000908 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
909 }
910
Chris Lattner4d391482007-12-12 07:09:47 +0000911
912 // Check the protocol list for unimplemented methods in the @implementation
913 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000914 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
915 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
916 E = I->protocol_end(); PI != E; ++PI)
917 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
918 InsMap, ClsMap, I);
919 // Check class extensions (unnamed categories)
920 for (ObjCCategoryDecl *Categories = I->getCategoryList();
921 Categories; Categories = Categories->getNextClassCategory()) {
922 if (!Categories->getIdentifier()) {
923 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
924 break;
925 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000926 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000927 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
928 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
929 E = C->protocol_end(); PI != E; ++PI)
930 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
931 InsMap, ClsMap, C->getClassInterface());
932 } else
933 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000934}
935
936/// ActOnForwardClassDeclaration -
937Action::DeclTy *
938Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000939 IdentifierInfo **IdentList,
940 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000942
943 for (unsigned i = 0; i != NumElts; ++i) {
944 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000945 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000946 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000947 // Maybe we will complain about the shadowed template parameter.
948 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
949 // Just pretend that we didn't see the previous declaration.
950 PrevDecl = 0;
951 }
952
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000954 // GCC apparently allows the following idiom:
955 //
956 // typedef NSObject < XCElementTogglerP > XCElementToggler;
957 // @class XCElementToggler;
958 //
959 // FIXME: Make an extension?
960 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
961 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000963 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000964 }
Chris Lattner4d391482007-12-12 07:09:47 +0000965 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000967 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000968 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
969 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000970 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000971
Douglas Gregord0434102009-01-09 00:49:46 +0000972 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000973 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000974 // Remember that this needs to be removed when the scope is popped.
975 TUScope->AddDecl(IDecl);
976 }
977
978 Interfaces.push_back(IDecl);
979 }
980
Douglas Gregord0434102009-01-09 00:49:46 +0000981 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000982 &Interfaces[0],
983 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000984 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000985 CheckObjCDeclScope(CDecl);
986 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000987}
988
989
990/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
991/// returns true, or false, accordingly.
992/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000994 const ObjCMethodDecl *PrevMethod,
995 bool matchBasedOnSizeAndAlignment) {
996 QualType T1 = Context.getCanonicalType(Method->getResultType());
997 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
998
999 if (T1 != T2) {
1000 // The result types are different.
1001 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001002 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001003 // Incomplete types don't have a size and alignment.
1004 if (T1->isIncompleteType() || T2->isIncompleteType())
1005 return false;
1006 // Check is based on size and alignment.
1007 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1008 return false;
1009 }
Chris Lattner89951a82009-02-20 18:43:26 +00001010
1011 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1012 E = Method->param_end();
1013 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1014
1015 for (; ParamI != E; ++ParamI, ++PrevI) {
1016 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1017 T1 = Context.getCanonicalType((*ParamI)->getType());
1018 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001019 if (T1 != T2) {
1020 // The result types are different.
1021 if (!matchBasedOnSizeAndAlignment)
1022 return false;
1023 // Incomplete types don't have a size and alignment.
1024 if (T1->isIncompleteType() || T2->isIncompleteType())
1025 return false;
1026 // Check is based on size and alignment.
1027 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1028 return false;
1029 }
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
1031 return true;
1032}
1033
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001034void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001035 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1036 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001037 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001038 Entry.Method = Method;
1039 Entry.Next = 0;
1040 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001041 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001042
1043 // We've seen a method with this name, see if we have already seen this type
1044 // signature.
1045 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1046 if (MatchTwoMethodDeclarations(Method, List->Method))
1047 return;
1048
1049 // We have a new signature for an existing method - add it.
1050 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1051 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001052}
1053
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001054// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001055ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1056 SourceRange R) {
1057 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001058 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001059
1060 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001061 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1062 // This checks if the methods differ by size & alignment.
1063 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1064 issueWarning = true;
1065 }
1066 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001067 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001068 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001069 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001070 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001071 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001072 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001073 }
1074 return MethList.Method;
1075}
1076
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1078 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001079 if (!FirstMethod.Method) {
1080 // Haven't seen a method with this selector name yet - add it.
1081 FirstMethod.Method = Method;
1082 FirstMethod.Next = 0;
1083 } else {
1084 // We've seen a method with this name, now check the type signature(s).
1085 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1086
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001087 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001088 Next = Next->Next)
1089 match = MatchTwoMethodDeclarations(Method, Next->Method);
1090
1091 if (!match) {
1092 // We have a new signature for an existing method - add it.
1093 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001094 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001095 FirstMethod.Next = OMI;
1096 }
1097 }
1098}
1099
Steve Naroff0701bbb2009-01-08 17:28:14 +00001100/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1101/// have the property type and issue diagnostics if they don't.
1102/// Also synthesize a getter/setter method if none exist (and update the
1103/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1104/// methods is the "right" thing to do.
1105void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1106 ObjCContainerDecl *CD) {
1107 ObjCMethodDecl *GetterMethod, *SetterMethod;
1108
1109 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1110 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1111
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001112 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001113 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001114 Diag(property->getLocation(),
1115 diag::err_accessor_property_type_mismatch)
1116 << property->getDeclName()
1117 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001118 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1119 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001120
1121 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001122 if (Context.getCanonicalType(SetterMethod->getResultType())
1123 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001124 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001125 if (SetterMethod->param_size() != 1 ||
1126 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001127 Diag(property->getLocation(),
1128 diag::err_accessor_property_type_mismatch)
1129 << property->getDeclName()
1130 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001131 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1132 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001133 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001134
1135 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001136 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001137 // FIXME: The synthesized property we set here is misleading. We
1138 // almost always synthesize these methods unless the user explicitly
1139 // provided prototypes (which is odd, but allowed). Sema should be
1140 // typechecking that the declarations jive in that situation (which
1141 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001142 if (!GetterMethod) {
1143 // No instance method of same name as property getter name was found.
1144 // Declare a getter method and add it to the list of methods
1145 // for this class.
1146 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1147 property->getLocation(), property->getGetterName(),
1148 property->getType(), CD, true, false, true,
1149 (property->getPropertyImplementation() ==
1150 ObjCPropertyDecl::Optional) ?
1151 ObjCMethodDecl::Optional :
1152 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001153 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001154 } else
1155 // A user declared getter will be synthesize when @synthesize of
1156 // the property with the same name is seen in the @implementation
1157 GetterMethod->setIsSynthesized();
1158 property->setGetterMethodDecl(GetterMethod);
1159
1160 // Skip setter if property is read-only.
1161 if (!property->isReadOnly()) {
1162 // Find the default setter and if one not found, add one.
1163 if (!SetterMethod) {
1164 // No instance method of same name as property setter name was found.
1165 // Declare a setter method and add it to the list of methods
1166 // for this class.
1167 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1168 property->getLocation(),
1169 property->getSetterName(),
1170 Context.VoidTy, CD, true, false, true,
1171 (property->getPropertyImplementation() ==
1172 ObjCPropertyDecl::Optional) ?
1173 ObjCMethodDecl::Optional :
1174 ObjCMethodDecl::Required);
1175 // Invent the arguments for the setter. We don't bother making a
1176 // nice name for the argument.
1177 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1178 SourceLocation(),
1179 property->getIdentifier(),
1180 property->getType(),
1181 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001182 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001183 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001184 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001185 } else
1186 // A user declared setter will be synthesize when @synthesize of
1187 // the property with the same name is seen in the @implementation
1188 SetterMethod->setIsSynthesized();
1189 property->setSetterMethodDecl(SetterMethod);
1190 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001191 // Add any synthesized methods to the global pool. This allows us to
1192 // handle the following, which is supported by GCC (and part of the design).
1193 //
1194 // @interface Foo
1195 // @property double bar;
1196 // @end
1197 //
1198 // void thisIsUnfortunate() {
1199 // id foo;
1200 // double bar = [foo bar];
1201 // }
1202 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001203 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001204 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001205 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001206 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001207}
1208
Steve Naroffa56f6162007-12-18 01:30:32 +00001209// Note: For class/category implemenations, allMethods/allProperties is
1210// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001211void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1212 DeclTy **allMethods, unsigned allNum,
1213 DeclTy **allProperties, unsigned pNum) {
1214 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1215
Steve Naroffa56f6162007-12-18 01:30:32 +00001216 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1217 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001218 // should be true.
1219 if (!ClassDecl)
1220 return;
1221
Chris Lattner4d391482007-12-12 07:09:47 +00001222 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001223 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1224 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001225 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001226
Steve Naroff0701bbb2009-01-08 17:28:14 +00001227 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001228
1229 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1230 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1231 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1232
Chris Lattner4d391482007-12-12 07:09:47 +00001233 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 ObjCMethodDecl *Method =
1235 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001236
1237 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001238 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001239 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001240 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001241 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1242 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001243 if ((isInterfaceDeclKind && PrevMethod && !match)
1244 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001245 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001246 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001247 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001248 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001249 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001250 InsMap[Method->getSelector()] = Method;
1251 /// The following allows us to typecheck messages to "id".
1252 AddInstanceMethodToGlobalPool(Method);
1253 }
1254 }
1255 else {
1256 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001257 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001258 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1259 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001260 if ((isInterfaceDeclKind && PrevMethod && !match)
1261 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001262 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001263 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001264 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001265 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001266 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001267 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001268 /// The following allows us to typecheck messages to "Class".
1269 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001270 }
1271 }
1272 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001273 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001274 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001275 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001276 ComparePropertiesInBaseAndSuper(I);
1277 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001278 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001279 // Categories are used to extend the class by declaring new methods.
1280 // By the same token, they are also used to add new properties. No
1281 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001282
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001283 // Merge protocol properties into category
1284 MergeProtocolPropertiesIntoClass(C, C);
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001285 if (C->getIdentifier() == 0)
1286 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001287 }
Steve Naroff09c47192009-01-09 15:36:25 +00001288 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1289 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1290 // user-defined setter/getter. It also synthesizes setter/getter methods
1291 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001292 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1293 E = CDecl->prop_end(); I != E; ++I)
1294 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001295 CDecl->setAtEndLoc(AtEndLoc);
1296 }
1297 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001298 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001299 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001300 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001301 } else if (ObjCCategoryImplDecl* CatImplClass =
1302 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001303 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001304
Chris Lattner4d391482007-12-12 07:09:47 +00001305 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001306 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001307 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001308 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001309 Categories; Categories = Categories->getNextClassCategory()) {
1310 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001311 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001312 break;
1313 }
1314 }
1315 }
1316 }
1317}
1318
1319
1320/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1321/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322static Decl::ObjCDeclQualifier
1323CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1324 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1325 if (PQTVal & ObjCDeclSpec::DQ_In)
1326 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1327 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1328 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1329 if (PQTVal & ObjCDeclSpec::DQ_Out)
1330 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1331 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1332 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1333 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1334 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1335 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1336 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001337
1338 return ret;
1339}
1340
1341Sema::DeclTy *Sema::ActOnMethodDeclaration(
1342 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001343 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001345 Selector Sel,
1346 // optional arguments. The number of types/arguments is obtained
1347 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001349 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001350 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1351 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001352 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001353
1354 // Make sure we can establish a context for the method.
1355 if (!ClassDecl) {
1356 Diag(MethodLoc, diag::error_missing_method_context);
1357 return 0;
1358 }
Chris Lattner4d391482007-12-12 07:09:47 +00001359 QualType resultDeclType;
1360
Steve Naroffccef3712009-02-20 22:59:16 +00001361 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001362 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001363
1364 // Methods cannot return interface types. All ObjC objects are
1365 // passed by reference.
1366 if (resultDeclType->isObjCInterfaceType()) {
1367 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1368 << "returned";
1369 return 0;
1370 }
1371 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001372 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001373
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001374 ObjCMethodDecl* ObjCMethod =
1375 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001376 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001377 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001378 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001379 MethodDeclKind == tok::objc_optional ?
1380 ObjCMethodDecl::Optional :
1381 ObjCMethodDecl::Required);
1382
Chris Lattner0ed844b2008-04-04 06:12:32 +00001383 llvm::SmallVector<ParmVarDecl*, 16> Params;
1384
1385 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1386 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001387 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001388
Steve Naroff6082c622008-12-09 19:36:17 +00001389 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001390 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001391 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001392 if (argType->isArrayType()) { // (char *[]) -> (char **)
1393 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001394 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001395 }
Steve Naroff6082c622008-12-09 19:36:17 +00001396 else if (argType->isFunctionType())
1397 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001398 else if (argType->isObjCInterfaceType()) {
1399 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001400 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1401 << "passed";
1402 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001403 return 0;
1404 }
Steve Naroff6082c622008-12-09 19:36:17 +00001405 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001406 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001407 ParmVarDecl* Param;
1408 if (originalArgType.isNull())
1409 Param = ParmVarDecl::Create(Context, ObjCMethod,
1410 SourceLocation(/*FIXME*/),
1411 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001412 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001413 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001414 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1415 SourceLocation(/*FIXME*/),
1416 ArgNames[i], argType, originalArgType,
1417 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001418
Chris Lattner0ed844b2008-04-04 06:12:32 +00001419 Param->setObjCDeclQualifier(
1420 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1421 Params.push_back(Param);
1422 }
1423
Chris Lattner38af2de2009-02-20 21:35:13 +00001424 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001425 ObjCMethod->setObjCDeclQualifier(
1426 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1427 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001428
1429 if (AttrList)
1430 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001431
1432 // For implementations (which can be very "coarse grain"), we add the
1433 // method now. This allows the AST to implement lookup methods that work
1434 // incrementally (without waiting until we parse the @end). It also allows
1435 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001436 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001437 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001438 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001439 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001440 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001441 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001442 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001443 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001444 }
1445 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001446 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001447 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001448 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001449 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001450 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001451 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001452 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001453 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001454 }
1455 }
1456 if (PrevMethod) {
1457 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001458 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001459 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001460 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001461 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001463}
1464
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001465void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1466 SourceLocation Loc,
1467 unsigned &Attributes) {
1468 // FIXME: Improve the reported location.
1469
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001470 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001471 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001472 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1473 ObjCDeclSpec::DQ_PR_assign |
1474 ObjCDeclSpec::DQ_PR_copy |
1475 ObjCDeclSpec::DQ_PR_retain))) {
1476 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1477 "readwrite" :
1478 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1479 "assign" :
1480 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1481 "copy" : "retain";
1482
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001483 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001484 diag::err_objc_property_attr_mutually_exclusive :
1485 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001486 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001487 }
1488
1489 // Check for copy or retain on non-object types.
1490 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1491 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001492 Diag(Loc, diag::err_objc_property_requires_object)
1493 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001494 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1495 }
1496
1497 // Check for more than one of { assign, copy, retain }.
1498 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1499 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001500 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1501 << "assign" << "copy";
1502 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001503 }
1504 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001505 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1506 << "assign" << "retain";
1507 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001508 }
1509 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1510 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001511 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1512 << "copy" << "retain";
1513 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001514 }
1515 }
1516
1517 // Warn if user supplied no assignment attribute, property is
1518 // readwrite, and this is an object type.
1519 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1520 ObjCDeclSpec::DQ_PR_retain)) &&
1521 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1522 Context.isObjCObjectPointerType(PropertyTy)) {
1523 // Skip this warning in gc-only mode.
1524 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1525 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1526
1527 // If non-gc code warn that this is likely inappropriate.
1528 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1529 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1530
1531 // FIXME: Implement warning dependent on NSCopying being
1532 // implemented. See also:
1533 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1534 // (please trim this list while you are at it).
1535 }
1536}
1537
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001538Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1539 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001540 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001541 Selector GetterSel,
1542 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001543 DeclTy *ClassCategory,
1544 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001545 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001546 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001547 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1548 // default is readwrite!
1549 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1550 // property is defaulted to 'assign' if it is readwrite and is
1551 // not retain or copy
1552 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1553 (isReadWrite &&
1554 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1555 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1556 QualType T = GetTypeForDeclarator(FD.D, S);
1557 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001558
1559 // May modify Attributes.
1560 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001561
1562 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1563 if (!CDecl->getIdentifier()) {
1564 // This is an anonymous category. property requires special
1565 // handling.
1566 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1567 if (ObjCPropertyDecl *PIDecl =
1568 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1569 // property 'PIDecl's readonly attribute will be over-ridden
1570 // with anonymous category's readwrite property attribute!
1571 unsigned PIkind = PIDecl->getPropertyAttributes();
1572 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001573 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001574 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1575 Diag(AtLoc, diag::warn_property_attr_mismatch);
1576 PIDecl->makeitReadWriteAttribute();
1577 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1578 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1579 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1580 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1581 PIDecl->setSetterName(SetterSel);
1582 // FIXME: use a common routine with addPropertyMethods.
1583 ObjCMethodDecl *SetterDecl =
1584 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1585 Context.VoidTy,
1586 ICDecl,
1587 true, false, true,
1588 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001589 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001590 SourceLocation(),
1591 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001592 T, VarDecl::None, 0);
1593 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001594 PIDecl->setSetterMethodDecl(SetterDecl);
1595 }
1596 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001597 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001598 *isOverridingProperty = true;
1599 return 0;
1600 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001601 // No matching property found in the main class. Just fall thru
1602 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001603 // it works as is. This category becomes just like a category
1604 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001605 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001606 Diag(CDecl->getLocation(), diag::err_continuation_class);
1607 *isOverridingProperty = true;
1608 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001609 }
1610 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001611
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001612 Type *t = T.getTypePtr();
1613 if (t->isArrayType() || t->isFunctionType())
1614 Diag(AtLoc, diag::err_property_type) << T;
1615
Steve Naroff93983f82009-01-11 12:47:58 +00001616 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1617 assert(DC && "ClassDecl is not a DeclContext");
1618 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001619 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001620 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001621
1622 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001623
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001624 // Regardless of setter/getter attribute, we save the default getter/setter
1625 // selector names in anticipation of declaration of setter/getter methods.
1626 PDecl->setGetterName(GetterSel);
1627 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001628
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001629 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001631
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001632 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001634
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001635 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001636 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001637
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001638 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001640
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001641 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001643
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001644 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001646
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001647 if (isAssign)
1648 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1649
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001650 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001651 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001652
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001653 if (MethodImplKind == tok::objc_required)
1654 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1655 else if (MethodImplKind == tok::objc_optional)
1656 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1657
Chris Lattner4d391482007-12-12 07:09:47 +00001658 return PDecl;
1659}
1660
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001661/// ActOnPropertyImplDecl - This routine performs semantic checks and
1662/// builds the AST node for a property implementation declaration; declared
1663/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001664///
1665Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1666 SourceLocation PropertyLoc,
1667 bool Synthesize,
1668 DeclTy *ClassCatImpDecl,
1669 IdentifierInfo *PropertyId,
1670 IdentifierInfo *PropertyIvar) {
1671 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1672 // Make sure we have a context for the property implementation declaration.
1673 if (!ClassImpDecl) {
1674 Diag(AtLoc, diag::error_missing_property_context);
1675 return 0;
1676 }
1677 ObjCPropertyDecl *property = 0;
1678 ObjCInterfaceDecl* IDecl = 0;
1679 // Find the class or category class where this property must have
1680 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001681 ObjCImplementationDecl *IC = 0;
1682 ObjCCategoryImplDecl* CatImplClass = 0;
1683 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001684 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001685 // We always synthesize an interface for an implementation
1686 // without an interface decl. So, IDecl is always non-zero.
1687 assert(IDecl &&
1688 "ActOnPropertyImplDecl - @implementation without @interface");
1689
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001690 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001691 property = IDecl->FindPropertyDeclaration(PropertyId);
1692 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001693 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001694 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001695 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001696 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001697 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001698 if (Synthesize) {
1699 Diag(AtLoc, diag::error_synthesize_category_decl);
1700 return 0;
1701 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001702 IDecl = CatImplClass->getClassInterface();
1703 if (!IDecl) {
1704 Diag(AtLoc, diag::error_missing_property_interface);
1705 return 0;
1706 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001707 ObjCCategoryDecl *Category =
1708 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1709
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001710 // If category for this implementation not found, it is an error which
1711 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001712 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001713 return 0;
1714 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001715 property = Category->FindPropertyDeclaration(PropertyId);
1716 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001717 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001718 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001719 return 0;
1720 }
1721 }
1722 else {
1723 Diag(AtLoc, diag::error_bad_property_context);
1724 return 0;
1725 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001726 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001727 // Check that we have a valid, previously declared ivar for @synthesize
1728 if (Synthesize) {
1729 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001730 if (!PropertyIvar)
1731 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001732 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001733 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001734 if (!Ivar) {
Steve Narofff4c00ff2009-03-03 22:09:41 +00001735 if (!getLangOptions().ObjCNonFragileABI)
1736 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001737 return 0;
1738 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001739 QualType PropType = Context.getCanonicalType(property->getType());
1740 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1741
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001742 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001743 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001744 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001745 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001746 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001747 return 0;
1748 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001749 else {
1750 // FIXME! Rules for properties are somewhat different that those
1751 // for assignments. Use a new routine to consolidate all cases;
1752 // specifically for property redeclarations as well as for ivars.
1753 QualType lhsType =
1754 Context.getCanonicalType(PropType).getUnqualifiedType();
1755 QualType rhsType =
1756 Context.getCanonicalType(IvarType).getUnqualifiedType();
1757 if (lhsType != rhsType &&
1758 lhsType->isArithmeticType()) {
1759 Diag(PropertyLoc, diag::error_property_ivar_type)
1760 << property->getDeclName() << Ivar->getDeclName();
1761 return 0;
1762 }
Fariborz Jahanian9bc77b22009-02-27 22:38:11 +00001763 // __weak is explicit. So it works on Canonical type.
1764 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1765 Diag(PropertyLoc, diag::error_weak_property)
1766 << property->getDeclName() << Ivar->getDeclName();
1767 return 0;
1768 }
1769 if ((Context.isObjCObjectPointerType(property->getType()) ||
1770 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1771 Diag(PropertyLoc, diag::error_strong_property)
1772 << property->getDeclName() << Ivar->getDeclName();
1773 return 0;
1774 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001775 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001776 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001777 } else if (PropertyIvar) {
1778 // @dynamic
1779 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1780 return 0;
1781 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001782 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001783 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001784 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1785 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001786 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001787 ObjCPropertyImplDecl::Synthesize
1788 : ObjCPropertyImplDecl::Dynamic),
1789 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001790 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001791 if (IC) {
1792 if (Synthesize)
1793 if (ObjCPropertyImplDecl *PPIDecl =
1794 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1795 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1796 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1797 << PropertyIvar;
1798 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1799 }
1800
1801 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1802 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1803 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1804 return 0;
1805 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001806 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001807 }
1808 else {
1809 if (Synthesize)
1810 if (ObjCPropertyImplDecl *PPIDecl =
1811 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1812 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1813 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1814 << PropertyIvar;
1815 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1816 }
1817
1818 if (ObjCPropertyImplDecl *PPIDecl =
1819 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1820 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1821 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1822 return 0;
1823 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001824 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001825 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001826
1827 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001828}
Anders Carlsson15281452008-11-04 16:57:32 +00001829
Chris Lattnercc98eac2008-12-17 07:13:27 +00001830bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001831 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001832 return false;
1833
1834 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1835 D->setInvalidDecl();
1836
1837 return true;
1838}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001839
1840/// Collect the instance variables declared in an Objective-C object. Used in
1841/// the creation of structures from objects using the @defs directive.
1842/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1843/// part of the AST generation logic of @defs.
1844static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1845 ASTContext& Ctx,
1846 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1847 if (Class->getSuperClass())
1848 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1849
1850 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1851 for (ObjCInterfaceDecl::ivar_iterator
1852 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1853
1854 ObjCIvarDecl* ID = *I;
1855 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1856 ID->getLocation(),
1857 ID->getIdentifier(),
1858 ID->getType(),
1859 ID->getBitWidth()));
1860 }
1861}
1862
1863/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1864/// instance variables of ClassName into Decls.
1865void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1866 IdentifierInfo *ClassName,
1867 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1868 // Check that ClassName is a valid class
1869 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1870 if (!Class) {
1871 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1872 return;
1873 }
1874 // Collect the instance variables
1875 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1876
1877 // Introduce all of these fields into the appropriate scope.
1878 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1879 D != Decls.end(); ++D) {
1880 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1881 if (getLangOptions().CPlusPlus)
1882 PushOnScopeChains(cast<FieldDecl>(FD), S);
1883 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001884 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001885 }
1886}
1887