blob: 660f745228b8252f31fc1d5ff9a29c0565d06e3e [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
Ted Kremenek42730c52008-01-07 19:49:32 +000020/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000021/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000022void Sema::ObjCActOnStartOfMethodDef(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
39 // Create Decl objects for each parameter, entrring them in the scope for
40 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000041
42 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000043 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000044
Daniel Dunbareaf91c32008-08-26 06:07:48 +000045 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
46 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000047
Chris Lattner97316c02008-04-10 02:22:51 +000048 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000049 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
50 E = MDecl->param_end(); PI != E; ++PI)
51 if ((*PI)->getIdentifier())
52 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000053}
54
Chris Lattnere705e5e2008-07-21 22:17:28 +000055Sema::DeclTy *Sema::
56ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
57 IdentifierInfo *ClassName, SourceLocation ClassLoc,
58 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000059 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000060 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000061 assert(ClassName && "Missing class identifier");
62
63 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000064 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000065 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000066 // Maybe we will complain about the shadowed template parameter.
67 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
68 // Just pretend that we didn't see the previous declaration.
69 PrevDecl = 0;
70 }
71
Ted Kremenek42730c52008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000075 }
76
Ted Kremenek42730c52008-01-07 19:49:32 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000078 if (IDecl) {
79 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000080 if (!IDecl->isForwardDecl()) {
Chris Lattner271d4c22008-11-24 05:29:24 +000081 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000082 Diag(IDecl->getLocation(), diag::note_previous_definition);
83
Steve Naroff1422a622008-11-18 19:15:30 +000084 // Return the previous class interface.
85 // FIXME: don't leak the objects passed in!
86 return IDecl;
87 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000088 IDecl->setLocation(AtInterfaceLoc);
89 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000090 }
Chris Lattner5cece462008-07-21 07:06:49 +000091 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000092 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000093 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000094 if (AttrList)
95 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000096
Steve Naroff15208162008-04-02 18:30:49 +000097 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000098 // FIXME: PushOnScopeChains
Douglas Gregor03b2ad22009-01-12 23:27:07 +000099 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100 // Remember that this needs to be removed when the scope is popped.
101 TUScope->AddDecl(IDecl);
102 }
103
104 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000106 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000107
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000108 ObjCInterfaceDecl *SuperClassDecl =
109 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000110
111 // Diagnose classes that inherit from deprecated classes.
112 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000113 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000114
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000115 if (PrevDecl && SuperClassDecl == 0) {
116 // The previous declaration was not a class decl. Check if we have a
117 // typedef. If we do, get the underlying class type.
118 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
119 QualType T = TDecl->getUnderlyingType();
120 if (T->isObjCInterfaceType()) {
121 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
122 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
123 }
124 }
Chris Lattner7699a532009-02-16 21:33:09 +0000125
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000126 // This handles the following case:
127 //
128 // typedef int SuperClass;
129 // @interface MyClass : SuperClass {} @end
130 //
131 if (!SuperClassDecl) {
132 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
133 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
134 }
135 }
Chris Lattner7699a532009-02-16 21:33:09 +0000136
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000137 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
138 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000139 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000140 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000141 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000143 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000144 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000145 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000146 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000147 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000148 IDecl->setLocEnd(SuperLoc);
149 } else { // we have a root class.
150 IDecl->setLocEnd(ClassLoc);
151 }
152
Steve Naroff1422a622008-11-18 19:15:30 +0000153 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000154 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000155 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
156 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 IDecl->setLocEnd(EndProtoLoc);
158 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000159
160 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 return IDecl;
162}
163
164/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000165/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000166Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
167 IdentifierInfo *AliasName,
168 SourceLocation AliasLocation,
169 IdentifierInfo *ClassName,
170 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000171 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000172 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000173 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000174 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000176 else
Chris Lattner65cae292008-11-19 08:23:25 +0000177 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000178 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 return 0;
180 }
181 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000182 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000183 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
184 QualType T = TDecl->getUnderlyingType();
185 if (T->isObjCInterfaceType()) {
186 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
187 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000188 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000189 }
190 }
191 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000192 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
193 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000194 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000195 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000196 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000197 return 0;
198 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000199
200 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000202 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000203
204 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000205
206 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000207 CurContext->addDecl(AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000208 if (!CheckObjCDeclScope(AliasDecl))
209 TUScope->AddDecl(AliasDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000210
Chris Lattner855e51f2007-12-12 07:09:47 +0000211 return AliasDecl;
212}
213
Chris Lattner2bdedd62008-07-26 04:03:38 +0000214Sema::DeclTy *
215Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
216 IdentifierInfo *ProtocolName,
217 SourceLocation ProtocolLoc,
218 DeclTy * const *ProtoRefs,
219 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000220 SourceLocation EndProtoLoc,
221 AttributeList *AttrList) {
222 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000223 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 if (PDecl) {
226 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000227 if (!PDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000228 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000229 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000230 // Just return the protocol we already had.
231 // FIXME: don't leak the objects passed in!
232 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000233 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000234 // Make sure the cached decl gets a valid start location.
235 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000236 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000237 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000238 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
239 AtProtoInterfaceLoc,ProtocolName);
240 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000241 CurContext->addDecl(PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000242 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000243 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000244 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000245 if (AttrList)
246 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000247 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000248 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000249 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000250 PDecl->setLocEnd(EndProtoLoc);
251 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000252
253 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000254 return PDecl;
255}
256
257/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000258/// issues an error if they are not declared. It returns list of
259/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000260void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000261Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000262 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000263 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000264 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000265 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000266 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
267 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000268 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000269 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000270 continue;
271 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000272
Douglas Gregoraa57e862009-02-18 21:56:37 +0000273 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000274
275 // If this is a forward declaration and we are supposed to warn in this
276 // case, do it.
277 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000278 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000279 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000280 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000281 }
282}
283
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000284/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000285/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000287void
288Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
289 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000290 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000291 ObjCPropertyDecl::PropertyAttributeKind CAttr =
292 Property->getPropertyAttributes();
293 ObjCPropertyDecl::PropertyAttributeKind SAttr =
294 SuperProperty->getPropertyAttributes();
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
296 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000297 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000298 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000299 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
300 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000301 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000302 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
304 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000307
308 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
309 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000310 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000311 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000312 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000313 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000314 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000315 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000316 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000317 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000318
Chris Lattnerc5cff302008-07-26 20:50:02 +0000319 if (Context.getCanonicalType(Property->getType()) !=
320 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000322 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000323
324}
325
326/// ComparePropertiesInBaseAndSuper - This routine compares property
327/// declarations in base and its super class, if any, and issues
328/// diagnostics in a variety of inconsistant situations.
329///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000330void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000331 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
332 if (!SDecl)
333 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000334 // FIXME: O(N^2)
Steve Naroff451f83c2009-01-09 15:36:25 +0000335 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
336 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000337 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000338 // Does property in super class has declaration in current class?
Steve Naroff451f83c2009-01-09 15:36:25 +0000339 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
340 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000341 ObjCPropertyDecl *PDecl = (*I);
342 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000343 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000344 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000345 }
346 }
347}
348
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000349/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
350/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000351/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000352void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000353Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000354 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000355 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
356 if (!IDecl) {
357 // Category
358 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
359 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff451f83c2009-01-09 15:36:25 +0000360 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
361 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000362 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000363 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000364 // Is this property already in category's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000365 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000366 CP != CE; ++CP)
367 if ((*CP)->getIdentifier() == Pr->getIdentifier())
368 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000369 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000370 // Property protocol already exist in class. Diagnose any mismatch.
371 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
372 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000373 return;
374 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000375 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
376 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000377 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000378 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000379 // Is this property already in class's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000380 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000381 CP != CE; ++CP)
382 if ((*CP)->getIdentifier() == Pr->getIdentifier())
383 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000384 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000385 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000386 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000387 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000388}
389
390/// MergeProtocolPropertiesIntoClass - This routine merges properties
391/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000392/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000393///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000394void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
395 DeclTy *MergeItsProtocols) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000396 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000397 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
398
399 if (!IDecl) {
400 // Category
401 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
402 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
403 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
404 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
405 E = MDecl->protocol_end(); P != E; ++P)
406 // Merge properties of category (*P) into IDECL's
407 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
408
409 // Go thru the list of protocols for this category and recursively merge
410 // their properties into this class as well.
411 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
412 E = CatDecl->protocol_end(); P != E; ++P)
413 MergeProtocolPropertiesIntoClass(CatDecl, *P);
414 } else {
415 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
416 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
417 E = MD->protocol_end(); P != E; ++P)
418 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
419 }
420 return;
421 }
422
Chris Lattner5cece462008-07-21 07:06:49 +0000423 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000424 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
425 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000426 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000427 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
428
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000429 // Go thru the list of protocols for this class and recursively merge
430 // their properties into this class as well.
431 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
432 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000433 MergeProtocolPropertiesIntoClass(IDecl, *P);
434 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000435 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
436 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
437 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000438 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000439 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000440}
441
Chris Lattner855e51f2007-12-12 07:09:47 +0000442/// ActOnForwardProtocolDeclaration -
443Action::DeclTy *
444Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000445 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000446 unsigned NumElts,
447 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000448 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000449
450 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000451 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000452 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000453 if (PDecl == 0) { // Not already seen?
454 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
455 IdentList[i].second, Ident);
456 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000457 CurContext->addDecl(PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000458 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000459 if (attrList)
460 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 Protocols.push_back(PDecl);
462 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000463
464 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000465 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000466 &Protocols[0], Protocols.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000467 CurContext->addDecl(PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000468 CheckObjCDeclScope(PDecl);
469 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000470}
471
Chris Lattnere705e5e2008-07-21 22:17:28 +0000472Sema::DeclTy *Sema::
473ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
474 IdentifierInfo *ClassName, SourceLocation ClassLoc,
475 IdentifierInfo *CategoryName,
476 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000477 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000478 unsigned NumProtoRefs,
479 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000480 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000481 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
482 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000483 CurContext->addDecl(CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000484
485 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000486 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000487 if (!IDecl || IDecl->isForwardDecl()) {
488 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000489 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner1fa7f622009-02-16 21:26:43 +0000490 return CDecl;
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000491 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000492
Chris Lattner1fa7f622009-02-16 21:26:43 +0000493 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000494
495 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000496 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000497
498 /// Check for duplicate interface declaration for this category
499 ObjCCategoryDecl *CDeclChain;
500 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
501 CDeclChain = CDeclChain->getNextClassCategory()) {
502 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
503 Diag(CategoryLoc, diag::warn_dup_category_def)
504 << ClassName << CategoryName;
505 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
506 break;
507 }
508 }
509 if (!CDeclChain)
510 CDecl->insertNextClassCategory();
511
Chris Lattner855e51f2007-12-12 07:09:47 +0000512 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000513 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000514 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000515 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000516
517 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000518 return CDecl;
519}
520
521/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000522/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000523/// object.
524Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
525 SourceLocation AtCatImplLoc,
526 IdentifierInfo *ClassName, SourceLocation ClassLoc,
527 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000528 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000529 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000530 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
531 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000532 /// Check that class of this category is already completely declared.
533 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000534 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000535
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000536 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000537 CurContext->addDecl(CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000538
Chris Lattner855e51f2007-12-12 07:09:47 +0000539 /// TODO: Check that CatName, category name, is not used in another
540 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000541 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000542
543 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000544 return CDecl;
545}
546
547Sema::DeclTy *Sema::ActOnStartClassImplementation(
548 SourceLocation AtClassImplLoc,
549 IdentifierInfo *ClassName, SourceLocation ClassLoc,
550 IdentifierInfo *SuperClassname,
551 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000552 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000554 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000555 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000556 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000557 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000558 }
559 else {
560 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000563 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000564 }
565
566 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000567 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000568 if (SuperClassname) {
569 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000570 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000571 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000572 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
573 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000574 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000575 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000576 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000577 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000578 Diag(SuperClassLoc, diag::err_undef_superclass)
579 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000580 else if (IDecl && IDecl->getSuperClass() != SDecl) {
581 // This implementation and its interface do not have the same
582 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000583 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000584 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000585 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000586 }
587 }
588 }
589
590 if (!IDecl) {
591 // Legacy case of @implementation with no corresponding @interface.
592 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000593
594 // FIXME: Do we support attributes on the @implementation? If so
595 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000596 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
597 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000598 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000599 IDecl->setSuperClass(SDecl);
600 IDecl->setLocEnd(ClassLoc);
601
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000602 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000603 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 // Remember that this needs to be removed when the scope is popped.
605 TUScope->AddDecl(IDecl);
606 }
607
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000609 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000610 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000611
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000612 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000613 CurContext->addDecl(IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000614
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000615 if (CheckObjCDeclScope(IMPDecl))
616 return IMPDecl;
617
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000620 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000622 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000623 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 return IMPDecl;
625}
626
Ted Kremenek42730c52008-01-07 19:49:32 +0000627void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
628 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 SourceLocation RBrace) {
630 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000631 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000632 if (!IDecl)
633 return;
634 /// Check case of non-existing @interface decl.
635 /// (legacy objective-c @implementation decl without an @interface decl).
636 /// Add implementations's ivar to the synthesize class's ivar list.
637 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000638 IDecl->setIVarList(ivars, numIvars, Context);
639 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 return;
641 }
642 // If implementation has empty ivar list, just return.
643 if (numIvars == 0)
644 return;
645
646 assert(ivars && "missing @implementation ivars");
647
648 // Check interface's Ivar list against those in the implementation.
649 // names and types must match.
650 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000652 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000653 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
654 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 ObjCIvarDecl* ImplIvar = ivars[j++];
656 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000657 assert (ImplIvar && "missing implementation ivar");
658 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000659 if (Context.getCanonicalType(ImplIvar->getType()) !=
660 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000661 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000662 << ImplIvar->getIdentifier()
663 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000664 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000665 }
666 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
667 // as error.
668 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000669 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000670 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000671 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000672 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000673 }
674 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000675 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000676
677 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000678 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000679 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000680 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000681}
682
Steve Naroffb4f48512008-02-10 21:38:56 +0000683void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
684 bool &IncompleteImpl) {
685 if (!IncompleteImpl) {
686 Diag(ImpLoc, diag::warn_incomplete_impl);
687 IncompleteImpl = true;
688 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000689 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000690}
691
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000692void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
693 ObjCMethodDecl *IntfMethodDecl) {
694 bool err = false;
695 QualType ImpMethodQType =
696 Context.getCanonicalType(ImpMethodDecl->getResultType());
697 QualType IntfMethodQType =
698 Context.getCanonicalType(IntfMethodDecl->getResultType());
699 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
700 err = true;
701 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
702 IF=IntfMethodDecl->param_begin(),
703 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
704 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
705 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
706 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
707 err = true;
708 break;
709 }
710 }
711 if (err) {
712 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
713 << ImpMethodDecl->getDeclName();
714 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
715 }
716}
717
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000718/// isPropertyReadonly - Return true if property is readonly, by searching
719/// for the property in the class and in its categories and implementations
720///
721bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
722 ObjCInterfaceDecl *IDecl) const {
723 // by far the most common case.
724 if (!PDecl->isReadOnly())
725 return false;
726 // Even if property is ready only, if interface has a user defined setter,
727 // it is not considered read only.
728 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
729 return false;
730
731 // Main class has the property as 'readonly'. Must search
732 // through the category list to see if the property's
733 // attribute has been over-ridden to 'readwrite'.
734 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
735 Category; Category = Category->getNextClassCategory()) {
736 // Even if property is ready only, if a category has a user defined setter,
737 // it is not considered read only.
738 if (Category->getInstanceMethod(PDecl->getSetterName()))
739 return false;
740 ObjCPropertyDecl *P =
741 Category->FindPropertyDeclaration(PDecl->getIdentifier());
742 if (P && !P->isReadOnly())
743 return false;
744 }
745
746 // Also, check for definition of a setter method in the implementation if
747 // all else failed.
748 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
749 if (ObjCImplementationDecl *IMD =
750 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
751 if (IMD->getInstanceMethod(PDecl->getSetterName()))
752 return false;
753 }
754 else if (ObjCCategoryImplDecl *CIMD =
755 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
756 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
757 return false;
758 }
759 }
760 return true;
761}
762
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000763/// FIXME: Type hierarchies in Objective-C can be deep. We could most
764/// likely improve the efficiency of selector lookups and type
765/// checking by associating with each protocol / interface / category
766/// the flattened instance tables. If we used an immutable set to keep
767/// the table then it wouldn't add significant memory cost and it
768/// would be handy for lookups.
769
Steve Naroffb268d2a2008-02-08 22:06:17 +0000770/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000771/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000772void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
773 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000774 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000775 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000776 const llvm::DenseSet<Selector> &ClsMap,
777 ObjCInterfaceDecl *IDecl) {
778 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
779
780 // If a method lookup fails locally we still need to look and see if
781 // the method was implemented by a base class or an inherited
782 // protocol. This lookup is slow, but occurs rarely in correct code
783 // and otherwise would terminate in a warning.
784
Chris Lattner855e51f2007-12-12 07:09:47 +0000785 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000787 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000788 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000789 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000790 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000791 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000792 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000793 }
794 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000795 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000796 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000797 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000798 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
799 !ClsMap.count(method->getSelector()) &&
800 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000801 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000802 }
Chris Lattner0be08822008-07-21 21:32:27 +0000803 // Check on this protocols's referenced protocols, recursively.
804 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
805 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000806 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000807}
808
Ted Kremenek42730c52008-01-07 19:49:32 +0000809void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
810 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000811 llvm::DenseSet<Selector> InsMap;
812 // Check and see if instance methods in class interface have been
813 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000814 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000815 E = IMPDecl->instmeth_end(); I != E; ++I)
816 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000817
818 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000819 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner28400082009-02-16 19:25:52 +0000820 E = IDecl->instmeth_end(); I != E; ++I) {
821 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000822 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000823 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000824 }
Chris Lattner28400082009-02-16 19:25:52 +0000825
826 ObjCMethodDecl *ImpMethodDecl =
827 IMPDecl->getInstanceMethod((*I)->getSelector());
828 ObjCMethodDecl *IntfMethodDecl =
829 IDecl->getInstanceMethod((*I)->getSelector());
830 assert(IntfMethodDecl &&
831 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
832 // ImpMethodDecl may be null as in a @dynamic property.
833 if (ImpMethodDecl)
834 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
835 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000836
Chris Lattner855e51f2007-12-12 07:09:47 +0000837 llvm::DenseSet<Selector> ClsMap;
838 // Check and see if class methods in class interface have been
839 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000841 E = IMPDecl->classmeth_end(); I != E; ++I)
842 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000843
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000845 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000846 if (!ClsMap.count((*I)->getSelector()))
847 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000848 else {
849 ObjCMethodDecl *ImpMethodDecl =
850 IMPDecl->getClassMethod((*I)->getSelector());
851 ObjCMethodDecl *IntfMethodDecl =
852 IDecl->getClassMethod((*I)->getSelector());
853 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
854 }
855
Chris Lattner855e51f2007-12-12 07:09:47 +0000856
857 // Check the protocol list for unimplemented methods in the @implementation
858 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000859 const ObjCList<ObjCProtocolDecl> &Protocols =
860 IDecl->getReferencedProtocols();
861 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
862 E = Protocols.end(); I != E; ++I)
863 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000864 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000865}
866
867/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000868/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000869void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
870 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000871 llvm::DenseSet<Selector> InsMap;
872 // Check and see if instance methods in category interface have been
873 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000874 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000875 E = CatImplDecl->instmeth_end(); I != E; ++I)
876 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000877
878 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000879 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000880 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000881 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000882 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000883 else {
884 ObjCMethodDecl *ImpMethodDecl =
885 CatImplDecl->getInstanceMethod((*I)->getSelector());
886 ObjCMethodDecl *IntfMethodDecl =
887 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000888 assert(IntfMethodDecl &&
889 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
890 // ImpMethodDecl may be null as in a @dynamic property.
891 if (ImpMethodDecl)
892 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000893 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000894
Chris Lattner855e51f2007-12-12 07:09:47 +0000895 llvm::DenseSet<Selector> ClsMap;
896 // Check and see if class methods in category interface have been
897 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000899 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
900 I != E; ++I)
901 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000902
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000904 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000905 if (!ClsMap.count((*I)->getSelector()))
906 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000907 else {
908 ObjCMethodDecl *ImpMethodDecl =
909 CatImplDecl->getClassMethod((*I)->getSelector());
910 ObjCMethodDecl *IntfMethodDecl =
911 CatClassDecl->getClassMethod((*I)->getSelector());
912 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
913 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000914 // Check the protocol list for unimplemented methods in the @implementation
915 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000916 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
917 E = CatClassDecl->protocol_end(); PI != E; ++PI)
918 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000919 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000920}
921
922/// ActOnForwardClassDeclaration -
923Action::DeclTy *
924Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000925 IdentifierInfo **IdentList,
926 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000927 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000928
929 for (unsigned i = 0; i != NumElts; ++i) {
930 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000931 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000932 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000933 // Maybe we will complain about the shadowed template parameter.
934 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
935 // Just pretend that we didn't see the previous declaration.
936 PrevDecl = 0;
937 }
938
Ted Kremenek42730c52008-01-07 19:49:32 +0000939 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000940 // GCC apparently allows the following idiom:
941 //
942 // typedef NSObject < XCElementTogglerP > XCElementToggler;
943 // @class XCElementToggler;
944 //
945 // FIXME: Make an extension?
946 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
947 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000948 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000949 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000950 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000951 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000952 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000953 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000954 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
955 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000956 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000957
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000958 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000959 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000960 // Remember that this needs to be removed when the scope is popped.
961 TUScope->AddDecl(IDecl);
962 }
963
964 Interfaces.push_back(IDecl);
965 }
966
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000967 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000968 &Interfaces[0],
969 Interfaces.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000970 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000971 CheckObjCDeclScope(CDecl);
972 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000973}
974
975
976/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
977/// returns true, or false, accordingly.
978/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000979bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000980 const ObjCMethodDecl *PrevMethod,
981 bool matchBasedOnSizeAndAlignment) {
982 QualType T1 = Context.getCanonicalType(Method->getResultType());
983 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
984
985 if (T1 != T2) {
986 // The result types are different.
987 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000988 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000989 // Incomplete types don't have a size and alignment.
990 if (T1->isIncompleteType() || T2->isIncompleteType())
991 return false;
992 // Check is based on size and alignment.
993 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
994 return false;
995 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000996
997 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
998 E = Method->param_end();
999 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1000
1001 for (; ParamI != E; ++ParamI, ++PrevI) {
1002 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1003 T1 = Context.getCanonicalType((*ParamI)->getType());
1004 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001005 if (T1 != T2) {
1006 // The result types are different.
1007 if (!matchBasedOnSizeAndAlignment)
1008 return false;
1009 // Incomplete types don't have a size and alignment.
1010 if (T1->isIncompleteType() || T2->isIncompleteType())
1011 return false;
1012 // Check is based on size and alignment.
1013 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1014 return false;
1015 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001016 }
1017 return true;
1018}
1019
Ted Kremenek42730c52008-01-07 19:49:32 +00001020void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1021 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001022 if (!FirstMethod.Method) {
1023 // Haven't seen a method with this selector name yet - add it.
1024 FirstMethod.Method = Method;
1025 FirstMethod.Next = 0;
1026 } else {
1027 // We've seen a method with this name, now check the type signature(s).
1028 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1029
Ted Kremenek42730c52008-01-07 19:49:32 +00001030 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001031 Next = Next->Next)
1032 match = MatchTwoMethodDeclarations(Method, Next->Method);
1033
1034 if (!match) {
1035 // We have a new signature for an existing method - add it.
1036 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +00001037 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +00001038 }
1039 }
1040}
1041
Steve Naroffec7e0882008-10-21 10:50:19 +00001042// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001043ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1044 SourceRange R) {
1045 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001046 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001047
1048 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001049 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1050 // This checks if the methods differ by size & alignment.
1051 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1052 issueWarning = true;
1053 }
1054 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001055 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001056 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001057 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001058 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001059 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001060 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001061 }
1062 return MethList.Method;
1063}
1064
Ted Kremenek42730c52008-01-07 19:49:32 +00001065void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1066 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001067 if (!FirstMethod.Method) {
1068 // Haven't seen a method with this selector name yet - add it.
1069 FirstMethod.Method = Method;
1070 FirstMethod.Next = 0;
1071 } else {
1072 // We've seen a method with this name, now check the type signature(s).
1073 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1074
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001076 Next = Next->Next)
1077 match = MatchTwoMethodDeclarations(Method, Next->Method);
1078
1079 if (!match) {
1080 // We have a new signature for an existing method - add it.
1081 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001082 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001083 FirstMethod.Next = OMI;
1084 }
1085 }
1086}
1087
Steve Naroffab63fd62009-01-08 17:28:14 +00001088/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1089/// have the property type and issue diagnostics if they don't.
1090/// Also synthesize a getter/setter method if none exist (and update the
1091/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1092/// methods is the "right" thing to do.
1093void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1094 ObjCContainerDecl *CD) {
1095 ObjCMethodDecl *GetterMethod, *SetterMethod;
1096
1097 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1098 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1099
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001100 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001101 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001102 Diag(property->getLocation(),
1103 diag::err_accessor_property_type_mismatch)
1104 << property->getDeclName()
1105 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001106 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1107 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001108
1109 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001110 if (Context.getCanonicalType(SetterMethod->getResultType())
1111 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001112 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001113 if (SetterMethod->param_size() != 1 ||
1114 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001115 Diag(property->getLocation(),
1116 diag::err_accessor_property_type_mismatch)
1117 << property->getDeclName()
1118 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001119 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1120 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001121 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001122
1123 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001124 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001125 // FIXME: The synthesized property we set here is misleading. We
1126 // almost always synthesize these methods unless the user explicitly
1127 // provided prototypes (which is odd, but allowed). Sema should be
1128 // typechecking that the declarations jive in that situation (which
1129 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001130 if (!GetterMethod) {
1131 // No instance method of same name as property getter name was found.
1132 // Declare a getter method and add it to the list of methods
1133 // for this class.
1134 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1135 property->getLocation(), property->getGetterName(),
1136 property->getType(), CD, true, false, true,
1137 (property->getPropertyImplementation() ==
1138 ObjCPropertyDecl::Optional) ?
1139 ObjCMethodDecl::Optional :
1140 ObjCMethodDecl::Required);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001141 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001142 } else
1143 // A user declared getter will be synthesize when @synthesize of
1144 // the property with the same name is seen in the @implementation
1145 GetterMethod->setIsSynthesized();
1146 property->setGetterMethodDecl(GetterMethod);
1147
1148 // Skip setter if property is read-only.
1149 if (!property->isReadOnly()) {
1150 // Find the default setter and if one not found, add one.
1151 if (!SetterMethod) {
1152 // No instance method of same name as property setter name was found.
1153 // Declare a setter method and add it to the list of methods
1154 // for this class.
1155 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1156 property->getLocation(),
1157 property->getSetterName(),
1158 Context.VoidTy, CD, true, false, true,
1159 (property->getPropertyImplementation() ==
1160 ObjCPropertyDecl::Optional) ?
1161 ObjCMethodDecl::Optional :
1162 ObjCMethodDecl::Required);
1163 // Invent the arguments for the setter. We don't bother making a
1164 // nice name for the argument.
1165 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1166 SourceLocation(),
1167 property->getIdentifier(),
1168 property->getType(),
1169 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001170 0);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001171 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001172 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001173 } else
1174 // A user declared setter will be synthesize when @synthesize of
1175 // the property with the same name is seen in the @implementation
1176 SetterMethod->setIsSynthesized();
1177 property->setSetterMethodDecl(SetterMethod);
1178 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001179 // Add any synthesized methods to the global pool. This allows us to
1180 // handle the following, which is supported by GCC (and part of the design).
1181 //
1182 // @interface Foo
1183 // @property double bar;
1184 // @end
1185 //
1186 // void thisIsUnfortunate() {
1187 // id foo;
1188 // double bar = [foo bar];
1189 // }
1190 //
Douglas Gregord1675382009-01-09 19:42:16 +00001191 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001192 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001193 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001194 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001195}
1196
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001197// Note: For class/category implemenations, allMethods/allProperties is
1198// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001199void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1200 DeclTy **allMethods, unsigned allNum,
1201 DeclTy **allProperties, unsigned pNum) {
1202 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1203
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001204 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1205 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001206 // should be true.
1207 if (!ClassDecl)
1208 return;
1209
Chris Lattner855e51f2007-12-12 07:09:47 +00001210 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001211 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1212 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001213 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001214
Steve Naroffab63fd62009-01-08 17:28:14 +00001215 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001216
1217 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1218 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1219 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1220
Chris Lattner855e51f2007-12-12 07:09:47 +00001221 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001222 ObjCMethodDecl *Method =
1223 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001224
1225 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001226 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001227 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001228 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001229 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1230 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001231 if ((isInterfaceDeclKind && PrevMethod && !match)
1232 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001233 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001234 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001235 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001236 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001237 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001238 InsMap[Method->getSelector()] = Method;
1239 /// The following allows us to typecheck messages to "id".
1240 AddInstanceMethodToGlobalPool(Method);
1241 }
1242 }
1243 else {
1244 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001245 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001246 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1247 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001248 if ((isInterfaceDeclKind && PrevMethod && !match)
1249 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001250 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001251 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001252 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001253 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001254 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001255 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001256 /// The following allows us to typecheck messages to "Class".
1257 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001258 }
1259 }
1260 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001261 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001262 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001263 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001264 ComparePropertiesInBaseAndSuper(I);
1265 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff451f83c2009-01-09 15:36:25 +00001266 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001267 // Categories are used to extend the class by declaring new methods.
1268 // By the same token, they are also used to add new properties. No
1269 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001270
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001271 // Merge protocol properties into category
1272 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner855e51f2007-12-12 07:09:47 +00001273 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001274 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1275 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1276 // user-defined setter/getter. It also synthesizes setter/getter methods
1277 // and adds them to the DeclContext and global method pools.
Chris Lattner22063402009-02-16 18:32:47 +00001278 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1279 E = CDecl->prop_end(); I != E; ++I)
1280 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001281 CDecl->setAtEndLoc(AtEndLoc);
1282 }
1283 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001284 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001285 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001286 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001287 } else if (ObjCCategoryImplDecl* CatImplClass =
1288 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001289 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001290
Chris Lattner855e51f2007-12-12 07:09:47 +00001291 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001292 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001293 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001294 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001295 Categories; Categories = Categories->getNextClassCategory()) {
1296 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1297 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1298 break;
1299 }
1300 }
1301 }
1302 }
1303}
1304
1305
1306/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1307/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001308static Decl::ObjCDeclQualifier
1309CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1310 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1311 if (PQTVal & ObjCDeclSpec::DQ_In)
1312 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1313 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1314 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1315 if (PQTVal & ObjCDeclSpec::DQ_Out)
1316 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1317 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1318 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1319 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1320 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1321 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1322 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001323
1324 return ret;
1325}
1326
1327Sema::DeclTy *Sema::ActOnMethodDeclaration(
1328 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001329 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001330 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001331 Selector Sel,
1332 // optional arguments. The number of types/arguments is obtained
1333 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001334 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001335 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001336 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1337 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001338 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001339
1340 // Make sure we can establish a context for the method.
1341 if (!ClassDecl) {
1342 Diag(MethodLoc, diag::error_missing_method_context);
1343 return 0;
1344 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001345 QualType resultDeclType;
1346
Steve Naroffa442ad92009-02-20 22:59:16 +00001347 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001348 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001349
1350 // Methods cannot return interface types. All ObjC objects are
1351 // passed by reference.
1352 if (resultDeclType->isObjCInterfaceType()) {
1353 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1354 << "returned";
1355 return 0;
1356 }
1357 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001358 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001359
Chris Lattner114add62008-03-16 00:49:28 +00001360 ObjCMethodDecl* ObjCMethod =
1361 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001362 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001363 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001364 false,
Chris Lattner114add62008-03-16 00:49:28 +00001365 MethodDeclKind == tok::objc_optional ?
1366 ObjCMethodDecl::Optional :
1367 ObjCMethodDecl::Required);
1368
Chris Lattnereee57c02008-04-04 06:12:32 +00001369 llvm::SmallVector<ParmVarDecl*, 16> Params;
1370
1371 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1372 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001373 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001374
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001375 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001376 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001377 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001378 if (argType->isArrayType()) { // (char *[]) -> (char **)
1379 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001380 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001381 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001382 else if (argType->isFunctionType())
1383 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001384 else if (argType->isObjCInterfaceType()) {
1385 // FIXME! provide more precise location for the parameter
Steve Naroffa442ad92009-02-20 22:59:16 +00001386 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1387 << "passed";
1388 ObjCMethod->setInvalidDecl();
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001389 return 0;
1390 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001391 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001392 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001393 ParmVarDecl* Param;
1394 if (originalArgType.isNull())
1395 Param = ParmVarDecl::Create(Context, ObjCMethod,
1396 SourceLocation(/*FIXME*/),
1397 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001398 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001399 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001400 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1401 SourceLocation(/*FIXME*/),
1402 ArgNames[i], argType, originalArgType,
1403 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001404
Chris Lattnereee57c02008-04-04 06:12:32 +00001405 Param->setObjCDeclQualifier(
1406 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1407 Params.push_back(Param);
1408 }
1409
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001410 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremenek42730c52008-01-07 19:49:32 +00001411 ObjCMethod->setObjCDeclQualifier(
1412 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1413 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001414
1415 if (AttrList)
1416 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001417
1418 // For implementations (which can be very "coarse grain"), we add the
1419 // method now. This allows the AST to implement lookup methods that work
1420 // incrementally (without waiting until we parse the @end). It also allows
1421 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001422 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001423 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001424 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001425 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001426 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001427 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001428 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001429 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001430 }
1431 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001432 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001433 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001434 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001435 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001436 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001437 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001438 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001439 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001440 }
1441 }
1442 if (PrevMethod) {
1443 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001444 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001445 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001446 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001447 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001448 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001449}
1450
Daniel Dunbar540ff472008-09-23 21:53:23 +00001451void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1452 SourceLocation Loc,
1453 unsigned &Attributes) {
1454 // FIXME: Improve the reported location.
1455
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001456 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001457 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001458 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1459 ObjCDeclSpec::DQ_PR_assign |
1460 ObjCDeclSpec::DQ_PR_copy |
1461 ObjCDeclSpec::DQ_PR_retain))) {
1462 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1463 "readwrite" :
1464 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1465 "assign" :
1466 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1467 "copy" : "retain";
1468
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001469 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001470 diag::err_objc_property_attr_mutually_exclusive :
1471 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001472 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001473 }
1474
1475 // Check for copy or retain on non-object types.
1476 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1477 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001478 Diag(Loc, diag::err_objc_property_requires_object)
1479 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001480 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1481 }
1482
1483 // Check for more than one of { assign, copy, retain }.
1484 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1485 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001486 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1487 << "assign" << "copy";
1488 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001489 }
1490 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001491 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1492 << "assign" << "retain";
1493 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001494 }
1495 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1496 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001497 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1498 << "copy" << "retain";
1499 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001500 }
1501 }
1502
1503 // Warn if user supplied no assignment attribute, property is
1504 // readwrite, and this is an object type.
1505 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1506 ObjCDeclSpec::DQ_PR_retain)) &&
1507 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1508 Context.isObjCObjectPointerType(PropertyTy)) {
1509 // Skip this warning in gc-only mode.
1510 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1511 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1512
1513 // If non-gc code warn that this is likely inappropriate.
1514 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1515 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1516
1517 // FIXME: Implement warning dependent on NSCopying being
1518 // implemented. See also:
1519 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1520 // (please trim this list while you are at it).
1521 }
1522}
1523
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001524Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1525 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001526 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001527 Selector GetterSel,
1528 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001529 DeclTy *ClassCategory,
1530 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001531 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001532 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001533 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1534 // default is readwrite!
1535 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1536 // property is defaulted to 'assign' if it is readwrite and is
1537 // not retain or copy
1538 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1539 (isReadWrite &&
1540 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1541 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1542 QualType T = GetTypeForDeclarator(FD.D, S);
1543 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001544
1545 // May modify Attributes.
1546 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001547
1548 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1549 if (!CDecl->getIdentifier()) {
1550 // This is an anonymous category. property requires special
1551 // handling.
1552 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1553 if (ObjCPropertyDecl *PIDecl =
1554 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1555 // property 'PIDecl's readonly attribute will be over-ridden
1556 // with anonymous category's readwrite property attribute!
1557 unsigned PIkind = PIDecl->getPropertyAttributes();
1558 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001559 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001560 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1561 Diag(AtLoc, diag::warn_property_attr_mismatch);
1562 PIDecl->makeitReadWriteAttribute();
1563 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1564 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1565 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1566 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1567 PIDecl->setSetterName(SetterSel);
1568 // FIXME: use a common routine with addPropertyMethods.
1569 ObjCMethodDecl *SetterDecl =
1570 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1571 Context.VoidTy,
1572 ICDecl,
1573 true, false, true,
1574 ObjCMethodDecl::Required);
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001575 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001576 SourceLocation(),
1577 FD.D.getIdentifier(),
Chris Lattnerdf6133b2009-02-20 21:35:13 +00001578 T, VarDecl::None, 0);
1579 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001580 PIDecl->setSetterMethodDecl(SetterDecl);
1581 }
1582 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001583 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001584 *isOverridingProperty = true;
1585 return 0;
1586 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001587 // No matching property found in the main class. Just fall thru
1588 // and add property to the anonymous category. It looks like
Ben Laurie74d601e2009-02-16 09:18:41 +00001589 // it works as is. This category becomes just like a category
1590 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001591 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001592 Diag(CDecl->getLocation(), diag::err_continuation_class);
1593 *isOverridingProperty = true;
1594 return 0;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001595 }
1596 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001597
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001598 Type *t = T.getTypePtr();
1599 if (t->isArrayType() || t->isFunctionType())
1600 Diag(AtLoc, diag::err_property_type) << T;
1601
Steve Naroffdcf1e842009-01-11 12:47:58 +00001602 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1603 assert(DC && "ClassDecl is not a DeclContext");
1604 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001605 FD.D.getIdentifier(), T);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001606 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001607
1608 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001609
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001610 // Regardless of setter/getter attribute, we save the default getter/setter
1611 // selector names in anticipation of declaration of setter/getter methods.
1612 PDecl->setGetterName(GetterSel);
1613 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001614
Daniel Dunbar540ff472008-09-23 21:53:23 +00001615 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001617
Daniel Dunbar540ff472008-09-23 21:53:23 +00001618 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001620
Daniel Dunbar540ff472008-09-23 21:53:23 +00001621 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001623
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001624 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001626
Daniel Dunbar540ff472008-09-23 21:53:23 +00001627 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001629
Daniel Dunbar540ff472008-09-23 21:53:23 +00001630 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001632
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001633 if (isAssign)
1634 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1635
Daniel Dunbar540ff472008-09-23 21:53:23 +00001636 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001638
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001639 if (MethodImplKind == tok::objc_required)
1640 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1641 else if (MethodImplKind == tok::objc_optional)
1642 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1643
Chris Lattner855e51f2007-12-12 07:09:47 +00001644 return PDecl;
1645}
1646
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001647/// ActOnPropertyImplDecl - This routine performs semantic checks and
1648/// builds the AST node for a property implementation declaration; declared
1649/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001650///
1651Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1652 SourceLocation PropertyLoc,
1653 bool Synthesize,
1654 DeclTy *ClassCatImpDecl,
1655 IdentifierInfo *PropertyId,
1656 IdentifierInfo *PropertyIvar) {
1657 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1658 // Make sure we have a context for the property implementation declaration.
1659 if (!ClassImpDecl) {
1660 Diag(AtLoc, diag::error_missing_property_context);
1661 return 0;
1662 }
1663 ObjCPropertyDecl *property = 0;
1664 ObjCInterfaceDecl* IDecl = 0;
1665 // Find the class or category class where this property must have
1666 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001667 ObjCImplementationDecl *IC = 0;
1668 ObjCCategoryImplDecl* CatImplClass = 0;
1669 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001670 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001671 // We always synthesize an interface for an implementation
1672 // without an interface decl. So, IDecl is always non-zero.
1673 assert(IDecl &&
1674 "ActOnPropertyImplDecl - @implementation without @interface");
1675
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001676 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001677 property = IDecl->FindPropertyDeclaration(PropertyId);
1678 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001679 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001680 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001681 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001682 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001683 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001684 if (Synthesize) {
1685 Diag(AtLoc, diag::error_synthesize_category_decl);
1686 return 0;
1687 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001688 IDecl = CatImplClass->getClassInterface();
1689 if (!IDecl) {
1690 Diag(AtLoc, diag::error_missing_property_interface);
1691 return 0;
1692 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001693 ObjCCategoryDecl *Category =
1694 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1695
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001696 // If category for this implementation not found, it is an error which
1697 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001698 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001699 return 0;
1700 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001701 property = Category->FindPropertyDeclaration(PropertyId);
1702 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001703 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001704 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001705 return 0;
1706 }
1707 }
1708 else {
1709 Diag(AtLoc, diag::error_bad_property_context);
1710 return 0;
1711 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001712 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001713 // Check that we have a valid, previously declared ivar for @synthesize
1714 if (Synthesize) {
1715 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001716 if (!PropertyIvar)
1717 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001718 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianbeae78e2009-02-16 19:35:27 +00001719 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001720 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001721 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001722 return 0;
1723 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001724 QualType PropType = Context.getCanonicalType(property->getType());
1725 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1726
Steve Naroff631e3922008-09-30 00:24:17 +00001727 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001728 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001729 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001730 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001731 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001732 return 0;
1733 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001734 else {
1735 // FIXME! Rules for properties are somewhat different that those
1736 // for assignments. Use a new routine to consolidate all cases;
1737 // specifically for property redeclarations as well as for ivars.
1738 QualType lhsType =
1739 Context.getCanonicalType(PropType).getUnqualifiedType();
1740 QualType rhsType =
1741 Context.getCanonicalType(IvarType).getUnqualifiedType();
1742 if (lhsType != rhsType &&
1743 lhsType->isArithmeticType()) {
1744 Diag(PropertyLoc, diag::error_property_ivar_type)
1745 << property->getDeclName() << Ivar->getDeclName();
1746 return 0;
1747 }
1748 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001749 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001750 } else if (PropertyIvar) {
1751 // @dynamic
1752 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1753 return 0;
1754 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001755 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001756 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001757 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1758 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001759 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001760 ObjCPropertyImplDecl::Synthesize
1761 : ObjCPropertyImplDecl::Dynamic),
1762 Ivar);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001763 CurContext->addDecl(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001764 if (IC) {
1765 if (Synthesize)
1766 if (ObjCPropertyImplDecl *PPIDecl =
1767 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1768 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1769 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1770 << PropertyIvar;
1771 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1772 }
1773
1774 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1775 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1776 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1777 return 0;
1778 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001779 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001780 }
1781 else {
1782 if (Synthesize)
1783 if (ObjCPropertyImplDecl *PPIDecl =
1784 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1785 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1786 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1787 << PropertyIvar;
1788 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1789 }
1790
1791 if (ObjCPropertyImplDecl *PPIDecl =
1792 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1793 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1794 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1795 return 0;
1796 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001797 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001798 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001799
1800 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001801}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001802
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001803bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001804 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001805 return false;
1806
1807 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1808 D->setInvalidDecl();
1809
1810 return true;
1811}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001812
1813/// Collect the instance variables declared in an Objective-C object. Used in
1814/// the creation of structures from objects using the @defs directive.
1815/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1816/// part of the AST generation logic of @defs.
1817static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1818 ASTContext& Ctx,
1819 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1820 if (Class->getSuperClass())
1821 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1822
1823 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1824 for (ObjCInterfaceDecl::ivar_iterator
1825 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1826
1827 ObjCIvarDecl* ID = *I;
1828 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1829 ID->getLocation(),
1830 ID->getIdentifier(),
1831 ID->getType(),
1832 ID->getBitWidth()));
1833 }
1834}
1835
1836/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1837/// instance variables of ClassName into Decls.
1838void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1839 IdentifierInfo *ClassName,
1840 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1841 // Check that ClassName is a valid class
1842 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1843 if (!Class) {
1844 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1845 return;
1846 }
1847 // Collect the instance variables
1848 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1849
1850 // Introduce all of these fields into the appropriate scope.
1851 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1852 D != Decls.end(); ++D) {
1853 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1854 if (getLangOptions().CPlusPlus)
1855 PushOnScopeChains(cast<FieldDecl>(FD), S);
1856 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001857 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001858 }
1859}
1860