blob: 0e655a493da9ae0d0c661fd12e00d508450be76e [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 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001078 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001079 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001080 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001081 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1082 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001083 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001084 }
1085
1086 Interfaces.push_back(IDecl);
1087 }
1088
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001089 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001090 &Interfaces[0],
1091 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001092 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001093 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001094 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001095}
1096
1097
1098/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1099/// returns true, or false, accordingly.
1100/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001101bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001102 const ObjCMethodDecl *PrevMethod,
1103 bool matchBasedOnSizeAndAlignment) {
1104 QualType T1 = Context.getCanonicalType(Method->getResultType());
1105 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1106
1107 if (T1 != T2) {
1108 // The result types are different.
1109 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001110 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001111 // Incomplete types don't have a size and alignment.
1112 if (T1->isIncompleteType() || T2->isIncompleteType())
1113 return false;
1114 // Check is based on size and alignment.
1115 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1116 return false;
1117 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001118
1119 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1120 E = Method->param_end();
1121 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1122
1123 for (; ParamI != E; ++ParamI, ++PrevI) {
1124 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1125 T1 = Context.getCanonicalType((*ParamI)->getType());
1126 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001127 if (T1 != T2) {
1128 // The result types are different.
1129 if (!matchBasedOnSizeAndAlignment)
1130 return false;
1131 // Incomplete types don't have a size and alignment.
1132 if (T1->isIncompleteType() || T2->isIncompleteType())
1133 return false;
1134 // Check is based on size and alignment.
1135 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1136 return false;
1137 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001138 }
1139 return true;
1140}
1141
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001142/// \brief Read the contents of the instance and factory method pools
1143/// for a given selector from external storage.
1144///
1145/// This routine should only be called once, when neither the instance
1146/// nor the factory method pool has an entry for this selector.
1147Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1148 bool isInstance) {
1149 assert(ExternalSource && "We need an external AST source");
1150 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1151 "Selector data already loaded into the instance method pool");
1152 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1153 "Selector data already loaded into the factory method pool");
1154
1155 // Read the method list from the external source.
1156 std::pair<ObjCMethodList, ObjCMethodList> Methods
1157 = ExternalSource->ReadMethodPool(Sel);
1158
1159 if (isInstance) {
1160 if (Methods.second.Method)
1161 FactoryMethodPool[Sel] = Methods.second;
1162 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1163 }
1164
1165 if (Methods.first.Method)
1166 InstanceMethodPool[Sel] = Methods.first;
1167
1168 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1169}
1170
Ted Kremenek42730c52008-01-07 19:49:32 +00001171void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001172 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1173 = InstanceMethodPool.find(Method->getSelector());
1174 if (Pos == InstanceMethodPool.end()) {
1175 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1176 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1177 else
1178 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1179 ObjCMethodList())).first;
1180 }
1181
1182 ObjCMethodList &Entry = Pos->second;
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001183 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001184 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001185 Entry.Method = Method;
1186 Entry.Next = 0;
1187 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001188 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001189
1190 // We've seen a method with this name, see if we have already seen this type
1191 // signature.
1192 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1193 if (MatchTwoMethodDeclarations(Method, List->Method))
1194 return;
1195
1196 // We have a new signature for an existing method - add it.
1197 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1198 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001199}
1200
Steve Naroffec7e0882008-10-21 10:50:19 +00001201// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001202ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1203 SourceRange R) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001204 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1205 = InstanceMethodPool.find(Sel);
Douglas Gregor89f2c8b2009-04-24 22:23:41 +00001206 if (Pos == InstanceMethodPool.end()) {
1207 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001208 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1209 else
1210 return 0;
1211 }
1212
1213 ObjCMethodList &MethList = Pos->second;
Steve Naroffb91afca2008-10-21 10:37:50 +00001214 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001215
1216 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001217 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1218 // This checks if the methods differ by size & alignment.
1219 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1220 issueWarning = true;
1221 }
1222 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001223 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001224 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001225 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001226 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001227 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001228 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001229 }
1230 return MethList.Method;
1231}
1232
Ted Kremenek42730c52008-01-07 19:49:32 +00001233void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001234 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1235 = FactoryMethodPool.find(Method->getSelector());
1236 if (Pos == FactoryMethodPool.end()) {
1237 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1238 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1239 else
1240 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1241 ObjCMethodList())).first;
1242 }
1243
1244 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner855e51f2007-12-12 07:09:47 +00001245 if (!FirstMethod.Method) {
1246 // Haven't seen a method with this selector name yet - add it.
1247 FirstMethod.Method = Method;
1248 FirstMethod.Next = 0;
1249 } else {
1250 // We've seen a method with this name, now check the type signature(s).
1251 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1252
Ted Kremenek42730c52008-01-07 19:49:32 +00001253 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001254 Next = Next->Next)
1255 match = MatchTwoMethodDeclarations(Method, Next->Method);
1256
1257 if (!match) {
1258 // We have a new signature for an existing method - add it.
1259 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001260 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001261 FirstMethod.Next = OMI;
1262 }
1263 }
1264}
1265
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001266ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1267 SourceRange R) {
1268 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1269 = FactoryMethodPool.find(Sel);
1270 if (Pos == FactoryMethodPool.end()) {
1271 if (ExternalSource && !InstanceMethodPool.count(Sel))
1272 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1273 else
1274 return 0;
1275 }
1276
1277 ObjCMethodList &MethList = Pos->second;
1278 bool issueWarning = false;
1279
1280 if (MethList.Method && MethList.Next) {
1281 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1282 // This checks if the methods differ by size & alignment.
1283 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1284 issueWarning = true;
1285 }
1286 if (issueWarning && (MethList.Method && MethList.Next)) {
1287 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1288 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1289 << MethList.Method->getSourceRange();
1290 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1291 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1292 << Next->Method->getSourceRange();
1293 }
1294 return MethList.Method;
1295}
1296
Steve Naroffab63fd62009-01-08 17:28:14 +00001297/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1298/// have the property type and issue diagnostics if they don't.
1299/// Also synthesize a getter/setter method if none exist (and update the
1300/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1301/// methods is the "right" thing to do.
1302void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1303 ObjCContainerDecl *CD) {
1304 ObjCMethodDecl *GetterMethod, *SetterMethod;
1305
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001306 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1307 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroffab63fd62009-01-08 17:28:14 +00001308
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001309 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001310 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001311 Diag(property->getLocation(),
1312 diag::err_accessor_property_type_mismatch)
1313 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001314 << GetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001315 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1316 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001317
1318 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001319 if (Context.getCanonicalType(SetterMethod->getResultType())
1320 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001321 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001322 if (SetterMethod->param_size() != 1 ||
1323 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001324 Diag(property->getLocation(),
1325 diag::err_accessor_property_type_mismatch)
1326 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001327 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001328 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1329 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001330 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001331
1332 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001333 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001334 // FIXME: The synthesized property we set here is misleading. We
1335 // almost always synthesize these methods unless the user explicitly
1336 // provided prototypes (which is odd, but allowed). Sema should be
1337 // typechecking that the declarations jive in that situation (which
1338 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001339 if (!GetterMethod) {
1340 // No instance method of same name as property getter name was found.
1341 // Declare a getter method and add it to the list of methods
1342 // for this class.
1343 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1344 property->getLocation(), property->getGetterName(),
1345 property->getType(), CD, true, false, true,
1346 (property->getPropertyImplementation() ==
1347 ObjCPropertyDecl::Optional) ?
1348 ObjCMethodDecl::Optional :
1349 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001350 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001351 } else
1352 // A user declared getter will be synthesize when @synthesize of
1353 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001354 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001355 property->setGetterMethodDecl(GetterMethod);
1356
1357 // Skip setter if property is read-only.
1358 if (!property->isReadOnly()) {
1359 // Find the default setter and if one not found, add one.
1360 if (!SetterMethod) {
1361 // No instance method of same name as property setter name was found.
1362 // Declare a setter method and add it to the list of methods
1363 // for this class.
1364 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1365 property->getLocation(),
1366 property->getSetterName(),
1367 Context.VoidTy, CD, true, false, true,
1368 (property->getPropertyImplementation() ==
1369 ObjCPropertyDecl::Optional) ?
1370 ObjCMethodDecl::Optional :
1371 ObjCMethodDecl::Required);
1372 // Invent the arguments for the setter. We don't bother making a
1373 // nice name for the argument.
1374 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001375 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001376 property->getIdentifier(),
1377 property->getType(),
1378 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001379 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001380 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001381 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001382 } else
1383 // A user declared setter will be synthesize when @synthesize of
1384 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001385 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001386 property->setSetterMethodDecl(SetterMethod);
1387 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001388 // Add any synthesized methods to the global pool. This allows us to
1389 // handle the following, which is supported by GCC (and part of the design).
1390 //
1391 // @interface Foo
1392 // @property double bar;
1393 // @end
1394 //
1395 // void thisIsUnfortunate() {
1396 // id foo;
1397 // double bar = [foo bar];
1398 // }
1399 //
Douglas Gregord1675382009-01-09 19:42:16 +00001400 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001401 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001402 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001403 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001404}
1405
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001406// Note: For class/category implemenations, allMethods/allProperties is
1407// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001408void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1409 DeclPtrTy *allMethods, unsigned allNum,
1410 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001411 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001412 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001413
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001414 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1415 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001416 // should be true.
1417 if (!ClassDecl)
1418 return;
1419
Chris Lattner855e51f2007-12-12 07:09:47 +00001420 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001421 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1422 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001423 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001424
Steve Naroffab63fd62009-01-08 17:28:14 +00001425 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001426
1427 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1428 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1429 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1430
Chris Lattner855e51f2007-12-12 07:09:47 +00001431 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001432 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001433 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001434
1435 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001436 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001437 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001438 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001439 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1440 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001441 if ((isInterfaceDeclKind && PrevMethod && !match)
1442 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001443 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001444 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001445 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001446 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001447 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001448 InsMap[Method->getSelector()] = Method;
1449 /// The following allows us to typecheck messages to "id".
1450 AddInstanceMethodToGlobalPool(Method);
1451 }
1452 }
1453 else {
1454 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001455 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001456 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1457 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001458 if ((isInterfaceDeclKind && PrevMethod && !match)
1459 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001460 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001461 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001462 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001463 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001464 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001465 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001466 /// The following allows us to typecheck messages to "Class".
1467 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001468 }
1469 }
1470 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001471 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001472 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001473 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001474 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001475 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001476 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001477 // Categories are used to extend the class by declaring new methods.
1478 // By the same token, they are also used to add new properties. No
1479 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001480
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001481 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001482 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001483 if (C->getIdentifier() == 0)
1484 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001485 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001486 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1487 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1488 // user-defined setter/getter. It also synthesizes setter/getter methods
1489 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001490 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1491 E = CDecl->prop_end(Context);
1492 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001493 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001494 CDecl->setAtEndLoc(AtEndLoc);
1495 }
1496 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001497 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001498 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001499 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001500 } else if (ObjCCategoryImplDecl* CatImplClass =
1501 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001502 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001503
Chris Lattner855e51f2007-12-12 07:09:47 +00001504 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001505 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001506 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001507 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001508 Categories; Categories = Categories->getNextClassCategory()) {
1509 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001510 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001511 break;
1512 }
1513 }
1514 }
1515 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001516 if (isInterfaceDeclKind) {
1517 // Reject invalid vardecls.
1518 for (unsigned i = 0; i != tuvNum; i++) {
1519 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1520 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1521 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001522 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001523 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001524 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001525 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001526 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001527}
1528
1529
1530/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1531/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001532static Decl::ObjCDeclQualifier
1533CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1534 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1535 if (PQTVal & ObjCDeclSpec::DQ_In)
1536 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1537 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1538 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1539 if (PQTVal & ObjCDeclSpec::DQ_Out)
1540 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1541 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1542 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1543 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1544 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1545 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1546 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001547
1548 return ret;
1549}
1550
Chris Lattner5261d0c2009-03-28 19:18:32 +00001551Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001552 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001553 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001555 Selector Sel,
1556 // optional arguments. The number of types/arguments is obtained
1557 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001558 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001559 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001560 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1561 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001562 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001563
1564 // Make sure we can establish a context for the method.
1565 if (!ClassDecl) {
1566 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001567 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001568 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001569 QualType resultDeclType;
1570
Steve Naroffa442ad92009-02-20 22:59:16 +00001571 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001572 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001573
1574 // Methods cannot return interface types. All ObjC objects are
1575 // passed by reference.
1576 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001577 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1578 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001579 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001580 }
1581 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001582 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001583
Chris Lattner114add62008-03-16 00:49:28 +00001584 ObjCMethodDecl* ObjCMethod =
1585 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001586 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001587 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001588 false,
Chris Lattner114add62008-03-16 00:49:28 +00001589 MethodDeclKind == tok::objc_optional ?
1590 ObjCMethodDecl::Optional :
1591 ObjCMethodDecl::Required);
1592
Chris Lattnereee57c02008-04-04 06:12:32 +00001593 llvm::SmallVector<ParmVarDecl*, 16> Params;
1594
Chris Lattner4f0496b2009-04-11 19:42:43 +00001595 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001596 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001597
Chris Lattner612a2f72009-04-11 18:57:04 +00001598 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001599 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001600 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001601 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001602 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001603 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001604 }
1605
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001606 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001607 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001608 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001609 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001610 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001611 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001612 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001613 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001614 ArgInfo[i].Name, ArgType,
1615 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001616 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001617
Chris Lattner761b72f2009-04-11 19:34:56 +00001618 if (ArgType->isObjCInterfaceType()) {
1619 Diag(ArgInfo[i].NameLoc,
1620 diag::err_object_cannot_be_passed_returned_by_value)
1621 << 1 << ArgType;
1622 Param->setInvalidDecl();
1623 }
1624
Chris Lattnereee57c02008-04-04 06:12:32 +00001625 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001626 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001627
1628 // Apply the attributes to the parameter.
1629 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1630
Chris Lattnereee57c02008-04-04 06:12:32 +00001631 Params.push_back(Param);
1632 }
1633
Steve Naroff79ea0e02009-04-20 15:06:07 +00001634 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001635 ObjCMethod->setObjCDeclQualifier(
1636 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1637 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001638
1639 if (AttrList)
1640 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenek2b0e6da2009-04-30 18:41:06 +00001641
Chris Lattner855e51f2007-12-12 07:09:47 +00001642 // For implementations (which can be very "coarse grain"), we add the
1643 // method now. This allows the AST to implement lookup methods that work
1644 // incrementally (without waiting until we parse the @end). It also allows
1645 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001646 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001647 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001648 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001649 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1650 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001651 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001652 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1653 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001654 }
1655 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001656 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001657 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001658 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001659 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1660 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001661 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001662 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1663 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001664 }
1665 }
1666 if (PrevMethod) {
1667 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001668 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001669 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001670 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001671 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001672 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001673}
1674
Daniel Dunbar540ff472008-09-23 21:53:23 +00001675void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1676 SourceLocation Loc,
1677 unsigned &Attributes) {
1678 // FIXME: Improve the reported location.
1679
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001680 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001681 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001682 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1683 ObjCDeclSpec::DQ_PR_assign |
1684 ObjCDeclSpec::DQ_PR_copy |
1685 ObjCDeclSpec::DQ_PR_retain))) {
1686 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1687 "readwrite" :
1688 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1689 "assign" :
1690 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1691 "copy" : "retain";
1692
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001693 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001694 diag::err_objc_property_attr_mutually_exclusive :
1695 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001696 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001697 }
1698
1699 // Check for copy or retain on non-object types.
1700 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1701 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001702 Diag(Loc, diag::err_objc_property_requires_object)
1703 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001704 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1705 }
1706
1707 // Check for more than one of { assign, copy, retain }.
1708 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1709 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001710 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1711 << "assign" << "copy";
1712 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001713 }
1714 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001715 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1716 << "assign" << "retain";
1717 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001718 }
1719 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1720 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001721 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1722 << "copy" << "retain";
1723 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001724 }
1725 }
1726
1727 // Warn if user supplied no assignment attribute, property is
1728 // readwrite, and this is an object type.
1729 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1730 ObjCDeclSpec::DQ_PR_retain)) &&
1731 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1732 Context.isObjCObjectPointerType(PropertyTy)) {
1733 // Skip this warning in gc-only mode.
1734 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1735 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1736
1737 // If non-gc code warn that this is likely inappropriate.
1738 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1739 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1740
1741 // FIXME: Implement warning dependent on NSCopying being
1742 // implemented. See also:
1743 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1744 // (please trim this list while you are at it).
1745 }
1746}
1747
Chris Lattner5261d0c2009-03-28 19:18:32 +00001748Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1749 FieldDeclarator &FD,
1750 ObjCDeclSpec &ODS,
1751 Selector GetterSel,
1752 Selector SetterSel,
1753 DeclPtrTy ClassCategory,
1754 bool *isOverridingProperty,
1755 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001756 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001757 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1758 // default is readwrite!
1759 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1760 // property is defaulted to 'assign' if it is readwrite and is
1761 // not retain or copy
1762 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1763 (isReadWrite &&
1764 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1765 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1766 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001767 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001768 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001769 // May modify Attributes.
1770 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001771 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1772 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001773 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001774 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001775 if ((CCPrimary = CDecl->getClassInterface())) {
1776 // Find the property in continuation class's primary class only.
1777 ObjCPropertyDecl *PIDecl = 0;
1778 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001779 for (ObjCInterfaceDecl::prop_iterator
1780 I = CCPrimary->prop_begin(Context),
1781 E = CCPrimary->prop_end(Context);
1782 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001783 if ((*I)->getIdentifier() == PropertyId) {
1784 PIDecl = *I;
1785 break;
1786 }
1787
1788 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001789 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001790 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001791 unsigned PIkind = PIDecl->getPropertyAttributes();
1792 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001793 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001794 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1795 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001796 PIDecl->makeitReadWriteAttribute();
1797 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1798 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1799 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1800 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1801 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001802 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001803 else
1804 Diag(AtLoc, diag::err_use_continuation_class)
1805 << CCPrimary->getDeclName();
1806 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001807 // Make sure setter decl is synthesized, and added to primary
1808 // class's list.
1809 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001810 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001811 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001812 // No matching property found in the primary class. Just fall thru
1813 // and add property to continuation class's primary class.
1814 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001815 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001816 Diag(CDecl->getLocation(), diag::err_continuation_class);
1817 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001818 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001819 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001820 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001821
Steve Naroffdcf1e842009-01-11 12:47:58 +00001822 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1823 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001824 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1825 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001826 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001827 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001828
Chris Lattner6e48be22009-04-11 20:14:49 +00001829 if (T->isArrayType() || T->isFunctionType()) {
1830 Diag(AtLoc, diag::err_property_type) << T;
1831 PDecl->setInvalidDecl();
1832 }
1833
Chris Lattner22063402009-02-16 18:32:47 +00001834 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001835
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001836 // Regardless of setter/getter attribute, we save the default getter/setter
1837 // selector names in anticipation of declaration of setter/getter methods.
1838 PDecl->setGetterName(GetterSel);
1839 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001840
Daniel Dunbar540ff472008-09-23 21:53:23 +00001841 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001842 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001843
Daniel Dunbar540ff472008-09-23 21:53:23 +00001844 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001845 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001846
Daniel Dunbar540ff472008-09-23 21:53:23 +00001847 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001848 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001849
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001850 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001851 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001852
Daniel Dunbar540ff472008-09-23 21:53:23 +00001853 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001854 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001855
Daniel Dunbar540ff472008-09-23 21:53:23 +00001856 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001857 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001858
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001859 if (isAssign)
1860 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1861
Daniel Dunbar540ff472008-09-23 21:53:23 +00001862 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001863 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001864
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001865 if (MethodImplKind == tok::objc_required)
1866 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1867 else if (MethodImplKind == tok::objc_optional)
1868 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001869 // A case of continuation class adding a new property in the class. This
1870 // is not what it was meant for. However, gcc supports it and so should we.
1871 // Make sure setter/getters are declared here.
1872 if (CCPrimary)
1873 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001874
Chris Lattner5261d0c2009-03-28 19:18:32 +00001875 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001876}
1877
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001878/// ActOnPropertyImplDecl - This routine performs semantic checks and
1879/// builds the AST node for a property implementation declaration; declared
1880/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001881///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001882Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1883 SourceLocation PropertyLoc,
1884 bool Synthesize,
1885 DeclPtrTy ClassCatImpDecl,
1886 IdentifierInfo *PropertyId,
1887 IdentifierInfo *PropertyIvar) {
1888 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001889 // Make sure we have a context for the property implementation declaration.
1890 if (!ClassImpDecl) {
1891 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001892 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001893 }
1894 ObjCPropertyDecl *property = 0;
1895 ObjCInterfaceDecl* IDecl = 0;
1896 // Find the class or category class where this property must have
1897 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001898 ObjCImplementationDecl *IC = 0;
1899 ObjCCategoryImplDecl* CatImplClass = 0;
1900 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001901 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001902 // We always synthesize an interface for an implementation
1903 // without an interface decl. So, IDecl is always non-zero.
1904 assert(IDecl &&
1905 "ActOnPropertyImplDecl - @implementation without @interface");
1906
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001907 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001908 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001909 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001910 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001911 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001912 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001913 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001914 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001915 if (Synthesize) {
1916 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001917 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001918 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001919 IDecl = CatImplClass->getClassInterface();
1920 if (!IDecl) {
1921 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001922 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001923 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001924 ObjCCategoryDecl *Category =
1925 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1926
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001927 // If category for this implementation not found, it is an error which
1928 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001929 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001930 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001931 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001932 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001933 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001934 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001935 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001936 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001937 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001938 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001939 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001940 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001941 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001942 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001943 // Check that we have a valid, previously declared ivar for @synthesize
1944 if (Synthesize) {
1945 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001946 if (!PropertyIvar)
1947 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001948 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001949 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001950 ObjCInterfaceDecl *ClassDeclared;
1951 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001952 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001953 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1954 PropertyIvar, PropType,
1955 ObjCIvarDecl::Public,
1956 (Expr *)0);
1957 property->setPropertyIvarDecl(Ivar);
1958 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001959 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001960 // Note! I deliberately want it to fall thru so, we have a
1961 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001962 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001963 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian76389e42009-04-29 21:45:02 +00001964 ClassDeclared != IDecl) {
Fariborz Jahanianc181f5a2009-04-30 21:39:24 +00001965 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001966 << property->getDeclName() << Ivar->getDeclName()
1967 << ClassDeclared->getDeclName();
1968 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1969 << Ivar << Ivar->getNameAsCString();
1970 // Note! I deliberately want it to fall thru so more errors are caught.
1971 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001972 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1973
Steve Naroff631e3922008-09-30 00:24:17 +00001974 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001975 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001976 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001977 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001978 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001979 // Note! I deliberately want it to fall thru so, we have a
1980 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001981 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001982
1983 // FIXME! Rules for properties are somewhat different that those
1984 // for assignments. Use a new routine to consolidate all cases;
1985 // specifically for property redeclarations as well as for ivars.
1986 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1987 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1988 if (lhsType != rhsType &&
1989 lhsType->isArithmeticType()) {
1990 Diag(PropertyLoc, diag::error_property_ivar_type)
1991 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001992 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00001993 }
1994 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00001995 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1996 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001997 Diag(PropertyLoc, diag::error_weak_property)
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 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00002002 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2003 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002004 Diag(PropertyLoc, diag::error_strong_property)
2005 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002006 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00002007 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002008 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002009 } else if (PropertyIvar)
2010 // @dynamic
2011 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002012 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002013 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00002014 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2015 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002016 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00002017 ObjCPropertyImplDecl::Synthesize
2018 : ObjCPropertyImplDecl::Dynamic),
2019 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002020 if (IC) {
2021 if (Synthesize)
2022 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002023 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002024 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2025 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2026 << PropertyIvar;
2027 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2028 }
2029
Douglas Gregorcd19b572009-04-23 01:02:12 +00002030 if (ObjCPropertyImplDecl *PPIDecl
2031 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002032 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2033 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002034 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002035 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002036 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002037 }
2038 else {
2039 if (Synthesize)
2040 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002041 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002042 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2043 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2044 << PropertyIvar;
2045 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2046 }
2047
2048 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002049 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002050 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2051 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002052 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002053 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002054 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002055 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002056
Chris Lattner5261d0c2009-03-28 19:18:32 +00002057 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002058}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002059
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002060bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00002061 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002062 return false;
2063
2064 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2065 D->setInvalidDecl();
2066
2067 return true;
2068}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002069
2070/// Collect the instance variables declared in an Objective-C object. Used in
2071/// the creation of structures from objects using the @defs directive.
2072/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2073/// part of the AST generation logic of @defs.
2074static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2075 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002076 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002077 if (Class->getSuperClass())
2078 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2079
2080 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002081 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2082 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002083 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00002084 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2085 ID->getIdentifier(), ID->getType(),
2086 ID->getBitWidth());
2087 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002088 }
2089}
2090
2091/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2092/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002093void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002094 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002095 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002096 // Check that ClassName is a valid class
2097 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2098 if (!Class) {
2099 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2100 return;
2101 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00002102 if (LangOpts.ObjCNonFragileABI) {
2103 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2104 return;
2105 }
2106
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002107 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00002108 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002109
2110 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002111 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002112 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002113 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002114 if (getLangOptions().CPlusPlus)
2115 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002116 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002117 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002118 }
2119}
2120