blob: 27f127716e5d1401784d9042b814c0d563932293 [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"
Douglas Gregorc3221aa2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff58d0d6f2009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000020using namespace clang;
21
Steve Naroff8df46022009-02-28 16:59:13 +000022/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000024void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000027
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000031
Chris Lattnerc92920c2009-04-19 05:21:20 +000032 CurFunctionNeedsScopeChecking = false;
33
Steve Narofffe9eb6a2007-12-18 01:30:32 +000034 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000035 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000036 AddInstanceMethodToGlobalPool(MDecl);
37 else
38 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
40 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000041 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000042
43 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000048
Daniel Dunbareaf91c32008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000051
Chris Lattner97316c02008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000057}
58
Chris Lattner5261d0c2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000068 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremenek42730c52008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000079 }
80
Ted Kremenek42730c52008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +000085 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +000086 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000087 Diag(IDecl->getLocation(), diag::note_previous_definition);
88
Steve Naroff1422a622008-11-18 19:15:30 +000089 // Return the previous class interface.
90 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +000091 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +000092 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000093 IDecl->setLocation(AtInterfaceLoc);
94 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000095 }
Chris Lattner5cece462008-07-21 07:06:49 +000096 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000097 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000098 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000099 if (AttrList)
100 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000101
Steve Naroff104956f2009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103 }
104
105 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000106 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000107 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000108
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000109 ObjCInterfaceDecl *SuperClassDecl =
110 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000111
112 // Diagnose classes that inherit from deprecated classes.
113 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000114 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000115
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000116 if (PrevDecl && SuperClassDecl == 0) {
117 // The previous declaration was not a class decl. Check if we have a
118 // typedef. If we do, get the underlying class type.
119 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
120 QualType T = TDecl->getUnderlyingType();
121 if (T->isObjCInterfaceType()) {
122 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
123 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
124 }
125 }
Chris Lattner7699a532009-02-16 21:33:09 +0000126
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000127 // This handles the following case:
128 //
129 // typedef int SuperClass;
130 // @interface MyClass : SuperClass {} @end
131 //
132 if (!SuperClassDecl) {
133 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
134 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
135 }
136 }
Chris Lattner7699a532009-02-16 21:33:09 +0000137
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000138 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000140 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000141 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000142 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000144 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000145 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000146 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000147 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000148 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000149 IDecl->setLocEnd(SuperLoc);
150 } else { // we have a root class.
151 IDecl->setLocEnd(ClassLoc);
152 }
153
Steve Naroff1422a622008-11-18 19:15:30 +0000154 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000155 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000156 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
157 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 IDecl->setLocEnd(EndProtoLoc);
159 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000160
161 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000162 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000163}
164
165/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000166/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000167Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
168 IdentifierInfo *AliasName,
169 SourceLocation AliasLocation,
170 IdentifierInfo *ClassName,
171 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000172 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000173 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000174 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000175 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000176 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000177 else
Chris Lattner65cae292008-11-19 08:23:25 +0000178 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000179 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000180 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000181 }
182 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000183 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000184 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
185 QualType T = TDecl->getUnderlyingType();
186 if (T->isObjCInterfaceType()) {
187 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
188 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000189 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000190 }
191 }
192 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000193 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
194 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000195 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000196 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000197 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000198 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000199 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000200
201 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000202 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000203 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000204
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000205 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor54713b62009-04-24 02:57:34 +0000206 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000207
Chris Lattner5261d0c2009-03-28 19:18:32 +0000208 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000209}
210
Steve Narofffe56a632009-03-05 15:22:01 +0000211void Sema::CheckForwardProtocolDeclarationForCircularDependency(
212 IdentifierInfo *PName,
213 SourceLocation &Ploc, SourceLocation PrevLoc,
214 const ObjCList<ObjCProtocolDecl> &PList)
215{
216 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
217 E = PList.end(); I != E; ++I) {
218
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000219 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-03-05 15:22:01 +0000220 if (PDecl->getIdentifier() == PName) {
221 Diag(Ploc, diag::err_protocol_has_circular_dependency);
222 Diag(PrevLoc, diag::note_previous_definition);
223 }
224 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
225 PDecl->getLocation(), PDecl->getReferencedProtocols());
226 }
227 }
228}
229
Chris Lattner5261d0c2009-03-28 19:18:32 +0000230Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000231Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
232 IdentifierInfo *ProtocolName,
233 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000234 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000235 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000236 SourceLocation EndProtoLoc,
237 AttributeList *AttrList) {
238 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000239 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000240 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000241 if (PDecl) {
242 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000243 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000244 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000245 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000246 // Just return the protocol we already had.
247 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000248 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000249 }
Steve Narofffe56a632009-03-05 15:22:01 +0000250 ObjCList<ObjCProtocolDecl> PList;
251 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
252 CheckForwardProtocolDeclarationForCircularDependency(
253 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
254 PList.Destroy(Context);
255
Steve Naroff9a9e5212008-08-13 16:39:22 +0000256 // Make sure the cached decl gets a valid start location.
257 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000258 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000259 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000260 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
261 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000262 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000263 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000264 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000265 if (AttrList)
266 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000267 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000268 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000269 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000270 PDecl->setLocEnd(EndProtoLoc);
271 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000272
273 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000274 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000275}
276
277/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000278/// issues an error if they are not declared. It returns list of
279/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000280void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000281Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000282 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000283 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000284 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000285 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000286 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000287 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000288 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000289 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000290 continue;
291 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000292
Douglas Gregoraa57e862009-02-18 21:56:37 +0000293 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000294
295 // If this is a forward declaration and we are supposed to warn in this
296 // case, do it.
297 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000298 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000299 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000300 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000301 }
302}
303
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000304/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000305/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000306///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000307void
308Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
309 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000310 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311 ObjCPropertyDecl::PropertyAttributeKind CAttr =
312 Property->getPropertyAttributes();
313 ObjCPropertyDecl::PropertyAttributeKind SAttr =
314 SuperProperty->getPropertyAttributes();
315 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
316 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000317 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000318 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000319 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
320 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000322 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000323 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
324 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000325 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000326 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000327
328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
329 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000330 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000331 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000332 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000334 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000335 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000337 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000338
339 QualType LHSType =
340 Context.getCanonicalType(SuperProperty->getType());
341 QualType RHSType =
342 Context.getCanonicalType(Property->getType());
343
344 if (!Context.typesAreCompatible(LHSType, RHSType)) {
345 // FIXME: Incorporate this test with typesAreCompatible.
346 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
347 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
348 return;
349 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
350 << Property->getType() << SuperProperty->getType() << inheritedName;
351 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000352}
353
354/// ComparePropertiesInBaseAndSuper - This routine compares property
355/// declarations in base and its super class, if any, and issues
356/// diagnostics in a variety of inconsistant situations.
357///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000358void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000359 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
360 if (!SDecl)
361 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000362 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000363 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
364 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000365 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000366 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000367 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
368 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000369 ObjCPropertyDecl *PDecl = (*I);
370 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000371 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000372 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000373 }
374 }
375}
376
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000377/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
378/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000379/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000380void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000381Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000382 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000383 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
384 if (!IDecl) {
385 // Category
386 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
387 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000388 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
389 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000390 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000391 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000392 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000393 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000394 CP != CE; ++CP)
395 if ((*CP)->getIdentifier() == Pr->getIdentifier())
396 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000397 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000398 // Property protocol already exist in class. Diagnose any mismatch.
399 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
400 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401 return;
402 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000403 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
404 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000405 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000406 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000407 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000408 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000409 CP != CE; ++CP)
410 if ((*CP)->getIdentifier() == Pr->getIdentifier())
411 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000412 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000413 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000414 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000415 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000416}
417
418/// MergeProtocolPropertiesIntoClass - This routine merges properties
419/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000420/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000421///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000422void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000423 DeclPtrTy MergeItsProtocols) {
424 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000425 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
426
427 if (!IDecl) {
428 // Category
429 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
430 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
431 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
432 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
433 E = MDecl->protocol_end(); P != E; ++P)
434 // Merge properties of category (*P) into IDECL's
435 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
436
437 // Go thru the list of protocols for this category and recursively merge
438 // their properties into this class as well.
439 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
440 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000441 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000442 } else {
443 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
444 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
445 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000446 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000447 }
448 return;
449 }
450
Chris Lattner5cece462008-07-21 07:06:49 +0000451 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000452 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
453 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000454 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000455 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
456
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000457 // Go thru the list of protocols for this class and recursively merge
458 // their properties into this class as well.
459 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
460 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000461 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000462 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000463 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
464 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
465 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000466 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000467 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000468}
469
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000470/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000471/// a class method in its extension.
472///
473void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
474 ObjCInterfaceDecl *ID) {
475 if (!ID)
476 return; // Possibly due to previous error
477
478 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000479 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
480 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000481 ObjCMethodDecl *MD = *i;
482 MethodMap[MD->getSelector()] = MD;
483 }
484
485 if (MethodMap.empty())
486 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000487 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
488 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000489 ObjCMethodDecl *Method = *i;
490 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
491 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
492 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
493 << Method->getDeclName();
494 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
495 }
496 }
497}
498
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000499/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000500Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000501Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000502 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000503 unsigned NumElts,
504 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000505 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000506
507 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000508 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000509 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000510 if (PDecl == 0) { // Not already seen?
511 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
512 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000513 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000514 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000515 if (attrList)
516 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 Protocols.push_back(PDecl);
518 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000519
520 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000521 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000522 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000523 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000524 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000525 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000526}
527
Chris Lattner5261d0c2009-03-28 19:18:32 +0000528Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000529ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
531 IdentifierInfo *CategoryName,
532 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000533 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000534 unsigned NumProtoRefs,
535 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000536 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000537 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
538 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000539 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000540
541 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000542 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000543 if (!IDecl || IDecl->isForwardDecl()) {
544 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000545 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000546 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000547 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000548
Chris Lattner1fa7f622009-02-16 21:26:43 +0000549 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000550
551 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000552 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000553
554 /// Check for duplicate interface declaration for this category
555 ObjCCategoryDecl *CDeclChain;
556 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
557 CDeclChain = CDeclChain->getNextClassCategory()) {
558 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
559 Diag(CategoryLoc, diag::warn_dup_category_def)
560 << ClassName << CategoryName;
561 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
562 break;
563 }
564 }
565 if (!CDeclChain)
566 CDecl->insertNextClassCategory();
567
Chris Lattner855e51f2007-12-12 07:09:47 +0000568 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000569 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000570 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000572
573 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000574 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000575}
576
577/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000578/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000579/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000580Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 SourceLocation AtCatImplLoc,
582 IdentifierInfo *ClassName, SourceLocation ClassLoc,
583 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000585 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000586 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
587 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000588 /// Check that class of this category is already completely declared.
589 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000591
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000592 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000593 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000594
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 /// TODO: Check that CatName, category name, is not used in another
596 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000597 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000598
599 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000600 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000601}
602
Chris Lattner5261d0c2009-03-28 19:18:32 +0000603Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 SourceLocation AtClassImplLoc,
605 IdentifierInfo *ClassName, SourceLocation ClassLoc,
606 IdentifierInfo *SuperClassname,
607 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000609 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000610 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000611 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000612 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000613 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000614 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000617 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000618 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000619 IDecl = 0;
620 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 }
622
623 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 if (SuperClassname) {
626 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000627 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000629 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
630 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000631 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000632 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000633 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_undef_superclass)
636 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000637 else if (IDecl && IDecl->getSuperClass() != SDecl) {
638 // This implementation and its interface do not have the same
639 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000640 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000641 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000642 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000643 }
644 }
645 }
646
647 if (!IDecl) {
648 // Legacy case of @implementation with no corresponding @interface.
649 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000650
651 // FIXME: Do we support attributes on the @implementation? If so
652 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000653 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
654 ClassName, ClassLoc, false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000655 IDecl->setSuperClass(SDecl);
656 IDecl->setLocEnd(ClassLoc);
Douglas Gregor453e11d2009-04-24 00:16:12 +0000657
658 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000659 } else {
660 // Mark the interface as being completed, even if it was just as
661 // @class ....;
662 // declaration; the user cannot reopen it.
663 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000664 }
665
Ted Kremenek42730c52008-01-07 19:49:32 +0000666 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000667 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000668 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000669
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000670 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000671 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000672
Chris Lattner855e51f2007-12-12 07:09:47 +0000673 // Check that there is no duplicate implementation of this class.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000674 if (LookupObjCImplementation(ClassName))
Chris Lattner1b6de332008-03-16 20:53:07 +0000675 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000676 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000677 else // add it to the list.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000678 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000679 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000680}
681
Ted Kremenek42730c52008-01-07 19:49:32 +0000682void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
683 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000684 SourceLocation RBrace) {
685 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000686 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000687 if (!IDecl)
688 return;
689 /// Check case of non-existing @interface decl.
690 /// (legacy objective-c @implementation decl without an @interface decl).
691 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000692 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000693 IDecl->setIVarList(ivars, numIvars, Context);
694 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000695 return;
696 }
697 // If implementation has empty ivar list, just return.
698 if (numIvars == 0)
699 return;
700
701 assert(ivars && "missing @implementation ivars");
702
703 // Check interface's Ivar list against those in the implementation.
704 // names and types must match.
705 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000706 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000707 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000708 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
709 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000710 ObjCIvarDecl* ImplIvar = ivars[j++];
711 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000712 assert (ImplIvar && "missing implementation ivar");
713 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000714
715 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000716 if (Context.getCanonicalType(ImplIvar->getType()) !=
717 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000718 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000719 << ImplIvar->getIdentifier()
720 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000721 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000722 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
723 Expr *ImplBitWidth = ImplIvar->getBitWidth();
724 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman5255e7a2009-04-26 19:19:15 +0000725 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
726 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000727 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
728 << ImplIvar->getIdentifier();
729 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
730 }
731 }
732 // Make sure the names are identical.
733 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000734 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000735 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000736 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000737 }
738 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000739 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000740
741 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000742 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000743 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000744 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000745}
746
Steve Naroffb4f48512008-02-10 21:38:56 +0000747void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
748 bool &IncompleteImpl) {
749 if (!IncompleteImpl) {
750 Diag(ImpLoc, diag::warn_incomplete_impl);
751 IncompleteImpl = true;
752 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000753 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000754}
755
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000756void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
757 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000758 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000759 ImpMethodDecl->getResultType())) {
760 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
761 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
762 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000763 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
764 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000765
766 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
767 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
768 IM != EM; ++IM, ++IF) {
769 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
770 continue;
771
772 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
773 << ImpMethodDecl->getDeclName() << (*IF)->getType()
774 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000775 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000776 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000777}
778
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000779/// isPropertyReadonly - Return true if property is readonly, by searching
780/// for the property in the class and in its categories and implementations
781///
782bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000783 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000784 // by far the most common case.
785 if (!PDecl->isReadOnly())
786 return false;
787 // Even if property is ready only, if interface has a user defined setter,
788 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000789 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000790 return false;
791
792 // Main class has the property as 'readonly'. Must search
793 // through the category list to see if the property's
794 // attribute has been over-ridden to 'readwrite'.
795 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
796 Category; Category = Category->getNextClassCategory()) {
797 // Even if property is ready only, if a category has a user defined setter,
798 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000799 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000800 return false;
801 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000802 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000803 if (P && !P->isReadOnly())
804 return false;
805 }
806
807 // Also, check for definition of a setter method in the implementation if
808 // all else failed.
809 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
810 if (ObjCImplementationDecl *IMD =
811 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000812 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000813 return false;
814 }
815 else if (ObjCCategoryImplDecl *CIMD =
816 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000817 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000818 return false;
819 }
820 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000821 // Lastly, look through the implementation (if one is in scope).
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000822 if (ObjCImplementationDecl *ImpDecl
823 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000824 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000825 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000826 // If all fails, look at the super class.
827 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
828 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000829 return true;
830}
831
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000832/// FIXME: Type hierarchies in Objective-C can be deep. We could most
833/// likely improve the efficiency of selector lookups and type
834/// checking by associating with each protocol / interface / category
835/// the flattened instance tables. If we used an immutable set to keep
836/// the table then it wouldn't add significant memory cost and it
837/// would be handy for lookups.
838
Steve Naroffb268d2a2008-02-08 22:06:17 +0000839/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000840/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000841void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
842 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000843 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000844 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000845 const llvm::DenseSet<Selector> &ClsMap,
846 ObjCInterfaceDecl *IDecl) {
847 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
848
849 // If a method lookup fails locally we still need to look and see if
850 // the method was implemented by a base class or an inherited
851 // protocol. This lookup is slow, but occurs rarely in correct code
852 // and otherwise would terminate in a warning.
853
Chris Lattner855e51f2007-12-12 07:09:47 +0000854 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000855 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
856 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000857 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000858 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000859 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000860 (!Super ||
861 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000862 // Ugly, but necessary. Method declared in protcol might have
863 // have been synthesized due to a property declared in the class which
864 // uses the protocol.
865 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000866 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000867 if (!MethodInClass || !MethodInClass->isSynthesized())
868 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
869 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000870 }
871 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000872 for (ObjCProtocolDecl::classmeth_iterator
873 I = PDecl->classmeth_begin(Context),
874 E = PDecl->classmeth_end(Context);
875 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000877 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
878 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000879 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000880 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000881 }
Chris Lattner0be08822008-07-21 21:32:27 +0000882 // Check on this protocols's referenced protocols, recursively.
883 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
884 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000885 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000886}
887
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000888/// MatchAllMethodDeclarations - Check methods declaraed in interface or
889/// or protocol against those declared in their implementations.
890///
891void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
892 const llvm::DenseSet<Selector> &ClsMap,
893 llvm::DenseSet<Selector> &InsMapSeen,
894 llvm::DenseSet<Selector> &ClsMapSeen,
895 ObjCImplDecl* IMPDecl,
896 ObjCContainerDecl* CDecl,
897 bool &IncompleteImpl,
898 bool ImmediateClass)
899{
900 // Check and see if instance methods in class interface have been
901 // implemented in the implementation class. If so, their types match.
902 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
903 E = CDecl->instmeth_end(Context); I != E; ++I) {
904 if (InsMapSeen.count((*I)->getSelector()))
905 continue;
906 InsMapSeen.insert((*I)->getSelector());
907 if (!(*I)->isSynthesized() &&
908 !InsMap.count((*I)->getSelector())) {
909 if (ImmediateClass)
910 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
911 continue;
912 }
913 else {
914 ObjCMethodDecl *ImpMethodDecl =
915 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
916 ObjCMethodDecl *IntfMethodDecl =
917 CDecl->getInstanceMethod(Context, (*I)->getSelector());
918 assert(IntfMethodDecl &&
919 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
920 // ImpMethodDecl may be null as in a @dynamic property.
921 if (ImpMethodDecl)
922 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
923 }
924 }
925
926 // Check and see if class methods in class interface have been
927 // implemented in the implementation class. If so, their types match.
928 for (ObjCInterfaceDecl::classmeth_iterator
929 I = CDecl->classmeth_begin(Context),
930 E = CDecl->classmeth_end(Context);
931 I != E; ++I) {
932 if (ClsMapSeen.count((*I)->getSelector()))
933 continue;
934 ClsMapSeen.insert((*I)->getSelector());
935 if (!ClsMap.count((*I)->getSelector())) {
936 if (ImmediateClass)
937 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
938 }
939 else {
940 ObjCMethodDecl *ImpMethodDecl =
941 IMPDecl->getClassMethod(Context, (*I)->getSelector());
942 ObjCMethodDecl *IntfMethodDecl =
943 CDecl->getClassMethod(Context, (*I)->getSelector());
944 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
945 }
946 }
947 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
948 // Check for any implementation of a methods declared in protocol.
949 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
950 E = I->protocol_end(); PI != E; ++PI)
951 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
952 IMPDecl,
953 (*PI), IncompleteImpl, false);
954 if (I->getSuperClass())
955 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
956 IMPDecl,
957 I->getSuperClass(), IncompleteImpl, false);
958 }
959}
960
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000961void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
962 ObjCContainerDecl* CDecl,
963 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000964 llvm::DenseSet<Selector> InsMap;
965 // Check and see if instance methods in class interface have been
966 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000967 for (ObjCImplementationDecl::instmeth_iterator
968 I = IMPDecl->instmeth_begin(Context),
969 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000970 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000971
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000972 // Check and see if properties declared in the interface have either 1)
973 // an implementation or 2) there is a @synthesize/@dynamic implementation
974 // of the property in the @implementation.
975 if (isa<ObjCInterfaceDecl>(CDecl))
976 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
977 E = CDecl->prop_end(Context); P != E; ++P) {
978 ObjCPropertyDecl *Prop = (*P);
979 if (Prop->isInvalidDecl())
980 continue;
981 ObjCPropertyImplDecl *PI = 0;
982 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +0000983 for (ObjCImplDecl::propimpl_iterator
984 I = IMPDecl->propimpl_begin(Context),
985 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000986 if ((*I)->getPropertyDecl() == Prop) {
987 PI = (*I);
988 break;
989 }
990 if (PI)
991 continue;
992 if (!InsMap.count(Prop->getGetterName())) {
993 Diag(Prop->getLocation(),
994 diag::warn_setter_getter_impl_required)
995 << Prop->getDeclName() << Prop->getGetterName();
996 Diag(IMPDecl->getLocation(),
997 diag::note_property_impl_required);
998 }
999
1000 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1001 Diag(Prop->getLocation(),
1002 diag::warn_setter_getter_impl_required)
1003 << Prop->getDeclName() << Prop->getSetterName();
1004 Diag(IMPDecl->getLocation(),
1005 diag::note_property_impl_required);
1006 }
1007 }
1008
Chris Lattner855e51f2007-12-12 07:09:47 +00001009 llvm::DenseSet<Selector> ClsMap;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001010 for (ObjCImplementationDecl::classmeth_iterator
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001011 I = IMPDecl->classmeth_begin(Context),
1012 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +00001013 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +00001014
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001015 // Check for type conflict of methods declared in a class/protocol and
1016 // its implementation; if any.
1017 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1018 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1019 IMPDecl, CDecl,
1020 IncompleteImpl, true);
Chris Lattner855e51f2007-12-12 07:09:47 +00001021
1022 // Check the protocol list for unimplemented methods in the @implementation
1023 // class.
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001024 // Check and see if class methods in class interface have been
1025 // implemented in the implementation class.
1026
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001027 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001028 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001029 E = I->protocol_end(); PI != E; ++PI)
1030 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1031 InsMap, ClsMap, I);
1032 // Check class extensions (unnamed categories)
1033 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1034 Categories; Categories = Categories->getNextClassCategory()) {
1035 if (!Categories->getIdentifier()) {
1036 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1037 break;
1038 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +00001039 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001040 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1041 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1042 E = C->protocol_end(); PI != E; ++PI)
1043 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1044 InsMap, ClsMap, C->getClassInterface());
1045 } else
1046 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001047}
1048
1049/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001050Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001051Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001052 IdentifierInfo **IdentList,
1053 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001055
1056 for (unsigned i = 0; i != NumElts; ++i) {
1057 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001058 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001059 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001060 // Maybe we will complain about the shadowed template parameter.
1061 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1062 // Just pretend that we didn't see the previous declaration.
1063 PrevDecl = 0;
1064 }
1065
Ted Kremenek42730c52008-01-07 19:49:32 +00001066 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001067 // GCC apparently allows the following idiom:
1068 //
1069 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1070 // @class XCElementToggler;
1071 //
1072 // FIXME: Make an extension?
1073 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1074 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001075 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001076 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001077 }
Fariborz Jahanian4c16d762009-05-07 21:49:26 +00001078 else if (TDD) {
1079 // a forward class declaration matching a typedef name of a class
1080 // refers to the underlying class.
1081 if (ObjCInterfaceType * OI =
1082 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1083 PrevDecl = OI->getDecl();
1084 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001085 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001087 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001088 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1089 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001090 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001091 }
1092
1093 Interfaces.push_back(IDecl);
1094 }
1095
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001096 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001097 &Interfaces[0],
1098 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001099 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001100 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001101 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001102}
1103
1104
1105/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1106/// returns true, or false, accordingly.
1107/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001108bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001109 const ObjCMethodDecl *PrevMethod,
1110 bool matchBasedOnSizeAndAlignment) {
1111 QualType T1 = Context.getCanonicalType(Method->getResultType());
1112 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1113
1114 if (T1 != T2) {
1115 // The result types are different.
1116 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001117 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001118 // Incomplete types don't have a size and alignment.
1119 if (T1->isIncompleteType() || T2->isIncompleteType())
1120 return false;
1121 // Check is based on size and alignment.
1122 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1123 return false;
1124 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001125
1126 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1127 E = Method->param_end();
1128 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1129
1130 for (; ParamI != E; ++ParamI, ++PrevI) {
1131 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1132 T1 = Context.getCanonicalType((*ParamI)->getType());
1133 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001134 if (T1 != T2) {
1135 // The result types are different.
1136 if (!matchBasedOnSizeAndAlignment)
1137 return false;
1138 // Incomplete types don't have a size and alignment.
1139 if (T1->isIncompleteType() || T2->isIncompleteType())
1140 return false;
1141 // Check is based on size and alignment.
1142 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1143 return false;
1144 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001145 }
1146 return true;
1147}
1148
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001149/// \brief Read the contents of the instance and factory method pools
1150/// for a given selector from external storage.
1151///
1152/// This routine should only be called once, when neither the instance
1153/// nor the factory method pool has an entry for this selector.
1154Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1155 bool isInstance) {
1156 assert(ExternalSource && "We need an external AST source");
1157 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1158 "Selector data already loaded into the instance method pool");
1159 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1160 "Selector data already loaded into the factory method pool");
1161
1162 // Read the method list from the external source.
1163 std::pair<ObjCMethodList, ObjCMethodList> Methods
1164 = ExternalSource->ReadMethodPool(Sel);
1165
1166 if (isInstance) {
1167 if (Methods.second.Method)
1168 FactoryMethodPool[Sel] = Methods.second;
1169 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1170 }
1171
1172 if (Methods.first.Method)
1173 InstanceMethodPool[Sel] = Methods.first;
1174
1175 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1176}
1177
Ted Kremenek42730c52008-01-07 19:49:32 +00001178void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001179 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1180 = InstanceMethodPool.find(Method->getSelector());
1181 if (Pos == InstanceMethodPool.end()) {
1182 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1183 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1184 else
1185 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1186 ObjCMethodList())).first;
1187 }
1188
1189 ObjCMethodList &Entry = Pos->second;
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001190 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001191 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001192 Entry.Method = Method;
1193 Entry.Next = 0;
1194 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001195 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001196
1197 // We've seen a method with this name, see if we have already seen this type
1198 // signature.
1199 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1200 if (MatchTwoMethodDeclarations(Method, List->Method))
1201 return;
1202
1203 // We have a new signature for an existing method - add it.
1204 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1205 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001206}
1207
Steve Naroffec7e0882008-10-21 10:50:19 +00001208// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001209ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1210 SourceRange R) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001211 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1212 = InstanceMethodPool.find(Sel);
Douglas Gregor89f2c8b2009-04-24 22:23:41 +00001213 if (Pos == InstanceMethodPool.end()) {
1214 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001215 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1216 else
1217 return 0;
1218 }
1219
1220 ObjCMethodList &MethList = Pos->second;
Steve Naroffb91afca2008-10-21 10:37:50 +00001221 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001222
1223 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001224 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1225 // This checks if the methods differ by size & alignment.
1226 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1227 issueWarning = true;
1228 }
1229 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001230 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001231 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001232 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001233 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001234 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001235 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001236 }
1237 return MethList.Method;
1238}
1239
Ted Kremenek42730c52008-01-07 19:49:32 +00001240void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001241 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1242 = FactoryMethodPool.find(Method->getSelector());
1243 if (Pos == FactoryMethodPool.end()) {
1244 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1245 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1246 else
1247 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1248 ObjCMethodList())).first;
1249 }
1250
1251 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner855e51f2007-12-12 07:09:47 +00001252 if (!FirstMethod.Method) {
1253 // Haven't seen a method with this selector name yet - add it.
1254 FirstMethod.Method = Method;
1255 FirstMethod.Next = 0;
1256 } else {
1257 // We've seen a method with this name, now check the type signature(s).
1258 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1259
Ted Kremenek42730c52008-01-07 19:49:32 +00001260 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001261 Next = Next->Next)
1262 match = MatchTwoMethodDeclarations(Method, Next->Method);
1263
1264 if (!match) {
1265 // We have a new signature for an existing method - add it.
1266 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001268 FirstMethod.Next = OMI;
1269 }
1270 }
1271}
1272
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001273ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1274 SourceRange R) {
1275 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1276 = FactoryMethodPool.find(Sel);
1277 if (Pos == FactoryMethodPool.end()) {
1278 if (ExternalSource && !InstanceMethodPool.count(Sel))
1279 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1280 else
1281 return 0;
1282 }
1283
1284 ObjCMethodList &MethList = Pos->second;
1285 bool issueWarning = false;
1286
1287 if (MethList.Method && MethList.Next) {
1288 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1289 // This checks if the methods differ by size & alignment.
1290 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1291 issueWarning = true;
1292 }
1293 if (issueWarning && (MethList.Method && MethList.Next)) {
1294 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1295 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1296 << MethList.Method->getSourceRange();
1297 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1298 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1299 << Next->Method->getSourceRange();
1300 }
1301 return MethList.Method;
1302}
1303
Steve Naroffab63fd62009-01-08 17:28:14 +00001304/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1305/// have the property type and issue diagnostics if they don't.
1306/// Also synthesize a getter/setter method if none exist (and update the
1307/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1308/// methods is the "right" thing to do.
1309void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1310 ObjCContainerDecl *CD) {
1311 ObjCMethodDecl *GetterMethod, *SetterMethod;
1312
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001313 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1314 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001315
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001316 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001317 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001318 Diag(property->getLocation(),
1319 diag::err_accessor_property_type_mismatch)
1320 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001321 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001322 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1323 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001324
1325 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001326 if (Context.getCanonicalType(SetterMethod->getResultType())
1327 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001328 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001329 if (SetterMethod->param_size() != 1 ||
1330 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001331 Diag(property->getLocation(),
1332 diag::err_accessor_property_type_mismatch)
1333 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001334 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001335 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1336 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001337 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001338
1339 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001340 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001341 // FIXME: The synthesized property we set here is misleading. We
1342 // almost always synthesize these methods unless the user explicitly
1343 // provided prototypes (which is odd, but allowed). Sema should be
1344 // typechecking that the declarations jive in that situation (which
1345 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001346 if (!GetterMethod) {
1347 // No instance method of same name as property getter name was found.
1348 // Declare a getter method and add it to the list of methods
1349 // for this class.
1350 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1351 property->getLocation(), property->getGetterName(),
1352 property->getType(), CD, true, false, true,
1353 (property->getPropertyImplementation() ==
1354 ObjCPropertyDecl::Optional) ?
1355 ObjCMethodDecl::Optional :
1356 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001357 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001358 } else
1359 // A user declared getter will be synthesize when @synthesize of
1360 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001361 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001362 property->setGetterMethodDecl(GetterMethod);
1363
1364 // Skip setter if property is read-only.
1365 if (!property->isReadOnly()) {
1366 // Find the default setter and if one not found, add one.
1367 if (!SetterMethod) {
1368 // No instance method of same name as property setter name was found.
1369 // Declare a setter method and add it to the list of methods
1370 // for this class.
1371 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1372 property->getLocation(),
1373 property->getSetterName(),
1374 Context.VoidTy, CD, true, false, true,
1375 (property->getPropertyImplementation() ==
1376 ObjCPropertyDecl::Optional) ?
1377 ObjCMethodDecl::Optional :
1378 ObjCMethodDecl::Required);
1379 // Invent the arguments for the setter. We don't bother making a
1380 // nice name for the argument.
1381 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001382 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001383 property->getIdentifier(),
1384 property->getType(),
1385 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001386 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001387 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001388 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001389 } else
1390 // A user declared setter will be synthesize when @synthesize of
1391 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001392 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001393 property->setSetterMethodDecl(SetterMethod);
1394 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001395 // Add any synthesized methods to the global pool. This allows us to
1396 // handle the following, which is supported by GCC (and part of the design).
1397 //
1398 // @interface Foo
1399 // @property double bar;
1400 // @end
1401 //
1402 // void thisIsUnfortunate() {
1403 // id foo;
1404 // double bar = [foo bar];
1405 // }
1406 //
Douglas Gregord1675382009-01-09 19:42:16 +00001407 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001408 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001409 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001410 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001411}
1412
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001413// Note: For class/category implemenations, allMethods/allProperties is
1414// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001415void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1416 DeclPtrTy *allMethods, unsigned allNum,
1417 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001418 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001419 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001420
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001421 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1422 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001423 // should be true.
1424 if (!ClassDecl)
1425 return;
1426
Chris Lattner855e51f2007-12-12 07:09:47 +00001427 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001428 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1429 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001430 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001431
Steve Naroffab63fd62009-01-08 17:28:14 +00001432 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001433
1434 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1435 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1436 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1437
Chris Lattner855e51f2007-12-12 07:09:47 +00001438 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001439 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001440 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001441
1442 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001443 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001444 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001445 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001446 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1447 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001448 if ((isInterfaceDeclKind && PrevMethod && !match)
1449 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001450 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001451 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001452 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001453 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001454 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001455 InsMap[Method->getSelector()] = Method;
1456 /// The following allows us to typecheck messages to "id".
1457 AddInstanceMethodToGlobalPool(Method);
1458 }
1459 }
1460 else {
1461 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001462 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001463 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1464 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001465 if ((isInterfaceDeclKind && PrevMethod && !match)
1466 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001467 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001468 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001469 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001470 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001471 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001472 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001473 /// The following allows us to typecheck messages to "Class".
1474 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001475 }
1476 }
1477 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001479 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001480 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001481 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001482 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001483 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001484 // Categories are used to extend the class by declaring new methods.
1485 // By the same token, they are also used to add new properties. No
1486 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001487
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001488 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001489 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001490 if (C->getIdentifier() == 0)
1491 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001492 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001493 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1494 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1495 // user-defined setter/getter. It also synthesizes setter/getter methods
1496 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001497 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1498 E = CDecl->prop_end(Context);
1499 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001500 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001501 CDecl->setAtEndLoc(AtEndLoc);
1502 }
1503 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001504 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001505 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001506 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001507 } else if (ObjCCategoryImplDecl* CatImplClass =
1508 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001509 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001510
Chris Lattner855e51f2007-12-12 07:09:47 +00001511 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001512 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001513 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001514 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001515 Categories; Categories = Categories->getNextClassCategory()) {
1516 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001517 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001518 break;
1519 }
1520 }
1521 }
1522 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001523 if (isInterfaceDeclKind) {
1524 // Reject invalid vardecls.
1525 for (unsigned i = 0; i != tuvNum; i++) {
1526 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1527 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1528 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001529 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001530 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001531 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001532 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001533 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001534}
1535
1536
1537/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1538/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001539static Decl::ObjCDeclQualifier
1540CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1541 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1542 if (PQTVal & ObjCDeclSpec::DQ_In)
1543 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1544 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1545 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1546 if (PQTVal & ObjCDeclSpec::DQ_Out)
1547 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1548 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1549 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1550 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1551 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1552 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1553 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001554
1555 return ret;
1556}
1557
Chris Lattner5261d0c2009-03-28 19:18:32 +00001558Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001559 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001560 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001561 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001562 Selector Sel,
1563 // optional arguments. The number of types/arguments is obtained
1564 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001565 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001566 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001567 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1568 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001569 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001570
1571 // Make sure we can establish a context for the method.
1572 if (!ClassDecl) {
1573 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001574 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001575 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001576 QualType resultDeclType;
1577
Steve Naroffa442ad92009-02-20 22:59:16 +00001578 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001579 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001580
1581 // Methods cannot return interface types. All ObjC objects are
1582 // passed by reference.
1583 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001584 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1585 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001586 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001587 }
1588 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001589 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001590
Chris Lattner114add62008-03-16 00:49:28 +00001591 ObjCMethodDecl* ObjCMethod =
1592 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001593 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001594 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001595 false,
Chris Lattner114add62008-03-16 00:49:28 +00001596 MethodDeclKind == tok::objc_optional ?
1597 ObjCMethodDecl::Optional :
1598 ObjCMethodDecl::Required);
1599
Chris Lattnereee57c02008-04-04 06:12:32 +00001600 llvm::SmallVector<ParmVarDecl*, 16> Params;
1601
Chris Lattner4f0496b2009-04-11 19:42:43 +00001602 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001603 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001604
Chris Lattner612a2f72009-04-11 18:57:04 +00001605 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001606 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001607 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001608 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001609 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001610 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001611 }
1612
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001613 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001614 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001615 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001616 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001617 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001618 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001619 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001620 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001621 ArgInfo[i].Name, ArgType,
1622 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001623 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001624
Chris Lattner761b72f2009-04-11 19:34:56 +00001625 if (ArgType->isObjCInterfaceType()) {
1626 Diag(ArgInfo[i].NameLoc,
1627 diag::err_object_cannot_be_passed_returned_by_value)
1628 << 1 << ArgType;
1629 Param->setInvalidDecl();
1630 }
1631
Chris Lattnereee57c02008-04-04 06:12:32 +00001632 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001633 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001634
1635 // Apply the attributes to the parameter.
1636 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1637
Chris Lattnereee57c02008-04-04 06:12:32 +00001638 Params.push_back(Param);
1639 }
1640
Steve Naroff79ea0e02009-04-20 15:06:07 +00001641 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001642 ObjCMethod->setObjCDeclQualifier(
1643 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1644 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001645
1646 if (AttrList)
1647 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenek2b0e6da2009-04-30 18:41:06 +00001648
Chris Lattner855e51f2007-12-12 07:09:47 +00001649 // For implementations (which can be very "coarse grain"), we add the
1650 // method now. This allows the AST to implement lookup methods that work
1651 // incrementally (without waiting until we parse the @end). It also allows
1652 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001653 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001654 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001655 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001656 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1657 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001658 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001659 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1660 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001661 }
1662 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001663 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001664 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001665 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001666 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1667 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001668 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001669 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1670 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001671 }
1672 }
1673 if (PrevMethod) {
1674 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001675 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001676 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001677 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001678 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001679 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001680}
1681
Daniel Dunbar540ff472008-09-23 21:53:23 +00001682void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1683 SourceLocation Loc,
1684 unsigned &Attributes) {
1685 // FIXME: Improve the reported location.
1686
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001687 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001688 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001689 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1690 ObjCDeclSpec::DQ_PR_assign |
1691 ObjCDeclSpec::DQ_PR_copy |
1692 ObjCDeclSpec::DQ_PR_retain))) {
1693 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1694 "readwrite" :
1695 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1696 "assign" :
1697 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1698 "copy" : "retain";
1699
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001700 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001701 diag::err_objc_property_attr_mutually_exclusive :
1702 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001703 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001704 }
1705
1706 // Check for copy or retain on non-object types.
1707 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1708 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001709 Diag(Loc, diag::err_objc_property_requires_object)
1710 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001711 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1712 }
1713
1714 // Check for more than one of { assign, copy, retain }.
1715 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1716 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001717 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1718 << "assign" << "copy";
1719 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001720 }
1721 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001722 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1723 << "assign" << "retain";
1724 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001725 }
1726 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1727 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001728 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1729 << "copy" << "retain";
1730 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001731 }
1732 }
1733
1734 // Warn if user supplied no assignment attribute, property is
1735 // readwrite, and this is an object type.
1736 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1737 ObjCDeclSpec::DQ_PR_retain)) &&
1738 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1739 Context.isObjCObjectPointerType(PropertyTy)) {
1740 // Skip this warning in gc-only mode.
1741 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1742 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1743
1744 // If non-gc code warn that this is likely inappropriate.
1745 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1746 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1747
1748 // FIXME: Implement warning dependent on NSCopying being
1749 // implemented. See also:
1750 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1751 // (please trim this list while you are at it).
1752 }
1753}
1754
Chris Lattner5261d0c2009-03-28 19:18:32 +00001755Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1756 FieldDeclarator &FD,
1757 ObjCDeclSpec &ODS,
1758 Selector GetterSel,
1759 Selector SetterSel,
1760 DeclPtrTy ClassCategory,
1761 bool *isOverridingProperty,
1762 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001763 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001764 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1765 // default is readwrite!
1766 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1767 // property is defaulted to 'assign' if it is readwrite and is
1768 // not retain or copy
1769 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1770 (isReadWrite &&
1771 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1772 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1773 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001774 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001775 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001776 // May modify Attributes.
1777 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001778 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1779 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001780 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001781 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001782 if ((CCPrimary = CDecl->getClassInterface())) {
1783 // Find the property in continuation class's primary class only.
1784 ObjCPropertyDecl *PIDecl = 0;
1785 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001786 for (ObjCInterfaceDecl::prop_iterator
1787 I = CCPrimary->prop_begin(Context),
1788 E = CCPrimary->prop_end(Context);
1789 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001790 if ((*I)->getIdentifier() == PropertyId) {
1791 PIDecl = *I;
1792 break;
1793 }
1794
1795 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001796 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001797 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001798 unsigned PIkind = PIDecl->getPropertyAttributes();
1799 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001800 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001801 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1802 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001803 PIDecl->makeitReadWriteAttribute();
1804 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1805 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1806 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1807 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1808 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001809 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001810 else
1811 Diag(AtLoc, diag::err_use_continuation_class)
1812 << CCPrimary->getDeclName();
1813 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001814 // Make sure setter decl is synthesized, and added to primary
1815 // class's list.
1816 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001817 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001818 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001819 // No matching property found in the primary class. Just fall thru
1820 // and add property to continuation class's primary class.
1821 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001822 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001823 Diag(CDecl->getLocation(), diag::err_continuation_class);
1824 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001825 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001826 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001827 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001828
Steve Naroffdcf1e842009-01-11 12:47:58 +00001829 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1830 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001831 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1832 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001833 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001834 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001835
Chris Lattner6e48be22009-04-11 20:14:49 +00001836 if (T->isArrayType() || T->isFunctionType()) {
1837 Diag(AtLoc, diag::err_property_type) << T;
1838 PDecl->setInvalidDecl();
1839 }
1840
Chris Lattner22063402009-02-16 18:32:47 +00001841 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001842
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001843 // Regardless of setter/getter attribute, we save the default getter/setter
1844 // selector names in anticipation of declaration of setter/getter methods.
1845 PDecl->setGetterName(GetterSel);
1846 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001847
Daniel Dunbar540ff472008-09-23 21:53:23 +00001848 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001849 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001850
Daniel Dunbar540ff472008-09-23 21:53:23 +00001851 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001852 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001853
Daniel Dunbar540ff472008-09-23 21:53:23 +00001854 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001855 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001856
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001857 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001858 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001859
Daniel Dunbar540ff472008-09-23 21:53:23 +00001860 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001861 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001862
Daniel Dunbar540ff472008-09-23 21:53:23 +00001863 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001864 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001865
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001866 if (isAssign)
1867 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1868
Daniel Dunbar540ff472008-09-23 21:53:23 +00001869 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001870 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001871
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001872 if (MethodImplKind == tok::objc_required)
1873 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1874 else if (MethodImplKind == tok::objc_optional)
1875 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001876 // A case of continuation class adding a new property in the class. This
1877 // is not what it was meant for. However, gcc supports it and so should we.
1878 // Make sure setter/getters are declared here.
1879 if (CCPrimary)
1880 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001881
Chris Lattner5261d0c2009-03-28 19:18:32 +00001882 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001883}
1884
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001885/// ActOnPropertyImplDecl - This routine performs semantic checks and
1886/// builds the AST node for a property implementation declaration; declared
1887/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001888///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001889Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1890 SourceLocation PropertyLoc,
1891 bool Synthesize,
1892 DeclPtrTy ClassCatImpDecl,
1893 IdentifierInfo *PropertyId,
1894 IdentifierInfo *PropertyIvar) {
1895 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001896 // Make sure we have a context for the property implementation declaration.
1897 if (!ClassImpDecl) {
1898 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001899 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001900 }
1901 ObjCPropertyDecl *property = 0;
1902 ObjCInterfaceDecl* IDecl = 0;
1903 // Find the class or category class where this property must have
1904 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001905 ObjCImplementationDecl *IC = 0;
1906 ObjCCategoryImplDecl* CatImplClass = 0;
1907 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001908 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001909 // We always synthesize an interface for an implementation
1910 // without an interface decl. So, IDecl is always non-zero.
1911 assert(IDecl &&
1912 "ActOnPropertyImplDecl - @implementation without @interface");
1913
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001914 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001915 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001916 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001917 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001918 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001919 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001920 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001921 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001922 if (Synthesize) {
1923 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001924 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001925 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001926 IDecl = CatImplClass->getClassInterface();
1927 if (!IDecl) {
1928 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001929 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001930 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001931 ObjCCategoryDecl *Category =
1932 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1933
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001934 // If category for this implementation not found, it is an error which
1935 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001936 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001937 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001938 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001939 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001940 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001941 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001942 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001943 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001944 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001945 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001946 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001947 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001948 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001949 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001950 // Check that we have a valid, previously declared ivar for @synthesize
1951 if (Synthesize) {
1952 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001953 if (!PropertyIvar)
1954 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001955 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001956 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001957 ObjCInterfaceDecl *ClassDeclared;
1958 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001959 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001960 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1961 PropertyIvar, PropType,
1962 ObjCIvarDecl::Public,
1963 (Expr *)0);
1964 property->setPropertyIvarDecl(Ivar);
1965 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001966 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001967 // Note! I deliberately want it to fall thru so, we have a
1968 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001969 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001970 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian76389e42009-04-29 21:45:02 +00001971 ClassDeclared != IDecl) {
Fariborz Jahanianc181f5a2009-04-30 21:39:24 +00001972 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001973 << property->getDeclName() << Ivar->getDeclName()
1974 << ClassDeclared->getDeclName();
1975 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1976 << Ivar << Ivar->getNameAsCString();
1977 // Note! I deliberately want it to fall thru so more errors are caught.
1978 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001979 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1980
Steve Naroff631e3922008-09-30 00:24:17 +00001981 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001982 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001983 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001984 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001985 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001986 // Note! I deliberately want it to fall thru so, we have a
1987 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001988 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001989
1990 // FIXME! Rules for properties are somewhat different that those
1991 // for assignments. Use a new routine to consolidate all cases;
1992 // specifically for property redeclarations as well as for ivars.
1993 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1994 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1995 if (lhsType != rhsType &&
1996 lhsType->isArithmeticType()) {
1997 Diag(PropertyLoc, diag::error_property_ivar_type)
1998 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001999 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002000 }
2001 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00002002 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2003 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002004 Diag(PropertyLoc, diag::error_weak_property)
2005 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002006 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002007 }
2008 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00002009 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2010 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002011 Diag(PropertyLoc, diag::error_strong_property)
2012 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002013 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00002014 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002015 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002016 } else if (PropertyIvar)
2017 // @dynamic
2018 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002019 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002020 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00002021 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2022 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002023 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00002024 ObjCPropertyImplDecl::Synthesize
2025 : ObjCPropertyImplDecl::Dynamic),
2026 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002027 if (IC) {
2028 if (Synthesize)
2029 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002030 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002031 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2032 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2033 << PropertyIvar;
2034 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2035 }
2036
Douglas Gregorcd19b572009-04-23 01:02:12 +00002037 if (ObjCPropertyImplDecl *PPIDecl
2038 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002039 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2040 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002041 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002042 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002043 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002044 }
2045 else {
2046 if (Synthesize)
2047 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002048 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002049 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2050 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2051 << PropertyIvar;
2052 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2053 }
2054
2055 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002056 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002057 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2058 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002059 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002060 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002061 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002062 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002063
Chris Lattner5261d0c2009-03-28 19:18:32 +00002064 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002065}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002066
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002067bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00002068 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002069 return false;
2070
2071 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2072 D->setInvalidDecl();
2073
2074 return true;
2075}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002076
2077/// Collect the instance variables declared in an Objective-C object. Used in
2078/// the creation of structures from objects using the @defs directive.
2079/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2080/// part of the AST generation logic of @defs.
2081static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2082 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002083 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002084 if (Class->getSuperClass())
2085 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2086
2087 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002088 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2089 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002090 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00002091 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2092 ID->getIdentifier(), ID->getType(),
2093 ID->getBitWidth());
2094 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002095 }
2096}
2097
2098/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2099/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002100void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002101 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002102 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002103 // Check that ClassName is a valid class
2104 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2105 if (!Class) {
2106 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2107 return;
2108 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00002109 if (LangOpts.ObjCNonFragileABI) {
2110 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2111 return;
2112 }
2113
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002114 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00002115 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002116
2117 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002118 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002119 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002120 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002121 if (getLangOptions().CPlusPlus)
2122 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002123 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002124 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002125 }
2126}
2127