blob: 19a2a636db8d50efc12c6cc2dde18395c779c421 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner855e51f2007-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 Naroff58d0d6f2009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroff8df46022009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Steve Naroff8df46022009-02-28 16:59:13 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-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 Narofffe9eb6a2007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
Steve Naroff313c4162009-02-28 16:48:43 +000040 ActiveScope = FnBodyScope;
41
Chris Lattner855e51f2007-12-12 07:09:47 +000042 // Create Decl objects for each parameter, entrring them in the scope for
43 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000047
Daniel Dunbareaf91c32008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000050
Chris Lattner97316c02008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-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 Lattner855e51f2007-12-12 07:09:47 +000056}
57
Chris Lattnere705e5e2008-07-21 22:17:28 +000058Sema::DeclTy *Sema::
59ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000062 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-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 Kremenek42730c52008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000078 }
79
Ted Kremenek42730c52008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroff1422a622008-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 Lattner855e51f2007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000094 }
Chris Lattner5cece462008-07-21 07:06:49 +000095 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000103 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-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 Lattner855e51f2007-12-12 07:09:47 +0000109 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000110 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000111
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000112 ObjCInterfaceDecl *SuperClassDecl =
113 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000114
115 // Diagnose classes that inherit from deprecated classes.
116 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000117 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000118
Steve Naroffcb3beaf2009-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 Lattner7699a532009-02-16 21:33:09 +0000129
Steve Naroffcb3beaf2009-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 Lattner7699a532009-02-16 21:33:09 +0000140
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000141 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
142 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000144 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000145 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000146 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000147 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000148 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000149 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000150 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000151 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000152 IDecl->setLocEnd(SuperLoc);
153 } else { // we have a root class.
154 IDecl->setLocEnd(ClassLoc);
155 }
156
Steve Naroff1422a622008-11-18 19:15:30 +0000157 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000158 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000159 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
160 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 IDecl->setLocEnd(EndProtoLoc);
162 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000163
164 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000165 return IDecl;
166}
167
168/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000169/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000170Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
171 IdentifierInfo *AliasName,
172 SourceLocation AliasLocation,
173 IdentifierInfo *ClassName,
174 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000176 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000178 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000180 else
Chris Lattner65cae292008-11-19 08:23:25 +0000181 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000182 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 return 0;
184 }
185 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000186 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-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 Gregor52ae30c2009-01-30 01:04:22 +0000192 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000193 }
194 }
195 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000196 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
197 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000198 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000199 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000200 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 return 0;
202 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000203
204 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000205 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000206 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000207
208 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000209
210 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000211 CurContext->addDecl(AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000212 if (!CheckObjCDeclScope(AliasDecl))
213 TUScope->AddDecl(AliasDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000214
Chris Lattner855e51f2007-12-12 07:09:47 +0000215 return AliasDecl;
216}
217
Chris Lattner2bdedd62008-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 Dunbar28680d12008-09-26 04:48:09 +0000224 SourceLocation EndProtoLoc,
225 AttributeList *AttrList) {
226 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000227 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000228 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000229 if (PDecl) {
230 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000231 if (!PDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +0000232 PDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000233 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000234 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-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 Lattner855e51f2007-12-12 07:09:47 +0000238 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000239 // Make sure the cached decl gets a valid start location.
240 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000241 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000242 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000243 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
244 AtProtoInterfaceLoc,ProtocolName);
245 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000246 CurContext->addDecl(PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000247 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000249 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000250 if (AttrList)
251 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000252 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000253 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000254 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000255 PDecl->setLocEnd(EndProtoLoc);
256 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000257
258 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000259 return PDecl;
260}
261
262/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-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 Lattner855e51f2007-12-12 07:09:47 +0000265void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000266Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000267 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000268 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000269 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000270 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000271 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
272 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000273 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000274 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000275 continue;
276 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000277
Douglas Gregoraa57e862009-02-18 21:56:37 +0000278 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-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 Lattner8ba580c2008-11-19 05:08:23 +0000283 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000284 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000285 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000286 }
287}
288
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000289/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000290/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000291///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000292void
293Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
294 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000295 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-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 Lattner8ba580c2008-11-19 05:08:23 +0000302 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000303 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000304 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
305 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000306 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000307 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
309 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000310 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000311 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000312
313 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
314 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000315 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000316 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000317 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000318 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000319 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000320 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000322 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000323
Chris Lattnerc5cff302008-07-26 20:50:02 +0000324 if (Context.getCanonicalType(Property->getType()) !=
325 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000326 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000327 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000328
329}
330
331/// ComparePropertiesInBaseAndSuper - This routine compares property
332/// declarations in base and its super class, if any, and issues
333/// diagnostics in a variety of inconsistant situations.
334///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000335void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000336 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
337 if (!SDecl)
338 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000339 // FIXME: O(N^2)
Steve Naroff451f83c2009-01-09 15:36:25 +0000340 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
341 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000342 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000343 // Does property in super class has declaration in current class?
Steve Naroff451f83c2009-01-09 15:36:25 +0000344 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
345 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000346 ObjCPropertyDecl *PDecl = (*I);
347 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000348 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000349 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000350 }
351 }
352}
353
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000354/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
355/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000356/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000357void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000358Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000359 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000360 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
361 if (!IDecl) {
362 // Category
363 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
364 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff451f83c2009-01-09 15:36:25 +0000365 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
366 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000367 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000368 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000369 // Is this property already in category's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000370 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000371 CP != CE; ++CP)
372 if ((*CP)->getIdentifier() == Pr->getIdentifier())
373 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000374 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000375 // Property protocol already exist in class. Diagnose any mismatch.
376 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
377 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000378 return;
379 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000380 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
381 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000382 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000383 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000384 // Is this property already in class's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000385 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000386 CP != CE; ++CP)
387 if ((*CP)->getIdentifier() == Pr->getIdentifier())
388 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000389 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000390 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000391 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000392 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000393}
394
395/// MergeProtocolPropertiesIntoClass - This routine merges properties
396/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000397/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000398///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000399void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
400 DeclTy *MergeItsProtocols) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000401 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000402 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
403
404 if (!IDecl) {
405 // Category
406 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
407 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
408 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
409 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
410 E = MDecl->protocol_end(); P != E; ++P)
411 // Merge properties of category (*P) into IDECL's
412 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
413
414 // Go thru the list of protocols for this category and recursively merge
415 // their properties into this class as well.
416 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
417 E = CatDecl->protocol_end(); P != E; ++P)
418 MergeProtocolPropertiesIntoClass(CatDecl, *P);
419 } else {
420 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
421 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
422 E = MD->protocol_end(); P != E; ++P)
423 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
424 }
425 return;
426 }
427
Chris Lattner5cece462008-07-21 07:06:49 +0000428 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000429 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
430 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000431 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000432 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
433
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000434 // Go thru the list of protocols for this class and recursively merge
435 // their properties into this class as well.
436 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
437 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000438 MergeProtocolPropertiesIntoClass(IDecl, *P);
439 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000440 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
441 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
442 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000443 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000444 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000445}
446
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000447/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000448/// a class method in its extension.
449///
450void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
451 ObjCInterfaceDecl *ID) {
452 if (!ID)
453 return; // Possibly due to previous error
454
455 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
456 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
457 e = ID->meth_end(); i != e; ++i) {
458 ObjCMethodDecl *MD = *i;
459 MethodMap[MD->getSelector()] = MD;
460 }
461
462 if (MethodMap.empty())
463 return;
464 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
465 e = CAT->meth_end(); i != e; ++i) {
466 ObjCMethodDecl *Method = *i;
467 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
468 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
469 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
470 << Method->getDeclName();
471 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
472 }
473 }
474}
475
Chris Lattner855e51f2007-12-12 07:09:47 +0000476/// ActOnForwardProtocolDeclaration -
477Action::DeclTy *
478Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000479 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000480 unsigned NumElts,
481 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000483
484 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000485 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000486 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000487 if (PDecl == 0) { // Not already seen?
488 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
489 IdentList[i].second, Ident);
490 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000491 CurContext->addDecl(PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000492 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000493 if (attrList)
494 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 Protocols.push_back(PDecl);
496 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000497
498 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000499 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000500 &Protocols[0], Protocols.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000501 CurContext->addDecl(PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000502 CheckObjCDeclScope(PDecl);
503 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000504}
505
Chris Lattnere705e5e2008-07-21 22:17:28 +0000506Sema::DeclTy *Sema::
507ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
508 IdentifierInfo *ClassName, SourceLocation ClassLoc,
509 IdentifierInfo *CategoryName,
510 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000511 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000512 unsigned NumProtoRefs,
513 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000514 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000515 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
516 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000517 CurContext->addDecl(CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000518
519 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000520 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000521 if (!IDecl || IDecl->isForwardDecl()) {
522 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000523 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner1fa7f622009-02-16 21:26:43 +0000524 return CDecl;
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000525 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000526
Chris Lattner1fa7f622009-02-16 21:26:43 +0000527 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000528
529 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000530 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000531
532 /// Check for duplicate interface declaration for this category
533 ObjCCategoryDecl *CDeclChain;
534 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
535 CDeclChain = CDeclChain->getNextClassCategory()) {
536 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
537 Diag(CategoryLoc, diag::warn_dup_category_def)
538 << ClassName << CategoryName;
539 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
540 break;
541 }
542 }
543 if (!CDeclChain)
544 CDecl->insertNextClassCategory();
545
Chris Lattner855e51f2007-12-12 07:09:47 +0000546 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000547 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000548 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000549 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000550
551 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 return CDecl;
553}
554
555/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000556/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000557/// object.
558Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
559 SourceLocation AtCatImplLoc,
560 IdentifierInfo *ClassName, SourceLocation ClassLoc,
561 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000562 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000563 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000564 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
565 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000566 /// Check that class of this category is already completely declared.
567 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000568 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000569
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000570 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000571 CurContext->addDecl(CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000572
Chris Lattner855e51f2007-12-12 07:09:47 +0000573 /// TODO: Check that CatName, category name, is not used in another
574 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000575 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000576
577 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000578 return CDecl;
579}
580
581Sema::DeclTy *Sema::ActOnStartClassImplementation(
582 SourceLocation AtClassImplLoc,
583 IdentifierInfo *ClassName, SourceLocation ClassLoc,
584 IdentifierInfo *SuperClassname,
585 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000588 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000591 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000592 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000593 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000596 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000597 }
598
599 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000600 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 if (SuperClassname) {
602 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000603 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000605 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
606 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000607 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000608 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000609 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000610 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000611 Diag(SuperClassLoc, diag::err_undef_superclass)
612 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000613 else if (IDecl && IDecl->getSuperClass() != SDecl) {
614 // This implementation and its interface do not have the same
615 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000616 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000617 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000618 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000619 }
620 }
621 }
622
623 if (!IDecl) {
624 // Legacy case of @implementation with no corresponding @interface.
625 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000626
627 // FIXME: Do we support attributes on the @implementation? If so
628 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000629 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
630 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000631 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000632 IDecl->setSuperClass(SDecl);
633 IDecl->setLocEnd(ClassLoc);
634
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000635 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000636 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000637 // Remember that this needs to be removed when the scope is popped.
638 TUScope->AddDecl(IDecl);
639 }
640
Ted Kremenek42730c52008-01-07 19:49:32 +0000641 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000642 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000643 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000644
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000645 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000646 CurContext->addDecl(IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000647
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000648 if (CheckObjCDeclScope(IMPDecl))
649 return IMPDecl;
650
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000652 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000653 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000654 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000655 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000656 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000657 return IMPDecl;
658}
659
Ted Kremenek42730c52008-01-07 19:49:32 +0000660void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
661 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 SourceLocation RBrace) {
663 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000664 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000665 if (!IDecl)
666 return;
667 /// Check case of non-existing @interface decl.
668 /// (legacy objective-c @implementation decl without an @interface decl).
669 /// Add implementations's ivar to the synthesize class's ivar list.
670 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000671 IDecl->setIVarList(ivars, numIvars, Context);
672 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000673 return;
674 }
675 // If implementation has empty ivar list, just return.
676 if (numIvars == 0)
677 return;
678
679 assert(ivars && "missing @implementation ivars");
680
681 // Check interface's Ivar list against those in the implementation.
682 // names and types must match.
683 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000684 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000686 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
687 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000688 ObjCIvarDecl* ImplIvar = ivars[j++];
689 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 assert (ImplIvar && "missing implementation ivar");
691 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000692
693 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000694 if (Context.getCanonicalType(ImplIvar->getType()) !=
695 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000696 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000697 << ImplIvar->getIdentifier()
698 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000699 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000700 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
701 Expr *ImplBitWidth = ImplIvar->getBitWidth();
702 Expr *ClsBitWidth = ClsIvar->getBitWidth();
703 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
704 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
705 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
706 << ImplIvar->getIdentifier();
707 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
708 }
709 }
710 // Make sure the names are identical.
711 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000712 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000713 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000714 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000715 }
716 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000717 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000718
719 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000720 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000721 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000722 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000723}
724
Steve Naroffb4f48512008-02-10 21:38:56 +0000725void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
726 bool &IncompleteImpl) {
727 if (!IncompleteImpl) {
728 Diag(ImpLoc, diag::warn_incomplete_impl);
729 IncompleteImpl = true;
730 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000731 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000732}
733
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000734void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
735 ObjCMethodDecl *IntfMethodDecl) {
736 bool err = false;
737 QualType ImpMethodQType =
738 Context.getCanonicalType(ImpMethodDecl->getResultType());
739 QualType IntfMethodQType =
740 Context.getCanonicalType(IntfMethodDecl->getResultType());
741 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
742 err = true;
743 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
744 IF=IntfMethodDecl->param_begin(),
745 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
746 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
747 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
748 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
749 err = true;
750 break;
751 }
752 }
753 if (err) {
754 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
755 << ImpMethodDecl->getDeclName();
756 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
757 }
758}
759
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000760/// isPropertyReadonly - Return true if property is readonly, by searching
761/// for the property in the class and in its categories and implementations
762///
763bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000764 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000765 // by far the most common case.
766 if (!PDecl->isReadOnly())
767 return false;
768 // Even if property is ready only, if interface has a user defined setter,
769 // it is not considered read only.
770 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
771 return false;
772
773 // Main class has the property as 'readonly'. Must search
774 // through the category list to see if the property's
775 // attribute has been over-ridden to 'readwrite'.
776 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
777 Category; Category = Category->getNextClassCategory()) {
778 // Even if property is ready only, if a category has a user defined setter,
779 // it is not considered read only.
780 if (Category->getInstanceMethod(PDecl->getSetterName()))
781 return false;
782 ObjCPropertyDecl *P =
783 Category->FindPropertyDeclaration(PDecl->getIdentifier());
784 if (P && !P->isReadOnly())
785 return false;
786 }
787
788 // Also, check for definition of a setter method in the implementation if
789 // all else failed.
790 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
791 if (ObjCImplementationDecl *IMD =
792 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
793 if (IMD->getInstanceMethod(PDecl->getSetterName()))
794 return false;
795 }
796 else if (ObjCCategoryImplDecl *CIMD =
797 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
798 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
799 return false;
800 }
801 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000802 // Lastly, look through the implementation (if one is in scope).
803 if (ObjCImplementationDecl *ImpDecl =
804 ObjCImplementations[IDecl->getIdentifier()])
805 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
806 return false;
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000807 return true;
808}
809
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000810/// FIXME: Type hierarchies in Objective-C can be deep. We could most
811/// likely improve the efficiency of selector lookups and type
812/// checking by associating with each protocol / interface / category
813/// the flattened instance tables. If we used an immutable set to keep
814/// the table then it wouldn't add significant memory cost and it
815/// would be handy for lookups.
816
Steve Naroffb268d2a2008-02-08 22:06:17 +0000817/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000818/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000819void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
820 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000821 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000822 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000823 const llvm::DenseSet<Selector> &ClsMap,
824 ObjCInterfaceDecl *IDecl) {
825 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
826
827 // If a method lookup fails locally we still need to look and see if
828 // the method was implemented by a base class or an inherited
829 // protocol. This lookup is slow, but occurs rarely in correct code
830 // and otherwise would terminate in a warning.
831
Chris Lattner855e51f2007-12-12 07:09:47 +0000832 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000833 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000834 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000836 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000837 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000838 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000839 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000840 }
841 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000843 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000845 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
846 !ClsMap.count(method->getSelector()) &&
847 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000848 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000849 }
Chris Lattner0be08822008-07-21 21:32:27 +0000850 // Check on this protocols's referenced protocols, recursively.
851 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
852 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000853 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000854}
855
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000856void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
857 ObjCContainerDecl* CDecl,
858 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000859 llvm::DenseSet<Selector> InsMap;
860 // Check and see if instance methods in class interface have been
861 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000862 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000863 E = IMPDecl->instmeth_end(); I != E; ++I)
864 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000865
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000866 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
867 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000868 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000869 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000870 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000871 }
Chris Lattner28400082009-02-16 19:25:52 +0000872
873 ObjCMethodDecl *ImpMethodDecl =
874 IMPDecl->getInstanceMethod((*I)->getSelector());
875 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000876 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000877 assert(IntfMethodDecl &&
878 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
879 // ImpMethodDecl may be null as in a @dynamic property.
880 if (ImpMethodDecl)
881 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
882 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000883
Chris Lattner855e51f2007-12-12 07:09:47 +0000884 llvm::DenseSet<Selector> ClsMap;
885 // Check and see if class methods in class interface have been
886 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000888 E = IMPDecl->classmeth_end(); I != E; ++I)
889 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000890
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000891 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
892 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000893 if (!ClsMap.count((*I)->getSelector()))
894 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000895 else {
896 ObjCMethodDecl *ImpMethodDecl =
897 IMPDecl->getClassMethod((*I)->getSelector());
898 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000899 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000900 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
901 }
902
Chris Lattner855e51f2007-12-12 07:09:47 +0000903
904 // Check the protocol list for unimplemented methods in the @implementation
905 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000906 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
907 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
908 E = I->protocol_end(); PI != E; ++PI)
909 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
910 InsMap, ClsMap, I);
911 // Check class extensions (unnamed categories)
912 for (ObjCCategoryDecl *Categories = I->getCategoryList();
913 Categories; Categories = Categories->getNextClassCategory()) {
914 if (!Categories->getIdentifier()) {
915 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
916 break;
917 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000918 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000919 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
920 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
921 E = C->protocol_end(); PI != E; ++PI)
922 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
923 InsMap, ClsMap, C->getClassInterface());
924 } else
925 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +0000926}
927
928/// ActOnForwardClassDeclaration -
929Action::DeclTy *
930Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000931 IdentifierInfo **IdentList,
932 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000934
935 for (unsigned i = 0; i != NumElts; ++i) {
936 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000937 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000938 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000939 // Maybe we will complain about the shadowed template parameter.
940 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
941 // Just pretend that we didn't see the previous declaration.
942 PrevDecl = 0;
943 }
944
Ted Kremenek42730c52008-01-07 19:49:32 +0000945 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000946 // GCC apparently allows the following idiom:
947 //
948 // typedef NSObject < XCElementTogglerP > XCElementToggler;
949 // @class XCElementToggler;
950 //
951 // FIXME: Make an extension?
952 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
953 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000954 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000955 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000956 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000957 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000958 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000959 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000960 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
961 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000962 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000963
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000964 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000965 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000966 // Remember that this needs to be removed when the scope is popped.
967 TUScope->AddDecl(IDecl);
968 }
969
970 Interfaces.push_back(IDecl);
971 }
972
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000973 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000974 &Interfaces[0],
975 Interfaces.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000976 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000977 CheckObjCDeclScope(CDecl);
978 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000979}
980
981
982/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
983/// returns true, or false, accordingly.
984/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000985bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000986 const ObjCMethodDecl *PrevMethod,
987 bool matchBasedOnSizeAndAlignment) {
988 QualType T1 = Context.getCanonicalType(Method->getResultType());
989 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
990
991 if (T1 != T2) {
992 // The result types are different.
993 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000994 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000995 // Incomplete types don't have a size and alignment.
996 if (T1->isIncompleteType() || T2->isIncompleteType())
997 return false;
998 // Check is based on size and alignment.
999 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1000 return false;
1001 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001002
1003 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1004 E = Method->param_end();
1005 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1006
1007 for (; ParamI != E; ++ParamI, ++PrevI) {
1008 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1009 T1 = Context.getCanonicalType((*ParamI)->getType());
1010 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001011 if (T1 != T2) {
1012 // The result types are different.
1013 if (!matchBasedOnSizeAndAlignment)
1014 return false;
1015 // Incomplete types don't have a size and alignment.
1016 if (T1->isIncompleteType() || T2->isIncompleteType())
1017 return false;
1018 // Check is based on size and alignment.
1019 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1020 return false;
1021 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001022 }
1023 return true;
1024}
1025
Ted Kremenek42730c52008-01-07 19:49:32 +00001026void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1027 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001028 if (!FirstMethod.Method) {
1029 // Haven't seen a method with this selector name yet - add it.
1030 FirstMethod.Method = Method;
1031 FirstMethod.Next = 0;
1032 } else {
1033 // We've seen a method with this name, now check the type signature(s).
1034 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1035
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001037 Next = Next->Next)
1038 match = MatchTwoMethodDeclarations(Method, Next->Method);
1039
1040 if (!match) {
1041 // We have a new signature for an existing method - add it.
1042 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +00001043 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +00001044 }
1045 }
1046}
1047
Steve Naroffec7e0882008-10-21 10:50:19 +00001048// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001049ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1050 SourceRange R) {
1051 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001052 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001053
1054 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001055 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1056 // This checks if the methods differ by size & alignment.
1057 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1058 issueWarning = true;
1059 }
1060 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001061 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001062 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001063 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001064 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001065 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001066 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001067 }
1068 return MethList.Method;
1069}
1070
Ted Kremenek42730c52008-01-07 19:49:32 +00001071void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1072 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001073 if (!FirstMethod.Method) {
1074 // Haven't seen a method with this selector name yet - add it.
1075 FirstMethod.Method = Method;
1076 FirstMethod.Next = 0;
1077 } else {
1078 // We've seen a method with this name, now check the type signature(s).
1079 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1080
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001082 Next = Next->Next)
1083 match = MatchTwoMethodDeclarations(Method, Next->Method);
1084
1085 if (!match) {
1086 // We have a new signature for an existing method - add it.
1087 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001088 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001089 FirstMethod.Next = OMI;
1090 }
1091 }
1092}
1093
Steve Naroffab63fd62009-01-08 17:28:14 +00001094/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1095/// have the property type and issue diagnostics if they don't.
1096/// Also synthesize a getter/setter method if none exist (and update the
1097/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1098/// methods is the "right" thing to do.
1099void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1100 ObjCContainerDecl *CD) {
1101 ObjCMethodDecl *GetterMethod, *SetterMethod;
1102
1103 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1104 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1105
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001106 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001107 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001108 Diag(property->getLocation(),
1109 diag::err_accessor_property_type_mismatch)
1110 << property->getDeclName()
1111 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001112 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1113 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001114
1115 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001116 if (Context.getCanonicalType(SetterMethod->getResultType())
1117 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001118 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001119 if (SetterMethod->param_size() != 1 ||
1120 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001121 Diag(property->getLocation(),
1122 diag::err_accessor_property_type_mismatch)
1123 << property->getDeclName()
1124 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001125 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1126 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001127 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001128
1129 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001130 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001131 // FIXME: The synthesized property we set here is misleading. We
1132 // almost always synthesize these methods unless the user explicitly
1133 // provided prototypes (which is odd, but allowed). Sema should be
1134 // typechecking that the declarations jive in that situation (which
1135 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001136 if (!GetterMethod) {
1137 // No instance method of same name as property getter name was found.
1138 // Declare a getter method and add it to the list of methods
1139 // for this class.
1140 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1141 property->getLocation(), property->getGetterName(),
1142 property->getType(), CD, true, false, true,
1143 (property->getPropertyImplementation() ==
1144 ObjCPropertyDecl::Optional) ?
1145 ObjCMethodDecl::Optional :
1146 ObjCMethodDecl::Required);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001147 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001148 } else
1149 // A user declared getter will be synthesize when @synthesize of
1150 // the property with the same name is seen in the @implementation
1151 GetterMethod->setIsSynthesized();
1152 property->setGetterMethodDecl(GetterMethod);
1153
1154 // Skip setter if property is read-only.
1155 if (!property->isReadOnly()) {
1156 // Find the default setter and if one not found, add one.
1157 if (!SetterMethod) {
1158 // No instance method of same name as property setter name was found.
1159 // Declare a setter method and add it to the list of methods
1160 // for this class.
1161 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1162 property->getLocation(),
1163 property->getSetterName(),
1164 Context.VoidTy, CD, true, false, true,
1165 (property->getPropertyImplementation() ==
1166 ObjCPropertyDecl::Optional) ?
1167 ObjCMethodDecl::Optional :
1168 ObjCMethodDecl::Required);
1169 // Invent the arguments for the setter. We don't bother making a
1170 // nice name for the argument.
1171 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1172 SourceLocation(),
1173 property->getIdentifier(),
1174 property->getType(),
1175 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001176 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001177 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001178 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001179 } else
1180 // A user declared setter will be synthesize when @synthesize of
1181 // the property with the same name is seen in the @implementation
1182 SetterMethod->setIsSynthesized();
1183 property->setSetterMethodDecl(SetterMethod);
1184 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001185 // Add any synthesized methods to the global pool. This allows us to
1186 // handle the following, which is supported by GCC (and part of the design).
1187 //
1188 // @interface Foo
1189 // @property double bar;
1190 // @end
1191 //
1192 // void thisIsUnfortunate() {
1193 // id foo;
1194 // double bar = [foo bar];
1195 // }
1196 //
Douglas Gregord1675382009-01-09 19:42:16 +00001197 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001198 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001199 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001200 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001201}
1202
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001203// Note: For class/category implemenations, allMethods/allProperties is
1204// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001205void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1206 DeclTy **allMethods, unsigned allNum,
1207 DeclTy **allProperties, unsigned pNum) {
1208 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1209
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001210 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1211 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001212 // should be true.
1213 if (!ClassDecl)
1214 return;
1215
Chris Lattner855e51f2007-12-12 07:09:47 +00001216 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001217 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1218 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001219 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001220
Steve Naroffab63fd62009-01-08 17:28:14 +00001221 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001222
1223 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1224 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1225 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1226
Chris Lattner855e51f2007-12-12 07:09:47 +00001227 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001228 ObjCMethodDecl *Method =
1229 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001230
1231 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001232 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001233 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001234 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001235 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1236 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001237 if ((isInterfaceDeclKind && PrevMethod && !match)
1238 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001239 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001240 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001241 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001242 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001243 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001244 InsMap[Method->getSelector()] = Method;
1245 /// The following allows us to typecheck messages to "id".
1246 AddInstanceMethodToGlobalPool(Method);
1247 }
1248 }
1249 else {
1250 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001251 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001252 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1253 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001254 if ((isInterfaceDeclKind && PrevMethod && !match)
1255 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001256 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001257 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001258 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001259 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001260 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001261 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001262 /// The following allows us to typecheck messages to "Class".
1263 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001264 }
1265 }
1266 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001268 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001269 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001270 ComparePropertiesInBaseAndSuper(I);
1271 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff451f83c2009-01-09 15:36:25 +00001272 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001273 // Categories are used to extend the class by declaring new methods.
1274 // By the same token, they are also used to add new properties. No
1275 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001276
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001277 // Merge protocol properties into category
1278 MergeProtocolPropertiesIntoClass(C, C);
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001279 if (C->getIdentifier() == 0)
1280 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001281 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001282 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1283 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1284 // user-defined setter/getter. It also synthesizes setter/getter methods
1285 // and adds them to the DeclContext and global method pools.
Chris Lattner22063402009-02-16 18:32:47 +00001286 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1287 E = CDecl->prop_end(); I != E; ++I)
1288 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001289 CDecl->setAtEndLoc(AtEndLoc);
1290 }
1291 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001292 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001293 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001294 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001295 } else if (ObjCCategoryImplDecl* CatImplClass =
1296 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001297 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001298
Chris Lattner855e51f2007-12-12 07:09:47 +00001299 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001300 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001301 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001302 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001303 Categories; Categories = Categories->getNextClassCategory()) {
1304 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001305 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001306 break;
1307 }
1308 }
1309 }
1310 }
1311}
1312
1313
1314/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1315/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001316static Decl::ObjCDeclQualifier
1317CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1318 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1319 if (PQTVal & ObjCDeclSpec::DQ_In)
1320 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1321 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1322 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1323 if (PQTVal & ObjCDeclSpec::DQ_Out)
1324 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1325 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1326 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1327 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1328 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1329 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1330 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001331
1332 return ret;
1333}
1334
1335Sema::DeclTy *Sema::ActOnMethodDeclaration(
1336 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001337 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001338 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001339 Selector Sel,
1340 // optional arguments. The number of types/arguments is obtained
1341 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001342 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001343 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001344 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1345 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001346 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001347
1348 // Make sure we can establish a context for the method.
1349 if (!ClassDecl) {
1350 Diag(MethodLoc, diag::error_missing_method_context);
1351 return 0;
1352 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001353 QualType resultDeclType;
1354
Steve Naroffa442ad92009-02-20 22:59:16 +00001355 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001356 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001357
1358 // Methods cannot return interface types. All ObjC objects are
1359 // passed by reference.
1360 if (resultDeclType->isObjCInterfaceType()) {
1361 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1362 << "returned";
1363 return 0;
1364 }
1365 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001366 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001367
Chris Lattner114add62008-03-16 00:49:28 +00001368 ObjCMethodDecl* ObjCMethod =
1369 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001370 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001371 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001372 false,
Chris Lattner114add62008-03-16 00:49:28 +00001373 MethodDeclKind == tok::objc_optional ?
1374 ObjCMethodDecl::Optional :
1375 ObjCMethodDecl::Required);
1376
Chris Lattnereee57c02008-04-04 06:12:32 +00001377 llvm::SmallVector<ParmVarDecl*, 16> Params;
1378
1379 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1380 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001381 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001382
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001383 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001384 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001385 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001386 if (argType->isArrayType()) { // (char *[]) -> (char **)
1387 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001388 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001389 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001390 else if (argType->isFunctionType())
1391 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001392 else if (argType->isObjCInterfaceType()) {
1393 // FIXME! provide more precise location for the parameter
Steve Naroffa442ad92009-02-20 22:59:16 +00001394 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1395 << "passed";
1396 ObjCMethod->setInvalidDecl();
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001397 return 0;
1398 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001399 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001400 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001401 ParmVarDecl* Param;
1402 if (originalArgType.isNull())
1403 Param = ParmVarDecl::Create(Context, ObjCMethod,
1404 SourceLocation(/*FIXME*/),
1405 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001406 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001407 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001408 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1409 SourceLocation(/*FIXME*/),
1410 ArgNames[i], argType, originalArgType,
1411 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001412
Chris Lattnereee57c02008-04-04 06:12:32 +00001413 Param->setObjCDeclQualifier(
1414 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1415 Params.push_back(Param);
1416 }
1417
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001418 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001419 ObjCMethod->setObjCDeclQualifier(
1420 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1421 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001422
1423 if (AttrList)
1424 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001425
1426 // For implementations (which can be very "coarse grain"), we add the
1427 // method now. This allows the AST to implement lookup methods that work
1428 // incrementally (without waiting until we parse the @end). It also allows
1429 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001430 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001431 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001432 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001433 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001434 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001435 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001436 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001437 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001438 }
1439 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001440 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001441 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001442 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001443 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001445 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001446 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001447 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001448 }
1449 }
1450 if (PrevMethod) {
1451 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001452 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001453 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001454 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001455 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001456 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001457}
1458
Daniel Dunbar540ff472008-09-23 21:53:23 +00001459void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1460 SourceLocation Loc,
1461 unsigned &Attributes) {
1462 // FIXME: Improve the reported location.
1463
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001464 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001465 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001466 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1467 ObjCDeclSpec::DQ_PR_assign |
1468 ObjCDeclSpec::DQ_PR_copy |
1469 ObjCDeclSpec::DQ_PR_retain))) {
1470 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1471 "readwrite" :
1472 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1473 "assign" :
1474 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1475 "copy" : "retain";
1476
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001477 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001478 diag::err_objc_property_attr_mutually_exclusive :
1479 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001480 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001481 }
1482
1483 // Check for copy or retain on non-object types.
1484 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1485 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001486 Diag(Loc, diag::err_objc_property_requires_object)
1487 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001488 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1489 }
1490
1491 // Check for more than one of { assign, copy, retain }.
1492 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1493 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001494 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1495 << "assign" << "copy";
1496 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001497 }
1498 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001499 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1500 << "assign" << "retain";
1501 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001502 }
1503 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1504 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001505 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1506 << "copy" << "retain";
1507 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001508 }
1509 }
1510
1511 // Warn if user supplied no assignment attribute, property is
1512 // readwrite, and this is an object type.
1513 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1514 ObjCDeclSpec::DQ_PR_retain)) &&
1515 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1516 Context.isObjCObjectPointerType(PropertyTy)) {
1517 // Skip this warning in gc-only mode.
1518 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1519 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1520
1521 // If non-gc code warn that this is likely inappropriate.
1522 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1523 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1524
1525 // FIXME: Implement warning dependent on NSCopying being
1526 // implemented. See also:
1527 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1528 // (please trim this list while you are at it).
1529 }
1530}
1531
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001532Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1533 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001534 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001535 Selector GetterSel,
1536 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001537 DeclTy *ClassCategory,
1538 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001539 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001540 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001541 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1542 // default is readwrite!
1543 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1544 // property is defaulted to 'assign' if it is readwrite and is
1545 // not retain or copy
1546 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1547 (isReadWrite &&
1548 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1549 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1550 QualType T = GetTypeForDeclarator(FD.D, S);
1551 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001552
1553 // May modify Attributes.
1554 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001555
1556 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1557 if (!CDecl->getIdentifier()) {
1558 // This is an anonymous category. property requires special
1559 // handling.
1560 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1561 if (ObjCPropertyDecl *PIDecl =
1562 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1563 // property 'PIDecl's readonly attribute will be over-ridden
1564 // with anonymous category's readwrite property attribute!
1565 unsigned PIkind = PIDecl->getPropertyAttributes();
1566 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001567 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001568 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1569 Diag(AtLoc, diag::warn_property_attr_mismatch);
1570 PIDecl->makeitReadWriteAttribute();
1571 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1572 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1573 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1574 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1575 PIDecl->setSetterName(SetterSel);
1576 // FIXME: use a common routine with addPropertyMethods.
1577 ObjCMethodDecl *SetterDecl =
1578 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1579 Context.VoidTy,
1580 ICDecl,
1581 true, false, true,
1582 ObjCMethodDecl::Required);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001583 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001584 SourceLocation(),
1585 FD.D.getIdentifier(),
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001586 T, VarDecl::None, 0);
1587 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001588 PIDecl->setSetterMethodDecl(SetterDecl);
1589 }
1590 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001591 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001592 *isOverridingProperty = true;
1593 return 0;
1594 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001595 // No matching property found in the main class. Just fall thru
1596 // and add property to the anonymous category. It looks like
Ben Laurie74d601e2009-02-16 09:18:41 +00001597 // it works as is. This category becomes just like a category
1598 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001599 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001600 Diag(CDecl->getLocation(), diag::err_continuation_class);
1601 *isOverridingProperty = true;
1602 return 0;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001603 }
1604 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001605
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001606 Type *t = T.getTypePtr();
1607 if (t->isArrayType() || t->isFunctionType())
1608 Diag(AtLoc, diag::err_property_type) << T;
1609
Steve Naroffdcf1e842009-01-11 12:47:58 +00001610 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1611 assert(DC && "ClassDecl is not a DeclContext");
1612 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001613 FD.D.getIdentifier(), T);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001614 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001615
1616 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001617
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001618 // Regardless of setter/getter attribute, we save the default getter/setter
1619 // selector names in anticipation of declaration of setter/getter methods.
1620 PDecl->setGetterName(GetterSel);
1621 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001622
Daniel Dunbar540ff472008-09-23 21:53:23 +00001623 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001625
Daniel Dunbar540ff472008-09-23 21:53:23 +00001626 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001628
Daniel Dunbar540ff472008-09-23 21:53:23 +00001629 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001631
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001632 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001634
Daniel Dunbar540ff472008-09-23 21:53:23 +00001635 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001636 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001637
Daniel Dunbar540ff472008-09-23 21:53:23 +00001638 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001640
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001641 if (isAssign)
1642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1643
Daniel Dunbar540ff472008-09-23 21:53:23 +00001644 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001646
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001647 if (MethodImplKind == tok::objc_required)
1648 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1649 else if (MethodImplKind == tok::objc_optional)
1650 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1651
Chris Lattner855e51f2007-12-12 07:09:47 +00001652 return PDecl;
1653}
1654
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001655/// ActOnPropertyImplDecl - This routine performs semantic checks and
1656/// builds the AST node for a property implementation declaration; declared
1657/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001658///
1659Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1660 SourceLocation PropertyLoc,
1661 bool Synthesize,
1662 DeclTy *ClassCatImpDecl,
1663 IdentifierInfo *PropertyId,
1664 IdentifierInfo *PropertyIvar) {
1665 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1666 // Make sure we have a context for the property implementation declaration.
1667 if (!ClassImpDecl) {
1668 Diag(AtLoc, diag::error_missing_property_context);
1669 return 0;
1670 }
1671 ObjCPropertyDecl *property = 0;
1672 ObjCInterfaceDecl* IDecl = 0;
1673 // Find the class or category class where this property must have
1674 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001675 ObjCImplementationDecl *IC = 0;
1676 ObjCCategoryImplDecl* CatImplClass = 0;
1677 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001678 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001679 // We always synthesize an interface for an implementation
1680 // without an interface decl. So, IDecl is always non-zero.
1681 assert(IDecl &&
1682 "ActOnPropertyImplDecl - @implementation without @interface");
1683
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001684 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001685 property = IDecl->FindPropertyDeclaration(PropertyId);
1686 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001687 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001688 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001689 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001690 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001691 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001692 if (Synthesize) {
1693 Diag(AtLoc, diag::error_synthesize_category_decl);
1694 return 0;
1695 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001696 IDecl = CatImplClass->getClassInterface();
1697 if (!IDecl) {
1698 Diag(AtLoc, diag::error_missing_property_interface);
1699 return 0;
1700 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001701 ObjCCategoryDecl *Category =
1702 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1703
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001704 // If category for this implementation not found, it is an error which
1705 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001706 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001707 return 0;
1708 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001709 property = Category->FindPropertyDeclaration(PropertyId);
1710 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001711 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001712 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001713 return 0;
1714 }
1715 }
1716 else {
1717 Diag(AtLoc, diag::error_bad_property_context);
1718 return 0;
1719 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001720 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001721 // Check that we have a valid, previously declared ivar for @synthesize
1722 if (Synthesize) {
1723 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001724 if (!PropertyIvar)
1725 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001726 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianbeae78e2009-02-16 19:35:27 +00001727 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001728 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001729 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001730 return 0;
1731 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001732 QualType PropType = Context.getCanonicalType(property->getType());
1733 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1734
Steve Naroff631e3922008-09-30 00:24:17 +00001735 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001736 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001737 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001738 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001739 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001740 return 0;
1741 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001742 else {
1743 // FIXME! Rules for properties are somewhat different that those
1744 // for assignments. Use a new routine to consolidate all cases;
1745 // specifically for property redeclarations as well as for ivars.
1746 QualType lhsType =
1747 Context.getCanonicalType(PropType).getUnqualifiedType();
1748 QualType rhsType =
1749 Context.getCanonicalType(IvarType).getUnqualifiedType();
1750 if (lhsType != rhsType &&
1751 lhsType->isArithmeticType()) {
1752 Diag(PropertyLoc, diag::error_property_ivar_type)
1753 << property->getDeclName() << Ivar->getDeclName();
1754 return 0;
1755 }
Fariborz Jahanian50956e42009-02-27 22:38:11 +00001756 // __weak is explicit. So it works on Canonical type.
1757 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1758 Diag(PropertyLoc, diag::error_weak_property)
1759 << property->getDeclName() << Ivar->getDeclName();
1760 return 0;
1761 }
1762 if ((Context.isObjCObjectPointerType(property->getType()) ||
1763 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1764 Diag(PropertyLoc, diag::error_strong_property)
1765 << property->getDeclName() << Ivar->getDeclName();
1766 return 0;
1767 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001768 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001769 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001770 } else if (PropertyIvar) {
1771 // @dynamic
1772 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1773 return 0;
1774 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001775 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001776 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001777 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1778 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001779 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001780 ObjCPropertyImplDecl::Synthesize
1781 : ObjCPropertyImplDecl::Dynamic),
1782 Ivar);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001783 CurContext->addDecl(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001784 if (IC) {
1785 if (Synthesize)
1786 if (ObjCPropertyImplDecl *PPIDecl =
1787 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1788 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1789 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1790 << PropertyIvar;
1791 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1792 }
1793
1794 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1795 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1796 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1797 return 0;
1798 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001799 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001800 }
1801 else {
1802 if (Synthesize)
1803 if (ObjCPropertyImplDecl *PPIDecl =
1804 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1805 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1806 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1807 << PropertyIvar;
1808 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1809 }
1810
1811 if (ObjCPropertyImplDecl *PPIDecl =
1812 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1813 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1814 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1815 return 0;
1816 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001817 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001818 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001819
1820 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001821}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001822
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001823bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001824 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001825 return false;
1826
1827 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1828 D->setInvalidDecl();
1829
1830 return true;
1831}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001832
1833/// Collect the instance variables declared in an Objective-C object. Used in
1834/// the creation of structures from objects using the @defs directive.
1835/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1836/// part of the AST generation logic of @defs.
1837static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1838 ASTContext& Ctx,
1839 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1840 if (Class->getSuperClass())
1841 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1842
1843 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1844 for (ObjCInterfaceDecl::ivar_iterator
1845 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1846
1847 ObjCIvarDecl* ID = *I;
1848 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1849 ID->getLocation(),
1850 ID->getIdentifier(),
1851 ID->getType(),
1852 ID->getBitWidth()));
1853 }
1854}
1855
1856/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1857/// instance variables of ClassName into Decls.
1858void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1859 IdentifierInfo *ClassName,
1860 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1861 // Check that ClassName is a valid class
1862 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1863 if (!Class) {
1864 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1865 return;
1866 }
1867 // Collect the instance variables
1868 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1869
1870 // Introduce all of these fields into the appropriate scope.
1871 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1872 D != Decls.end(); ++D) {
1873 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1874 if (getLangOptions().CPlusPlus)
1875 PushOnScopeChains(cast<FieldDecl>(FD), S);
1876 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001877 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001878 }
1879}
1880