blob: 65cd4b4e5485c463d77bb6f5d02ed3f9b2e03375 [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) {
155 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000156 IDecl->setLocEnd(EndProtoLoc);
157 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000158
159 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 return IDecl;
161}
162
163/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000164/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000165Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
166 IdentifierInfo *AliasName,
167 SourceLocation AliasLocation,
168 IdentifierInfo *ClassName,
169 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000170 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000171 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000172 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000173 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000174 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000175 else
Chris Lattner65cae292008-11-19 08:23:25 +0000176 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000177 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 return 0;
179 }
180 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000181 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000182 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
183 QualType T = TDecl->getUnderlyingType();
184 if (T->isObjCInterfaceType()) {
185 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
186 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000187 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000188 }
189 }
190 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000191 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
192 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000193 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000194 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000195 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000196 return 0;
197 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000198
199 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000200 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000201 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000202
203 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000204
205 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000206 CurContext->addDecl(AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000207 if (!CheckObjCDeclScope(AliasDecl))
208 TUScope->AddDecl(AliasDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000209
Chris Lattner855e51f2007-12-12 07:09:47 +0000210 return AliasDecl;
211}
212
Chris Lattner2bdedd62008-07-26 04:03:38 +0000213Sema::DeclTy *
214Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
215 IdentifierInfo *ProtocolName,
216 SourceLocation ProtocolLoc,
217 DeclTy * const *ProtoRefs,
218 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000219 SourceLocation EndProtoLoc,
220 AttributeList *AttrList) {
221 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000222 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000223 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000224 if (PDecl) {
225 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000226 if (!PDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000227 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000228 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000229 // Just return the protocol we already had.
230 // FIXME: don't leak the objects passed in!
231 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000232 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000233 // Make sure the cached decl gets a valid start location.
234 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000235 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000236 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000237 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
238 AtProtoInterfaceLoc,ProtocolName);
239 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000240 CurContext->addDecl(PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000241 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000242 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000243 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000244 if (AttrList)
245 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000246 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000247 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000248 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000249 PDecl->setLocEnd(EndProtoLoc);
250 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000251
252 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000253 return PDecl;
254}
255
256/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000257/// issues an error if they are not declared. It returns list of
258/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000259void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000260Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000261 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000262 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000263 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000264 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000265 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
266 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000267 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000268 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000269 continue;
270 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000271
Douglas Gregoraa57e862009-02-18 21:56:37 +0000272 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000273
274 // If this is a forward declaration and we are supposed to warn in this
275 // case, do it.
276 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000277 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000278 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000279 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000280 }
281}
282
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000283/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000284/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000285///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000286void
287Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
288 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000289 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290 ObjCPropertyDecl::PropertyAttributeKind CAttr =
291 Property->getPropertyAttributes();
292 ObjCPropertyDecl::PropertyAttributeKind SAttr =
293 SuperProperty->getPropertyAttributes();
294 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
295 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000296 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000297 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000298 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
299 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000300 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000301 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000302 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
303 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000304 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000305 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000306
307 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
308 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000309 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000310 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000312 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000313 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000314 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000315 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000316 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000317
Chris Lattnerc5cff302008-07-26 20:50:02 +0000318 if (Context.getCanonicalType(Property->getType()) !=
319 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000320 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000321 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000322
323}
324
325/// ComparePropertiesInBaseAndSuper - This routine compares property
326/// declarations in base and its super class, if any, and issues
327/// diagnostics in a variety of inconsistant situations.
328///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000329void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000330 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
331 if (!SDecl)
332 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000333 // FIXME: O(N^2)
Steve Naroff451f83c2009-01-09 15:36:25 +0000334 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
335 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000336 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000337 // Does property in super class has declaration in current class?
Steve Naroff451f83c2009-01-09 15:36:25 +0000338 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
339 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000340 ObjCPropertyDecl *PDecl = (*I);
341 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000342 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000343 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000344 }
345 }
346}
347
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000348/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
349/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000350/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000351void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000352Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000353 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000354 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
355 if (!IDecl) {
356 // Category
357 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
358 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff451f83c2009-01-09 15:36:25 +0000359 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
360 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000361 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000362 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000363 // Is this property already in category's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000364 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000365 CP != CE; ++CP)
366 if ((*CP)->getIdentifier() == Pr->getIdentifier())
367 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000368 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000369 // Property protocol already exist in class. Diagnose any mismatch.
370 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
371 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000372 return;
373 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000374 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
375 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000376 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000377 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000378 // Is this property already in class's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000379 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000380 CP != CE; ++CP)
381 if ((*CP)->getIdentifier() == Pr->getIdentifier())
382 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000383 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000384 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000385 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000386 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000387}
388
389/// MergeProtocolPropertiesIntoClass - This routine merges properties
390/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000391/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000392///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000393void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
394 DeclTy *MergeItsProtocols) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000395 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000396 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
397
398 if (!IDecl) {
399 // Category
400 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
401 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
402 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
403 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
404 E = MDecl->protocol_end(); P != E; ++P)
405 // Merge properties of category (*P) into IDECL's
406 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
407
408 // Go thru the list of protocols for this category and recursively merge
409 // their properties into this class as well.
410 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
411 E = CatDecl->protocol_end(); P != E; ++P)
412 MergeProtocolPropertiesIntoClass(CatDecl, *P);
413 } else {
414 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
415 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
416 E = MD->protocol_end(); P != E; ++P)
417 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
418 }
419 return;
420 }
421
Chris Lattner5cece462008-07-21 07:06:49 +0000422 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000423 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
424 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000425 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000426 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
427
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428 // Go thru the list of protocols for this class and recursively merge
429 // their properties into this class as well.
430 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
431 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000432 MergeProtocolPropertiesIntoClass(IDecl, *P);
433 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000434 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
435 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
436 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000437 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000438 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000439}
440
Chris Lattner855e51f2007-12-12 07:09:47 +0000441/// ActOnForwardProtocolDeclaration -
442Action::DeclTy *
443Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000444 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000445 unsigned NumElts,
446 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000448
449 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000450 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000451 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000452 if (PDecl == 0) { // Not already seen?
453 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
454 IdentList[i].second, Ident);
455 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000456 CurContext->addDecl(PDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000457 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000458 if (attrList)
459 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000460 Protocols.push_back(PDecl);
461 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000462
463 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000464 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000465 &Protocols[0], Protocols.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000466 CurContext->addDecl(PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000467 CheckObjCDeclScope(PDecl);
468 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000469}
470
Chris Lattnere705e5e2008-07-21 22:17:28 +0000471Sema::DeclTy *Sema::
472ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
473 IdentifierInfo *ClassName, SourceLocation ClassLoc,
474 IdentifierInfo *CategoryName,
475 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000476 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000477 unsigned NumProtoRefs,
478 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000479 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000480 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
481 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000482 CurContext->addDecl(CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000483
484 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000485 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000486 if (!IDecl || IDecl->isForwardDecl()) {
487 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000488 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner1fa7f622009-02-16 21:26:43 +0000489 return CDecl;
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000490 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000491
Chris Lattner1fa7f622009-02-16 21:26:43 +0000492 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000493
494 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000495 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000496
497 /// Check for duplicate interface declaration for this category
498 ObjCCategoryDecl *CDeclChain;
499 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
500 CDeclChain = CDeclChain->getNextClassCategory()) {
501 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
502 Diag(CategoryLoc, diag::warn_dup_category_def)
503 << ClassName << CategoryName;
504 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
505 break;
506 }
507 }
508 if (!CDeclChain)
509 CDecl->insertNextClassCategory();
510
Chris Lattner855e51f2007-12-12 07:09:47 +0000511 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000512 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
513 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000514 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000515
516 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 return CDecl;
518}
519
520/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000521/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000522/// object.
523Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
524 SourceLocation AtCatImplLoc,
525 IdentifierInfo *ClassName, SourceLocation ClassLoc,
526 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000527 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000528 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000529 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
530 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000531 /// Check that class of this category is already completely declared.
532 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000533 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000534
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000535 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000536 CurContext->addDecl(CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000537
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 /// TODO: Check that CatName, category name, is not used in another
539 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000540 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000541
542 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000543 return CDecl;
544}
545
546Sema::DeclTy *Sema::ActOnStartClassImplementation(
547 SourceLocation AtClassImplLoc,
548 IdentifierInfo *ClassName, SourceLocation ClassLoc,
549 IdentifierInfo *SuperClassname,
550 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000551 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000553 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000554 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000555 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000556 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000557 }
558 else {
559 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000561 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000562 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 }
564
565 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000566 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000567 if (SuperClassname) {
568 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000569 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000571 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
572 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000573 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000574 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000575 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000576 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000577 Diag(SuperClassLoc, diag::err_undef_superclass)
578 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 else if (IDecl && IDecl->getSuperClass() != SDecl) {
580 // This implementation and its interface do not have the same
581 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000582 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000583 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000584 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 }
586 }
587 }
588
589 if (!IDecl) {
590 // Legacy case of @implementation with no corresponding @interface.
591 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000592
593 // FIXME: Do we support attributes on the @implementation? If so
594 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000595 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
596 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000597 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000598 IDecl->setSuperClass(SDecl);
599 IDecl->setLocEnd(ClassLoc);
600
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000601 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000602 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000603 // Remember that this needs to be removed when the scope is popped.
604 TUScope->AddDecl(IDecl);
605 }
606
Ted Kremenek42730c52008-01-07 19:49:32 +0000607 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000608 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000609 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000610
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000611 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000612 CurContext->addDecl(IMPDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000613
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000614 if (CheckObjCDeclScope(IMPDecl))
615 return IMPDecl;
616
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000618 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000619 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000620 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 return IMPDecl;
624}
625
Ted Kremenek42730c52008-01-07 19:49:32 +0000626void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
627 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000628 SourceLocation RBrace) {
629 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000630 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 if (!IDecl)
632 return;
633 /// Check case of non-existing @interface decl.
634 /// (legacy objective-c @implementation decl without an @interface decl).
635 /// Add implementations's ivar to the synthesize class's ivar list.
636 if (IDecl->ImplicitInterfaceDecl()) {
637 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
638 return;
639 }
640 // If implementation has empty ivar list, just return.
641 if (numIvars == 0)
642 return;
643
644 assert(ivars && "missing @implementation ivars");
645
646 // Check interface's Ivar list against those in the implementation.
647 // names and types must match.
648 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000650 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000651 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
652 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 ObjCIvarDecl* ImplIvar = ivars[j++];
654 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000655 assert (ImplIvar && "missing implementation ivar");
656 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000657 if (Context.getCanonicalType(ImplIvar->getType()) !=
658 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000659 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000660 << ImplIvar->getIdentifier()
661 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000662 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000663 }
664 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
665 // as error.
666 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000667 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000668 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000669 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000670 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000671 }
672 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000673 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000674
675 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000676 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000677 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000678 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000679}
680
Steve Naroffb4f48512008-02-10 21:38:56 +0000681void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
682 bool &IncompleteImpl) {
683 if (!IncompleteImpl) {
684 Diag(ImpLoc, diag::warn_incomplete_impl);
685 IncompleteImpl = true;
686 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000687 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000688}
689
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000690void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
691 ObjCMethodDecl *IntfMethodDecl) {
692 bool err = false;
693 QualType ImpMethodQType =
694 Context.getCanonicalType(ImpMethodDecl->getResultType());
695 QualType IntfMethodQType =
696 Context.getCanonicalType(IntfMethodDecl->getResultType());
697 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
698 err = true;
699 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
700 IF=IntfMethodDecl->param_begin(),
701 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
702 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
703 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
704 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
705 err = true;
706 break;
707 }
708 }
709 if (err) {
710 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
711 << ImpMethodDecl->getDeclName();
712 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
713 }
714}
715
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000716/// isPropertyReadonly - Return true if property is readonly, by searching
717/// for the property in the class and in its categories and implementations
718///
719bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
720 ObjCInterfaceDecl *IDecl) const {
721 // by far the most common case.
722 if (!PDecl->isReadOnly())
723 return false;
724 // Even if property is ready only, if interface has a user defined setter,
725 // it is not considered read only.
726 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
727 return false;
728
729 // Main class has the property as 'readonly'. Must search
730 // through the category list to see if the property's
731 // attribute has been over-ridden to 'readwrite'.
732 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
733 Category; Category = Category->getNextClassCategory()) {
734 // Even if property is ready only, if a category has a user defined setter,
735 // it is not considered read only.
736 if (Category->getInstanceMethod(PDecl->getSetterName()))
737 return false;
738 ObjCPropertyDecl *P =
739 Category->FindPropertyDeclaration(PDecl->getIdentifier());
740 if (P && !P->isReadOnly())
741 return false;
742 }
743
744 // Also, check for definition of a setter method in the implementation if
745 // all else failed.
746 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
747 if (ObjCImplementationDecl *IMD =
748 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
749 if (IMD->getInstanceMethod(PDecl->getSetterName()))
750 return false;
751 }
752 else if (ObjCCategoryImplDecl *CIMD =
753 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
754 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
755 return false;
756 }
757 }
758 return true;
759}
760
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000761/// FIXME: Type hierarchies in Objective-C can be deep. We could most
762/// likely improve the efficiency of selector lookups and type
763/// checking by associating with each protocol / interface / category
764/// the flattened instance tables. If we used an immutable set to keep
765/// the table then it wouldn't add significant memory cost and it
766/// would be handy for lookups.
767
Steve Naroffb268d2a2008-02-08 22:06:17 +0000768/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000769/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000770void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
771 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000772 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000773 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000774 const llvm::DenseSet<Selector> &ClsMap,
775 ObjCInterfaceDecl *IDecl) {
776 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
777
778 // If a method lookup fails locally we still need to look and see if
779 // the method was implemented by a base class or an inherited
780 // protocol. This lookup is slow, but occurs rarely in correct code
781 // and otherwise would terminate in a warning.
782
Chris Lattner855e51f2007-12-12 07:09:47 +0000783 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000784 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000785 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000787 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000788 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000789 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000790 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000791 }
792 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000793 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000794 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000795 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000796 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
797 !ClsMap.count(method->getSelector()) &&
798 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000799 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000800 }
Chris Lattner0be08822008-07-21 21:32:27 +0000801 // Check on this protocols's referenced protocols, recursively.
802 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
803 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000804 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000805}
806
Ted Kremenek42730c52008-01-07 19:49:32 +0000807void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
808 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000809 llvm::DenseSet<Selector> InsMap;
810 // Check and see if instance methods in class interface have been
811 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000812 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000813 E = IMPDecl->instmeth_end(); I != E; ++I)
814 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000815
816 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner28400082009-02-16 19:25:52 +0000818 E = IDecl->instmeth_end(); I != E; ++I) {
819 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroffb4f48512008-02-10 21:38:56 +0000820 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner28400082009-02-16 19:25:52 +0000821 continue;
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000822 }
Chris Lattner28400082009-02-16 19:25:52 +0000823
824 ObjCMethodDecl *ImpMethodDecl =
825 IMPDecl->getInstanceMethod((*I)->getSelector());
826 ObjCMethodDecl *IntfMethodDecl =
827 IDecl->getInstanceMethod((*I)->getSelector());
828 assert(IntfMethodDecl &&
829 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
830 // ImpMethodDecl may be null as in a @dynamic property.
831 if (ImpMethodDecl)
832 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
833 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000834
Chris Lattner855e51f2007-12-12 07:09:47 +0000835 llvm::DenseSet<Selector> ClsMap;
836 // Check and see if class methods in class interface have been
837 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000838 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000839 E = IMPDecl->classmeth_end(); I != E; ++I)
840 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000841
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000843 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000844 if (!ClsMap.count((*I)->getSelector()))
845 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000846 else {
847 ObjCMethodDecl *ImpMethodDecl =
848 IMPDecl->getClassMethod((*I)->getSelector());
849 ObjCMethodDecl *IntfMethodDecl =
850 IDecl->getClassMethod((*I)->getSelector());
851 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
852 }
853
Chris Lattner855e51f2007-12-12 07:09:47 +0000854
855 // Check the protocol list for unimplemented methods in the @implementation
856 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000857 const ObjCList<ObjCProtocolDecl> &Protocols =
858 IDecl->getReferencedProtocols();
859 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
860 E = Protocols.end(); I != E; ++I)
861 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000862 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000863}
864
865/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000866/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000867void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
868 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000869 llvm::DenseSet<Selector> InsMap;
870 // Check and see if instance methods in category interface have been
871 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000872 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000873 E = CatImplDecl->instmeth_end(); I != E; ++I)
874 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000875
876 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000878 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000879 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000880 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000881 else {
882 ObjCMethodDecl *ImpMethodDecl =
883 CatImplDecl->getInstanceMethod((*I)->getSelector());
884 ObjCMethodDecl *IntfMethodDecl =
885 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000886 assert(IntfMethodDecl &&
887 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
888 // ImpMethodDecl may be null as in a @dynamic property.
889 if (ImpMethodDecl)
890 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000891 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000892
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 llvm::DenseSet<Selector> ClsMap;
894 // Check and see if class methods in category interface have been
895 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000897 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
898 I != E; ++I)
899 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000900
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000902 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000903 if (!ClsMap.count((*I)->getSelector()))
904 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000905 else {
906 ObjCMethodDecl *ImpMethodDecl =
907 CatImplDecl->getClassMethod((*I)->getSelector());
908 ObjCMethodDecl *IntfMethodDecl =
909 CatClassDecl->getClassMethod((*I)->getSelector());
910 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
911 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 // Check the protocol list for unimplemented methods in the @implementation
913 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000914 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
915 E = CatClassDecl->protocol_end(); PI != E; ++PI)
916 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000917 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000918}
919
920/// ActOnForwardClassDeclaration -
921Action::DeclTy *
922Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +0000923 IdentifierInfo **IdentList,
924 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000926
927 for (unsigned i = 0; i != NumElts; ++i) {
928 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000929 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000930 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000931 // Maybe we will complain about the shadowed template parameter.
932 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
933 // Just pretend that we didn't see the previous declaration.
934 PrevDecl = 0;
935 }
936
Ted Kremenek42730c52008-01-07 19:49:32 +0000937 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000938 // GCC apparently allows the following idiom:
939 //
940 // typedef NSObject < XCElementTogglerP > XCElementToggler;
941 // @class XCElementToggler;
942 //
943 // FIXME: Make an extension?
944 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
945 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000946 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000947 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000948 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000949 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000950 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000951 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000952 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
953 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000954 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000955
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000956 // FIXME: PushOnScopeChains?
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000957 CurContext->addDecl(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000958 // Remember that this needs to be removed when the scope is popped.
959 TUScope->AddDecl(IDecl);
960 }
961
962 Interfaces.push_back(IDecl);
963 }
964
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000965 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000966 &Interfaces[0],
967 Interfaces.size());
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000968 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000969 CheckObjCDeclScope(CDecl);
970 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000971}
972
973
974/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
975/// returns true, or false, accordingly.
976/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000977bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000978 const ObjCMethodDecl *PrevMethod,
979 bool matchBasedOnSizeAndAlignment) {
980 QualType T1 = Context.getCanonicalType(Method->getResultType());
981 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
982
983 if (T1 != T2) {
984 // The result types are different.
985 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000986 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000987 // Incomplete types don't have a size and alignment.
988 if (T1->isIncompleteType() || T2->isIncompleteType())
989 return false;
990 // Check is based on size and alignment.
991 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
992 return false;
993 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000994
995 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
996 E = Method->param_end();
997 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
998
999 for (; ParamI != E; ++ParamI, ++PrevI) {
1000 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1001 T1 = Context.getCanonicalType((*ParamI)->getType());
1002 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001003 if (T1 != T2) {
1004 // The result types are different.
1005 if (!matchBasedOnSizeAndAlignment)
1006 return false;
1007 // Incomplete types don't have a size and alignment.
1008 if (T1->isIncompleteType() || T2->isIncompleteType())
1009 return false;
1010 // Check is based on size and alignment.
1011 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1012 return false;
1013 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001014 }
1015 return true;
1016}
1017
Ted Kremenek42730c52008-01-07 19:49:32 +00001018void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1019 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001020 if (!FirstMethod.Method) {
1021 // Haven't seen a method with this selector name yet - add it.
1022 FirstMethod.Method = Method;
1023 FirstMethod.Next = 0;
1024 } else {
1025 // We've seen a method with this name, now check the type signature(s).
1026 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1027
Ted Kremenek42730c52008-01-07 19:49:32 +00001028 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001029 Next = Next->Next)
1030 match = MatchTwoMethodDeclarations(Method, Next->Method);
1031
1032 if (!match) {
1033 // We have a new signature for an existing method - add it.
1034 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +00001035 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +00001036 }
1037 }
1038}
1039
Steve Naroffec7e0882008-10-21 10:50:19 +00001040// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001041ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1042 SourceRange R) {
1043 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +00001044 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001045
1046 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001047 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1048 // This checks if the methods differ by size & alignment.
1049 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1050 issueWarning = true;
1051 }
1052 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001053 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001054 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001055 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001056 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001057 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001058 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001059 }
1060 return MethList.Method;
1061}
1062
Ted Kremenek42730c52008-01-07 19:49:32 +00001063void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1064 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001065 if (!FirstMethod.Method) {
1066 // Haven't seen a method with this selector name yet - add it.
1067 FirstMethod.Method = Method;
1068 FirstMethod.Next = 0;
1069 } else {
1070 // We've seen a method with this name, now check the type signature(s).
1071 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1072
Ted Kremenek42730c52008-01-07 19:49:32 +00001073 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001074 Next = Next->Next)
1075 match = MatchTwoMethodDeclarations(Method, Next->Method);
1076
1077 if (!match) {
1078 // We have a new signature for an existing method - add it.
1079 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001080 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001081 FirstMethod.Next = OMI;
1082 }
1083 }
1084}
1085
Steve Naroffab63fd62009-01-08 17:28:14 +00001086/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1087/// have the property type and issue diagnostics if they don't.
1088/// Also synthesize a getter/setter method if none exist (and update the
1089/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1090/// methods is the "right" thing to do.
1091void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1092 ObjCContainerDecl *CD) {
1093 ObjCMethodDecl *GetterMethod, *SetterMethod;
1094
1095 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1096 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1097
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001098 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001099 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001100 Diag(property->getLocation(),
1101 diag::err_accessor_property_type_mismatch)
1102 << property->getDeclName()
1103 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001104 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1105 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001106
1107 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001108 if (Context.getCanonicalType(SetterMethod->getResultType())
1109 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001110 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001111 if (SetterMethod->param_size() != 1 ||
1112 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001113 Diag(property->getLocation(),
1114 diag::err_accessor_property_type_mismatch)
1115 << property->getDeclName()
1116 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001117 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1118 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001119 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001120
1121 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001122 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001123 // FIXME: The synthesized property we set here is misleading. We
1124 // almost always synthesize these methods unless the user explicitly
1125 // provided prototypes (which is odd, but allowed). Sema should be
1126 // typechecking that the declarations jive in that situation (which
1127 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001128 if (!GetterMethod) {
1129 // No instance method of same name as property getter name was found.
1130 // Declare a getter method and add it to the list of methods
1131 // for this class.
1132 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1133 property->getLocation(), property->getGetterName(),
1134 property->getType(), CD, true, false, true,
1135 (property->getPropertyImplementation() ==
1136 ObjCPropertyDecl::Optional) ?
1137 ObjCMethodDecl::Optional :
1138 ObjCMethodDecl::Required);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001139 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001140 } else
1141 // A user declared getter will be synthesize when @synthesize of
1142 // the property with the same name is seen in the @implementation
1143 GetterMethod->setIsSynthesized();
1144 property->setGetterMethodDecl(GetterMethod);
1145
1146 // Skip setter if property is read-only.
1147 if (!property->isReadOnly()) {
1148 // Find the default setter and if one not found, add one.
1149 if (!SetterMethod) {
1150 // No instance method of same name as property setter name was found.
1151 // Declare a setter method and add it to the list of methods
1152 // for this class.
1153 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1154 property->getLocation(),
1155 property->getSetterName(),
1156 Context.VoidTy, CD, true, false, true,
1157 (property->getPropertyImplementation() ==
1158 ObjCPropertyDecl::Optional) ?
1159 ObjCMethodDecl::Optional :
1160 ObjCMethodDecl::Required);
1161 // Invent the arguments for the setter. We don't bother making a
1162 // nice name for the argument.
1163 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1164 SourceLocation(),
1165 property->getIdentifier(),
1166 property->getType(),
1167 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001168 0);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001169 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001170 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001171 } else
1172 // A user declared setter will be synthesize when @synthesize of
1173 // the property with the same name is seen in the @implementation
1174 SetterMethod->setIsSynthesized();
1175 property->setSetterMethodDecl(SetterMethod);
1176 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001177 // Add any synthesized methods to the global pool. This allows us to
1178 // handle the following, which is supported by GCC (and part of the design).
1179 //
1180 // @interface Foo
1181 // @property double bar;
1182 // @end
1183 //
1184 // void thisIsUnfortunate() {
1185 // id foo;
1186 // double bar = [foo bar];
1187 // }
1188 //
Douglas Gregord1675382009-01-09 19:42:16 +00001189 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001190 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001191 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001192 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001193}
1194
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001195// Note: For class/category implemenations, allMethods/allProperties is
1196// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001197void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1198 DeclTy **allMethods, unsigned allNum,
1199 DeclTy **allProperties, unsigned pNum) {
1200 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1201
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001202 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1203 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001204 // should be true.
1205 if (!ClassDecl)
1206 return;
1207
Chris Lattner855e51f2007-12-12 07:09:47 +00001208 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001209 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1210 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001211 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001212
Steve Naroffab63fd62009-01-08 17:28:14 +00001213 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001214
1215 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1216 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1217 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1218
Chris Lattner855e51f2007-12-12 07:09:47 +00001219 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001220 ObjCMethodDecl *Method =
1221 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001222
1223 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001224 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001225 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001226 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001227 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1228 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001229 if ((isInterfaceDeclKind && PrevMethod && !match)
1230 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001231 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001232 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001233 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001234 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001235 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001236 InsMap[Method->getSelector()] = Method;
1237 /// The following allows us to typecheck messages to "id".
1238 AddInstanceMethodToGlobalPool(Method);
1239 }
1240 }
1241 else {
1242 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001243 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001244 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1245 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001246 if ((isInterfaceDeclKind && PrevMethod && !match)
1247 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001248 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001249 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001250 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001251 } else {
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001252 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001253 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001254 /// The following allows us to typecheck messages to "Class".
1255 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001256 }
1257 }
1258 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001259 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001260 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001261 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001262 ComparePropertiesInBaseAndSuper(I);
1263 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff451f83c2009-01-09 15:36:25 +00001264 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001265 // Categories are used to extend the class by declaring new methods.
1266 // By the same token, they are also used to add new properties. No
1267 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001268
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001269 // Merge protocol properties into category
1270 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner855e51f2007-12-12 07:09:47 +00001271 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001272 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1273 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1274 // user-defined setter/getter. It also synthesizes setter/getter methods
1275 // and adds them to the DeclContext and global method pools.
Chris Lattner22063402009-02-16 18:32:47 +00001276 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1277 E = CDecl->prop_end(); I != E; ++I)
1278 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001279 CDecl->setAtEndLoc(AtEndLoc);
1280 }
1281 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001282 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001283 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001284 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001285 } else if (ObjCCategoryImplDecl* CatImplClass =
1286 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001287 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001288
Chris Lattner855e51f2007-12-12 07:09:47 +00001289 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001290 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001291 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001292 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001293 Categories; Categories = Categories->getNextClassCategory()) {
1294 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1295 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1296 break;
1297 }
1298 }
1299 }
1300 }
1301}
1302
1303
1304/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1305/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001306static Decl::ObjCDeclQualifier
1307CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1308 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1309 if (PQTVal & ObjCDeclSpec::DQ_In)
1310 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1311 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1312 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1313 if (PQTVal & ObjCDeclSpec::DQ_Out)
1314 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1315 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1316 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1317 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1318 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1319 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1320 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001321
1322 return ret;
1323}
1324
1325Sema::DeclTy *Sema::ActOnMethodDeclaration(
1326 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001327 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001328 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001329 Selector Sel,
1330 // optional arguments. The number of types/arguments is obtained
1331 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001332 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001333 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001334 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1335 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001336 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001337
1338 // Make sure we can establish a context for the method.
1339 if (!ClassDecl) {
1340 Diag(MethodLoc, diag::error_missing_method_context);
1341 return 0;
1342 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001343 QualType resultDeclType;
1344
1345 if (ReturnType)
1346 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1347 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001348 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001349
Chris Lattner114add62008-03-16 00:49:28 +00001350 ObjCMethodDecl* ObjCMethod =
1351 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001352 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001353 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001354 false,
Chris Lattner114add62008-03-16 00:49:28 +00001355 MethodDeclKind == tok::objc_optional ?
1356 ObjCMethodDecl::Optional :
1357 ObjCMethodDecl::Required);
1358
Chris Lattnereee57c02008-04-04 06:12:32 +00001359 llvm::SmallVector<ParmVarDecl*, 16> Params;
1360
1361 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1362 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001363 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001364
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001365 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001366 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001367 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001368 if (argType->isArrayType()) { // (char *[]) -> (char **)
1369 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001370 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001371 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001372 else if (argType->isFunctionType())
1373 argType = Context.getPointerType(argType);
Fariborz Jahanian7b4695f2009-01-17 21:57:49 +00001374 else if (argType->isObjCInterfaceType()) {
1375 // FIXME! provide more precise location for the parameter
1376 Diag(MethodLoc, diag::err_object_as_method_param);
1377 return 0;
1378 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001379 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001380 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001381 ParmVarDecl* Param;
1382 if (originalArgType.isNull())
1383 Param = ParmVarDecl::Create(Context, ObjCMethod,
1384 SourceLocation(/*FIXME*/),
1385 ArgNames[i], argType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001386 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001387 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001388 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1389 SourceLocation(/*FIXME*/),
1390 ArgNames[i], argType, originalArgType,
1391 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001392
Chris Lattnereee57c02008-04-04 06:12:32 +00001393 Param->setObjCDeclQualifier(
1394 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1395 Params.push_back(Param);
1396 }
1397
Ted Kremenek42730c52008-01-07 19:49:32 +00001398 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1399 ObjCMethod->setObjCDeclQualifier(
1400 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1401 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001402
1403 if (AttrList)
1404 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001405
1406 // For implementations (which can be very "coarse grain"), we add the
1407 // method now. This allows the AST to implement lookup methods that work
1408 // incrementally (without waiting until we parse the @end). It also allows
1409 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001410 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001411 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001412 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001413 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001414 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001415 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001416 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001417 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001418 }
1419 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001420 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001421 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001422 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001423 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001424 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001425 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001426 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001427 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001428 }
1429 }
1430 if (PrevMethod) {
1431 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001432 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001433 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001434 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001435 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001436 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001437}
1438
Daniel Dunbar540ff472008-09-23 21:53:23 +00001439void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1440 SourceLocation Loc,
1441 unsigned &Attributes) {
1442 // FIXME: Improve the reported location.
1443
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001444 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001445 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001446 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1447 ObjCDeclSpec::DQ_PR_assign |
1448 ObjCDeclSpec::DQ_PR_copy |
1449 ObjCDeclSpec::DQ_PR_retain))) {
1450 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1451 "readwrite" :
1452 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1453 "assign" :
1454 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1455 "copy" : "retain";
1456
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001457 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001458 diag::err_objc_property_attr_mutually_exclusive :
1459 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001460 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001461 }
1462
1463 // Check for copy or retain on non-object types.
1464 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1465 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001466 Diag(Loc, diag::err_objc_property_requires_object)
1467 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001468 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1469 }
1470
1471 // Check for more than one of { assign, copy, retain }.
1472 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1473 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001474 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1475 << "assign" << "copy";
1476 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001477 }
1478 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001479 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1480 << "assign" << "retain";
1481 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001482 }
1483 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1484 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001485 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1486 << "copy" << "retain";
1487 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001488 }
1489 }
1490
1491 // Warn if user supplied no assignment attribute, property is
1492 // readwrite, and this is an object type.
1493 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1494 ObjCDeclSpec::DQ_PR_retain)) &&
1495 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1496 Context.isObjCObjectPointerType(PropertyTy)) {
1497 // Skip this warning in gc-only mode.
1498 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1499 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1500
1501 // If non-gc code warn that this is likely inappropriate.
1502 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1503 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1504
1505 // FIXME: Implement warning dependent on NSCopying being
1506 // implemented. See also:
1507 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1508 // (please trim this list while you are at it).
1509 }
1510}
1511
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001512Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1513 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001514 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001515 Selector GetterSel,
1516 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001517 DeclTy *ClassCategory,
1518 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001519 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001520 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001521 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1522 // default is readwrite!
1523 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1524 // property is defaulted to 'assign' if it is readwrite and is
1525 // not retain or copy
1526 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1527 (isReadWrite &&
1528 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1529 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1530 QualType T = GetTypeForDeclarator(FD.D, S);
1531 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001532
1533 // May modify Attributes.
1534 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001535
1536 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1537 if (!CDecl->getIdentifier()) {
1538 // This is an anonymous category. property requires special
1539 // handling.
1540 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1541 if (ObjCPropertyDecl *PIDecl =
1542 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1543 // property 'PIDecl's readonly attribute will be over-ridden
1544 // with anonymous category's readwrite property attribute!
1545 unsigned PIkind = PIDecl->getPropertyAttributes();
1546 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001547 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001548 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1549 Diag(AtLoc, diag::warn_property_attr_mismatch);
1550 PIDecl->makeitReadWriteAttribute();
1551 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1552 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1553 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1554 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1555 PIDecl->setSetterName(SetterSel);
1556 // FIXME: use a common routine with addPropertyMethods.
1557 ObjCMethodDecl *SetterDecl =
1558 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1559 Context.VoidTy,
1560 ICDecl,
1561 true, false, true,
1562 ObjCMethodDecl::Required);
1563 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1564 SetterDecl,
1565 SourceLocation(),
1566 FD.D.getIdentifier(),
1567 T,
1568 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001569 0);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001570 SetterDecl->setMethodParams(&Argument, 1);
1571 PIDecl->setSetterMethodDecl(SetterDecl);
1572 }
1573 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001574 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001575 *isOverridingProperty = true;
1576 return 0;
1577 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001578 // No matching property found in the main class. Just fall thru
1579 // and add property to the anonymous category. It looks like
Ben Laurie74d601e2009-02-16 09:18:41 +00001580 // it works as is. This category becomes just like a category
1581 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001582 } else {
1583 Diag(CDecl->getLocation(), diag::err_continuation_class);
1584 *isOverridingProperty = true;
1585 return 0;
1586 }
1587 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001588
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001589 Type *t = T.getTypePtr();
1590 if (t->isArrayType() || t->isFunctionType())
1591 Diag(AtLoc, diag::err_property_type) << T;
1592
Steve Naroffdcf1e842009-01-11 12:47:58 +00001593 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1594 assert(DC && "ClassDecl is not a DeclContext");
1595 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001596 FD.D.getIdentifier(), T);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001597 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001598
1599 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001600
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001601 // Regardless of setter/getter attribute, we save the default getter/setter
1602 // selector names in anticipation of declaration of setter/getter methods.
1603 PDecl->setGetterName(GetterSel);
1604 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001605
Daniel Dunbar540ff472008-09-23 21:53:23 +00001606 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001608
Daniel Dunbar540ff472008-09-23 21:53:23 +00001609 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001611
Daniel Dunbar540ff472008-09-23 21:53:23 +00001612 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001614
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001615 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001617
Daniel Dunbar540ff472008-09-23 21:53:23 +00001618 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001620
Daniel Dunbar540ff472008-09-23 21:53:23 +00001621 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001623
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001624 if (isAssign)
1625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1626
Daniel Dunbar540ff472008-09-23 21:53:23 +00001627 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001629
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001630 if (MethodImplKind == tok::objc_required)
1631 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1632 else if (MethodImplKind == tok::objc_optional)
1633 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1634
Chris Lattner855e51f2007-12-12 07:09:47 +00001635 return PDecl;
1636}
1637
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001638/// ActOnPropertyImplDecl - This routine performs semantic checks and
1639/// builds the AST node for a property implementation declaration; declared
1640/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001641///
1642Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1643 SourceLocation PropertyLoc,
1644 bool Synthesize,
1645 DeclTy *ClassCatImpDecl,
1646 IdentifierInfo *PropertyId,
1647 IdentifierInfo *PropertyIvar) {
1648 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1649 // Make sure we have a context for the property implementation declaration.
1650 if (!ClassImpDecl) {
1651 Diag(AtLoc, diag::error_missing_property_context);
1652 return 0;
1653 }
1654 ObjCPropertyDecl *property = 0;
1655 ObjCInterfaceDecl* IDecl = 0;
1656 // Find the class or category class where this property must have
1657 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001658 ObjCImplementationDecl *IC = 0;
1659 ObjCCategoryImplDecl* CatImplClass = 0;
1660 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001661 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001662 // We always synthesize an interface for an implementation
1663 // without an interface decl. So, IDecl is always non-zero.
1664 assert(IDecl &&
1665 "ActOnPropertyImplDecl - @implementation without @interface");
1666
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001667 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001668 property = IDecl->FindPropertyDeclaration(PropertyId);
1669 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001670 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001671 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001672 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001673 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001674 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001675 if (Synthesize) {
1676 Diag(AtLoc, diag::error_synthesize_category_decl);
1677 return 0;
1678 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001679 IDecl = CatImplClass->getClassInterface();
1680 if (!IDecl) {
1681 Diag(AtLoc, diag::error_missing_property_interface);
1682 return 0;
1683 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001684 ObjCCategoryDecl *Category =
1685 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1686
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001687 // If category for this implementation not found, it is an error which
1688 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001689 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001690 return 0;
1691 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001692 property = Category->FindPropertyDeclaration(PropertyId);
1693 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001694 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001695 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001696 return 0;
1697 }
1698 }
1699 else {
1700 Diag(AtLoc, diag::error_bad_property_context);
1701 return 0;
1702 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001703 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001704 // Check that we have a valid, previously declared ivar for @synthesize
1705 if (Synthesize) {
1706 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001707 if (!PropertyIvar)
1708 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001709 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianbeae78e2009-02-16 19:35:27 +00001710 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001711 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001712 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001713 return 0;
1714 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001715 QualType PropType = Context.getCanonicalType(property->getType());
1716 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1717
Steve Naroff631e3922008-09-30 00:24:17 +00001718 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001719 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001720 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001721 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001722 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001723 return 0;
1724 }
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00001725 else {
1726 // FIXME! Rules for properties are somewhat different that those
1727 // for assignments. Use a new routine to consolidate all cases;
1728 // specifically for property redeclarations as well as for ivars.
1729 QualType lhsType =
1730 Context.getCanonicalType(PropType).getUnqualifiedType();
1731 QualType rhsType =
1732 Context.getCanonicalType(IvarType).getUnqualifiedType();
1733 if (lhsType != rhsType &&
1734 lhsType->isArithmeticType()) {
1735 Diag(PropertyLoc, diag::error_property_ivar_type)
1736 << property->getDeclName() << Ivar->getDeclName();
1737 return 0;
1738 }
1739 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001740 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001741 } else if (PropertyIvar) {
1742 // @dynamic
1743 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1744 return 0;
1745 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001746 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001747 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001748 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1749 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001750 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001751 ObjCPropertyImplDecl::Synthesize
1752 : ObjCPropertyImplDecl::Dynamic),
1753 Ivar);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001754 CurContext->addDecl(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001755 if (IC) {
1756 if (Synthesize)
1757 if (ObjCPropertyImplDecl *PPIDecl =
1758 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1759 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1760 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1761 << PropertyIvar;
1762 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1763 }
1764
1765 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1766 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1767 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1768 return 0;
1769 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001770 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001771 }
1772 else {
1773 if (Synthesize)
1774 if (ObjCPropertyImplDecl *PPIDecl =
1775 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1776 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1777 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1778 << PropertyIvar;
1779 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1780 }
1781
1782 if (ObjCPropertyImplDecl *PPIDecl =
1783 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1784 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1785 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1786 return 0;
1787 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001788 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001789 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001790
1791 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001792}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001793
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001794bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001795 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001796 return false;
1797
1798 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1799 D->setInvalidDecl();
1800
1801 return true;
1802}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001803
1804/// Collect the instance variables declared in an Objective-C object. Used in
1805/// the creation of structures from objects using the @defs directive.
1806/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1807/// part of the AST generation logic of @defs.
1808static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1809 ASTContext& Ctx,
1810 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1811 if (Class->getSuperClass())
1812 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1813
1814 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1815 for (ObjCInterfaceDecl::ivar_iterator
1816 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1817
1818 ObjCIvarDecl* ID = *I;
1819 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1820 ID->getLocation(),
1821 ID->getIdentifier(),
1822 ID->getType(),
1823 ID->getBitWidth()));
1824 }
1825}
1826
1827/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1828/// instance variables of ClassName into Decls.
1829void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1830 IdentifierInfo *ClassName,
1831 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1832 // Check that ClassName is a valid class
1833 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1834 if (!Class) {
1835 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1836 return;
1837 }
1838 // Collect the instance variables
1839 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1840
1841 // Introduce all of these fields into the appropriate scope.
1842 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1843 D != Decls.end(); ++D) {
1844 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1845 if (getLangOptions().CPlusPlus)
1846 PushOnScopeChains(cast<FieldDecl>(FD), S);
1847 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001848 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001849 }
1850}
1851