blob: 981d13c15e4de48fdff6f5bfa3f312564784aaea [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
Fariborz Jahanian80ccaa92009-05-08 20:20:55 +000022bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
24 SourceLocation Loc) {
25 if (GetterMethod &&
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
28 if (Context.isObjCObjectPointerType(property->getType()))
29 result = CheckAssignmentConstraints(property->getType(),
30 GetterMethod->getResultType());
31 if (result != Compatible) {
32 Diag(Loc, diag::warn_accessor_property_type_mismatch)
33 << property->getDeclName()
34 << GetterMethod->getSelector();
35 Diag(GetterMethod->getLocation(), diag::note_declared_at);
36 return true;
37 }
38 }
39 return false;
40}
41
Steve Naroff8df46022009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000047
48 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000051
Chris Lattnerc92920c2009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
53
Steve Narofffe9eb6a2007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000059
60 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000062
63 // Create Decl objects for each parameter, entrring them in the scope for
64 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000068
Daniel Dunbareaf91c32008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000071
Chris Lattner97316c02008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000073 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
74 E = MDecl->param_end(); PI != E; ++PI)
75 if ((*PI)->getIdentifier())
76 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000077}
78
Chris Lattner5261d0c2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000084 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000085 assert(ClassName && "Missing class identifier");
86
87 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000088 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000089 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000090 // Maybe we will complain about the shadowed template parameter.
91 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
92 // Just pretend that we didn't see the previous declaration.
93 PrevDecl = 0;
94 }
95
Ted Kremenek42730c52008-01-07 19:49:32 +000096 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000097 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000098 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000099 }
100
Ted Kremenek42730c52008-01-07 19:49:32 +0000101 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 if (IDecl) {
103 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +0000104 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +0000105 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +0000106 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +0000107 Diag(IDecl->getLocation(), diag::note_previous_definition);
108
Steve Naroff1422a622008-11-18 19:15:30 +0000109 // Return the previous class interface.
110 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000111 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +0000112 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 IDecl->setLocation(AtInterfaceLoc);
114 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000115 }
Chris Lattner5cece462008-07-21 07:06:49 +0000116 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000117 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000118 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000119 if (AttrList)
120 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000121
Steve Naroff104956f2009-04-23 15:15:40 +0000122 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000123 }
124
125 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000126 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000127 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000128
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000129 ObjCInterfaceDecl *SuperClassDecl =
130 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000131
132 // Diagnose classes that inherit from deprecated classes.
133 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000134 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000135
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000136 if (PrevDecl && SuperClassDecl == 0) {
137 // The previous declaration was not a class decl. Check if we have a
138 // typedef. If we do, get the underlying class type.
139 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
140 QualType T = TDecl->getUnderlyingType();
141 if (T->isObjCInterfaceType()) {
142 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
143 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
144 }
145 }
Chris Lattner7699a532009-02-16 21:33:09 +0000146
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000147 // This handles the following case:
148 //
149 // typedef int SuperClass;
150 // @interface MyClass : SuperClass {} @end
151 //
152 if (!SuperClassDecl) {
153 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
154 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
155 }
156 }
Chris Lattner7699a532009-02-16 21:33:09 +0000157
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000158 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
159 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000160 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000161 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000162 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000163 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000164 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000165 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000166 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000167 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000168 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000169 IDecl->setLocEnd(SuperLoc);
170 } else { // we have a root class.
171 IDecl->setLocEnd(ClassLoc);
172 }
173
Steve Naroff1422a622008-11-18 19:15:30 +0000174 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000175 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000176 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
177 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 IDecl->setLocEnd(EndProtoLoc);
179 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000180
181 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000182 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183}
184
185/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000186/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000187Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
188 IdentifierInfo *AliasName,
189 SourceLocation AliasLocation,
190 IdentifierInfo *ClassName,
191 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000193 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000194 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000195 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000196 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000197 else
Chris Lattner65cae292008-11-19 08:23:25 +0000198 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000199 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000200 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 }
202 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000203 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000204 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
205 QualType T = TDecl->getUnderlyingType();
206 if (T->isObjCInterfaceType()) {
207 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
208 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000209 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000210 }
211 }
212 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000213 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
214 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000215 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000216 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000217 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000218 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000219 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000220
221 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000222 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000223 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000224
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000225 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor54713b62009-04-24 02:57:34 +0000226 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000227
Chris Lattner5261d0c2009-03-28 19:18:32 +0000228 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000229}
230
Steve Narofffe56a632009-03-05 15:22:01 +0000231void Sema::CheckForwardProtocolDeclarationForCircularDependency(
232 IdentifierInfo *PName,
233 SourceLocation &Ploc, SourceLocation PrevLoc,
234 const ObjCList<ObjCProtocolDecl> &PList)
235{
236 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
237 E = PList.end(); I != E; ++I) {
238
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000239 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-03-05 15:22:01 +0000240 if (PDecl->getIdentifier() == PName) {
241 Diag(Ploc, diag::err_protocol_has_circular_dependency);
242 Diag(PrevLoc, diag::note_previous_definition);
243 }
244 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
245 PDecl->getLocation(), PDecl->getReferencedProtocols());
246 }
247 }
248}
249
Chris Lattner5261d0c2009-03-28 19:18:32 +0000250Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000251Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
252 IdentifierInfo *ProtocolName,
253 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000254 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000255 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000256 SourceLocation EndProtoLoc,
257 AttributeList *AttrList) {
258 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000259 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000260 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000261 if (PDecl) {
262 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000263 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000264 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000265 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000266 // Just return the protocol we already had.
267 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000268 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000269 }
Steve Narofffe56a632009-03-05 15:22:01 +0000270 ObjCList<ObjCProtocolDecl> PList;
271 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
272 CheckForwardProtocolDeclarationForCircularDependency(
273 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
274 PList.Destroy(Context);
275
Steve Naroff9a9e5212008-08-13 16:39:22 +0000276 // Make sure the cached decl gets a valid start location.
277 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000278 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000279 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000280 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
281 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000282 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000283 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000284 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000285 if (AttrList)
286 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000287 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000288 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000289 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000290 PDecl->setLocEnd(EndProtoLoc);
291 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000292
293 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000294 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000295}
296
297/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000298/// issues an error if they are not declared. It returns list of
299/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000300void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000301Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000302 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000303 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000304 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000305 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000306 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000307 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000308 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000309 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000310 continue;
311 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000312
Douglas Gregoraa57e862009-02-18 21:56:37 +0000313 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000314
315 // If this is a forward declaration and we are supposed to warn in this
316 // case, do it.
317 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000318 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000319 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000320 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000321 }
322}
323
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000324/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000325/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000327void
328Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
329 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000330 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000331 ObjCPropertyDecl::PropertyAttributeKind CAttr =
332 Property->getPropertyAttributes();
333 ObjCPropertyDecl::PropertyAttributeKind SAttr =
334 SuperProperty->getPropertyAttributes();
335 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
336 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000337 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000338 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
340 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000341 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000342 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000343 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
344 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000345 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000346 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000347
348 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
349 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000350 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000351 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000352 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000353 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000354 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000355 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000356 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000357 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000358
359 QualType LHSType =
360 Context.getCanonicalType(SuperProperty->getType());
361 QualType RHSType =
362 Context.getCanonicalType(Property->getType());
363
364 if (!Context.typesAreCompatible(LHSType, RHSType)) {
365 // FIXME: Incorporate this test with typesAreCompatible.
366 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
367 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
368 return;
369 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
370 << Property->getType() << SuperProperty->getType() << inheritedName;
371 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000372}
373
374/// ComparePropertiesInBaseAndSuper - This routine compares property
375/// declarations in base and its super class, if any, and issues
376/// diagnostics in a variety of inconsistant situations.
377///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000378void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000379 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
380 if (!SDecl)
381 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000382 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000383 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
384 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000385 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000386 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000387 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
388 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000389 ObjCPropertyDecl *PDecl = (*I);
390 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000391 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000392 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000393 }
394 }
395}
396
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000397/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
398/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000399/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000400void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000401Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000402 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000403 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
404 if (!IDecl) {
405 // Category
406 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
407 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000408 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
409 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000410 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000411 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000412 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000413 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000414 CP != CE; ++CP)
415 if ((*CP)->getIdentifier() == Pr->getIdentifier())
416 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000417 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000418 // Property protocol already exist in class. Diagnose any mismatch.
419 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
420 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000421 return;
422 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000423 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
424 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000425 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000426 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000427 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000428 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000429 CP != CE; ++CP)
430 if ((*CP)->getIdentifier() == Pr->getIdentifier())
431 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000432 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000433 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000434 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000435 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000436}
437
438/// MergeProtocolPropertiesIntoClass - This routine merges properties
439/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000440/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000441///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000442void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000443 DeclPtrTy MergeItsProtocols) {
444 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000445 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
446
447 if (!IDecl) {
448 // Category
449 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
450 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
451 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
452 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
453 E = MDecl->protocol_end(); P != E; ++P)
454 // Merge properties of category (*P) into IDECL's
455 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
456
457 // Go thru the list of protocols for this category and recursively merge
458 // their properties into this class as well.
459 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
460 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000461 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000462 } else {
463 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(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000467 }
468 return;
469 }
470
Chris Lattner5cece462008-07-21 07:06:49 +0000471 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000472 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
473 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000474 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000475 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
476
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000477 // Go thru the list of protocols for this class and recursively merge
478 // their properties into this class as well.
479 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
480 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000481 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000482 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000483 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
484 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
485 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000486 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000487 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000488}
489
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000490/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000491/// a class method in its extension.
492///
493void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
494 ObjCInterfaceDecl *ID) {
495 if (!ID)
496 return; // Possibly due to previous error
497
498 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000499 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
500 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000501 ObjCMethodDecl *MD = *i;
502 MethodMap[MD->getSelector()] = MD;
503 }
504
505 if (MethodMap.empty())
506 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000507 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
508 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000509 ObjCMethodDecl *Method = *i;
510 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
511 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
512 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
513 << Method->getDeclName();
514 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
515 }
516 }
517}
518
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000519/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000520Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000521Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000522 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000523 unsigned NumElts,
524 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000525 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000526
527 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000528 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000529 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000530 if (PDecl == 0) { // Not already seen?
531 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
532 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000533 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000534 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000535 if (attrList)
536 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000537 Protocols.push_back(PDecl);
538 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000539
540 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000541 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000542 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000543 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000544 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000545 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000546}
547
Chris Lattner5261d0c2009-03-28 19:18:32 +0000548Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000549ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
550 IdentifierInfo *ClassName, SourceLocation ClassLoc,
551 IdentifierInfo *CategoryName,
552 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000553 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000554 unsigned NumProtoRefs,
555 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000556 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000557 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
558 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000559 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000560
561 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000562 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000563 if (!IDecl || IDecl->isForwardDecl()) {
564 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000565 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000566 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000567 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000568
Chris Lattner1fa7f622009-02-16 21:26:43 +0000569 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000570
571 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000572 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000573
574 /// Check for duplicate interface declaration for this category
575 ObjCCategoryDecl *CDeclChain;
576 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
577 CDeclChain = CDeclChain->getNextClassCategory()) {
578 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
579 Diag(CategoryLoc, diag::warn_dup_category_def)
580 << ClassName << CategoryName;
581 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
582 break;
583 }
584 }
585 if (!CDeclChain)
586 CDecl->insertNextClassCategory();
587
Chris Lattner855e51f2007-12-12 07:09:47 +0000588 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000589 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000590 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000591 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000592
593 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000594 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000595}
596
597/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000598/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000599/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000600Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 SourceLocation AtCatImplLoc,
602 IdentifierInfo *ClassName, SourceLocation ClassLoc,
603 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000605 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000606 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
607 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000608 /// Check that class of this category is already completely declared.
609 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000610 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000611
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000612 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000613 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000614
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 /// TODO: Check that CatName, category name, is not used in another
616 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000617 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000618
619 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000620 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000621}
622
Chris Lattner5261d0c2009-03-28 19:18:32 +0000623Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 SourceLocation AtClassImplLoc,
625 IdentifierInfo *ClassName, SourceLocation ClassLoc,
626 IdentifierInfo *SuperClassname,
627 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000630 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000631 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000632 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000633 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000634 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000636 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000637 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000638 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000639 IDecl = 0;
640 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000641 }
642
643 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000645 if (SuperClassname) {
646 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000647 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000648 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000649 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
650 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000651 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000652 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000655 Diag(SuperClassLoc, diag::err_undef_superclass)
656 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000657 else if (IDecl && IDecl->getSuperClass() != SDecl) {
658 // This implementation and its interface do not have the same
659 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000660 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000661 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000662 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000663 }
664 }
665 }
666
667 if (!IDecl) {
668 // Legacy case of @implementation with no corresponding @interface.
669 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000670
671 // FIXME: Do we support attributes on the @implementation? If so
672 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000673 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
674 ClassName, ClassLoc, false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000675 IDecl->setSuperClass(SDecl);
676 IDecl->setLocEnd(ClassLoc);
Douglas Gregor453e11d2009-04-24 00:16:12 +0000677
678 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000679 } else {
680 // Mark the interface as being completed, even if it was just as
681 // @class ....;
682 // declaration; the user cannot reopen it.
683 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000684 }
685
Ted Kremenek42730c52008-01-07 19:49:32 +0000686 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000687 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000688 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000689
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000690 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000691 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000692
Chris Lattner855e51f2007-12-12 07:09:47 +0000693 // Check that there is no duplicate implementation of this class.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000694 if (LookupObjCImplementation(ClassName))
Chris Lattner1b6de332008-03-16 20:53:07 +0000695 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000696 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000697 else // add it to the list.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000698 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000699 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000700}
701
Ted Kremenek42730c52008-01-07 19:49:32 +0000702void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
703 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000704 SourceLocation RBrace) {
705 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000706 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000707 if (!IDecl)
708 return;
709 /// Check case of non-existing @interface decl.
710 /// (legacy objective-c @implementation decl without an @interface decl).
711 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000712 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000713 IDecl->setIVarList(ivars, numIvars, Context);
714 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000715 return;
716 }
717 // If implementation has empty ivar list, just return.
718 if (numIvars == 0)
719 return;
720
721 assert(ivars && "missing @implementation ivars");
722
723 // Check interface's Ivar list against those in the implementation.
724 // names and types must match.
725 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000726 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000727 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000728 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
729 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000730 ObjCIvarDecl* ImplIvar = ivars[j++];
731 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000732 assert (ImplIvar && "missing implementation ivar");
733 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000734
735 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000736 if (Context.getCanonicalType(ImplIvar->getType()) !=
737 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000738 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000739 << ImplIvar->getIdentifier()
740 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000741 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000742 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
743 Expr *ImplBitWidth = ImplIvar->getBitWidth();
744 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman5255e7a2009-04-26 19:19:15 +0000745 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
746 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000747 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
748 << ImplIvar->getIdentifier();
749 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
750 }
751 }
752 // Make sure the names are identical.
753 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000754 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000755 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000756 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000757 }
758 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000759 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000760
761 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000762 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000763 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000764 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000765}
766
Steve Naroffb4f48512008-02-10 21:38:56 +0000767void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
768 bool &IncompleteImpl) {
769 if (!IncompleteImpl) {
770 Diag(ImpLoc, diag::warn_incomplete_impl);
771 IncompleteImpl = true;
772 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000773 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000774}
775
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000776void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
777 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000778 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000779 ImpMethodDecl->getResultType())) {
780 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
781 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
782 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000783 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
784 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000785
786 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
787 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
788 IM != EM; ++IM, ++IF) {
789 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
790 continue;
791
792 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
793 << ImpMethodDecl->getDeclName() << (*IF)->getType()
794 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000795 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000796 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000797}
798
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000799/// isPropertyReadonly - Return true if property is readonly, by searching
800/// for the property in the class and in its categories and implementations
801///
802bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000803 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000804 // by far the most common case.
805 if (!PDecl->isReadOnly())
806 return false;
807 // Even if property is ready only, if interface has a user defined setter,
808 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000809 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000810 return false;
811
812 // Main class has the property as 'readonly'. Must search
813 // through the category list to see if the property's
814 // attribute has been over-ridden to 'readwrite'.
815 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
816 Category; Category = Category->getNextClassCategory()) {
817 // Even if property is ready only, if a category has a user defined setter,
818 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000819 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000820 return false;
821 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000822 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000823 if (P && !P->isReadOnly())
824 return false;
825 }
826
827 // Also, check for definition of a setter method in the implementation if
828 // all else failed.
829 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
830 if (ObjCImplementationDecl *IMD =
831 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000832 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000833 return false;
834 }
835 else if (ObjCCategoryImplDecl *CIMD =
836 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000837 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000838 return false;
839 }
840 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000841 // Lastly, look through the implementation (if one is in scope).
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000842 if (ObjCImplementationDecl *ImpDecl
843 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000844 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000845 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000846 // If all fails, look at the super class.
847 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
848 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000849 return true;
850}
851
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000852/// FIXME: Type hierarchies in Objective-C can be deep. We could most
853/// likely improve the efficiency of selector lookups and type
854/// checking by associating with each protocol / interface / category
855/// the flattened instance tables. If we used an immutable set to keep
856/// the table then it wouldn't add significant memory cost and it
857/// would be handy for lookups.
858
Steve Naroffb268d2a2008-02-08 22:06:17 +0000859/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000860/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000861void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
862 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000863 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000864 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000865 const llvm::DenseSet<Selector> &ClsMap,
866 ObjCInterfaceDecl *IDecl) {
867 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
868
869 // If a method lookup fails locally we still need to look and see if
870 // the method was implemented by a base class or an inherited
871 // protocol. This lookup is slow, but occurs rarely in correct code
872 // and otherwise would terminate in a warning.
873
Chris Lattner855e51f2007-12-12 07:09:47 +0000874 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000875 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
876 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000878 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000879 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000880 (!Super ||
881 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000882 // Ugly, but necessary. Method declared in protcol might have
883 // have been synthesized due to a property declared in the class which
884 // uses the protocol.
885 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000886 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000887 if (!MethodInClass || !MethodInClass->isSynthesized())
888 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
889 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 }
891 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000892 for (ObjCProtocolDecl::classmeth_iterator
893 I = PDecl->classmeth_begin(Context),
894 E = PDecl->classmeth_end(Context);
895 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000897 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
898 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000899 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000900 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000901 }
Chris Lattner0be08822008-07-21 21:32:27 +0000902 // Check on this protocols's referenced protocols, recursively.
903 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
904 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000905 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000906}
907
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000908/// MatchAllMethodDeclarations - Check methods declaraed in interface or
909/// or protocol against those declared in their implementations.
910///
911void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
912 const llvm::DenseSet<Selector> &ClsMap,
913 llvm::DenseSet<Selector> &InsMapSeen,
914 llvm::DenseSet<Selector> &ClsMapSeen,
915 ObjCImplDecl* IMPDecl,
916 ObjCContainerDecl* CDecl,
917 bool &IncompleteImpl,
918 bool ImmediateClass)
919{
920 // Check and see if instance methods in class interface have been
921 // implemented in the implementation class. If so, their types match.
922 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
923 E = CDecl->instmeth_end(Context); I != E; ++I) {
924 if (InsMapSeen.count((*I)->getSelector()))
925 continue;
926 InsMapSeen.insert((*I)->getSelector());
927 if (!(*I)->isSynthesized() &&
928 !InsMap.count((*I)->getSelector())) {
929 if (ImmediateClass)
930 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
931 continue;
932 }
933 else {
934 ObjCMethodDecl *ImpMethodDecl =
935 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
936 ObjCMethodDecl *IntfMethodDecl =
937 CDecl->getInstanceMethod(Context, (*I)->getSelector());
938 assert(IntfMethodDecl &&
939 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
940 // ImpMethodDecl may be null as in a @dynamic property.
941 if (ImpMethodDecl)
942 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
943 }
944 }
945
946 // Check and see if class methods in class interface have been
947 // implemented in the implementation class. If so, their types match.
948 for (ObjCInterfaceDecl::classmeth_iterator
949 I = CDecl->classmeth_begin(Context),
950 E = CDecl->classmeth_end(Context);
951 I != E; ++I) {
952 if (ClsMapSeen.count((*I)->getSelector()))
953 continue;
954 ClsMapSeen.insert((*I)->getSelector());
955 if (!ClsMap.count((*I)->getSelector())) {
956 if (ImmediateClass)
957 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
958 }
959 else {
960 ObjCMethodDecl *ImpMethodDecl =
961 IMPDecl->getClassMethod(Context, (*I)->getSelector());
962 ObjCMethodDecl *IntfMethodDecl =
963 CDecl->getClassMethod(Context, (*I)->getSelector());
964 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
965 }
966 }
967 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
968 // Check for any implementation of a methods declared in protocol.
969 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
970 E = I->protocol_end(); PI != E; ++PI)
971 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
972 IMPDecl,
973 (*PI), IncompleteImpl, false);
974 if (I->getSuperClass())
975 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
976 IMPDecl,
977 I->getSuperClass(), IncompleteImpl, false);
978 }
979}
980
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000981void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
982 ObjCContainerDecl* CDecl,
983 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000984 llvm::DenseSet<Selector> InsMap;
985 // Check and see if instance methods in class interface have been
986 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000987 for (ObjCImplementationDecl::instmeth_iterator
988 I = IMPDecl->instmeth_begin(Context),
989 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000990 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000991
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000992 // Check and see if properties declared in the interface have either 1)
993 // an implementation or 2) there is a @synthesize/@dynamic implementation
994 // of the property in the @implementation.
995 if (isa<ObjCInterfaceDecl>(CDecl))
996 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
997 E = CDecl->prop_end(Context); P != E; ++P) {
998 ObjCPropertyDecl *Prop = (*P);
999 if (Prop->isInvalidDecl())
1000 continue;
1001 ObjCPropertyImplDecl *PI = 0;
1002 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +00001003 for (ObjCImplDecl::propimpl_iterator
1004 I = IMPDecl->propimpl_begin(Context),
1005 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001006 if ((*I)->getPropertyDecl() == Prop) {
1007 PI = (*I);
1008 break;
1009 }
1010 if (PI)
1011 continue;
1012 if (!InsMap.count(Prop->getGetterName())) {
1013 Diag(Prop->getLocation(),
1014 diag::warn_setter_getter_impl_required)
1015 << Prop->getDeclName() << Prop->getGetterName();
1016 Diag(IMPDecl->getLocation(),
1017 diag::note_property_impl_required);
1018 }
1019
1020 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1021 Diag(Prop->getLocation(),
1022 diag::warn_setter_getter_impl_required)
1023 << Prop->getDeclName() << Prop->getSetterName();
1024 Diag(IMPDecl->getLocation(),
1025 diag::note_property_impl_required);
1026 }
1027 }
1028
Chris Lattner855e51f2007-12-12 07:09:47 +00001029 llvm::DenseSet<Selector> ClsMap;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001030 for (ObjCImplementationDecl::classmeth_iterator
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001031 I = IMPDecl->classmeth_begin(Context),
1032 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +00001033 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +00001034
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001035 // Check for type conflict of methods declared in a class/protocol and
1036 // its implementation; if any.
1037 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1038 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1039 IMPDecl, CDecl,
1040 IncompleteImpl, true);
Chris Lattner855e51f2007-12-12 07:09:47 +00001041
1042 // Check the protocol list for unimplemented methods in the @implementation
1043 // class.
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001044 // Check and see if class methods in class interface have been
1045 // implemented in the implementation class.
1046
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001047 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001048 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001049 E = I->protocol_end(); PI != E; ++PI)
1050 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1051 InsMap, ClsMap, I);
1052 // Check class extensions (unnamed categories)
1053 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1054 Categories; Categories = Categories->getNextClassCategory()) {
1055 if (!Categories->getIdentifier()) {
1056 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1057 break;
1058 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +00001059 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001060 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1061 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1062 E = C->protocol_end(); PI != E; ++PI)
1063 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1064 InsMap, ClsMap, C->getClassInterface());
1065 } else
1066 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001067}
1068
1069/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001070Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001071Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001072 IdentifierInfo **IdentList,
1073 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001074 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001075
1076 for (unsigned i = 0; i != NumElts; ++i) {
1077 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001078 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001079 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001080 // Maybe we will complain about the shadowed template parameter.
1081 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1082 // Just pretend that we didn't see the previous declaration.
1083 PrevDecl = 0;
1084 }
1085
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001087 // GCC apparently allows the following idiom:
1088 //
1089 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1090 // @class XCElementToggler;
1091 //
1092 // FIXME: Make an extension?
1093 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1094 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001095 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001096 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001097 }
Fariborz Jahanian4c16d762009-05-07 21:49:26 +00001098 else if (TDD) {
1099 // a forward class declaration matching a typedef name of a class
1100 // refers to the underlying class.
1101 if (ObjCInterfaceType * OI =
1102 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1103 PrevDecl = OI->getDecl();
1104 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001105 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001106 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001107 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001108 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1109 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001110 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001111 }
1112
1113 Interfaces.push_back(IDecl);
1114 }
1115
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001116 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001117 &Interfaces[0],
1118 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001119 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001120 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001121 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001122}
1123
1124
1125/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1126/// returns true, or false, accordingly.
1127/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001128bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001129 const ObjCMethodDecl *PrevMethod,
1130 bool matchBasedOnSizeAndAlignment) {
1131 QualType T1 = Context.getCanonicalType(Method->getResultType());
1132 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1133
1134 if (T1 != T2) {
1135 // The result types are different.
1136 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001137 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001138 // Incomplete types don't have a size and alignment.
1139 if (T1->isIncompleteType() || T2->isIncompleteType())
1140 return false;
1141 // Check is based on size and alignment.
1142 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1143 return false;
1144 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001145
1146 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1147 E = Method->param_end();
1148 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1149
1150 for (; ParamI != E; ++ParamI, ++PrevI) {
1151 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1152 T1 = Context.getCanonicalType((*ParamI)->getType());
1153 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001154 if (T1 != T2) {
1155 // The result types are different.
1156 if (!matchBasedOnSizeAndAlignment)
1157 return false;
1158 // Incomplete types don't have a size and alignment.
1159 if (T1->isIncompleteType() || T2->isIncompleteType())
1160 return false;
1161 // Check is based on size and alignment.
1162 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1163 return false;
1164 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001165 }
1166 return true;
1167}
1168
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001169/// \brief Read the contents of the instance and factory method pools
1170/// for a given selector from external storage.
1171///
1172/// This routine should only be called once, when neither the instance
1173/// nor the factory method pool has an entry for this selector.
1174Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1175 bool isInstance) {
1176 assert(ExternalSource && "We need an external AST source");
1177 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1178 "Selector data already loaded into the instance method pool");
1179 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1180 "Selector data already loaded into the factory method pool");
1181
1182 // Read the method list from the external source.
1183 std::pair<ObjCMethodList, ObjCMethodList> Methods
1184 = ExternalSource->ReadMethodPool(Sel);
1185
1186 if (isInstance) {
1187 if (Methods.second.Method)
1188 FactoryMethodPool[Sel] = Methods.second;
1189 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1190 }
1191
1192 if (Methods.first.Method)
1193 InstanceMethodPool[Sel] = Methods.first;
1194
1195 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1196}
1197
Ted Kremenek42730c52008-01-07 19:49:32 +00001198void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001199 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1200 = InstanceMethodPool.find(Method->getSelector());
1201 if (Pos == InstanceMethodPool.end()) {
1202 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1203 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1204 else
1205 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1206 ObjCMethodList())).first;
1207 }
1208
1209 ObjCMethodList &Entry = Pos->second;
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001210 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001211 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001212 Entry.Method = Method;
1213 Entry.Next = 0;
1214 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001215 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001216
1217 // We've seen a method with this name, see if we have already seen this type
1218 // signature.
1219 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1220 if (MatchTwoMethodDeclarations(Method, List->Method))
1221 return;
1222
1223 // We have a new signature for an existing method - add it.
1224 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1225 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001226}
1227
Steve Naroffec7e0882008-10-21 10:50:19 +00001228// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001229ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1230 SourceRange R) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001231 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1232 = InstanceMethodPool.find(Sel);
Douglas Gregor89f2c8b2009-04-24 22:23:41 +00001233 if (Pos == InstanceMethodPool.end()) {
1234 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001235 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1236 else
1237 return 0;
1238 }
1239
1240 ObjCMethodList &MethList = Pos->second;
Steve Naroffb91afca2008-10-21 10:37:50 +00001241 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001242
1243 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001244 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1245 // This checks if the methods differ by size & alignment.
1246 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1247 issueWarning = true;
1248 }
1249 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001250 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001251 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001252 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001253 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001254 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001255 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001256 }
1257 return MethList.Method;
1258}
1259
Ted Kremenek42730c52008-01-07 19:49:32 +00001260void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001261 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1262 = FactoryMethodPool.find(Method->getSelector());
1263 if (Pos == FactoryMethodPool.end()) {
1264 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1265 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1266 else
1267 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1268 ObjCMethodList())).first;
1269 }
1270
1271 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner855e51f2007-12-12 07:09:47 +00001272 if (!FirstMethod.Method) {
1273 // Haven't seen a method with this selector name yet - add it.
1274 FirstMethod.Method = Method;
1275 FirstMethod.Next = 0;
1276 } else {
1277 // We've seen a method with this name, now check the type signature(s).
1278 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1279
Ted Kremenek42730c52008-01-07 19:49:32 +00001280 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001281 Next = Next->Next)
1282 match = MatchTwoMethodDeclarations(Method, Next->Method);
1283
1284 if (!match) {
1285 // We have a new signature for an existing method - add it.
1286 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001287 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001288 FirstMethod.Next = OMI;
1289 }
1290 }
1291}
1292
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001293ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1294 SourceRange R) {
1295 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1296 = FactoryMethodPool.find(Sel);
1297 if (Pos == FactoryMethodPool.end()) {
1298 if (ExternalSource && !InstanceMethodPool.count(Sel))
1299 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1300 else
1301 return 0;
1302 }
1303
1304 ObjCMethodList &MethList = Pos->second;
1305 bool issueWarning = false;
1306
1307 if (MethList.Method && MethList.Next) {
1308 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1309 // This checks if the methods differ by size & alignment.
1310 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1311 issueWarning = true;
1312 }
1313 if (issueWarning && (MethList.Method && MethList.Next)) {
1314 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1315 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1316 << MethList.Method->getSourceRange();
1317 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1318 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1319 << Next->Method->getSourceRange();
1320 }
1321 return MethList.Method;
1322}
1323
Steve Naroffab63fd62009-01-08 17:28:14 +00001324/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1325/// have the property type and issue diagnostics if they don't.
1326/// Also synthesize a getter/setter method if none exist (and update the
1327/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1328/// methods is the "right" thing to do.
1329void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1330 ObjCContainerDecl *CD) {
1331 ObjCMethodDecl *GetterMethod, *SetterMethod;
1332
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001333 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1334 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Fariborz Jahanian80ccaa92009-05-08 20:20:55 +00001335 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1336 property->getLocation());
1337
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001338 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001339 if (Context.getCanonicalType(SetterMethod->getResultType())
1340 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001341 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001342 if (SetterMethod->param_size() != 1 ||
1343 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001344 Diag(property->getLocation(),
Fariborz Jahaniana996bb02009-05-08 19:36:34 +00001345 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001346 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001347 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001348 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1349 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001350 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001351
1352 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001353 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001354 // FIXME: The synthesized property we set here is misleading. We
1355 // almost always synthesize these methods unless the user explicitly
1356 // provided prototypes (which is odd, but allowed). Sema should be
1357 // typechecking that the declarations jive in that situation (which
1358 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001359 if (!GetterMethod) {
1360 // No instance method of same name as property getter name was found.
1361 // Declare a getter method and add it to the list of methods
1362 // for this class.
1363 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1364 property->getLocation(), property->getGetterName(),
1365 property->getType(), CD, true, false, true,
1366 (property->getPropertyImplementation() ==
1367 ObjCPropertyDecl::Optional) ?
1368 ObjCMethodDecl::Optional :
1369 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001370 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001371 } else
1372 // A user declared getter will be synthesize when @synthesize of
1373 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001374 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001375 property->setGetterMethodDecl(GetterMethod);
1376
1377 // Skip setter if property is read-only.
1378 if (!property->isReadOnly()) {
1379 // Find the default setter and if one not found, add one.
1380 if (!SetterMethod) {
1381 // No instance method of same name as property setter name was found.
1382 // Declare a setter method and add it to the list of methods
1383 // for this class.
1384 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1385 property->getLocation(),
1386 property->getSetterName(),
1387 Context.VoidTy, CD, true, false, true,
1388 (property->getPropertyImplementation() ==
1389 ObjCPropertyDecl::Optional) ?
1390 ObjCMethodDecl::Optional :
1391 ObjCMethodDecl::Required);
1392 // Invent the arguments for the setter. We don't bother making a
1393 // nice name for the argument.
1394 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001395 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001396 property->getIdentifier(),
1397 property->getType(),
1398 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001399 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001400 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001401 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001402 } else
1403 // A user declared setter will be synthesize when @synthesize of
1404 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001405 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001406 property->setSetterMethodDecl(SetterMethod);
1407 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001408 // Add any synthesized methods to the global pool. This allows us to
1409 // handle the following, which is supported by GCC (and part of the design).
1410 //
1411 // @interface Foo
1412 // @property double bar;
1413 // @end
1414 //
1415 // void thisIsUnfortunate() {
1416 // id foo;
1417 // double bar = [foo bar];
1418 // }
1419 //
Douglas Gregord1675382009-01-09 19:42:16 +00001420 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001421 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001422 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001423 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001424}
1425
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001426// Note: For class/category implemenations, allMethods/allProperties is
1427// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001428void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1429 DeclPtrTy *allMethods, unsigned allNum,
1430 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001431 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001432 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001433
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001434 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1435 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001436 // should be true.
1437 if (!ClassDecl)
1438 return;
1439
Chris Lattner855e51f2007-12-12 07:09:47 +00001440 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001441 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1442 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001443 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001444
Steve Naroffab63fd62009-01-08 17:28:14 +00001445 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001446
1447 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1448 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1449 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1450
Chris Lattner855e51f2007-12-12 07:09:47 +00001451 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001452 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001453 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001454
1455 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001456 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001457 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001458 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001459 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1460 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001461 if ((isInterfaceDeclKind && PrevMethod && !match)
1462 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001463 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001464 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001465 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001466 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001467 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001468 InsMap[Method->getSelector()] = Method;
1469 /// The following allows us to typecheck messages to "id".
1470 AddInstanceMethodToGlobalPool(Method);
1471 }
1472 }
1473 else {
1474 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001475 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001476 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1477 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001478 if ((isInterfaceDeclKind && PrevMethod && !match)
1479 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001480 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001481 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001482 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001483 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001484 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001485 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001486 /// The following allows us to typecheck messages to "Class".
1487 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001488 }
1489 }
1490 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001491 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001492 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001493 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001494 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001495 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001496 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001497 // Categories are used to extend the class by declaring new methods.
1498 // By the same token, they are also used to add new properties. No
1499 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001500
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001501 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001502 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001503 if (C->getIdentifier() == 0)
1504 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001505 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001506 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1507 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1508 // user-defined setter/getter. It also synthesizes setter/getter methods
1509 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001510 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1511 E = CDecl->prop_end(Context);
1512 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001513 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001514 CDecl->setAtEndLoc(AtEndLoc);
1515 }
1516 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001517 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001518 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001519 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001520 } else if (ObjCCategoryImplDecl* CatImplClass =
1521 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001522 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001523
Chris Lattner855e51f2007-12-12 07:09:47 +00001524 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001525 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001526 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001527 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001528 Categories; Categories = Categories->getNextClassCategory()) {
1529 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001530 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001531 break;
1532 }
1533 }
1534 }
1535 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001536 if (isInterfaceDeclKind) {
1537 // Reject invalid vardecls.
1538 for (unsigned i = 0; i != tuvNum; i++) {
1539 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1540 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1541 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001542 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001543 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001544 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001545 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001546 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001547}
1548
1549
1550/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1551/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001552static Decl::ObjCDeclQualifier
1553CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1554 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1555 if (PQTVal & ObjCDeclSpec::DQ_In)
1556 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1557 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1558 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1559 if (PQTVal & ObjCDeclSpec::DQ_Out)
1560 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1561 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1562 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1563 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1564 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1565 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1566 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001567
1568 return ret;
1569}
1570
Chris Lattner5261d0c2009-03-28 19:18:32 +00001571Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001572 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001573 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001574 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001575 Selector Sel,
1576 // optional arguments. The number of types/arguments is obtained
1577 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001578 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001579 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001580 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1581 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001582 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001583
1584 // Make sure we can establish a context for the method.
1585 if (!ClassDecl) {
1586 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001587 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001588 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001589 QualType resultDeclType;
1590
Steve Naroffa442ad92009-02-20 22:59:16 +00001591 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001592 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001593
1594 // Methods cannot return interface types. All ObjC objects are
1595 // passed by reference.
1596 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001597 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1598 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001599 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001600 }
1601 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001602 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001603
Chris Lattner114add62008-03-16 00:49:28 +00001604 ObjCMethodDecl* ObjCMethod =
1605 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001606 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001607 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001608 false,
Chris Lattner114add62008-03-16 00:49:28 +00001609 MethodDeclKind == tok::objc_optional ?
1610 ObjCMethodDecl::Optional :
1611 ObjCMethodDecl::Required);
1612
Chris Lattnereee57c02008-04-04 06:12:32 +00001613 llvm::SmallVector<ParmVarDecl*, 16> Params;
1614
Chris Lattner4f0496b2009-04-11 19:42:43 +00001615 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001616 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001617
Chris Lattner612a2f72009-04-11 18:57:04 +00001618 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001619 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001620 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001621 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001622 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001623 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001624 }
1625
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001626 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001627 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001628 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001629 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001630 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001631 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001632 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001633 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001634 ArgInfo[i].Name, ArgType,
1635 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001636 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001637
Chris Lattner761b72f2009-04-11 19:34:56 +00001638 if (ArgType->isObjCInterfaceType()) {
1639 Diag(ArgInfo[i].NameLoc,
1640 diag::err_object_cannot_be_passed_returned_by_value)
1641 << 1 << ArgType;
1642 Param->setInvalidDecl();
1643 }
1644
Chris Lattnereee57c02008-04-04 06:12:32 +00001645 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001646 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001647
1648 // Apply the attributes to the parameter.
1649 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1650
Chris Lattnereee57c02008-04-04 06:12:32 +00001651 Params.push_back(Param);
1652 }
1653
Steve Naroff79ea0e02009-04-20 15:06:07 +00001654 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001655 ObjCMethod->setObjCDeclQualifier(
1656 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1657 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001658
1659 if (AttrList)
1660 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenek2b0e6da2009-04-30 18:41:06 +00001661
Chris Lattner855e51f2007-12-12 07:09:47 +00001662 // For implementations (which can be very "coarse grain"), we add the
1663 // method now. This allows the AST to implement lookup methods that work
1664 // incrementally (without waiting until we parse the @end). It also allows
1665 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001666 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001667 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001668 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001669 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1670 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001671 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001672 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1673 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001674 }
1675 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001676 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001677 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001678 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001679 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1680 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001681 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001682 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1683 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001684 }
1685 }
1686 if (PrevMethod) {
1687 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001688 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001689 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001690 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001691 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001692 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001693}
1694
Daniel Dunbar540ff472008-09-23 21:53:23 +00001695void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1696 SourceLocation Loc,
1697 unsigned &Attributes) {
1698 // FIXME: Improve the reported location.
1699
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001700 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001701 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001702 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1703 ObjCDeclSpec::DQ_PR_assign |
1704 ObjCDeclSpec::DQ_PR_copy |
1705 ObjCDeclSpec::DQ_PR_retain))) {
1706 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1707 "readwrite" :
1708 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1709 "assign" :
1710 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1711 "copy" : "retain";
1712
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001713 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001714 diag::err_objc_property_attr_mutually_exclusive :
1715 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001716 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001717 }
1718
1719 // Check for copy or retain on non-object types.
1720 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1721 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001722 Diag(Loc, diag::err_objc_property_requires_object)
1723 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001724 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1725 }
1726
1727 // Check for more than one of { assign, copy, retain }.
1728 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1729 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001730 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1731 << "assign" << "copy";
1732 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001733 }
1734 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001735 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1736 << "assign" << "retain";
1737 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001738 }
1739 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1740 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001741 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1742 << "copy" << "retain";
1743 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001744 }
1745 }
1746
1747 // Warn if user supplied no assignment attribute, property is
1748 // readwrite, and this is an object type.
1749 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1750 ObjCDeclSpec::DQ_PR_retain)) &&
1751 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1752 Context.isObjCObjectPointerType(PropertyTy)) {
1753 // Skip this warning in gc-only mode.
1754 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1755 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1756
1757 // If non-gc code warn that this is likely inappropriate.
1758 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1759 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1760
1761 // FIXME: Implement warning dependent on NSCopying being
1762 // implemented. See also:
1763 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1764 // (please trim this list while you are at it).
1765 }
Mike Stump2f567c62009-05-07 23:06:50 +00001766
1767 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1768 && getLangOptions().getGCMode() == LangOptions::GCOnly
1769 && PropertyTy->isBlockPointerType())
1770 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001771}
1772
Chris Lattner5261d0c2009-03-28 19:18:32 +00001773Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1774 FieldDeclarator &FD,
1775 ObjCDeclSpec &ODS,
1776 Selector GetterSel,
1777 Selector SetterSel,
1778 DeclPtrTy ClassCategory,
1779 bool *isOverridingProperty,
1780 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001781 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001782 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1783 // default is readwrite!
1784 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1785 // property is defaulted to 'assign' if it is readwrite and is
1786 // not retain or copy
1787 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1788 (isReadWrite &&
1789 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1790 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1791 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001792 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001793 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001794 // May modify Attributes.
1795 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001796 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1797 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001798 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001799 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001800 if ((CCPrimary = CDecl->getClassInterface())) {
1801 // Find the property in continuation class's primary class only.
1802 ObjCPropertyDecl *PIDecl = 0;
1803 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001804 for (ObjCInterfaceDecl::prop_iterator
1805 I = CCPrimary->prop_begin(Context),
1806 E = CCPrimary->prop_end(Context);
1807 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001808 if ((*I)->getIdentifier() == PropertyId) {
1809 PIDecl = *I;
1810 break;
1811 }
1812
1813 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001814 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001815 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001816 unsigned PIkind = PIDecl->getPropertyAttributes();
1817 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001818 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001819 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1820 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001821 PIDecl->makeitReadWriteAttribute();
1822 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1823 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1824 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1825 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1826 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001827 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001828 else
1829 Diag(AtLoc, diag::err_use_continuation_class)
1830 << CCPrimary->getDeclName();
1831 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001832 // Make sure setter decl is synthesized, and added to primary
1833 // class's list.
1834 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001835 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001836 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001837 // No matching property found in the primary class. Just fall thru
1838 // and add property to continuation class's primary class.
1839 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001840 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001841 Diag(CDecl->getLocation(), diag::err_continuation_class);
1842 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001843 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001844 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001845 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001846
Steve Naroffdcf1e842009-01-11 12:47:58 +00001847 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1848 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001849 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1850 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001851 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001852 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001853
Chris Lattner6e48be22009-04-11 20:14:49 +00001854 if (T->isArrayType() || T->isFunctionType()) {
1855 Diag(AtLoc, diag::err_property_type) << T;
1856 PDecl->setInvalidDecl();
1857 }
1858
Chris Lattner22063402009-02-16 18:32:47 +00001859 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001860
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001861 // Regardless of setter/getter attribute, we save the default getter/setter
1862 // selector names in anticipation of declaration of setter/getter methods.
1863 PDecl->setGetterName(GetterSel);
1864 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001865
Daniel Dunbar540ff472008-09-23 21:53:23 +00001866 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001867 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001868
Daniel Dunbar540ff472008-09-23 21:53:23 +00001869 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001870 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001871
Daniel Dunbar540ff472008-09-23 21:53:23 +00001872 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001873 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001874
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001875 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001876 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001877
Daniel Dunbar540ff472008-09-23 21:53:23 +00001878 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001879 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001880
Daniel Dunbar540ff472008-09-23 21:53:23 +00001881 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001882 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001883
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001884 if (isAssign)
1885 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1886
Daniel Dunbar540ff472008-09-23 21:53:23 +00001887 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001888 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001889
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001890 if (MethodImplKind == tok::objc_required)
1891 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1892 else if (MethodImplKind == tok::objc_optional)
1893 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001894 // A case of continuation class adding a new property in the class. This
1895 // is not what it was meant for. However, gcc supports it and so should we.
1896 // Make sure setter/getters are declared here.
1897 if (CCPrimary)
1898 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001899
Chris Lattner5261d0c2009-03-28 19:18:32 +00001900 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001901}
1902
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001903/// ActOnPropertyImplDecl - This routine performs semantic checks and
1904/// builds the AST node for a property implementation declaration; declared
1905/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001906///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001907Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1908 SourceLocation PropertyLoc,
1909 bool Synthesize,
1910 DeclPtrTy ClassCatImpDecl,
1911 IdentifierInfo *PropertyId,
1912 IdentifierInfo *PropertyIvar) {
1913 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001914 // Make sure we have a context for the property implementation declaration.
1915 if (!ClassImpDecl) {
1916 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001917 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001918 }
1919 ObjCPropertyDecl *property = 0;
1920 ObjCInterfaceDecl* IDecl = 0;
1921 // Find the class or category class where this property must have
1922 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001923 ObjCImplementationDecl *IC = 0;
1924 ObjCCategoryImplDecl* CatImplClass = 0;
1925 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001926 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001927 // We always synthesize an interface for an implementation
1928 // without an interface decl. So, IDecl is always non-zero.
1929 assert(IDecl &&
1930 "ActOnPropertyImplDecl - @implementation without @interface");
1931
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001932 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001933 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001934 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001935 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001936 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001937 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001938 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001939 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001940 if (Synthesize) {
1941 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001942 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001943 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001944 IDecl = CatImplClass->getClassInterface();
1945 if (!IDecl) {
1946 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001947 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001948 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001949 ObjCCategoryDecl *Category =
1950 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1951
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001952 // If category for this implementation not found, it is an error which
1953 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001954 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001955 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001956 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001957 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001958 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001959 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001960 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001961 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001962 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001963 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001964 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001965 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001966 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001967 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001968 // Check that we have a valid, previously declared ivar for @synthesize
1969 if (Synthesize) {
1970 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001971 if (!PropertyIvar)
1972 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001973 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001974 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001975 ObjCInterfaceDecl *ClassDeclared;
1976 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001977 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001978 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1979 PropertyIvar, PropType,
1980 ObjCIvarDecl::Public,
1981 (Expr *)0);
1982 property->setPropertyIvarDecl(Ivar);
1983 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001984 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001985 // Note! I deliberately want it to fall thru so, we have a
1986 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001987 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001988 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian76389e42009-04-29 21:45:02 +00001989 ClassDeclared != IDecl) {
Fariborz Jahanianc181f5a2009-04-30 21:39:24 +00001990 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001991 << property->getDeclName() << Ivar->getDeclName()
1992 << ClassDeclared->getDeclName();
1993 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1994 << Ivar << Ivar->getNameAsCString();
1995 // Note! I deliberately want it to fall thru so more errors are caught.
1996 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001997 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1998
Steve Naroff631e3922008-09-30 00:24:17 +00001999 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002000 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00002001 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00002002 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00002003 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002004 // Note! I deliberately want it to fall thru so, we have a
2005 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002006 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00002007
2008 // FIXME! Rules for properties are somewhat different that those
2009 // for assignments. Use a new routine to consolidate all cases;
2010 // specifically for property redeclarations as well as for ivars.
2011 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2012 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2013 if (lhsType != rhsType &&
2014 lhsType->isArithmeticType()) {
2015 Diag(PropertyLoc, diag::error_property_ivar_type)
2016 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002017 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002018 }
2019 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00002020 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2021 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002022 Diag(PropertyLoc, diag::error_weak_property)
2023 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002024 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002025 }
2026 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00002027 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2028 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002029 Diag(PropertyLoc, diag::error_strong_property)
2030 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002031 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00002032 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002033 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002034 } else if (PropertyIvar)
2035 // @dynamic
2036 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002037 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002038 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00002039 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2040 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002041 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00002042 ObjCPropertyImplDecl::Synthesize
2043 : ObjCPropertyImplDecl::Dynamic),
2044 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002045 if (IC) {
2046 if (Synthesize)
2047 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002048 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002049 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2050 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2051 << PropertyIvar;
2052 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2053 }
2054
Douglas Gregorcd19b572009-04-23 01:02:12 +00002055 if (ObjCPropertyImplDecl *PPIDecl
2056 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002057 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2058 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002059 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002060 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002061 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002062 }
2063 else {
2064 if (Synthesize)
2065 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002066 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002067 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2068 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2069 << PropertyIvar;
2070 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2071 }
2072
2073 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002074 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002075 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2076 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002077 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002078 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002079 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002080 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002081
Chris Lattner5261d0c2009-03-28 19:18:32 +00002082 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002083}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002084
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002085bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00002086 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002087 return false;
2088
2089 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2090 D->setInvalidDecl();
2091
2092 return true;
2093}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002094
2095/// Collect the instance variables declared in an Objective-C object. Used in
2096/// the creation of structures from objects using the @defs directive.
2097/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2098/// part of the AST generation logic of @defs.
2099static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2100 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002101 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002102 if (Class->getSuperClass())
2103 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2104
2105 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002106 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2107 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002108 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00002109 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2110 ID->getIdentifier(), ID->getType(),
2111 ID->getBitWidth());
2112 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002113 }
2114}
2115
2116/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2117/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002118void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002119 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002120 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002121 // Check that ClassName is a valid class
2122 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2123 if (!Class) {
2124 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2125 return;
2126 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00002127 if (LangOpts.ObjCNonFragileABI) {
2128 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2129 return;
2130 }
2131
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002132 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00002133 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002134
2135 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002136 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002137 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002138 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002139 if (getLangOptions().CPlusPlus)
2140 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002141 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002142 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002143 }
2144}
2145