blob: f1f03844a2439b97794dc539de3af546d642bff6 [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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000018using namespace clang;
19
Steve Naroff8df46022009-02-28 16:59:13 +000020/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000021/// and user declared, in the method definition's AST.
Steve Naroff8df46022009-02-28 16:59:13 +000022void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000023 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-07-25 17:57:26 +000024 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
25
26 // If we don't have a valid method decl, simply return.
27 if (!MDecl)
28 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000029
30 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000031 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000032 AddInstanceMethodToGlobalPool(MDecl);
33 else
34 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000035
36 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000037 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000038
Steve Naroff313c4162009-02-28 16:48:43 +000039 ActiveScope = FnBodyScope;
40
Chris Lattner855e51f2007-12-12 07:09:47 +000041 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000046
Daniel Dunbareaf91c32008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000049
Chris Lattner97316c02008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000051 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
52 E = MDecl->param_end(); PI != E; ++PI)
53 if ((*PI)->getIdentifier())
54 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000055}
56
Chris Lattnere705e5e2008-07-21 22:17:28 +000057Sema::DeclTy *Sema::
58ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
59 IdentifierInfo *ClassName, SourceLocation ClassLoc,
60 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000061 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000062 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000063 assert(ClassName && "Missing class identifier");
64
65 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000066 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000067 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000068 // Maybe we will complain about the shadowed template parameter.
69 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
70 // Just pretend that we didn't see the previous declaration.
71 PrevDecl = 0;
72 }
73
Ted Kremenek42730c52008-01-07 19:49:32 +000074 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000075 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000076 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000077 }
78
Ted Kremenek42730c52008-01-07 19:49:32 +000079 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000080 if (IDecl) {
81 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000082 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000083 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000084 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000085 Diag(IDecl->getLocation(), diag::note_previous_definition);
86
Steve Naroff1422a622008-11-18 19:15:30 +000087 // Return the previous class interface.
88 // FIXME: don't leak the objects passed in!
89 return IDecl;
90 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000091 IDecl->setLocation(AtInterfaceLoc);
92 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000093 }
Chris Lattner5cece462008-07-21 07:06:49 +000094 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000095 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000096 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000097 if (AttrList)
98 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000099
Steve Naroff15208162008-04-02 18:30:49 +0000100 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000101 // FIXME: PushOnScopeChains
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000102 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
104 TUScope->AddDecl(IDecl);
105 }
106
107 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000110
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000113
114 // Diagnose classes that inherit from deprecated classes.
115 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000116 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000117
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000118 if (PrevDecl && SuperClassDecl == 0) {
119 // The previous declaration was not a class decl. Check if we have a
120 // typedef. If we do, get the underlying class type.
121 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
122 QualType T = TDecl->getUnderlyingType();
123 if (T->isObjCInterfaceType()) {
124 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
125 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
126 }
127 }
Chris Lattner7699a532009-02-16 21:33:09 +0000128
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000129 // This handles the following case:
130 //
131 // typedef int SuperClass;
132 // @interface MyClass : SuperClass {} @end
133 //
134 if (!SuperClassDecl) {
135 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
136 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
137 }
138 }
Chris Lattner7699a532009-02-16 21:33:09 +0000139
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000140 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
141 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000144 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000145 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000146 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000147 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000148 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000149 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000150 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000151 IDecl->setLocEnd(SuperLoc);
152 } else { // we have a root class.
153 IDecl->setLocEnd(ClassLoc);
154 }
155
Steve Naroff1422a622008-11-18 19:15:30 +0000156 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000157 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000158 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
159 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 IDecl->setLocEnd(EndProtoLoc);
161 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000162
163 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000164 return IDecl;
165}
166
167/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000168/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000169Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
170 IdentifierInfo *AliasName,
171 SourceLocation AliasLocation,
172 IdentifierInfo *ClassName,
173 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000174 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000175 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000176 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000177 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000179 else
Chris Lattner65cae292008-11-19 08:23:25 +0000180 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000181 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000182 return 0;
183 }
184 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000185 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000186 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
187 QualType T = TDecl->getUnderlyingType();
188 if (T->isObjCInterfaceType()) {
189 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
190 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000191 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000192 }
193 }
194 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000195 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
196 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000197 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000198 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000199 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 return 0;
201 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000202
203 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000205 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000206
207 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000208
209 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000210 CurContext->addDecl(AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000211 if (!CheckObjCDeclScope(AliasDecl))
212 TUScope->AddDecl(AliasDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000213
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 return AliasDecl;
215}
216
Chris Lattner2bdedd62008-07-26 04:03:38 +0000217Sema::DeclTy *
218Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
219 IdentifierInfo *ProtocolName,
220 SourceLocation ProtocolLoc,
221 DeclTy * const *ProtoRefs,
222 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000223 SourceLocation EndProtoLoc,
224 AttributeList *AttrList) {
225 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000226 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000228 if (PDecl) {
229 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000230 if (!PDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +0000231 PDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000232 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000233 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000234 // Just return the protocol we already had.
235 // FIXME: don't leak the objects passed in!
236 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000237 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000238 // Make sure the cached decl gets a valid start location.
239 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000240 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000241 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000242 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
243 AtProtoInterfaceLoc,ProtocolName);
244 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000245 CurContext->addDecl(PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000246 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000247 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000248 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000249 if (AttrList)
250 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000251 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000252 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000253 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000254 PDecl->setLocEnd(EndProtoLoc);
255 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000256
257 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000258 return PDecl;
259}
260
261/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000262/// issues an error if they are not declared. It returns list of
263/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000264void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000265Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000266 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000267 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000268 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000269 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000270 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
271 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000272 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000273 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000274 continue;
275 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000276
Douglas Gregoraa57e862009-02-18 21:56:37 +0000277 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000278
279 // If this is a forward declaration and we are supposed to warn in this
280 // case, do it.
281 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000282 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000283 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000284 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000285 }
286}
287
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000288/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000289/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000291void
292Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
293 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000294 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000295 ObjCPropertyDecl::PropertyAttributeKind CAttr =
296 Property->getPropertyAttributes();
297 ObjCPropertyDecl::PropertyAttributeKind SAttr =
298 SuperProperty->getPropertyAttributes();
299 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
300 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000301 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000302 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
304 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000305 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000306 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000307 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
308 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000309 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000310 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311
312 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
313 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000315 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000316 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000317 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000318 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000319 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000320 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000321 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000322
Chris Lattnerc5cff302008-07-26 20:50:02 +0000323 if (Context.getCanonicalType(Property->getType()) !=
324 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000325 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000326 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000327
328}
329
330/// ComparePropertiesInBaseAndSuper - This routine compares property
331/// declarations in base and its super class, if any, and issues
332/// diagnostics in a variety of inconsistant situations.
333///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000334void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000335 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
336 if (!SDecl)
337 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000338 // FIXME: O(N^2)
Steve Naroff451f83c2009-01-09 15:36:25 +0000339 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
340 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000341 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000342 // Does property in super class has declaration in current class?
Steve Naroff451f83c2009-01-09 15:36:25 +0000343 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
344 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000345 ObjCPropertyDecl *PDecl = (*I);
346 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000347 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000348 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000349 }
350 }
351}
352
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000353/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
354/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000355/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000356void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000357Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000358 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000359 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
360 if (!IDecl) {
361 // Category
362 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
363 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff451f83c2009-01-09 15:36:25 +0000364 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
365 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000366 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000367 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000368 // Is this property already in category's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000369 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000370 CP != CE; ++CP)
371 if ((*CP)->getIdentifier() == Pr->getIdentifier())
372 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000373 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000374 // Property protocol already exist in class. Diagnose any mismatch.
375 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
376 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000377 return;
378 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000379 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
380 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000381 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000382 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000383 // Is this property already in class's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000384 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000385 CP != CE; ++CP)
386 if ((*CP)->getIdentifier() == Pr->getIdentifier())
387 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000388 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000389 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000390 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000391 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000392}
393
394/// MergeProtocolPropertiesIntoClass - This routine merges properties
395/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000396/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000397///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000398void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
399 DeclTy *MergeItsProtocols) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000400 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
402
403 if (!IDecl) {
404 // Category
405 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
406 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
407 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
408 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
409 E = MDecl->protocol_end(); P != E; ++P)
410 // Merge properties of category (*P) into IDECL's
411 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
412
413 // Go thru the list of protocols for this category and recursively merge
414 // their properties into this class as well.
415 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
416 E = CatDecl->protocol_end(); P != E; ++P)
417 MergeProtocolPropertiesIntoClass(CatDecl, *P);
418 } else {
419 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
420 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
421 E = MD->protocol_end(); P != E; ++P)
422 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
423 }
424 return;
425 }
426
Chris Lattner5cece462008-07-21 07:06:49 +0000427 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
429 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000430 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000431 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
432
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000433 // Go thru the list of protocols for this class and recursively merge
434 // their properties into this class as well.
435 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
436 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000437 MergeProtocolPropertiesIntoClass(IDecl, *P);
438 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000439 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
440 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
441 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000442 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000443 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000444}
445
Chris Lattner855e51f2007-12-12 07:09:47 +0000446/// ActOnForwardProtocolDeclaration -
447Action::DeclTy *
448Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000449 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000450 unsigned NumElts,
451 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000452 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000453
454 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000455 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000456 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000457 if (PDecl == 0) { // Not already seen?
458 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
459 IdentList[i].second, Ident);
460 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000461 CurContext->addDecl(PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000462 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000463 if (attrList)
464 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000465 Protocols.push_back(PDecl);
466 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000467
468 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000469 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000470 &Protocols[0], Protocols.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000471 CurContext->addDecl(PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000472 CheckObjCDeclScope(PDecl);
473 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000474}
475
Chris Lattnere705e5e2008-07-21 22:17:28 +0000476Sema::DeclTy *Sema::
477ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
478 IdentifierInfo *ClassName, SourceLocation ClassLoc,
479 IdentifierInfo *CategoryName,
480 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000481 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000482 unsigned NumProtoRefs,
483 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000484 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000485 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
486 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000487 CurContext->addDecl(CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000488
489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000490 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000491 if (!IDecl || IDecl->isForwardDecl()) {
492 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000493 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner1fa7f622009-02-16 21:26:43 +0000494 return CDecl;
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000495 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000496
Chris Lattner1fa7f622009-02-16 21:26:43 +0000497 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000498
499 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000500 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000501
502 /// Check for duplicate interface declaration for this category
503 ObjCCategoryDecl *CDeclChain;
504 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
505 CDeclChain = CDeclChain->getNextClassCategory()) {
506 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
507 Diag(CategoryLoc, diag::warn_dup_category_def)
508 << ClassName << CategoryName;
509 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
510 break;
511 }
512 }
513 if (!CDeclChain)
514 CDecl->insertNextClassCategory();
515
Chris Lattner855e51f2007-12-12 07:09:47 +0000516 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000517 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000518 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000519 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000520
521 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000522 return CDecl;
523}
524
525/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000526/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000527/// object.
528Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
529 SourceLocation AtCatImplLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
531 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000532 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000533 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000534 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
535 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 /// Check that class of this category is already completely declared.
537 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000538 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000539
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000540 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000541 CurContext->addDecl(CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000542
Chris Lattner855e51f2007-12-12 07:09:47 +0000543 /// TODO: Check that CatName, category name, is not used in another
544 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000545 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000546
547 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 return CDecl;
549}
550
551Sema::DeclTy *Sema::ActOnStartClassImplementation(
552 SourceLocation AtClassImplLoc,
553 IdentifierInfo *ClassName, SourceLocation ClassLoc,
554 IdentifierInfo *SuperClassname,
555 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000557 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000558 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000559 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000560 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000561 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000562 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000565 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000566 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000567 }
568
569 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 if (SuperClassname) {
572 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000573 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000574 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000575 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
576 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000577 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000578 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000579 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000580 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000581 Diag(SuperClassLoc, diag::err_undef_superclass)
582 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000583 else if (IDecl && IDecl->getSuperClass() != SDecl) {
584 // This implementation and its interface do not have the same
585 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000586 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000587 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000588 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000589 }
590 }
591 }
592
593 if (!IDecl) {
594 // Legacy case of @implementation with no corresponding @interface.
595 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000596
597 // FIXME: Do we support attributes on the @implementation? If so
598 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000599 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
600 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000601 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000602 IDecl->setSuperClass(SDecl);
603 IDecl->setLocEnd(ClassLoc);
604
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000605 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000606 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 // Remember that this needs to be removed when the scope is popped.
608 TUScope->AddDecl(IDecl);
609 }
610
Ted Kremenek42730c52008-01-07 19:49:32 +0000611 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000612 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000613 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000614
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000615 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000616 CurContext->addDecl(IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000617
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000618 if (CheckObjCDeclScope(IMPDecl))
619 return IMPDecl;
620
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000623 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000626 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 return IMPDecl;
628}
629
Ted Kremenek42730c52008-01-07 19:49:32 +0000630void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
631 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000632 SourceLocation RBrace) {
633 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000634 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 if (!IDecl)
636 return;
637 /// Check case of non-existing @interface decl.
638 /// (legacy objective-c @implementation decl without an @interface decl).
639 /// Add implementations's ivar to the synthesize class's ivar list.
640 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000641 IDecl->setIVarList(ivars, numIvars, Context);
642 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000643 return;
644 }
645 // If implementation has empty ivar list, just return.
646 if (numIvars == 0)
647 return;
648
649 assert(ivars && "missing @implementation ivars");
650
651 // Check interface's Ivar list against those in the implementation.
652 // names and types must match.
653 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000656 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
657 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 ObjCIvarDecl* ImplIvar = ivars[j++];
659 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000660 assert (ImplIvar && "missing implementation ivar");
661 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000662 if (Context.getCanonicalType(ImplIvar->getType()) !=
663 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000664 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000665 << ImplIvar->getIdentifier()
666 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000667 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 }
669 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
670 // as error.
671 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000672 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000673 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000674 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000675 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000676 }
677 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000678 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000679
680 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000681 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000682 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000683 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000684}
685
Steve Naroffb4f48512008-02-10 21:38:56 +0000686void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
687 bool &IncompleteImpl) {
688 if (!IncompleteImpl) {
689 Diag(ImpLoc, diag::warn_incomplete_impl);
690 IncompleteImpl = true;
691 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000692 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000693}
694
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000695void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
696 ObjCMethodDecl *IntfMethodDecl) {
697 bool err = false;
698 QualType ImpMethodQType =
699 Context.getCanonicalType(ImpMethodDecl->getResultType());
700 QualType IntfMethodQType =
701 Context.getCanonicalType(IntfMethodDecl->getResultType());
702 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
703 err = true;
704 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
705 IF=IntfMethodDecl->param_begin(),
706 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
707 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
708 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
709 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
710 err = true;
711 break;
712 }
713 }
714 if (err) {
715 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
716 << ImpMethodDecl->getDeclName();
717 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
718 }
719}
720
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000721/// isPropertyReadonly - Return true if property is readonly, by searching
722/// for the property in the class and in its categories and implementations
723///
724bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000725 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000726 // by far the most common case.
727 if (!PDecl->isReadOnly())
728 return false;
729 // Even if property is ready only, if interface has a user defined setter,
730 // it is not considered read only.
731 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
732 return false;
733
734 // Main class has the property as 'readonly'. Must search
735 // through the category list to see if the property's
736 // attribute has been over-ridden to 'readwrite'.
737 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
738 Category; Category = Category->getNextClassCategory()) {
739 // Even if property is ready only, if a category has a user defined setter,
740 // it is not considered read only.
741 if (Category->getInstanceMethod(PDecl->getSetterName()))
742 return false;
743 ObjCPropertyDecl *P =
744 Category->FindPropertyDeclaration(PDecl->getIdentifier());
745 if (P && !P->isReadOnly())
746 return false;
747 }
748
749 // Also, check for definition of a setter method in the implementation if
750 // all else failed.
751 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
752 if (ObjCImplementationDecl *IMD =
753 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
754 if (IMD->getInstanceMethod(PDecl->getSetterName()))
755 return false;
756 }
757 else if (ObjCCategoryImplDecl *CIMD =
758 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
759 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
760 return false;
761 }
762 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000763 // Lastly, look through the implementation (if one is in scope).
764 if (ObjCImplementationDecl *ImpDecl =
765 ObjCImplementations[IDecl->getIdentifier()])
766 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
767 return false;
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000768 return true;
769}
770
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000771/// FIXME: Type hierarchies in Objective-C can be deep. We could most
772/// likely improve the efficiency of selector lookups and type
773/// checking by associating with each protocol / interface / category
774/// the flattened instance tables. If we used an immutable set to keep
775/// the table then it wouldn't add significant memory cost and it
776/// would be handy for lookups.
777
Steve Naroffb268d2a2008-02-08 22:06:17 +0000778/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000779/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000780void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
781 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000782 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000783 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000784 const llvm::DenseSet<Selector> &ClsMap,
785 ObjCInterfaceDecl *IDecl) {
786 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
787
788 // If a method lookup fails locally we still need to look and see if
789 // the method was implemented by a base class or an inherited
790 // protocol. This lookup is slow, but occurs rarely in correct code
791 // and otherwise would terminate in a warning.
792
Chris Lattner855e51f2007-12-12 07:09:47 +0000793 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000794 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000795 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000796 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000797 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000798 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000799 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000800 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000801 }
802 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000803 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000804 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000805 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000806 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
807 !ClsMap.count(method->getSelector()) &&
808 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000809 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000810 }
Chris Lattner0be08822008-07-21 21:32:27 +0000811 // Check on this protocols's referenced protocols, recursively.
812 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
813 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000814 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000815}
816
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000817void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
818 ObjCContainerDecl* CDecl,
819 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000820 llvm::DenseSet<Selector> InsMap;
821 // Check and see if instance methods in class interface have been
822 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000824 E = IMPDecl->instmeth_end(); I != E; ++I)
825 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000826
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000827 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
828 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattner28400082009-02-16 19:25:52 +0000829 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000830 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000831 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000832 }
Chris Lattner28400082009-02-16 19:25:52 +0000833
834 ObjCMethodDecl *ImpMethodDecl =
835 IMPDecl->getInstanceMethod((*I)->getSelector());
836 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000837 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattner28400082009-02-16 19:25:52 +0000838 assert(IntfMethodDecl &&
839 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
840 // ImpMethodDecl may be null as in a @dynamic property.
841 if (ImpMethodDecl)
842 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
843 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000844
Chris Lattner855e51f2007-12-12 07:09:47 +0000845 llvm::DenseSet<Selector> ClsMap;
846 // Check and see if class methods in class interface have been
847 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000848 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000849 E = IMPDecl->classmeth_end(); I != E; ++I)
850 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000851
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000852 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
853 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000854 if (!ClsMap.count((*I)->getSelector()))
855 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000856 else {
857 ObjCMethodDecl *ImpMethodDecl =
858 IMPDecl->getClassMethod((*I)->getSelector());
859 ObjCMethodDecl *IntfMethodDecl =
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000860 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000861 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
862 }
863
Chris Lattner855e51f2007-12-12 07:09:47 +0000864
865 // Check the protocol list for unimplemented methods in the @implementation
866 // class.
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000867 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
868 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
869 E = I->protocol_end(); PI != E; ++PI)
870 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
871 InsMap, ClsMap, I);
872 // Check class extensions (unnamed categories)
873 for (ObjCCategoryDecl *Categories = I->getCategoryList();
874 Categories; Categories = Categories->getNextClassCategory()) {
875 if (!Categories->getIdentifier()) {
876 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
877 break;
878 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000879 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000880 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
881 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
882 E = C->protocol_end(); PI != E; ++PI)
883 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
884 InsMap, ClsMap, C->getClassInterface());
885 } else
886 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +0000887}
888
889/// ActOnForwardClassDeclaration -
890Action::DeclTy *
891Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000892 IdentifierInfo **IdentList,
893 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000894 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000895
896 for (unsigned i = 0; i != NumElts; ++i) {
897 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000898 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000899 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000900 // Maybe we will complain about the shadowed template parameter.
901 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
902 // Just pretend that we didn't see the previous declaration.
903 PrevDecl = 0;
904 }
905
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000907 // GCC apparently allows the following idiom:
908 //
909 // typedef NSObject < XCElementTogglerP > XCElementToggler;
910 // @class XCElementToggler;
911 //
912 // FIXME: Make an extension?
913 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
914 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000915 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000916 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000917 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000918 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000920 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000921 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
922 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000923 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000924
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000925 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000926 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000927 // Remember that this needs to be removed when the scope is popped.
928 TUScope->AddDecl(IDecl);
929 }
930
931 Interfaces.push_back(IDecl);
932 }
933
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000934 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000935 &Interfaces[0],
936 Interfaces.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000937 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000938 CheckObjCDeclScope(CDecl);
939 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000940}
941
942
943/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
944/// returns true, or false, accordingly.
945/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000946bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000947 const ObjCMethodDecl *PrevMethod,
948 bool matchBasedOnSizeAndAlignment) {
949 QualType T1 = Context.getCanonicalType(Method->getResultType());
950 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
951
952 if (T1 != T2) {
953 // The result types are different.
954 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000955 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000956 // Incomplete types don't have a size and alignment.
957 if (T1->isIncompleteType() || T2->isIncompleteType())
958 return false;
959 // Check is based on size and alignment.
960 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
961 return false;
962 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000963
964 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
965 E = Method->param_end();
966 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
967
968 for (; ParamI != E; ++ParamI, ++PrevI) {
969 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
970 T1 = Context.getCanonicalType((*ParamI)->getType());
971 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +0000972 if (T1 != T2) {
973 // The result types are different.
974 if (!matchBasedOnSizeAndAlignment)
975 return false;
976 // Incomplete types don't have a size and alignment.
977 if (T1->isIncompleteType() || T2->isIncompleteType())
978 return false;
979 // Check is based on size and alignment.
980 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
981 return false;
982 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000983 }
984 return true;
985}
986
Ted Kremenek42730c52008-01-07 19:49:32 +0000987void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
988 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000989 if (!FirstMethod.Method) {
990 // Haven't seen a method with this selector name yet - add it.
991 FirstMethod.Method = Method;
992 FirstMethod.Next = 0;
993 } else {
994 // We've seen a method with this name, now check the type signature(s).
995 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
996
Ted Kremenek42730c52008-01-07 19:49:32 +0000997 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000998 Next = Next->Next)
999 match = MatchTwoMethodDeclarations(Method, Next->Method);
1000
1001 if (!match) {
1002 // We have a new signature for an existing method - add it.
1003 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +00001004 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +00001005 }
1006 }
1007}
1008
Steve Naroffec7e0882008-10-21 10:50:19 +00001009// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001010ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1011 SourceRange R) {
1012 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001013 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001014
1015 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001016 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1017 // This checks if the methods differ by size & alignment.
1018 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1019 issueWarning = true;
1020 }
1021 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001022 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001023 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001024 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001025 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001026 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001027 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001028 }
1029 return MethList.Method;
1030}
1031
Ted Kremenek42730c52008-01-07 19:49:32 +00001032void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1033 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001034 if (!FirstMethod.Method) {
1035 // Haven't seen a method with this selector name yet - add it.
1036 FirstMethod.Method = Method;
1037 FirstMethod.Next = 0;
1038 } else {
1039 // We've seen a method with this name, now check the type signature(s).
1040 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1041
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001043 Next = Next->Next)
1044 match = MatchTwoMethodDeclarations(Method, Next->Method);
1045
1046 if (!match) {
1047 // We have a new signature for an existing method - add it.
1048 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001050 FirstMethod.Next = OMI;
1051 }
1052 }
1053}
1054
Steve Naroffab63fd62009-01-08 17:28:14 +00001055/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1056/// have the property type and issue diagnostics if they don't.
1057/// Also synthesize a getter/setter method if none exist (and update the
1058/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1059/// methods is the "right" thing to do.
1060void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1061 ObjCContainerDecl *CD) {
1062 ObjCMethodDecl *GetterMethod, *SetterMethod;
1063
1064 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1065 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1066
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001067 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001068 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001069 Diag(property->getLocation(),
1070 diag::err_accessor_property_type_mismatch)
1071 << property->getDeclName()
1072 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001073 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1074 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001075
1076 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001077 if (Context.getCanonicalType(SetterMethod->getResultType())
1078 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001079 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001080 if (SetterMethod->param_size() != 1 ||
1081 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001082 Diag(property->getLocation(),
1083 diag::err_accessor_property_type_mismatch)
1084 << property->getDeclName()
1085 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001086 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1087 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001088 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001089
1090 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001091 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001092 // FIXME: The synthesized property we set here is misleading. We
1093 // almost always synthesize these methods unless the user explicitly
1094 // provided prototypes (which is odd, but allowed). Sema should be
1095 // typechecking that the declarations jive in that situation (which
1096 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001097 if (!GetterMethod) {
1098 // No instance method of same name as property getter name was found.
1099 // Declare a getter method and add it to the list of methods
1100 // for this class.
1101 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1102 property->getLocation(), property->getGetterName(),
1103 property->getType(), CD, true, false, true,
1104 (property->getPropertyImplementation() ==
1105 ObjCPropertyDecl::Optional) ?
1106 ObjCMethodDecl::Optional :
1107 ObjCMethodDecl::Required);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001108 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001109 } else
1110 // A user declared getter will be synthesize when @synthesize of
1111 // the property with the same name is seen in the @implementation
1112 GetterMethod->setIsSynthesized();
1113 property->setGetterMethodDecl(GetterMethod);
1114
1115 // Skip setter if property is read-only.
1116 if (!property->isReadOnly()) {
1117 // Find the default setter and if one not found, add one.
1118 if (!SetterMethod) {
1119 // No instance method of same name as property setter name was found.
1120 // Declare a setter method and add it to the list of methods
1121 // for this class.
1122 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1123 property->getLocation(),
1124 property->getSetterName(),
1125 Context.VoidTy, CD, true, false, true,
1126 (property->getPropertyImplementation() ==
1127 ObjCPropertyDecl::Optional) ?
1128 ObjCMethodDecl::Optional :
1129 ObjCMethodDecl::Required);
1130 // Invent the arguments for the setter. We don't bother making a
1131 // nice name for the argument.
1132 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1133 SourceLocation(),
1134 property->getIdentifier(),
1135 property->getType(),
1136 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001137 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001138 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001139 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001140 } else
1141 // A user declared setter will be synthesize when @synthesize of
1142 // the property with the same name is seen in the @implementation
1143 SetterMethod->setIsSynthesized();
1144 property->setSetterMethodDecl(SetterMethod);
1145 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001146 // Add any synthesized methods to the global pool. This allows us to
1147 // handle the following, which is supported by GCC (and part of the design).
1148 //
1149 // @interface Foo
1150 // @property double bar;
1151 // @end
1152 //
1153 // void thisIsUnfortunate() {
1154 // id foo;
1155 // double bar = [foo bar];
1156 // }
1157 //
Douglas Gregord1675382009-01-09 19:42:16 +00001158 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001159 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001160 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001161 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001162}
1163
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001164// Note: For class/category implemenations, allMethods/allProperties is
1165// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001166void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1167 DeclTy **allMethods, unsigned allNum,
1168 DeclTy **allProperties, unsigned pNum) {
1169 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1170
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001171 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1172 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001173 // should be true.
1174 if (!ClassDecl)
1175 return;
1176
Chris Lattner855e51f2007-12-12 07:09:47 +00001177 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001178 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1179 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001180 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001181
Steve Naroffab63fd62009-01-08 17:28:14 +00001182 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001183
1184 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1185 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1186 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1187
Chris Lattner855e51f2007-12-12 07:09:47 +00001188 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001189 ObjCMethodDecl *Method =
1190 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001191
1192 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001193 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001194 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001195 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001196 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1197 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001198 if ((isInterfaceDeclKind && PrevMethod && !match)
1199 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001200 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001201 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001202 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001203 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001204 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001205 InsMap[Method->getSelector()] = Method;
1206 /// The following allows us to typecheck messages to "id".
1207 AddInstanceMethodToGlobalPool(Method);
1208 }
1209 }
1210 else {
1211 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001212 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001213 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1214 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001215 if ((isInterfaceDeclKind && PrevMethod && !match)
1216 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001217 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001218 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001219 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001220 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001221 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001222 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001223 /// The following allows us to typecheck messages to "Class".
1224 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001225 }
1226 }
1227 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001228 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001229 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001230 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001231 ComparePropertiesInBaseAndSuper(I);
1232 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff451f83c2009-01-09 15:36:25 +00001233 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001234 // Categories are used to extend the class by declaring new methods.
1235 // By the same token, they are also used to add new properties. No
1236 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001237
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001238 // Merge protocol properties into category
1239 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner855e51f2007-12-12 07:09:47 +00001240 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001241 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1242 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1243 // user-defined setter/getter. It also synthesizes setter/getter methods
1244 // and adds them to the DeclContext and global method pools.
Chris Lattner22063402009-02-16 18:32:47 +00001245 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1246 E = CDecl->prop_end(); I != E; ++I)
1247 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001248 CDecl->setAtEndLoc(AtEndLoc);
1249 }
1250 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001251 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001252 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001253 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001254 } else if (ObjCCategoryImplDecl* CatImplClass =
1255 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001256 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001257
Chris Lattner855e51f2007-12-12 07:09:47 +00001258 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001259 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001260 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001261 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001262 Categories; Categories = Categories->getNextClassCategory()) {
1263 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001264 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001265 break;
1266 }
1267 }
1268 }
1269 }
1270}
1271
1272
1273/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1274/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001275static Decl::ObjCDeclQualifier
1276CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1277 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1278 if (PQTVal & ObjCDeclSpec::DQ_In)
1279 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1280 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1281 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1282 if (PQTVal & ObjCDeclSpec::DQ_Out)
1283 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1284 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1285 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1286 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1287 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1288 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1289 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001290
1291 return ret;
1292}
1293
1294Sema::DeclTy *Sema::ActOnMethodDeclaration(
1295 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001296 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001297 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001298 Selector Sel,
1299 // optional arguments. The number of types/arguments is obtained
1300 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001301 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001302 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001303 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1304 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001305 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001306
1307 // Make sure we can establish a context for the method.
1308 if (!ClassDecl) {
1309 Diag(MethodLoc, diag::error_missing_method_context);
1310 return 0;
1311 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001312 QualType resultDeclType;
1313
Steve Naroffa442ad92009-02-20 22:59:16 +00001314 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001315 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001316
1317 // Methods cannot return interface types. All ObjC objects are
1318 // passed by reference.
1319 if (resultDeclType->isObjCInterfaceType()) {
1320 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1321 << "returned";
1322 return 0;
1323 }
1324 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001325 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001326
Chris Lattner114add62008-03-16 00:49:28 +00001327 ObjCMethodDecl* ObjCMethod =
1328 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001329 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001330 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001331 false,
Chris Lattner114add62008-03-16 00:49:28 +00001332 MethodDeclKind == tok::objc_optional ?
1333 ObjCMethodDecl::Optional :
1334 ObjCMethodDecl::Required);
1335
Chris Lattnereee57c02008-04-04 06:12:32 +00001336 llvm::SmallVector<ParmVarDecl*, 16> Params;
1337
1338 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1339 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001340 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001341
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001342 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001343 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001344 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001345 if (argType->isArrayType()) { // (char *[]) -> (char **)
1346 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001347 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001348 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001349 else if (argType->isFunctionType())
1350 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001351 else if (argType->isObjCInterfaceType()) {
1352 // FIXME! provide more precise location for the parameter
Steve Naroffa442ad92009-02-20 22:59:16 +00001353 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1354 << "passed";
1355 ObjCMethod->setInvalidDecl();
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001356 return 0;
1357 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001358 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001359 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001360 ParmVarDecl* Param;
1361 if (originalArgType.isNull())
1362 Param = ParmVarDecl::Create(Context, ObjCMethod,
1363 SourceLocation(/*FIXME*/),
1364 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001365 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001366 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001367 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1368 SourceLocation(/*FIXME*/),
1369 ArgNames[i], argType, originalArgType,
1370 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001371
Chris Lattnereee57c02008-04-04 06:12:32 +00001372 Param->setObjCDeclQualifier(
1373 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1374 Params.push_back(Param);
1375 }
1376
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001377 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001378 ObjCMethod->setObjCDeclQualifier(
1379 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1380 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001381
1382 if (AttrList)
1383 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001384
1385 // For implementations (which can be very "coarse grain"), we add the
1386 // method now. This allows the AST to implement lookup methods that work
1387 // incrementally (without waiting until we parse the @end). It also allows
1388 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001390 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001391 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001392 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001393 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001394 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001395 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001397 }
1398 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001399 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001400 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001401 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001402 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001403 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001404 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001405 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001406 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001407 }
1408 }
1409 if (PrevMethod) {
1410 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001411 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001412 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001413 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001414 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001415 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001416}
1417
Daniel Dunbar540ff472008-09-23 21:53:23 +00001418void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1419 SourceLocation Loc,
1420 unsigned &Attributes) {
1421 // FIXME: Improve the reported location.
1422
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001423 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001424 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001425 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1426 ObjCDeclSpec::DQ_PR_assign |
1427 ObjCDeclSpec::DQ_PR_copy |
1428 ObjCDeclSpec::DQ_PR_retain))) {
1429 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1430 "readwrite" :
1431 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1432 "assign" :
1433 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1434 "copy" : "retain";
1435
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001436 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001437 diag::err_objc_property_attr_mutually_exclusive :
1438 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001439 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001440 }
1441
1442 // Check for copy or retain on non-object types.
1443 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1444 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001445 Diag(Loc, diag::err_objc_property_requires_object)
1446 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001447 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1448 }
1449
1450 // Check for more than one of { assign, copy, retain }.
1451 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1452 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001453 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1454 << "assign" << "copy";
1455 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001456 }
1457 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001458 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1459 << "assign" << "retain";
1460 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001461 }
1462 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1463 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001464 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1465 << "copy" << "retain";
1466 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001467 }
1468 }
1469
1470 // Warn if user supplied no assignment attribute, property is
1471 // readwrite, and this is an object type.
1472 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1473 ObjCDeclSpec::DQ_PR_retain)) &&
1474 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1475 Context.isObjCObjectPointerType(PropertyTy)) {
1476 // Skip this warning in gc-only mode.
1477 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1478 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1479
1480 // If non-gc code warn that this is likely inappropriate.
1481 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1482 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1483
1484 // FIXME: Implement warning dependent on NSCopying being
1485 // implemented. See also:
1486 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1487 // (please trim this list while you are at it).
1488 }
1489}
1490
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001491Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1492 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001493 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001494 Selector GetterSel,
1495 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001496 DeclTy *ClassCategory,
1497 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001498 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001499 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001500 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1501 // default is readwrite!
1502 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1503 // property is defaulted to 'assign' if it is readwrite and is
1504 // not retain or copy
1505 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1506 (isReadWrite &&
1507 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1508 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1509 QualType T = GetTypeForDeclarator(FD.D, S);
1510 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001511
1512 // May modify Attributes.
1513 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001514
1515 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1516 if (!CDecl->getIdentifier()) {
1517 // This is an anonymous category. property requires special
1518 // handling.
1519 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1520 if (ObjCPropertyDecl *PIDecl =
1521 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1522 // property 'PIDecl's readonly attribute will be over-ridden
1523 // with anonymous category's readwrite property attribute!
1524 unsigned PIkind = PIDecl->getPropertyAttributes();
1525 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001526 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001527 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1528 Diag(AtLoc, diag::warn_property_attr_mismatch);
1529 PIDecl->makeitReadWriteAttribute();
1530 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1531 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1532 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1533 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1534 PIDecl->setSetterName(SetterSel);
1535 // FIXME: use a common routine with addPropertyMethods.
1536 ObjCMethodDecl *SetterDecl =
1537 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1538 Context.VoidTy,
1539 ICDecl,
1540 true, false, true,
1541 ObjCMethodDecl::Required);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001542 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001543 SourceLocation(),
1544 FD.D.getIdentifier(),
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001545 T, VarDecl::None, 0);
1546 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001547 PIDecl->setSetterMethodDecl(SetterDecl);
1548 }
1549 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001550 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001551 *isOverridingProperty = true;
1552 return 0;
1553 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001554 // No matching property found in the main class. Just fall thru
1555 // and add property to the anonymous category. It looks like
Ben Laurie74d601e2009-02-16 09:18:41 +00001556 // it works as is. This category becomes just like a category
1557 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001558 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001559 Diag(CDecl->getLocation(), diag::err_continuation_class);
1560 *isOverridingProperty = true;
1561 return 0;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001562 }
1563 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001564
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001565 Type *t = T.getTypePtr();
1566 if (t->isArrayType() || t->isFunctionType())
1567 Diag(AtLoc, diag::err_property_type) << T;
1568
Steve Naroffdcf1e842009-01-11 12:47:58 +00001569 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1570 assert(DC && "ClassDecl is not a DeclContext");
1571 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001572 FD.D.getIdentifier(), T);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001573 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001574
1575 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001576
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001577 // Regardless of setter/getter attribute, we save the default getter/setter
1578 // selector names in anticipation of declaration of setter/getter methods.
1579 PDecl->setGetterName(GetterSel);
1580 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001581
Daniel Dunbar540ff472008-09-23 21:53:23 +00001582 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001583 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001584
Daniel Dunbar540ff472008-09-23 21:53:23 +00001585 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001586 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001587
Daniel Dunbar540ff472008-09-23 21:53:23 +00001588 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001589 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001590
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001591 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001592 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001593
Daniel Dunbar540ff472008-09-23 21:53:23 +00001594 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001596
Daniel Dunbar540ff472008-09-23 21:53:23 +00001597 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001599
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001600 if (isAssign)
1601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1602
Daniel Dunbar540ff472008-09-23 21:53:23 +00001603 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001605
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001606 if (MethodImplKind == tok::objc_required)
1607 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1608 else if (MethodImplKind == tok::objc_optional)
1609 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1610
Chris Lattner855e51f2007-12-12 07:09:47 +00001611 return PDecl;
1612}
1613
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001614/// ActOnPropertyImplDecl - This routine performs semantic checks and
1615/// builds the AST node for a property implementation declaration; declared
1616/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001617///
1618Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1619 SourceLocation PropertyLoc,
1620 bool Synthesize,
1621 DeclTy *ClassCatImpDecl,
1622 IdentifierInfo *PropertyId,
1623 IdentifierInfo *PropertyIvar) {
1624 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1625 // Make sure we have a context for the property implementation declaration.
1626 if (!ClassImpDecl) {
1627 Diag(AtLoc, diag::error_missing_property_context);
1628 return 0;
1629 }
1630 ObjCPropertyDecl *property = 0;
1631 ObjCInterfaceDecl* IDecl = 0;
1632 // Find the class or category class where this property must have
1633 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001634 ObjCImplementationDecl *IC = 0;
1635 ObjCCategoryImplDecl* CatImplClass = 0;
1636 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001637 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001638 // We always synthesize an interface for an implementation
1639 // without an interface decl. So, IDecl is always non-zero.
1640 assert(IDecl &&
1641 "ActOnPropertyImplDecl - @implementation without @interface");
1642
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001643 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001644 property = IDecl->FindPropertyDeclaration(PropertyId);
1645 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001646 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001647 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001648 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001649 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001650 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001651 if (Synthesize) {
1652 Diag(AtLoc, diag::error_synthesize_category_decl);
1653 return 0;
1654 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001655 IDecl = CatImplClass->getClassInterface();
1656 if (!IDecl) {
1657 Diag(AtLoc, diag::error_missing_property_interface);
1658 return 0;
1659 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001660 ObjCCategoryDecl *Category =
1661 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1662
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001663 // If category for this implementation not found, it is an error which
1664 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001665 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001666 return 0;
1667 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001668 property = Category->FindPropertyDeclaration(PropertyId);
1669 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001670 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001671 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001672 return 0;
1673 }
1674 }
1675 else {
1676 Diag(AtLoc, diag::error_bad_property_context);
1677 return 0;
1678 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001679 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001680 // Check that we have a valid, previously declared ivar for @synthesize
1681 if (Synthesize) {
1682 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001683 if (!PropertyIvar)
1684 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001685 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianbeae78e2009-02-16 19:35:27 +00001686 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001687 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001688 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001689 return 0;
1690 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001691 QualType PropType = Context.getCanonicalType(property->getType());
1692 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1693
Steve Naroff631e3922008-09-30 00:24:17 +00001694 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001695 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001696 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001697 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001698 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001699 return 0;
1700 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001701 else {
1702 // FIXME! Rules for properties are somewhat different that those
1703 // for assignments. Use a new routine to consolidate all cases;
1704 // specifically for property redeclarations as well as for ivars.
1705 QualType lhsType =
1706 Context.getCanonicalType(PropType).getUnqualifiedType();
1707 QualType rhsType =
1708 Context.getCanonicalType(IvarType).getUnqualifiedType();
1709 if (lhsType != rhsType &&
1710 lhsType->isArithmeticType()) {
1711 Diag(PropertyLoc, diag::error_property_ivar_type)
1712 << property->getDeclName() << Ivar->getDeclName();
1713 return 0;
1714 }
Fariborz Jahanian50956e42009-02-27 22:38:11 +00001715 // __weak is explicit. So it works on Canonical type.
1716 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1717 Diag(PropertyLoc, diag::error_weak_property)
1718 << property->getDeclName() << Ivar->getDeclName();
1719 return 0;
1720 }
1721 if ((Context.isObjCObjectPointerType(property->getType()) ||
1722 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1723 Diag(PropertyLoc, diag::error_strong_property)
1724 << property->getDeclName() << Ivar->getDeclName();
1725 return 0;
1726 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001727 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001728 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001729 } else if (PropertyIvar) {
1730 // @dynamic
1731 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1732 return 0;
1733 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001734 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001735 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001736 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1737 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001738 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001739 ObjCPropertyImplDecl::Synthesize
1740 : ObjCPropertyImplDecl::Dynamic),
1741 Ivar);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001742 CurContext->addDecl(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001743 if (IC) {
1744 if (Synthesize)
1745 if (ObjCPropertyImplDecl *PPIDecl =
1746 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1747 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1748 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1749 << PropertyIvar;
1750 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1751 }
1752
1753 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1754 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1755 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1756 return 0;
1757 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001758 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001759 }
1760 else {
1761 if (Synthesize)
1762 if (ObjCPropertyImplDecl *PPIDecl =
1763 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1764 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1765 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1766 << PropertyIvar;
1767 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1768 }
1769
1770 if (ObjCPropertyImplDecl *PPIDecl =
1771 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1772 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1773 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1774 return 0;
1775 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001776 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001777 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001778
1779 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001780}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001781
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001782bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001783 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001784 return false;
1785
1786 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1787 D->setInvalidDecl();
1788
1789 return true;
1790}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001791
1792/// Collect the instance variables declared in an Objective-C object. Used in
1793/// the creation of structures from objects using the @defs directive.
1794/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1795/// part of the AST generation logic of @defs.
1796static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1797 ASTContext& Ctx,
1798 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1799 if (Class->getSuperClass())
1800 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1801
1802 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1803 for (ObjCInterfaceDecl::ivar_iterator
1804 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1805
1806 ObjCIvarDecl* ID = *I;
1807 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1808 ID->getLocation(),
1809 ID->getIdentifier(),
1810 ID->getType(),
1811 ID->getBitWidth()));
1812 }
1813}
1814
1815/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1816/// instance variables of ClassName into Decls.
1817void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1818 IdentifierInfo *ClassName,
1819 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1820 // Check that ClassName is a valid class
1821 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1822 if (!Class) {
1823 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1824 return;
1825 }
1826 // Collect the instance variables
1827 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1828
1829 // Introduce all of these fields into the appropriate scope.
1830 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1831 D != Decls.end(); ++D) {
1832 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1833 if (getLangOptions().CPlusPlus)
1834 PushOnScopeChains(cast<FieldDecl>(FD), S);
1835 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001836 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001837 }
1838}
1839